ScheduleDAGSDNodes.cpp 38 KB

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