ScheduleDAGSDNodes.cpp 38 KB

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