Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1 | //===--- ScheduleDAGSDNodes.cpp - Implement the ScheduleDAGSDNodes class --===// |
| 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 | // |
| 10 | // This implements the ScheduleDAG class, which is a base class used by |
| 11 | // scheduling implementation classes. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Dan Gohman | 483377c | 2009-02-06 17:22:58 +0000 | [diff] [blame] | 15 | #include "ScheduleDAGSDNodes.h" |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 16 | #include "InstrEmitter.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "SDNodeDbgValue.h" |
Evan Cheng | 9d92aaa | 2010-01-22 03:36:51 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
| 19 | #include "llvm/ADT/SmallPtrSet.h" |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallSet.h" |
Evan Cheng | 9d92aaa | 2010-01-22 03:36:51 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallVector.h" |
| 22 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 25 | #include "llvm/CodeGen/SelectionDAG.h" |
| 26 | #include "llvm/MC/MCInstrItineraries.h" |
Andrew Trick | 641e2d4 | 2011-03-05 08:00:22 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Debug.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetInstrInfo.h" |
| 31 | #include "llvm/Target/TargetLowering.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetRegisterInfo.h" |
| 33 | #include "llvm/Target/TargetSubtargetInfo.h" |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 36 | #define DEBUG_TYPE "pre-RA-sched" |
| 37 | |
Evan Cheng | 9d92aaa | 2010-01-22 03:36:51 +0000 | [diff] [blame] | 38 | STATISTIC(LoadsClustered, "Number of loads clustered together"); |
| 39 | |
Sanjay Patel | 25d3c1c | 2014-10-07 17:38:33 +0000 | [diff] [blame] | 40 | // This allows the latency-based scheduler to notice high latency instructions |
Sanjay Patel | eb0cc1b | 2014-10-07 17:36:50 +0000 | [diff] [blame] | 41 | // without a target itinerary. The choice of number here has more to do with |
Sanjay Patel | 25d3c1c | 2014-10-07 17:38:33 +0000 | [diff] [blame] | 42 | // balancing scheduler heuristics than with the actual machine latency. |
Andrew Trick | 641e2d4 | 2011-03-05 08:00:22 +0000 | [diff] [blame] | 43 | static cl::opt<int> HighLatencyCycles( |
| 44 | "sched-high-latency-cycles", cl::Hidden, cl::init(10), |
| 45 | cl::desc("Roughly estimate the number of cycles that 'long latency'" |
| 46 | "instructions take for targets with no itinerary")); |
| 47 | |
Dan Gohman | 619ef48 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 48 | ScheduleDAGSDNodes::ScheduleDAGSDNodes(MachineFunction &mf) |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 49 | : ScheduleDAG(mf), BB(nullptr), DAG(nullptr), |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 50 | InstrItins(mf.getSubtarget().getInstrItineraryData()) {} |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 51 | |
Dan Gohman | dfaf646 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 52 | /// Run - perform scheduling. |
| 53 | /// |
Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 54 | void ScheduleDAGSDNodes::Run(SelectionDAG *dag, MachineBasicBlock *bb) { |
| 55 | BB = bb; |
Dan Gohman | dfaf646 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 56 | DAG = dag; |
Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 57 | |
| 58 | // Clear the scheduler's SUnit DAG. |
| 59 | ScheduleDAG::clearDAG(); |
| 60 | Sequence.clear(); |
| 61 | |
| 62 | // Invoke the target's selection of scheduler. |
| 63 | Schedule(); |
Dan Gohman | dfaf646 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Evan Cheng | 4401f88 | 2010-05-20 23:26:43 +0000 | [diff] [blame] | 66 | /// NewSUnit - Creates a new SUnit and return a ptr to it. |
| 67 | /// |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 68 | SUnit *ScheduleDAGSDNodes::newSUnit(SDNode *N) { |
Evan Cheng | 4401f88 | 2010-05-20 23:26:43 +0000 | [diff] [blame] | 69 | #ifndef NDEBUG |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 70 | const SUnit *Addr = nullptr; |
Evan Cheng | 4401f88 | 2010-05-20 23:26:43 +0000 | [diff] [blame] | 71 | if (!SUnits.empty()) |
| 72 | Addr = &SUnits[0]; |
| 73 | #endif |
| 74 | SUnits.push_back(SUnit(N, (unsigned)SUnits.size())); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 75 | assert((Addr == nullptr || Addr == &SUnits[0]) && |
Evan Cheng | 4401f88 | 2010-05-20 23:26:43 +0000 | [diff] [blame] | 76 | "SUnits std::vector reallocated on the fly!"); |
| 77 | SUnits.back().OrigNode = &SUnits.back(); |
| 78 | SUnit *SU = &SUnits.back(); |
| 79 | const TargetLowering &TLI = DAG->getTargetLoweringInfo(); |
Evan Cheng | 23ef829 | 2010-08-10 02:39:45 +0000 | [diff] [blame] | 80 | if (!N || |
| 81 | (N->isMachineOpcode() && |
| 82 | N->getMachineOpcode() == TargetOpcode::IMPLICIT_DEF)) |
Evan Cheng | cc2efe1 | 2010-05-28 23:26:21 +0000 | [diff] [blame] | 83 | SU->SchedulingPref = Sched::None; |
| 84 | else |
| 85 | SU->SchedulingPref = TLI.getSchedulingPreference(N); |
Evan Cheng | 4401f88 | 2010-05-20 23:26:43 +0000 | [diff] [blame] | 86 | return SU; |
| 87 | } |
| 88 | |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 89 | SUnit *ScheduleDAGSDNodes::Clone(SUnit *Old) { |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 90 | SUnit *SU = newSUnit(Old->getNode()); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 91 | SU->OrigNode = Old->OrigNode; |
| 92 | SU->Latency = Old->Latency; |
Andrew Trick | 2ad0b37 | 2011-04-07 19:54:57 +0000 | [diff] [blame] | 93 | SU->isVRegCycle = Old->isVRegCycle; |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 94 | SU->isCall = Old->isCall; |
Evan Cheng | 1355bbd | 2011-04-26 21:31:35 +0000 | [diff] [blame] | 95 | SU->isCallOp = Old->isCallOp; |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 96 | SU->isTwoAddress = Old->isTwoAddress; |
| 97 | SU->isCommutable = Old->isCommutable; |
| 98 | SU->hasPhysRegDefs = Old->hasPhysRegDefs; |
Dan Gohman | 52c278e | 2009-03-23 16:10:52 +0000 | [diff] [blame] | 99 | SU->hasPhysRegClobbers = Old->hasPhysRegClobbers; |
Andrew Trick | bfbd972 | 2011-04-14 05:15:06 +0000 | [diff] [blame] | 100 | SU->isScheduleHigh = Old->isScheduleHigh; |
| 101 | SU->isScheduleLow = Old->isScheduleLow; |
Evan Cheng | 4401f88 | 2010-05-20 23:26:43 +0000 | [diff] [blame] | 102 | SU->SchedulingPref = Old->SchedulingPref; |
Evan Cheng | 968e2e7 | 2009-01-16 20:57:18 +0000 | [diff] [blame] | 103 | Old->isCloned = true; |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 104 | return SU; |
| 105 | } |
| 106 | |
| 107 | /// CheckForPhysRegDependency - Check if the dependency between def and use of |
| 108 | /// a specified operand is a physical register dependency. If so, returns the |
Evan Cheng | b2c42c6 | 2009-01-12 03:19:55 +0000 | [diff] [blame] | 109 | /// register and the cost of copying the register. |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 110 | static void CheckForPhysRegDependency(SDNode *Def, SDNode *User, unsigned Op, |
Andrew Trick | 3f924e4 | 2011-02-03 23:00:17 +0000 | [diff] [blame] | 111 | const TargetRegisterInfo *TRI, |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 112 | const TargetInstrInfo *TII, |
Evan Cheng | b2c42c6 | 2009-01-12 03:19:55 +0000 | [diff] [blame] | 113 | unsigned &PhysReg, int &Cost) { |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 114 | if (Op != 2 || User->getOpcode() != ISD::CopyToReg) |
| 115 | return; |
| 116 | |
| 117 | unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg(); |
| 118 | if (TargetRegisterInfo::isVirtualRegister(Reg)) |
| 119 | return; |
| 120 | |
| 121 | unsigned ResNo = User->getOperand(2).getResNo(); |
Tim Northover | e4c7be5 | 2014-10-23 22:31:48 +0000 | [diff] [blame] | 122 | if (Def->getOpcode() == ISD::CopyFromReg && |
| 123 | cast<RegisterSDNode>(Def->getOperand(1))->getReg() == Reg) { |
| 124 | PhysReg = Reg; |
| 125 | } else if (Def->isMachineOpcode()) { |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 126 | const MCInstrDesc &II = TII->get(Def->getMachineOpcode()); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 127 | if (ResNo >= II.getNumDefs() && |
Tim Northover | e4c7be5 | 2014-10-23 22:31:48 +0000 | [diff] [blame] | 128 | II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 129 | PhysReg = Reg; |
Tim Northover | e4c7be5 | 2014-10-23 22:31:48 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | if (PhysReg != 0) { |
| 133 | const TargetRegisterClass *RC = |
Craig Topper | 7f416c8 | 2014-11-16 21:17:18 +0000 | [diff] [blame] | 134 | TRI->getMinimalPhysRegClass(Reg, Def->getSimpleValueType(ResNo)); |
Tim Northover | e4c7be5 | 2014-10-23 22:31:48 +0000 | [diff] [blame] | 135 | Cost = RC->getCopyCost(); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Andrew Trick | 833f049 | 2012-04-28 01:03:23 +0000 | [diff] [blame] | 139 | // Helper for AddGlue to clone node operands. |
| 140 | static void CloneNodeWithValues(SDNode *N, SelectionDAG *DAG, |
| 141 | SmallVectorImpl<EVT> &VTs, |
| 142 | SDValue ExtraOper = SDValue()) { |
Hans Wennborg | 5f5b8cc | 2014-08-11 13:47:57 +0000 | [diff] [blame] | 143 | SmallVector<SDValue, 8> Ops; |
Bill Wendling | 2d3c490 | 2010-06-24 22:00:37 +0000 | [diff] [blame] | 144 | for (unsigned I = 0, E = N->getNumOperands(); I != E; ++I) |
| 145 | Ops.push_back(N->getOperand(I)); |
Bill Wendling | a136521 | 2010-06-23 18:16:24 +0000 | [diff] [blame] | 146 | |
Andrew Trick | 833f049 | 2012-04-28 01:03:23 +0000 | [diff] [blame] | 147 | if (ExtraOper.getNode()) |
| 148 | Ops.push_back(ExtraOper); |
Bill Wendling | a136521 | 2010-06-23 18:16:24 +0000 | [diff] [blame] | 149 | |
Craig Topper | abb4ac7 | 2014-04-16 06:10:51 +0000 | [diff] [blame] | 150 | SDVTList VTList = DAG->getVTList(VTs); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 151 | MachineSDNode::mmo_iterator Begin = nullptr, End = nullptr; |
Bill Wendling | a136521 | 2010-06-23 18:16:24 +0000 | [diff] [blame] | 152 | MachineSDNode *MN = dyn_cast<MachineSDNode>(N); |
| 153 | |
| 154 | // Store memory references. |
| 155 | if (MN) { |
| 156 | Begin = MN->memoperands_begin(); |
| 157 | End = MN->memoperands_end(); |
| 158 | } |
| 159 | |
Craig Topper | 131de82 | 2014-04-27 19:21:16 +0000 | [diff] [blame] | 160 | DAG->MorphNodeTo(N, N->getOpcode(), VTList, Ops); |
Bill Wendling | a136521 | 2010-06-23 18:16:24 +0000 | [diff] [blame] | 161 | |
| 162 | // Reset the memory references |
| 163 | if (MN) |
| 164 | MN->setMemRefs(Begin, End); |
Evan Cheng | 9d92aaa | 2010-01-22 03:36:51 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Andrew Trick | 833f049 | 2012-04-28 01:03:23 +0000 | [diff] [blame] | 167 | static bool AddGlue(SDNode *N, SDValue Glue, bool AddGlue, SelectionDAG *DAG) { |
| 168 | SmallVector<EVT, 4> VTs; |
| 169 | SDNode *GlueDestNode = Glue.getNode(); |
| 170 | |
| 171 | // Don't add glue from a node to itself. |
| 172 | if (GlueDestNode == N) return false; |
| 173 | |
| 174 | // Don't add a glue operand to something that already uses glue. |
| 175 | if (GlueDestNode && |
| 176 | N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Glue) { |
| 177 | return false; |
| 178 | } |
| 179 | // Don't add glue to something that already has a glue value. |
| 180 | if (N->getValueType(N->getNumValues() - 1) == MVT::Glue) return false; |
| 181 | |
| 182 | for (unsigned I = 0, E = N->getNumValues(); I != E; ++I) |
| 183 | VTs.push_back(N->getValueType(I)); |
| 184 | |
| 185 | if (AddGlue) |
| 186 | VTs.push_back(MVT::Glue); |
| 187 | |
| 188 | CloneNodeWithValues(N, DAG, VTs, Glue); |
| 189 | |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | // Cleanup after unsuccessful AddGlue. Use the standard method of morphing the |
| 194 | // node even though simply shrinking the value list is sufficient. |
| 195 | static void RemoveUnusedGlue(SDNode *N, SelectionDAG *DAG) { |
| 196 | assert((N->getValueType(N->getNumValues() - 1) == MVT::Glue && |
| 197 | !N->hasAnyUseOfValue(N->getNumValues() - 1)) && |
| 198 | "expected an unused glue value"); |
| 199 | |
| 200 | SmallVector<EVT, 4> VTs; |
| 201 | for (unsigned I = 0, E = N->getNumValues()-1; I != E; ++I) |
| 202 | VTs.push_back(N->getValueType(I)); |
| 203 | |
| 204 | CloneNodeWithValues(N, DAG, VTs); |
| 205 | } |
| 206 | |
Chris Lattner | 11a3381 | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 207 | /// ClusterNeighboringLoads - Force nearby loads together by "gluing" them. |
Evan Cheng | 9d92aaa | 2010-01-22 03:36:51 +0000 | [diff] [blame] | 208 | /// This function finds loads of the same base and different offsets. If the |
Chris Lattner | 3e5fbd7 | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 209 | /// offsets are not far apart (target specific), it add MVT::Glue inputs and |
Evan Cheng | 9d92aaa | 2010-01-22 03:36:51 +0000 | [diff] [blame] | 210 | /// outputs to ensure they are scheduled together and in order. This |
| 211 | /// optimization may benefit some targets by improving cache locality. |
Evan Cheng | 38f6560 | 2010-06-10 02:09:31 +0000 | [diff] [blame] | 212 | void ScheduleDAGSDNodes::ClusterNeighboringLoads(SDNode *Node) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 213 | SDNode *Chain = nullptr; |
Evan Cheng | 38f6560 | 2010-06-10 02:09:31 +0000 | [diff] [blame] | 214 | unsigned NumOps = Node->getNumOperands(); |
| 215 | if (Node->getOperand(NumOps-1).getValueType() == MVT::Other) |
| 216 | Chain = Node->getOperand(NumOps-1).getNode(); |
| 217 | if (!Chain) |
| 218 | return; |
| 219 | |
| 220 | // Look for other loads of the same chain. Find loads that are loading from |
| 221 | // the same base pointer and different offsets. |
Evan Cheng | 9d92aaa | 2010-01-22 03:36:51 +0000 | [diff] [blame] | 222 | SmallPtrSet<SDNode*, 16> Visited; |
| 223 | SmallVector<int64_t, 4> Offsets; |
| 224 | DenseMap<long long, SDNode*> O2SMap; // Map from offset to SDNode. |
Evan Cheng | 38f6560 | 2010-06-10 02:09:31 +0000 | [diff] [blame] | 225 | bool Cluster = false; |
| 226 | SDNode *Base = Node; |
Andrew Trick | 8d007bb | 2014-04-07 21:29:22 +0000 | [diff] [blame] | 227 | // This algorithm requires a reasonably low use count before finding a match |
| 228 | // to avoid uselessly blowing up compile time in large blocks. |
| 229 | unsigned UseCount = 0; |
Evan Cheng | 38f6560 | 2010-06-10 02:09:31 +0000 | [diff] [blame] | 230 | for (SDNode::use_iterator I = Chain->use_begin(), E = Chain->use_end(); |
Andrew Trick | 8d007bb | 2014-04-07 21:29:22 +0000 | [diff] [blame] | 231 | I != E && UseCount < 100; ++I, ++UseCount) { |
Evan Cheng | 38f6560 | 2010-06-10 02:09:31 +0000 | [diff] [blame] | 232 | SDNode *User = *I; |
| 233 | if (User == Node || !Visited.insert(User)) |
| 234 | continue; |
| 235 | int64_t Offset1, Offset2; |
| 236 | if (!TII->areLoadsFromSameBasePtr(Base, User, Offset1, Offset2) || |
| 237 | Offset1 == Offset2) |
| 238 | // FIXME: Should be ok if they addresses are identical. But earlier |
| 239 | // optimizations really should have eliminated one of the loads. |
| 240 | continue; |
| 241 | if (O2SMap.insert(std::make_pair(Offset1, Base)).second) |
| 242 | Offsets.push_back(Offset1); |
| 243 | O2SMap.insert(std::make_pair(Offset2, User)); |
| 244 | Offsets.push_back(Offset2); |
Duncan Sands | 2dc70be | 2010-06-25 14:48:39 +0000 | [diff] [blame] | 245 | if (Offset2 < Offset1) |
Evan Cheng | 38f6560 | 2010-06-10 02:09:31 +0000 | [diff] [blame] | 246 | Base = User; |
Evan Cheng | 38f6560 | 2010-06-10 02:09:31 +0000 | [diff] [blame] | 247 | Cluster = true; |
Andrew Trick | 8d007bb | 2014-04-07 21:29:22 +0000 | [diff] [blame] | 248 | // Reset UseCount to allow more matches. |
| 249 | UseCount = 0; |
Evan Cheng | 38f6560 | 2010-06-10 02:09:31 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | if (!Cluster) |
| 253 | return; |
| 254 | |
| 255 | // Sort them in increasing order. |
| 256 | std::sort(Offsets.begin(), Offsets.end()); |
| 257 | |
| 258 | // Check if the loads are close enough. |
| 259 | SmallVector<SDNode*, 4> Loads; |
| 260 | unsigned NumLoads = 0; |
| 261 | int64_t BaseOff = Offsets[0]; |
| 262 | SDNode *BaseLoad = O2SMap[BaseOff]; |
| 263 | Loads.push_back(BaseLoad); |
| 264 | for (unsigned i = 1, e = Offsets.size(); i != e; ++i) { |
| 265 | int64_t Offset = Offsets[i]; |
| 266 | SDNode *Load = O2SMap[Offset]; |
| 267 | if (!TII->shouldScheduleLoadsNear(BaseLoad, Load, BaseOff, Offset,NumLoads)) |
| 268 | break; // Stop right here. Ignore loads that are further away. |
| 269 | Loads.push_back(Load); |
| 270 | ++NumLoads; |
| 271 | } |
| 272 | |
| 273 | if (NumLoads == 0) |
| 274 | return; |
| 275 | |
Chris Lattner | 3e5fbd7 | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 276 | // Cluster loads by adding MVT::Glue outputs and inputs. This also |
Evan Cheng | 38f6560 | 2010-06-10 02:09:31 +0000 | [diff] [blame] | 277 | // ensure they are scheduled in order of increasing addresses. |
| 278 | SDNode *Lead = Loads[0]; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 279 | SDValue InGlue = SDValue(nullptr, 0); |
Andrew Trick | 833f049 | 2012-04-28 01:03:23 +0000 | [diff] [blame] | 280 | if (AddGlue(Lead, InGlue, true, DAG)) |
| 281 | InGlue = SDValue(Lead, Lead->getNumValues() - 1); |
Bill Wendling | 2d3c490 | 2010-06-24 22:00:37 +0000 | [diff] [blame] | 282 | for (unsigned I = 1, E = Loads.size(); I != E; ++I) { |
Chris Lattner | 11a3381 | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 283 | bool OutGlue = I < E - 1; |
Bill Wendling | 2d3c490 | 2010-06-24 22:00:37 +0000 | [diff] [blame] | 284 | SDNode *Load = Loads[I]; |
| 285 | |
Andrew Trick | 833f049 | 2012-04-28 01:03:23 +0000 | [diff] [blame] | 286 | // If AddGlue fails, we could leave an unsused glue value. This should not |
| 287 | // cause any |
| 288 | if (AddGlue(Load, InGlue, OutGlue, DAG)) { |
| 289 | if (OutGlue) |
| 290 | InGlue = SDValue(Load, Load->getNumValues() - 1); |
Bill Wendling | a136521 | 2010-06-23 18:16:24 +0000 | [diff] [blame] | 291 | |
Andrew Trick | 833f049 | 2012-04-28 01:03:23 +0000 | [diff] [blame] | 292 | ++LoadsClustered; |
| 293 | } |
| 294 | else if (!OutGlue && InGlue.getNode()) |
| 295 | RemoveUnusedGlue(InGlue.getNode(), DAG); |
Evan Cheng | 38f6560 | 2010-06-10 02:09:31 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | /// ClusterNodes - Cluster certain nodes which should be scheduled together. |
| 300 | /// |
| 301 | void ScheduleDAGSDNodes::ClusterNodes() { |
Evan Cheng | 9d92aaa | 2010-01-22 03:36:51 +0000 | [diff] [blame] | 302 | for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(), |
| 303 | E = DAG->allnodes_end(); NI != E; ++NI) { |
| 304 | SDNode *Node = &*NI; |
| 305 | if (!Node || !Node->isMachineOpcode()) |
| 306 | continue; |
| 307 | |
| 308 | unsigned Opc = Node->getMachineOpcode(); |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 309 | const MCInstrDesc &MCID = TII->get(Opc); |
| 310 | if (MCID.mayLoad()) |
Evan Cheng | 38f6560 | 2010-06-10 02:09:31 +0000 | [diff] [blame] | 311 | // Cluster loads from "near" addresses into combined SUnits. |
| 312 | ClusterNeighboringLoads(Node); |
Evan Cheng | 9d92aaa | 2010-01-22 03:36:51 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 316 | void ScheduleDAGSDNodes::BuildSchedUnits() { |
Dan Gohman | 92cf280 | 2008-12-23 17:24:50 +0000 | [diff] [blame] | 317 | // During scheduling, the NodeId field of SDNode is used to map SDNodes |
| 318 | // to their associated SUnits by holding SUnits table indices. A value |
| 319 | // of -1 means the SDNode does not yet have an associated SUnit. |
| 320 | unsigned NumNodes = 0; |
| 321 | for (SelectionDAG::allnodes_iterator NI = DAG->allnodes_begin(), |
| 322 | E = DAG->allnodes_end(); NI != E; ++NI) { |
| 323 | NI->setNodeId(-1); |
| 324 | ++NumNodes; |
| 325 | } |
| 326 | |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 327 | // Reserve entries in the vector for each of the SUnits we are creating. This |
| 328 | // ensure that reallocation of the vector won't happen, so SUnit*'s won't get |
| 329 | // invalidated. |
Dan Gohman | ce70fe2 | 2008-12-17 04:30:46 +0000 | [diff] [blame] | 330 | // FIXME: Multiply by 2 because we may clone nodes during scheduling. |
| 331 | // This is a temporary workaround. |
Dan Gohman | 92cf280 | 2008-12-23 17:24:50 +0000 | [diff] [blame] | 332 | SUnits.reserve(NumNodes * 2); |
Andrew Trick | 3f924e4 | 2011-02-03 23:00:17 +0000 | [diff] [blame] | 333 | |
Chris Lattner | df8a8a8 | 2010-02-24 06:11:37 +0000 | [diff] [blame] | 334 | // Add all nodes in depth first order. |
| 335 | SmallVector<SDNode*, 64> Worklist; |
| 336 | SmallPtrSet<SDNode*, 64> Visited; |
| 337 | Worklist.push_back(DAG->getRoot().getNode()); |
| 338 | Visited.insert(DAG->getRoot().getNode()); |
Andrew Trick | 3f924e4 | 2011-02-03 23:00:17 +0000 | [diff] [blame] | 339 | |
Evan Cheng | 1355bbd | 2011-04-26 21:31:35 +0000 | [diff] [blame] | 340 | SmallVector<SUnit*, 8> CallSUnits; |
Chris Lattner | df8a8a8 | 2010-02-24 06:11:37 +0000 | [diff] [blame] | 341 | while (!Worklist.empty()) { |
| 342 | SDNode *NI = Worklist.pop_back_val(); |
Andrew Trick | 3f924e4 | 2011-02-03 23:00:17 +0000 | [diff] [blame] | 343 | |
Chris Lattner | df8a8a8 | 2010-02-24 06:11:37 +0000 | [diff] [blame] | 344 | // Add all operands to the worklist unless they've already been added. |
| 345 | for (unsigned i = 0, e = NI->getNumOperands(); i != e; ++i) |
| 346 | if (Visited.insert(NI->getOperand(i).getNode())) |
| 347 | Worklist.push_back(NI->getOperand(i).getNode()); |
Andrew Trick | 3f924e4 | 2011-02-03 23:00:17 +0000 | [diff] [blame] | 348 | |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 349 | if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate. |
| 350 | continue; |
Andrew Trick | 3f924e4 | 2011-02-03 23:00:17 +0000 | [diff] [blame] | 351 | |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 352 | // If this node has already been processed, stop now. |
| 353 | if (NI->getNodeId() != -1) continue; |
Andrew Trick | 3f924e4 | 2011-02-03 23:00:17 +0000 | [diff] [blame] | 354 | |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 355 | SUnit *NodeSUnit = newSUnit(NI); |
Andrew Trick | 3f924e4 | 2011-02-03 23:00:17 +0000 | [diff] [blame] | 356 | |
Chris Lattner | 11a3381 | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 357 | // See if anything is glued to this node, if so, add them to glued |
| 358 | // nodes. Nodes can have at most one glue input and one glue output. Glue |
| 359 | // is required to be the last operand and result of a node. |
Andrew Trick | 3f924e4 | 2011-02-03 23:00:17 +0000 | [diff] [blame] | 360 | |
Chris Lattner | 11a3381 | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 361 | // Scan up to find glued preds. |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 362 | SDNode *N = NI; |
Dan Gohman | 3bdc4bd | 2009-03-20 20:42:23 +0000 | [diff] [blame] | 363 | while (N->getNumOperands() && |
Chris Lattner | 3e5fbd7 | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 364 | N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Glue) { |
Dan Gohman | 3bdc4bd | 2009-03-20 20:42:23 +0000 | [diff] [blame] | 365 | N = N->getOperand(N->getNumOperands()-1).getNode(); |
| 366 | assert(N->getNodeId() == -1 && "Node already inserted!"); |
| 367 | N->setNodeId(NodeSUnit->NodeNum); |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 368 | if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall()) |
| 369 | NodeSUnit->isCall = true; |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 370 | } |
Andrew Trick | 3f924e4 | 2011-02-03 23:00:17 +0000 | [diff] [blame] | 371 | |
Chris Lattner | 11a3381 | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 372 | // Scan down to find any glued succs. |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 373 | N = NI; |
Chris Lattner | 3e5fbd7 | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 374 | while (N->getValueType(N->getNumValues()-1) == MVT::Glue) { |
Chris Lattner | 11a3381 | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 375 | SDValue GlueVal(N, N->getNumValues()-1); |
Andrew Trick | 3f924e4 | 2011-02-03 23:00:17 +0000 | [diff] [blame] | 376 | |
Chris Lattner | 11a3381 | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 377 | // There are either zero or one users of the Glue result. |
| 378 | bool HasGlueUse = false; |
Andrew Trick | 3f924e4 | 2011-02-03 23:00:17 +0000 | [diff] [blame] | 379 | for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 380 | UI != E; ++UI) |
Chris Lattner | 11a3381 | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 381 | if (GlueVal.isOperandOf(*UI)) { |
| 382 | HasGlueUse = true; |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 383 | assert(N->getNodeId() == -1 && "Node already inserted!"); |
| 384 | N->setNodeId(NodeSUnit->NodeNum); |
| 385 | N = *UI; |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 386 | if (N->isMachineOpcode() && TII->get(N->getMachineOpcode()).isCall()) |
| 387 | NodeSUnit->isCall = true; |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 388 | break; |
| 389 | } |
Chris Lattner | 11a3381 | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 390 | if (!HasGlueUse) break; |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 391 | } |
Andrew Trick | 3f924e4 | 2011-02-03 23:00:17 +0000 | [diff] [blame] | 392 | |
Evan Cheng | 1355bbd | 2011-04-26 21:31:35 +0000 | [diff] [blame] | 393 | if (NodeSUnit->isCall) |
| 394 | CallSUnits.push_back(NodeSUnit); |
| 395 | |
Andrew Trick | bfbd972 | 2011-04-14 05:15:06 +0000 | [diff] [blame] | 396 | // Schedule zero-latency TokenFactor below any nodes that may increase the |
| 397 | // schedule height. Otherwise, ancestors of the TokenFactor may appear to |
| 398 | // have false stalls. |
| 399 | if (NI->getOpcode() == ISD::TokenFactor) |
| 400 | NodeSUnit->isScheduleLow = true; |
| 401 | |
Chris Lattner | 11a3381 | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 402 | // If there are glue operands involved, N is now the bottom-most node |
| 403 | // of the sequence of nodes that are glued together. |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 404 | // Update the SUnit. |
| 405 | NodeSUnit->setNode(N); |
| 406 | assert(N->getNodeId() == -1 && "Node already inserted!"); |
| 407 | N->setNodeId(NodeSUnit->NodeNum); |
| 408 | |
Andrew Trick | d0548ae | 2011-02-04 03:18:17 +0000 | [diff] [blame] | 409 | // Compute NumRegDefsLeft. This must be done before AddSchedEdges. |
| 410 | InitNumRegDefsLeft(NodeSUnit); |
| 411 | |
Dan Gohman | d1f33e2 | 2008-11-21 01:44:51 +0000 | [diff] [blame] | 412 | // Assign the Latency field of NodeSUnit using target-provided information. |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 413 | computeLatency(NodeSUnit); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 414 | } |
Evan Cheng | 1355bbd | 2011-04-26 21:31:35 +0000 | [diff] [blame] | 415 | |
| 416 | // Find all call operands. |
| 417 | while (!CallSUnits.empty()) { |
| 418 | SUnit *SU = CallSUnits.pop_back_val(); |
| 419 | for (const SDNode *SUNode = SU->getNode(); SUNode; |
| 420 | SUNode = SUNode->getGluedNode()) { |
| 421 | if (SUNode->getOpcode() != ISD::CopyToReg) |
| 422 | continue; |
| 423 | SDNode *SrcN = SUNode->getOperand(2).getNode(); |
| 424 | if (isPassiveNode(SrcN)) continue; // Not scheduled. |
| 425 | SUnit *SrcSU = &SUnits[SrcN->getNodeId()]; |
| 426 | SrcSU->isCallOp = true; |
| 427 | } |
| 428 | } |
Dan Gohman | 04543e7 | 2008-12-23 18:36:58 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | void ScheduleDAGSDNodes::AddSchedEdges() { |
Eric Christopher | edba30c | 2014-10-09 06:28:06 +0000 | [diff] [blame] | 432 | const TargetSubtargetInfo &ST = MF.getSubtarget(); |
David Goodwin | 90e6b8b | 2009-08-13 16:05:04 +0000 | [diff] [blame] | 433 | |
David Goodwin | 9b48cd4 | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 434 | // Check to see if the scheduler cares about latencies. |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 435 | bool UnitLatencies = forceUnitLatencies(); |
David Goodwin | 9b48cd4 | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 436 | |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 437 | // Pass 2: add the preds, succs, etc. |
| 438 | for (unsigned su = 0, e = SUnits.size(); su != e; ++su) { |
| 439 | SUnit *SU = &SUnits[su]; |
| 440 | SDNode *MainNode = SU->getNode(); |
Andrew Trick | 3f924e4 | 2011-02-03 23:00:17 +0000 | [diff] [blame] | 441 | |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 442 | if (MainNode->isMachineOpcode()) { |
| 443 | unsigned Opc = MainNode->getMachineOpcode(); |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 444 | const MCInstrDesc &MCID = TII->get(Opc); |
| 445 | for (unsigned i = 0; i != MCID.getNumOperands(); ++i) { |
| 446 | if (MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1) { |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 447 | SU->isTwoAddress = true; |
| 448 | break; |
| 449 | } |
| 450 | } |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 451 | if (MCID.isCommutable()) |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 452 | SU->isCommutable = true; |
| 453 | } |
Andrew Trick | 3f924e4 | 2011-02-03 23:00:17 +0000 | [diff] [blame] | 454 | |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 455 | // Find all predecessors and successors of the group. |
Chris Lattner | 11a3381 | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 456 | for (SDNode *N = SU->getNode(); N; N = N->getGluedNode()) { |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 457 | if (N->isMachineOpcode() && |
Dan Gohman | 52c278e | 2009-03-23 16:10:52 +0000 | [diff] [blame] | 458 | TII->get(N->getMachineOpcode()).getImplicitDefs()) { |
| 459 | SU->hasPhysRegClobbers = true; |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 460 | unsigned NumUsed = InstrEmitter::CountResults(N); |
Dan Gohman | f477262 | 2009-03-23 17:39:36 +0000 | [diff] [blame] | 461 | while (NumUsed != 0 && !N->hasAnyUseOfValue(NumUsed - 1)) |
| 462 | --NumUsed; // Skip over unused values at the end. |
| 463 | if (NumUsed > TII->get(N->getMachineOpcode()).getNumDefs()) |
Dan Gohman | 52c278e | 2009-03-23 16:10:52 +0000 | [diff] [blame] | 464 | SU->hasPhysRegDefs = true; |
| 465 | } |
Andrew Trick | 3f924e4 | 2011-02-03 23:00:17 +0000 | [diff] [blame] | 466 | |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 467 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 468 | SDNode *OpN = N->getOperand(i).getNode(); |
| 469 | if (isPassiveNode(OpN)) continue; // Not scheduled. |
| 470 | SUnit *OpSU = &SUnits[OpN->getNodeId()]; |
| 471 | assert(OpSU && "Node has no SUnit!"); |
| 472 | if (OpSU == SU) continue; // In the same group. |
| 473 | |
Owen Anderson | 53aa7a9 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 474 | EVT OpVT = N->getOperand(i).getValueType(); |
Chris Lattner | 11a3381 | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 475 | assert(OpVT != MVT::Glue && "Glued nodes should be in same sunit!"); |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 476 | bool isChain = OpVT == MVT::Other; |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 477 | |
| 478 | unsigned PhysReg = 0; |
Evan Cheng | b2c42c6 | 2009-01-12 03:19:55 +0000 | [diff] [blame] | 479 | int Cost = 1; |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 480 | // Determine if this is a physical register dependency. |
Evan Cheng | b2c42c6 | 2009-01-12 03:19:55 +0000 | [diff] [blame] | 481 | CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost); |
Dan Gohman | 2d17089 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 482 | assert((PhysReg == 0 || !isChain) && |
| 483 | "Chain dependence via physreg data?"); |
Evan Cheng | b2c42c6 | 2009-01-12 03:19:55 +0000 | [diff] [blame] | 484 | // FIXME: See ScheduleDAGSDNodes::EmitCopyFromReg. For now, scheduler |
| 485 | // emits a copy from the physical register to a virtual register unless |
| 486 | // it requires a cross class copy (cost < 0). That means we are only |
| 487 | // treating "expensive to copy" register dependency as physical register |
| 488 | // dependency. This may change in the future though. |
Andrew Trick | 3013b6a | 2011-06-15 17:16:12 +0000 | [diff] [blame] | 489 | if (Cost >= 0 && !StressSched) |
Evan Cheng | b2c42c6 | 2009-01-12 03:19:55 +0000 | [diff] [blame] | 490 | PhysReg = 0; |
David Goodwin | 90e6b8b | 2009-08-13 16:05:04 +0000 | [diff] [blame] | 491 | |
Evan Cheng | cc2efe1 | 2010-05-28 23:26:21 +0000 | [diff] [blame] | 492 | // If this is a ctrl dep, latency is 1. |
Andrew Trick | 1b60ad6 | 2011-04-12 20:14:07 +0000 | [diff] [blame] | 493 | unsigned OpLatency = isChain ? 1 : OpSU->Latency; |
Andrew Trick | b53a00d | 2011-04-13 00:38:32 +0000 | [diff] [blame] | 494 | // Special-case TokenFactor chains as zero-latency. |
| 495 | if(isChain && OpN->getOpcode() == ISD::TokenFactor) |
| 496 | OpLatency = 0; |
| 497 | |
Andrew Trick | baeaabb | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 498 | SDep Dep = isChain ? SDep(OpSU, SDep::Barrier) |
| 499 | : SDep(OpSU, SDep::Data, PhysReg); |
| 500 | Dep.setLatency(OpLatency); |
David Goodwin | 9b48cd4 | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 501 | if (!isChain && !UnitLatencies) { |
Andrew Trick | baeaabb | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 502 | computeOperandLatency(OpN, N, i, Dep); |
| 503 | ST.adjustSchedDependency(OpSU, SU, Dep); |
David Goodwin | 9b48cd4 | 2009-08-19 16:08:58 +0000 | [diff] [blame] | 504 | } |
David Goodwin | 90e6b8b | 2009-08-13 16:05:04 +0000 | [diff] [blame] | 505 | |
Andrew Trick | baeaabb | 2012-11-06 03:13:46 +0000 | [diff] [blame] | 506 | if (!SU->addPred(Dep) && !Dep.isCtrl() && OpSU->NumRegDefsLeft > 1) { |
Andrew Trick | d0548ae | 2011-02-04 03:18:17 +0000 | [diff] [blame] | 507 | // Multiple register uses are combined in the same SUnit. For example, |
| 508 | // we could have a set of glued nodes with all their defs consumed by |
| 509 | // another set of glued nodes. Register pressure tracking sees this as |
| 510 | // a single use, so to keep pressure balanced we reduce the defs. |
Andrew Trick | 072ed2e | 2011-03-09 19:12:43 +0000 | [diff] [blame] | 511 | // |
| 512 | // We can't tell (without more book-keeping) if this results from |
| 513 | // glued nodes or duplicate operands. As long as we don't reduce |
| 514 | // NumRegDefsLeft to zero, we handle the common cases well. |
Andrew Trick | d0548ae | 2011-02-04 03:18:17 +0000 | [diff] [blame] | 515 | --OpSU->NumRegDefsLeft; |
| 516 | } |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
Dan Gohman | 04543e7 | 2008-12-23 18:36:58 +0000 | [diff] [blame] | 522 | /// BuildSchedGraph - Build the SUnit graph from the selection dag that we |
| 523 | /// are input. This SUnit graph is similar to the SelectionDAG, but |
| 524 | /// excludes nodes that aren't interesting to scheduling, and represents |
Chris Lattner | 11a3381 | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 525 | /// glued together nodes with a single SUnit. |
Dan Gohman | 918ec53 | 2009-10-09 23:33:48 +0000 | [diff] [blame] | 526 | void ScheduleDAGSDNodes::BuildSchedGraph(AliasAnalysis *AA) { |
Evan Cheng | 38f6560 | 2010-06-10 02:09:31 +0000 | [diff] [blame] | 527 | // Cluster certain nodes which should be scheduled together. |
| 528 | ClusterNodes(); |
Dan Gohman | 04543e7 | 2008-12-23 18:36:58 +0000 | [diff] [blame] | 529 | // Populate the SUnits array. |
| 530 | BuildSchedUnits(); |
| 531 | // Compute all the scheduling dependencies between nodes. |
| 532 | AddSchedEdges(); |
| 533 | } |
| 534 | |
Andrew Trick | d0548ae | 2011-02-04 03:18:17 +0000 | [diff] [blame] | 535 | // Initialize NumNodeDefs for the current Node's opcode. |
| 536 | void ScheduleDAGSDNodes::RegDefIter::InitNodeNumDefs() { |
Eric Christopher | 7238cba | 2011-03-08 19:35:47 +0000 | [diff] [blame] | 537 | // Check for phys reg copy. |
| 538 | if (!Node) |
| 539 | return; |
| 540 | |
Andrew Trick | d0548ae | 2011-02-04 03:18:17 +0000 | [diff] [blame] | 541 | if (!Node->isMachineOpcode()) { |
| 542 | if (Node->getOpcode() == ISD::CopyFromReg) |
| 543 | NodeNumDefs = 1; |
| 544 | else |
| 545 | NodeNumDefs = 0; |
| 546 | return; |
| 547 | } |
| 548 | unsigned POpc = Node->getMachineOpcode(); |
| 549 | if (POpc == TargetOpcode::IMPLICIT_DEF) { |
| 550 | // No register need be allocated for this. |
| 551 | NodeNumDefs = 0; |
| 552 | return; |
| 553 | } |
| 554 | unsigned NRegDefs = SchedDAG->TII->get(Node->getMachineOpcode()).getNumDefs(); |
| 555 | // Some instructions define regs that are not represented in the selection DAG |
| 556 | // (e.g. unused flags). See tMOVi8. Make sure we don't access past NumValues. |
| 557 | NodeNumDefs = std::min(Node->getNumValues(), NRegDefs); |
| 558 | DefIdx = 0; |
| 559 | } |
| 560 | |
| 561 | // Construct a RegDefIter for this SUnit and find the first valid value. |
| 562 | ScheduleDAGSDNodes::RegDefIter::RegDefIter(const SUnit *SU, |
| 563 | const ScheduleDAGSDNodes *SD) |
| 564 | : SchedDAG(SD), Node(SU->getNode()), DefIdx(0), NodeNumDefs(0) { |
| 565 | InitNodeNumDefs(); |
| 566 | Advance(); |
| 567 | } |
| 568 | |
| 569 | // Advance to the next valid value defined by the SUnit. |
| 570 | void ScheduleDAGSDNodes::RegDefIter::Advance() { |
| 571 | for (;Node;) { // Visit all glued nodes. |
| 572 | for (;DefIdx < NodeNumDefs; ++DefIdx) { |
| 573 | if (!Node->hasAnyUseOfValue(DefIdx)) |
| 574 | continue; |
Patrik Hagglund | 0539435 | 2012-12-13 18:45:35 +0000 | [diff] [blame] | 575 | ValueType = Node->getSimpleValueType(DefIdx); |
Andrew Trick | d0548ae | 2011-02-04 03:18:17 +0000 | [diff] [blame] | 576 | ++DefIdx; |
| 577 | return; // Found a normal regdef. |
| 578 | } |
| 579 | Node = Node->getGluedNode(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 580 | if (!Node) { |
Andrew Trick | d0548ae | 2011-02-04 03:18:17 +0000 | [diff] [blame] | 581 | return; // No values left to visit. |
| 582 | } |
| 583 | InitNodeNumDefs(); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | void ScheduleDAGSDNodes::InitNumRegDefsLeft(SUnit *SU) { |
| 588 | assert(SU->NumRegDefsLeft == 0 && "expect a new node"); |
| 589 | for (RegDefIter I(SU, this); I.IsValid(); I.Advance()) { |
| 590 | assert(SU->NumRegDefsLeft < USHRT_MAX && "overflow is ok but unexpected"); |
| 591 | ++SU->NumRegDefsLeft; |
| 592 | } |
| 593 | } |
| 594 | |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 595 | void ScheduleDAGSDNodes::computeLatency(SUnit *SU) { |
Andrew Trick | b53a00d | 2011-04-13 00:38:32 +0000 | [diff] [blame] | 596 | SDNode *N = SU->getNode(); |
| 597 | |
| 598 | // TokenFactor operands are considered zero latency, and some schedulers |
| 599 | // (e.g. Top-Down list) may rely on the fact that operand latency is nonzero |
| 600 | // whenever node latency is nonzero. |
| 601 | if (N && N->getOpcode() == ISD::TokenFactor) { |
| 602 | SU->Latency = 0; |
| 603 | return; |
| 604 | } |
| 605 | |
Evan Cheng | 70e506e | 2010-05-19 22:42:23 +0000 | [diff] [blame] | 606 | // Check to see if the scheduler cares about latencies. |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 607 | if (forceUnitLatencies()) { |
Evan Cheng | 70e506e | 2010-05-19 22:42:23 +0000 | [diff] [blame] | 608 | SU->Latency = 1; |
| 609 | return; |
| 610 | } |
| 611 | |
Evan Cheng | bf40707 | 2010-09-10 01:29:16 +0000 | [diff] [blame] | 612 | if (!InstrItins || InstrItins->isEmpty()) { |
Andrew Trick | d7f4c21 | 2011-03-05 09:18:16 +0000 | [diff] [blame] | 613 | if (N && N->isMachineOpcode() && |
| 614 | TII->isHighLatencyDef(N->getMachineOpcode())) |
Andrew Trick | 641e2d4 | 2011-03-05 08:00:22 +0000 | [diff] [blame] | 615 | SU->Latency = HighLatencyCycles; |
| 616 | else |
| 617 | SU->Latency = 1; |
Evan Cheng | bdd062d | 2010-05-20 06:13:19 +0000 | [diff] [blame] | 618 | return; |
| 619 | } |
Andrew Trick | 3f924e4 | 2011-02-03 23:00:17 +0000 | [diff] [blame] | 620 | |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 621 | // Compute the latency for the node. We use the sum of the latencies for |
Chris Lattner | 11a3381 | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 622 | // all nodes glued together into this SUnit. |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 623 | SU->Latency = 0; |
Chris Lattner | 11a3381 | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 624 | for (SDNode *N = SU->getNode(); N; N = N->getGluedNode()) |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 625 | if (N->isMachineOpcode()) |
| 626 | SU->Latency += TII->getInstrLatency(InstrItins, N); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 629 | void ScheduleDAGSDNodes::computeOperandLatency(SDNode *Def, SDNode *Use, |
Evan Cheng | bdd062d | 2010-05-20 06:13:19 +0000 | [diff] [blame] | 630 | unsigned OpIdx, SDep& dep) const{ |
| 631 | // Check to see if the scheduler cares about latencies. |
Andrew Trick | 52226d4 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 632 | if (forceUnitLatencies()) |
Evan Cheng | bdd062d | 2010-05-20 06:13:19 +0000 | [diff] [blame] | 633 | return; |
| 634 | |
Evan Cheng | bdd062d | 2010-05-20 06:13:19 +0000 | [diff] [blame] | 635 | if (dep.getKind() != SDep::Data) |
| 636 | return; |
| 637 | |
| 638 | unsigned DefIdx = Use->getOperand(OpIdx).getResNo(); |
Evan Cheng | ff31073 | 2010-10-28 06:47:08 +0000 | [diff] [blame] | 639 | if (Use->isMachineOpcode()) |
| 640 | // Adjust the use operand index by num of defs. |
| 641 | OpIdx += TII->get(Use->getMachineOpcode()).getNumDefs(); |
Evan Cheng | 49d4c0b | 2010-10-06 06:27:31 +0000 | [diff] [blame] | 642 | int Latency = TII->getOperandLatency(InstrItins, Def, DefIdx, Use, OpIdx); |
Evan Cheng | 6c1414f | 2010-10-29 18:09:28 +0000 | [diff] [blame] | 643 | if (Latency > 1 && Use->getOpcode() == ISD::CopyToReg && |
| 644 | !BB->succ_empty()) { |
| 645 | unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg(); |
| 646 | if (TargetRegisterInfo::isVirtualRegister(Reg)) |
| 647 | // This copy is a liveout value. It is likely coalesced, so reduce the |
| 648 | // latency so not to penalize the def. |
| 649 | // FIXME: need target specific adjustment here? |
| 650 | Latency = (Latency > 1) ? Latency - 1 : 1; |
| 651 | } |
Evan Cheng | 4a010fd | 2010-09-29 22:42:35 +0000 | [diff] [blame] | 652 | if (Latency >= 0) |
| 653 | dep.setLatency(Latency); |
Evan Cheng | bdd062d | 2010-05-20 06:13:19 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 656 | void ScheduleDAGSDNodes::dumpNode(const SUnit *SU) const { |
Manman Ren | 19f49ac | 2012-09-11 22:23:19 +0000 | [diff] [blame] | 657 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Evan Cheng | b2c42c6 | 2009-01-12 03:19:55 +0000 | [diff] [blame] | 658 | if (!SU->getNode()) { |
David Greene | 4eb5bed | 2010-01-05 01:25:11 +0000 | [diff] [blame] | 659 | dbgs() << "PHYS REG COPY\n"; |
Evan Cheng | b2c42c6 | 2009-01-12 03:19:55 +0000 | [diff] [blame] | 660 | return; |
| 661 | } |
| 662 | |
| 663 | SU->getNode()->dump(DAG); |
David Greene | 4eb5bed | 2010-01-05 01:25:11 +0000 | [diff] [blame] | 664 | dbgs() << "\n"; |
Chris Lattner | 11a3381 | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 665 | SmallVector<SDNode *, 4> GluedNodes; |
| 666 | for (SDNode *N = SU->getNode()->getGluedNode(); N; N = N->getGluedNode()) |
| 667 | GluedNodes.push_back(N); |
| 668 | while (!GluedNodes.empty()) { |
David Greene | 4eb5bed | 2010-01-05 01:25:11 +0000 | [diff] [blame] | 669 | dbgs() << " "; |
Chris Lattner | 11a3381 | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 670 | GluedNodes.back()->dump(DAG); |
David Greene | 4eb5bed | 2010-01-05 01:25:11 +0000 | [diff] [blame] | 671 | dbgs() << "\n"; |
Chris Lattner | 11a3381 | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 672 | GluedNodes.pop_back(); |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 673 | } |
Manman Ren | 742534c | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 674 | #endif |
Dan Gohman | 60cb69e | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 675 | } |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 676 | |
Manman Ren | 19f49ac | 2012-09-11 22:23:19 +0000 | [diff] [blame] | 677 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Andrew Trick | edee68c | 2012-03-07 05:21:40 +0000 | [diff] [blame] | 678 | void ScheduleDAGSDNodes::dumpSchedule() const { |
| 679 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
| 680 | if (SUnit *SU = Sequence[i]) |
| 681 | SU->dump(this); |
| 682 | else |
| 683 | dbgs() << "**** NOOP ****\n"; |
| 684 | } |
| 685 | } |
Manman Ren | 742534c | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 686 | #endif |
Andrew Trick | edee68c | 2012-03-07 05:21:40 +0000 | [diff] [blame] | 687 | |
Andrew Trick | 46a5866 | 2012-03-07 05:21:36 +0000 | [diff] [blame] | 688 | #ifndef NDEBUG |
| 689 | /// VerifyScheduledSequence - Verify that all SUnits were scheduled and that |
| 690 | /// their state is consistent with the nodes listed in Sequence. |
| 691 | /// |
| 692 | void ScheduleDAGSDNodes::VerifyScheduledSequence(bool isBottomUp) { |
| 693 | unsigned ScheduledNodes = ScheduleDAG::VerifyScheduledDAG(isBottomUp); |
| 694 | unsigned Noops = 0; |
| 695 | for (unsigned i = 0, e = Sequence.size(); i != e; ++i) |
| 696 | if (!Sequence[i]) |
| 697 | ++Noops; |
| 698 | assert(Sequence.size() - Noops == ScheduledNodes && |
| 699 | "The number of nodes scheduled doesn't match the expected number!"); |
| 700 | } |
| 701 | #endif // NDEBUG |
| 702 | |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 703 | /// ProcessSDDbgValues - Process SDDbgValues associated with this node. |
Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 704 | static void |
| 705 | ProcessSDDbgValues(SDNode *N, SelectionDAG *DAG, InstrEmitter &Emitter, |
| 706 | SmallVectorImpl<std::pair<unsigned, MachineInstr*> > &Orders, |
| 707 | DenseMap<SDValue, unsigned> &VRBaseMap, unsigned Order) { |
Devang Patel | 1448e7c | 2011-01-26 18:20:04 +0000 | [diff] [blame] | 708 | if (!N->getHasDebugValue()) |
| 709 | return; |
| 710 | |
| 711 | // Opportunistically insert immediate dbg_value uses, i.e. those with source |
| 712 | // order number right after the N. |
| 713 | MachineBasicBlock *BB = Emitter.getBlock(); |
| 714 | MachineBasicBlock::iterator InsertPos = Emitter.getInsertPos(); |
Benjamin Kramer | e1fc29b | 2011-06-18 13:13:44 +0000 | [diff] [blame] | 715 | ArrayRef<SDDbgValue*> DVs = DAG->GetDbgValues(N); |
Devang Patel | 1448e7c | 2011-01-26 18:20:04 +0000 | [diff] [blame] | 716 | for (unsigned i = 0, e = DVs.size(); i != e; ++i) { |
| 717 | if (DVs[i]->isInvalidated()) |
| 718 | continue; |
| 719 | unsigned DVOrder = DVs[i]->getOrder(); |
| 720 | if (!Order || DVOrder == ++Order) { |
| 721 | MachineInstr *DbgMI = Emitter.EmitDbgValue(DVs[i], VRBaseMap); |
| 722 | if (DbgMI) { |
| 723 | Orders.push_back(std::make_pair(DVOrder, DbgMI)); |
| 724 | BB->insert(InsertPos, DbgMI); |
| 725 | } |
| 726 | DVs[i]->setIsInvalidated(); |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 731 | // ProcessSourceNode - Process nodes with source order numbers. These are added |
Jim Grosbach | caf9b3a | 2010-06-30 21:27:56 +0000 | [diff] [blame] | 732 | // to a vector which EmitSchedule uses to determine how to insert dbg_value |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 733 | // instructions in the right order. |
Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 734 | static void |
| 735 | ProcessSourceNode(SDNode *N, SelectionDAG *DAG, InstrEmitter &Emitter, |
| 736 | DenseMap<SDValue, unsigned> &VRBaseMap, |
| 737 | SmallVectorImpl<std::pair<unsigned, MachineInstr*> > &Orders, |
| 738 | SmallSet<unsigned, 8> &Seen) { |
Andrew Trick | e2431c6 | 2013-05-25 03:08:10 +0000 | [diff] [blame] | 739 | unsigned Order = N->getIROrder(); |
Devang Patel | 92b7077 | 2011-01-27 00:13:27 +0000 | [diff] [blame] | 740 | if (!Order || !Seen.insert(Order)) { |
| 741 | // Process any valid SDDbgValues even if node does not have any order |
| 742 | // assigned. |
| 743 | ProcessSDDbgValues(N, DAG, Emitter, Orders, VRBaseMap, 0); |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 744 | return; |
Devang Patel | 92b7077 | 2011-01-27 00:13:27 +0000 | [diff] [blame] | 745 | } |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 746 | |
| 747 | MachineBasicBlock *BB = Emitter.getBlock(); |
Bill Schmidt | 3684fdd | 2013-10-18 14:20:11 +0000 | [diff] [blame] | 748 | if (Emitter.getInsertPos() == BB->begin() || BB->back().isPHI() || |
| 749 | // Fast-isel may have inserted some instructions, in which case the |
| 750 | // BB->back().isPHI() test will not fire when we want it to. |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 751 | std::prev(Emitter.getInsertPos())->isPHI()) { |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 752 | // Did not insert any instruction. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 753 | Orders.push_back(std::make_pair(Order, (MachineInstr*)nullptr)); |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 754 | return; |
| 755 | } |
| 756 | |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 757 | Orders.push_back(std::make_pair(Order, std::prev(Emitter.getInsertPos()))); |
Devang Patel | 1448e7c | 2011-01-26 18:20:04 +0000 | [diff] [blame] | 758 | ProcessSDDbgValues(N, DAG, Emitter, Orders, VRBaseMap, Order); |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Andrew Trick | e932bb7 | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 761 | void ScheduleDAGSDNodes:: |
| 762 | EmitPhysRegCopy(SUnit *SU, DenseMap<SUnit*, unsigned> &VRBaseMap, |
| 763 | MachineBasicBlock::iterator InsertPos) { |
| 764 | for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 765 | I != E; ++I) { |
| 766 | if (I->isCtrl()) continue; // ignore chain preds |
| 767 | if (I->getSUnit()->CopyDstRC) { |
| 768 | // Copy to physical register. |
| 769 | DenseMap<SUnit*, unsigned>::iterator VRI = VRBaseMap.find(I->getSUnit()); |
| 770 | assert(VRI != VRBaseMap.end() && "Node emitted out of order - late"); |
| 771 | // Find the destination physical register. |
| 772 | unsigned Reg = 0; |
| 773 | for (SUnit::const_succ_iterator II = SU->Succs.begin(), |
| 774 | EE = SU->Succs.end(); II != EE; ++II) { |
| 775 | if (II->isCtrl()) continue; // ignore chain preds |
| 776 | if (II->getReg()) { |
| 777 | Reg = II->getReg(); |
| 778 | break; |
| 779 | } |
| 780 | } |
| 781 | BuildMI(*BB, InsertPos, DebugLoc(), TII->get(TargetOpcode::COPY), Reg) |
| 782 | .addReg(VRI->second); |
| 783 | } else { |
| 784 | // Copy from physical register. |
| 785 | assert(I->getReg() && "Unknown physical register!"); |
| 786 | unsigned VRBase = MRI.createVirtualRegister(SU->CopyDstRC); |
| 787 | bool isNew = VRBaseMap.insert(std::make_pair(SU, VRBase)).second; |
| 788 | (void)isNew; // Silence compiler warning. |
| 789 | assert(isNew && "Node emitted out of order - early"); |
| 790 | BuildMI(*BB, InsertPos, DebugLoc(), TII->get(TargetOpcode::COPY), VRBase) |
| 791 | .addReg(I->getReg()); |
| 792 | } |
| 793 | break; |
| 794 | } |
| 795 | } |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 796 | |
Andrew Trick | e932bb7 | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 797 | /// EmitSchedule - Emit the machine code in scheduled order. Return the new |
| 798 | /// InsertPos and MachineBasicBlock that contains this insertion |
| 799 | /// point. ScheduleDAGSDNodes holds a BB pointer for convenience, but this does |
| 800 | /// not necessarily refer to returned BB. The emitter may split blocks. |
Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 801 | MachineBasicBlock *ScheduleDAGSDNodes:: |
| 802 | EmitSchedule(MachineBasicBlock::iterator &InsertPos) { |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 803 | InstrEmitter Emitter(BB, InsertPos); |
| 804 | DenseMap<SDValue, unsigned> VRBaseMap; |
| 805 | DenseMap<SUnit*, unsigned> CopyVRBaseMap; |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 806 | SmallVector<std::pair<unsigned, MachineInstr*>, 32> Orders; |
| 807 | SmallSet<unsigned, 8> Seen; |
| 808 | bool HasDbg = DAG->hasDebugValues(); |
Dale Johannesen | 49de060 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 809 | |
Dale Johannesen | e098352 | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 810 | // If this is the first BB, emit byval parameter dbg_value's. |
| 811 | if (HasDbg && BB->getParent()->begin() == MachineFunction::iterator(BB)) { |
| 812 | SDDbgInfo::DbgIterator PDI = DAG->ByvalParmDbgBegin(); |
| 813 | SDDbgInfo::DbgIterator PDE = DAG->ByvalParmDbgEnd(); |
| 814 | for (; PDI != PDE; ++PDI) { |
Dan Gohman | 8acc8f7 | 2010-04-30 19:35:33 +0000 | [diff] [blame] | 815 | MachineInstr *DbgMI= Emitter.EmitDbgValue(*PDI, VRBaseMap); |
Dale Johannesen | e098352 | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 816 | if (DbgMI) |
Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 817 | BB->insert(InsertPos, DbgMI); |
Dale Johannesen | e098352 | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 821 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
| 822 | SUnit *SU = Sequence[i]; |
| 823 | if (!SU) { |
| 824 | // Null SUnit* is a noop. |
Andrew Trick | e932bb7 | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 825 | TII->insertNoop(*Emitter.getBlock(), InsertPos); |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 826 | continue; |
| 827 | } |
| 828 | |
| 829 | // For pre-regalloc scheduling, create instructions corresponding to the |
Chris Lattner | 11a3381 | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 830 | // SDNode and any glued SDNodes and append them to the block. |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 831 | if (!SU->getNode()) { |
| 832 | // Emit a copy. |
Andrew Trick | e932bb7 | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 833 | EmitPhysRegCopy(SU, CopyVRBaseMap, InsertPos); |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 834 | continue; |
| 835 | } |
| 836 | |
Chris Lattner | 11a3381 | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 837 | SmallVector<SDNode *, 4> GluedNodes; |
Evan Cheng | 839fb65 | 2012-10-17 19:39:36 +0000 | [diff] [blame] | 838 | for (SDNode *N = SU->getNode()->getGluedNode(); N; N = N->getGluedNode()) |
Chris Lattner | 11a3381 | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 839 | GluedNodes.push_back(N); |
| 840 | while (!GluedNodes.empty()) { |
| 841 | SDNode *N = GluedNodes.back(); |
| 842 | Emitter.EmitNode(GluedNodes.back(), SU->OrigNode != SU, SU->isCloned, |
Dan Gohman | 25c1653 | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 843 | VRBaseMap); |
Dale Johannesen | e098352 | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 844 | // Remember the source order of the inserted instruction. |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 845 | if (HasDbg) |
Dan Gohman | 8acc8f7 | 2010-04-30 19:35:33 +0000 | [diff] [blame] | 846 | ProcessSourceNode(N, DAG, Emitter, VRBaseMap, Orders, Seen); |
Chris Lattner | 11a3381 | 2010-12-23 17:24:32 +0000 | [diff] [blame] | 847 | GluedNodes.pop_back(); |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 848 | } |
| 849 | Emitter.EmitNode(SU->getNode(), SU->OrigNode != SU, SU->isCloned, |
Dan Gohman | 25c1653 | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 850 | VRBaseMap); |
Dale Johannesen | e098352 | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 851 | // Remember the source order of the inserted instruction. |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 852 | if (HasDbg) |
Dan Gohman | 8acc8f7 | 2010-04-30 19:35:33 +0000 | [diff] [blame] | 853 | ProcessSourceNode(SU->getNode(), DAG, Emitter, VRBaseMap, Orders, |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 854 | Seen); |
| 855 | } |
| 856 | |
Dale Johannesen | e098352 | 2010-04-26 20:06:49 +0000 | [diff] [blame] | 857 | // Insert all the dbg_values which have not already been inserted in source |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 858 | // order sequence. |
| 859 | if (HasDbg) { |
Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 860 | MachineBasicBlock::iterator BBBegin = BB->getFirstNonPHI(); |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 861 | |
| 862 | // Sort the source order instructions and use the order to insert debug |
| 863 | // values. |
Benjamin Kramer | b12cf01 | 2013-08-24 12:54:27 +0000 | [diff] [blame] | 864 | std::sort(Orders.begin(), Orders.end(), less_first()); |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 865 | |
| 866 | SDDbgInfo::DbgIterator DI = DAG->DbgBegin(); |
| 867 | SDDbgInfo::DbgIterator DE = DAG->DbgEnd(); |
| 868 | // Now emit the rest according to source order. |
| 869 | unsigned LastOrder = 0; |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 870 | for (unsigned i = 0, e = Orders.size(); i != e && DI != DE; ++i) { |
| 871 | unsigned Order = Orders[i].first; |
| 872 | MachineInstr *MI = Orders[i].second; |
| 873 | // Insert all SDDbgValue's whose order(s) are before "Order". |
| 874 | if (!MI) |
| 875 | continue; |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 876 | for (; DI != DE && |
| 877 | (*DI)->getOrder() >= LastOrder && (*DI)->getOrder() < Order; ++DI) { |
| 878 | if ((*DI)->isInvalidated()) |
| 879 | continue; |
Dan Gohman | 8acc8f7 | 2010-04-30 19:35:33 +0000 | [diff] [blame] | 880 | MachineInstr *DbgMI = Emitter.EmitDbgValue(*DI, VRBaseMap); |
Evan Cheng | ed69b38 | 2010-04-26 07:38:55 +0000 | [diff] [blame] | 881 | if (DbgMI) { |
| 882 | if (!LastOrder) |
| 883 | // Insert to start of the BB (after PHIs). |
| 884 | BB->insert(BBBegin, DbgMI); |
| 885 | else { |
Dan Gohman | a64a323 | 2010-07-10 22:42:31 +0000 | [diff] [blame] | 886 | // Insert at the instruction, which may be in a different |
| 887 | // block, if the block was split by a custom inserter. |
Evan Cheng | ed69b38 | 2010-04-26 07:38:55 +0000 | [diff] [blame] | 888 | MachineBasicBlock::iterator Pos = MI; |
Andrew Trick | c66d26a | 2013-05-26 08:58:50 +0000 | [diff] [blame] | 889 | MI->getParent()->insert(Pos, DbgMI); |
Evan Cheng | ed69b38 | 2010-04-26 07:38:55 +0000 | [diff] [blame] | 890 | } |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 891 | } |
Dale Johannesen | 49de060 | 2010-03-10 22:13:47 +0000 | [diff] [blame] | 892 | } |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 893 | LastOrder = Order; |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 894 | } |
| 895 | // Add trailing DbgValue's before the terminator. FIXME: May want to add |
| 896 | // some of them before one or more conditional branches? |
Bill Wendling | 618d573 | 2012-03-14 07:14:25 +0000 | [diff] [blame] | 897 | SmallVector<MachineInstr*, 8> DbgMIs; |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 898 | while (DI != DE) { |
Bill Wendling | 618d573 | 2012-03-14 07:14:25 +0000 | [diff] [blame] | 899 | if (!(*DI)->isInvalidated()) |
| 900 | if (MachineInstr *DbgMI = Emitter.EmitDbgValue(*DI, VRBaseMap)) |
| 901 | DbgMIs.push_back(DbgMI); |
Evan Cheng | 563fe3c | 2010-03-25 01:38:16 +0000 | [diff] [blame] | 902 | ++DI; |
| 903 | } |
Bill Wendling | 618d573 | 2012-03-14 07:14:25 +0000 | [diff] [blame] | 904 | |
| 905 | MachineBasicBlock *InsertBB = Emitter.getBlock(); |
| 906 | MachineBasicBlock::iterator Pos = InsertBB->getFirstTerminator(); |
| 907 | InsertBB->insert(Pos, DbgMIs.begin(), DbgMIs.end()); |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 910 | InsertPos = Emitter.getInsertPos(); |
Andrew Trick | 60cf03e | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 911 | return Emitter.getBlock(); |
Dan Gohman | b812077 | 2009-10-10 01:32:21 +0000 | [diff] [blame] | 912 | } |
Andrew Trick | 1b2324d | 2012-03-07 00:18:22 +0000 | [diff] [blame] | 913 | |
| 914 | /// Return the basic block label. |
| 915 | std::string ScheduleDAGSDNodes::getDAGName() const { |
| 916 | return "sunit-dag." + BB->getFullName(); |
| 917 | } |