InstrEmitter.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. //==--- InstrEmitter.cpp - Emit MachineInstrs for the SelectionDAG 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 Emit routines for the SelectionDAG class, which creates
  10. // MachineInstrs based on the decisions of the SelectionDAG instruction
  11. // selection.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "InstrEmitter.h"
  15. #include "SDNodeDbgValue.h"
  16. #include "llvm/ADT/Statistic.h"
  17. #include "llvm/CodeGen/MachineConstantPool.h"
  18. #include "llvm/CodeGen/MachineFunction.h"
  19. #include "llvm/CodeGen/MachineInstrBuilder.h"
  20. #include "llvm/CodeGen/MachineRegisterInfo.h"
  21. #include "llvm/CodeGen/StackMaps.h"
  22. #include "llvm/CodeGen/TargetInstrInfo.h"
  23. #include "llvm/CodeGen/TargetLowering.h"
  24. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  25. #include "llvm/IR/DataLayout.h"
  26. #include "llvm/IR/DebugInfo.h"
  27. #include "llvm/Support/Debug.h"
  28. #include "llvm/Support/ErrorHandling.h"
  29. #include "llvm/Support/MathExtras.h"
  30. using namespace llvm;
  31. #define DEBUG_TYPE "instr-emitter"
  32. /// MinRCSize - Smallest register class we allow when constraining virtual
  33. /// registers. If satisfying all register class constraints would require
  34. /// using a smaller register class, emit a COPY to a new virtual register
  35. /// instead.
  36. const unsigned MinRCSize = 4;
  37. /// CountResults - The results of target nodes have register or immediate
  38. /// operands first, then an optional chain, and optional glue operands (which do
  39. /// not go into the resulting MachineInstr).
  40. unsigned InstrEmitter::CountResults(SDNode *Node) {
  41. unsigned N = Node->getNumValues();
  42. while (N && Node->getValueType(N - 1) == MVT::Glue)
  43. --N;
  44. if (N && Node->getValueType(N - 1) == MVT::Other)
  45. --N; // Skip over chain result.
  46. return N;
  47. }
  48. /// countOperands - The inputs to target nodes have any actual inputs first,
  49. /// followed by an optional chain operand, then an optional glue operand.
  50. /// Compute the number of actual operands that will go into the resulting
  51. /// MachineInstr.
  52. ///
  53. /// Also count physreg RegisterSDNode and RegisterMaskSDNode operands preceding
  54. /// the chain and glue. These operands may be implicit on the machine instr.
  55. static unsigned countOperands(SDNode *Node, unsigned NumExpUses,
  56. unsigned &NumImpUses) {
  57. unsigned N = Node->getNumOperands();
  58. while (N && Node->getOperand(N - 1).getValueType() == MVT::Glue)
  59. --N;
  60. if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
  61. --N; // Ignore chain if it exists.
  62. // Count RegisterSDNode and RegisterMaskSDNode operands for NumImpUses.
  63. NumImpUses = N - NumExpUses;
  64. for (unsigned I = N; I > NumExpUses; --I) {
  65. if (isa<RegisterMaskSDNode>(Node->getOperand(I - 1)))
  66. continue;
  67. if (RegisterSDNode *RN = dyn_cast<RegisterSDNode>(Node->getOperand(I - 1)))
  68. if (Register::isPhysicalRegister(RN->getReg()))
  69. continue;
  70. NumImpUses = N - I;
  71. break;
  72. }
  73. return N;
  74. }
  75. /// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an
  76. /// implicit physical register output.
  77. void InstrEmitter::
  78. EmitCopyFromReg(SDNode *Node, unsigned ResNo, bool IsClone, bool IsCloned,
  79. unsigned SrcReg, DenseMap<SDValue, unsigned> &VRBaseMap) {
  80. unsigned VRBase = 0;
  81. if (Register::isVirtualRegister(SrcReg)) {
  82. // Just use the input register directly!
  83. SDValue Op(Node, ResNo);
  84. if (IsClone)
  85. VRBaseMap.erase(Op);
  86. bool isNew = VRBaseMap.insert(std::make_pair(Op, SrcReg)).second;
  87. (void)isNew; // Silence compiler warning.
  88. assert(isNew && "Node emitted out of order - early");
  89. return;
  90. }
  91. // If the node is only used by a CopyToReg and the dest reg is a vreg, use
  92. // the CopyToReg'd destination register instead of creating a new vreg.
  93. bool MatchReg = true;
  94. const TargetRegisterClass *UseRC = nullptr;
  95. MVT VT = Node->getSimpleValueType(ResNo);
  96. // Stick to the preferred register classes for legal types.
  97. if (TLI->isTypeLegal(VT))
  98. UseRC = TLI->getRegClassFor(VT, Node->isDivergent());
  99. if (!IsClone && !IsCloned)
  100. for (SDNode *User : Node->uses()) {
  101. bool Match = true;
  102. if (User->getOpcode() == ISD::CopyToReg &&
  103. User->getOperand(2).getNode() == Node &&
  104. User->getOperand(2).getResNo() == ResNo) {
  105. unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
  106. if (Register::isVirtualRegister(DestReg)) {
  107. VRBase = DestReg;
  108. Match = false;
  109. } else if (DestReg != SrcReg)
  110. Match = false;
  111. } else {
  112. for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
  113. SDValue Op = User->getOperand(i);
  114. if (Op.getNode() != Node || Op.getResNo() != ResNo)
  115. continue;
  116. MVT VT = Node->getSimpleValueType(Op.getResNo());
  117. if (VT == MVT::Other || VT == MVT::Glue)
  118. continue;
  119. Match = false;
  120. if (User->isMachineOpcode()) {
  121. const MCInstrDesc &II = TII->get(User->getMachineOpcode());
  122. const TargetRegisterClass *RC = nullptr;
  123. if (i+II.getNumDefs() < II.getNumOperands()) {
  124. RC = TRI->getAllocatableClass(
  125. TII->getRegClass(II, i+II.getNumDefs(), TRI, *MF));
  126. }
  127. if (!UseRC)
  128. UseRC = RC;
  129. else if (RC) {
  130. const TargetRegisterClass *ComRC =
  131. TRI->getCommonSubClass(UseRC, RC);
  132. // If multiple uses expect disjoint register classes, we emit
  133. // copies in AddRegisterOperand.
  134. if (ComRC)
  135. UseRC = ComRC;
  136. }
  137. }
  138. }
  139. }
  140. MatchReg &= Match;
  141. if (VRBase)
  142. break;
  143. }
  144. const TargetRegisterClass *SrcRC = nullptr, *DstRC = nullptr;
  145. SrcRC = TRI->getMinimalPhysRegClass(SrcReg, VT);
  146. // Figure out the register class to create for the destreg.
  147. if (VRBase) {
  148. DstRC = MRI->getRegClass(VRBase);
  149. } else if (UseRC) {
  150. assert(TRI->isTypeLegalForClass(*UseRC, VT) &&
  151. "Incompatible phys register def and uses!");
  152. DstRC = UseRC;
  153. } else {
  154. DstRC = TLI->getRegClassFor(VT, Node->isDivergent());
  155. }
  156. // If all uses are reading from the src physical register and copying the
  157. // register is either impossible or very expensive, then don't create a copy.
  158. if (MatchReg && SrcRC->getCopyCost() < 0) {
  159. VRBase = SrcReg;
  160. } else {
  161. // Create the reg, emit the copy.
  162. VRBase = MRI->createVirtualRegister(DstRC);
  163. BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY),
  164. VRBase).addReg(SrcReg);
  165. }
  166. SDValue Op(Node, ResNo);
  167. if (IsClone)
  168. VRBaseMap.erase(Op);
  169. bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
  170. (void)isNew; // Silence compiler warning.
  171. assert(isNew && "Node emitted out of order - early");
  172. }
  173. void InstrEmitter::CreateVirtualRegisters(SDNode *Node,
  174. MachineInstrBuilder &MIB,
  175. const MCInstrDesc &II,
  176. bool IsClone, bool IsCloned,
  177. DenseMap<SDValue, unsigned> &VRBaseMap) {
  178. assert(Node->getMachineOpcode() != TargetOpcode::IMPLICIT_DEF &&
  179. "IMPLICIT_DEF should have been handled as a special case elsewhere!");
  180. unsigned NumResults = CountResults(Node);
  181. for (unsigned i = 0; i < II.getNumDefs(); ++i) {
  182. // If the specific node value is only used by a CopyToReg and the dest reg
  183. // is a vreg in the same register class, use the CopyToReg'd destination
  184. // register instead of creating a new vreg.
  185. unsigned VRBase = 0;
  186. const TargetRegisterClass *RC =
  187. TRI->getAllocatableClass(TII->getRegClass(II, i, TRI, *MF));
  188. // Always let the value type influence the used register class. The
  189. // constraints on the instruction may be too lax to represent the value
  190. // type correctly. For example, a 64-bit float (X86::FR64) can't live in
  191. // the 32-bit float super-class (X86::FR32).
  192. if (i < NumResults && TLI->isTypeLegal(Node->getSimpleValueType(i))) {
  193. const TargetRegisterClass *VTRC = TLI->getRegClassFor(
  194. Node->getSimpleValueType(i),
  195. (Node->isDivergent() || (RC && TRI->isDivergentRegClass(RC))));
  196. if (RC)
  197. VTRC = TRI->getCommonSubClass(RC, VTRC);
  198. if (VTRC)
  199. RC = VTRC;
  200. }
  201. if (II.OpInfo[i].isOptionalDef()) {
  202. // Optional def must be a physical register.
  203. VRBase = cast<RegisterSDNode>(Node->getOperand(i-NumResults))->getReg();
  204. assert(Register::isPhysicalRegister(VRBase));
  205. MIB.addReg(VRBase, RegState::Define);
  206. }
  207. if (!VRBase && !IsClone && !IsCloned)
  208. for (SDNode *User : Node->uses()) {
  209. if (User->getOpcode() == ISD::CopyToReg &&
  210. User->getOperand(2).getNode() == Node &&
  211. User->getOperand(2).getResNo() == i) {
  212. unsigned Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
  213. if (Register::isVirtualRegister(Reg)) {
  214. const TargetRegisterClass *RegRC = MRI->getRegClass(Reg);
  215. if (RegRC == RC) {
  216. VRBase = Reg;
  217. MIB.addReg(VRBase, RegState::Define);
  218. break;
  219. }
  220. }
  221. }
  222. }
  223. // Create the result registers for this node and add the result regs to
  224. // the machine instruction.
  225. if (VRBase == 0) {
  226. assert(RC && "Isn't a register operand!");
  227. VRBase = MRI->createVirtualRegister(RC);
  228. MIB.addReg(VRBase, RegState::Define);
  229. }
  230. // If this def corresponds to a result of the SDNode insert the VRBase into
  231. // the lookup map.
  232. if (i < NumResults) {
  233. SDValue Op(Node, i);
  234. if (IsClone)
  235. VRBaseMap.erase(Op);
  236. bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
  237. (void)isNew; // Silence compiler warning.
  238. assert(isNew && "Node emitted out of order - early");
  239. }
  240. }
  241. }
  242. /// getVR - Return the virtual register corresponding to the specified result
  243. /// of the specified node.
  244. unsigned InstrEmitter::getVR(SDValue Op,
  245. DenseMap<SDValue, unsigned> &VRBaseMap) {
  246. if (Op.isMachineOpcode() &&
  247. Op.getMachineOpcode() == TargetOpcode::IMPLICIT_DEF) {
  248. // Add an IMPLICIT_DEF instruction before every use.
  249. // IMPLICIT_DEF can produce any type of result so its MCInstrDesc
  250. // does not include operand register class info.
  251. const TargetRegisterClass *RC = TLI->getRegClassFor(
  252. Op.getSimpleValueType(), Op.getNode()->isDivergent());
  253. Register VReg = MRI->createVirtualRegister(RC);
  254. BuildMI(*MBB, InsertPos, Op.getDebugLoc(),
  255. TII->get(TargetOpcode::IMPLICIT_DEF), VReg);
  256. return VReg;
  257. }
  258. DenseMap<SDValue, unsigned>::iterator I = VRBaseMap.find(Op);
  259. assert(I != VRBaseMap.end() && "Node emitted out of order - late");
  260. return I->second;
  261. }
  262. /// AddRegisterOperand - Add the specified register as an operand to the
  263. /// specified machine instr. Insert register copies if the register is
  264. /// not in the required register class.
  265. void
  266. InstrEmitter::AddRegisterOperand(MachineInstrBuilder &MIB,
  267. SDValue Op,
  268. unsigned IIOpNum,
  269. const MCInstrDesc *II,
  270. DenseMap<SDValue, unsigned> &VRBaseMap,
  271. bool IsDebug, bool IsClone, bool IsCloned) {
  272. assert(Op.getValueType() != MVT::Other &&
  273. Op.getValueType() != MVT::Glue &&
  274. "Chain and glue operands should occur at end of operand list!");
  275. // Get/emit the operand.
  276. unsigned VReg = getVR(Op, VRBaseMap);
  277. const MCInstrDesc &MCID = MIB->getDesc();
  278. bool isOptDef = IIOpNum < MCID.getNumOperands() &&
  279. MCID.OpInfo[IIOpNum].isOptionalDef();
  280. // If the instruction requires a register in a different class, create
  281. // a new virtual register and copy the value into it, but first attempt to
  282. // shrink VReg's register class within reason. For example, if VReg == GR32
  283. // and II requires a GR32_NOSP, just constrain VReg to GR32_NOSP.
  284. if (II) {
  285. const TargetRegisterClass *OpRC = nullptr;
  286. if (IIOpNum < II->getNumOperands())
  287. OpRC = TII->getRegClass(*II, IIOpNum, TRI, *MF);
  288. if (OpRC) {
  289. const TargetRegisterClass *ConstrainedRC
  290. = MRI->constrainRegClass(VReg, OpRC, MinRCSize);
  291. if (!ConstrainedRC) {
  292. OpRC = TRI->getAllocatableClass(OpRC);
  293. assert(OpRC && "Constraints cannot be fulfilled for allocation");
  294. Register NewVReg = MRI->createVirtualRegister(OpRC);
  295. BuildMI(*MBB, InsertPos, Op.getNode()->getDebugLoc(),
  296. TII->get(TargetOpcode::COPY), NewVReg).addReg(VReg);
  297. VReg = NewVReg;
  298. } else {
  299. assert(ConstrainedRC->isAllocatable() &&
  300. "Constraining an allocatable VReg produced an unallocatable class?");
  301. }
  302. }
  303. }
  304. // If this value has only one use, that use is a kill. This is a
  305. // conservative approximation. InstrEmitter does trivial coalescing
  306. // with CopyFromReg nodes, so don't emit kill flags for them.
  307. // Avoid kill flags on Schedule cloned nodes, since there will be
  308. // multiple uses.
  309. // Tied operands are never killed, so we need to check that. And that
  310. // means we need to determine the index of the operand.
  311. bool isKill = Op.hasOneUse() &&
  312. Op.getNode()->getOpcode() != ISD::CopyFromReg &&
  313. !IsDebug &&
  314. !(IsClone || IsCloned);
  315. if (isKill) {
  316. unsigned Idx = MIB->getNumOperands();
  317. while (Idx > 0 &&
  318. MIB->getOperand(Idx-1).isReg() &&
  319. MIB->getOperand(Idx-1).isImplicit())
  320. --Idx;
  321. bool isTied = MCID.getOperandConstraint(Idx, MCOI::TIED_TO) != -1;
  322. if (isTied)
  323. isKill = false;
  324. }
  325. MIB.addReg(VReg, getDefRegState(isOptDef) | getKillRegState(isKill) |
  326. getDebugRegState(IsDebug));
  327. }
  328. /// AddOperand - Add the specified operand to the specified machine instr. II
  329. /// specifies the instruction information for the node, and IIOpNum is the
  330. /// operand number (in the II) that we are adding.
  331. void InstrEmitter::AddOperand(MachineInstrBuilder &MIB,
  332. SDValue Op,
  333. unsigned IIOpNum,
  334. const MCInstrDesc *II,
  335. DenseMap<SDValue, unsigned> &VRBaseMap,
  336. bool IsDebug, bool IsClone, bool IsCloned) {
  337. if (Op.isMachineOpcode()) {
  338. AddRegisterOperand(MIB, Op, IIOpNum, II, VRBaseMap,
  339. IsDebug, IsClone, IsCloned);
  340. } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
  341. MIB.addImm(C->getSExtValue());
  342. } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) {
  343. MIB.addFPImm(F->getConstantFPValue());
  344. } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
  345. unsigned VReg = R->getReg();
  346. MVT OpVT = Op.getSimpleValueType();
  347. const TargetRegisterClass *IIRC =
  348. II ? TRI->getAllocatableClass(TII->getRegClass(*II, IIOpNum, TRI, *MF))
  349. : nullptr;
  350. const TargetRegisterClass *OpRC =
  351. TLI->isTypeLegal(OpVT)
  352. ? TLI->getRegClassFor(OpVT,
  353. Op.getNode()->isDivergent() ||
  354. (IIRC && TRI->isDivergentRegClass(IIRC)))
  355. : nullptr;
  356. if (OpRC && IIRC && OpRC != IIRC && Register::isVirtualRegister(VReg)) {
  357. Register NewVReg = MRI->createVirtualRegister(IIRC);
  358. BuildMI(*MBB, InsertPos, Op.getNode()->getDebugLoc(),
  359. TII->get(TargetOpcode::COPY), NewVReg).addReg(VReg);
  360. VReg = NewVReg;
  361. }
  362. // Turn additional physreg operands into implicit uses on non-variadic
  363. // instructions. This is used by call and return instructions passing
  364. // arguments in registers.
  365. bool Imp = II && (IIOpNum >= II->getNumOperands() && !II->isVariadic());
  366. MIB.addReg(VReg, getImplRegState(Imp));
  367. } else if (RegisterMaskSDNode *RM = dyn_cast<RegisterMaskSDNode>(Op)) {
  368. MIB.addRegMask(RM->getRegMask());
  369. } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
  370. MIB.addGlobalAddress(TGA->getGlobal(), TGA->getOffset(),
  371. TGA->getTargetFlags());
  372. } else if (BasicBlockSDNode *BBNode = dyn_cast<BasicBlockSDNode>(Op)) {
  373. MIB.addMBB(BBNode->getBasicBlock());
  374. } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
  375. MIB.addFrameIndex(FI->getIndex());
  376. } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
  377. MIB.addJumpTableIndex(JT->getIndex(), JT->getTargetFlags());
  378. } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
  379. int Offset = CP->getOffset();
  380. unsigned Align = CP->getAlignment();
  381. Type *Type = CP->getType();
  382. // MachineConstantPool wants an explicit alignment.
  383. if (Align == 0) {
  384. Align = MF->getDataLayout().getPrefTypeAlignment(Type);
  385. if (Align == 0) {
  386. // Alignment of vector types. FIXME!
  387. Align = MF->getDataLayout().getTypeAllocSize(Type);
  388. }
  389. }
  390. unsigned Idx;
  391. MachineConstantPool *MCP = MF->getConstantPool();
  392. if (CP->isMachineConstantPoolEntry())
  393. Idx = MCP->getConstantPoolIndex(CP->getMachineCPVal(), Align);
  394. else
  395. Idx = MCP->getConstantPoolIndex(CP->getConstVal(), Align);
  396. MIB.addConstantPoolIndex(Idx, Offset, CP->getTargetFlags());
  397. } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
  398. MIB.addExternalSymbol(ES->getSymbol(), ES->getTargetFlags());
  399. } else if (auto *SymNode = dyn_cast<MCSymbolSDNode>(Op)) {
  400. MIB.addSym(SymNode->getMCSymbol());
  401. } else if (BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op)) {
  402. MIB.addBlockAddress(BA->getBlockAddress(),
  403. BA->getOffset(),
  404. BA->getTargetFlags());
  405. } else if (TargetIndexSDNode *TI = dyn_cast<TargetIndexSDNode>(Op)) {
  406. MIB.addTargetIndex(TI->getIndex(), TI->getOffset(), TI->getTargetFlags());
  407. } else {
  408. assert(Op.getValueType() != MVT::Other &&
  409. Op.getValueType() != MVT::Glue &&
  410. "Chain and glue operands should occur at end of operand list!");
  411. AddRegisterOperand(MIB, Op, IIOpNum, II, VRBaseMap,
  412. IsDebug, IsClone, IsCloned);
  413. }
  414. }
  415. unsigned InstrEmitter::ConstrainForSubReg(unsigned VReg, unsigned SubIdx,
  416. MVT VT, bool isDivergent, const DebugLoc &DL) {
  417. const TargetRegisterClass *VRC = MRI->getRegClass(VReg);
  418. const TargetRegisterClass *RC = TRI->getSubClassWithSubReg(VRC, SubIdx);
  419. // RC is a sub-class of VRC that supports SubIdx. Try to constrain VReg
  420. // within reason.
  421. if (RC && RC != VRC)
  422. RC = MRI->constrainRegClass(VReg, RC, MinRCSize);
  423. // VReg has been adjusted. It can be used with SubIdx operands now.
  424. if (RC)
  425. return VReg;
  426. // VReg couldn't be reasonably constrained. Emit a COPY to a new virtual
  427. // register instead.
  428. RC = TRI->getSubClassWithSubReg(TLI->getRegClassFor(VT, isDivergent), SubIdx);
  429. assert(RC && "No legal register class for VT supports that SubIdx");
  430. Register NewReg = MRI->createVirtualRegister(RC);
  431. BuildMI(*MBB, InsertPos, DL, TII->get(TargetOpcode::COPY), NewReg)
  432. .addReg(VReg);
  433. return NewReg;
  434. }
  435. /// EmitSubregNode - Generate machine code for subreg nodes.
  436. ///
  437. void InstrEmitter::EmitSubregNode(SDNode *Node,
  438. DenseMap<SDValue, unsigned> &VRBaseMap,
  439. bool IsClone, bool IsCloned) {
  440. unsigned VRBase = 0;
  441. unsigned Opc = Node->getMachineOpcode();
  442. // If the node is only used by a CopyToReg and the dest reg is a vreg, use
  443. // the CopyToReg'd destination register instead of creating a new vreg.
  444. for (SDNode *User : Node->uses()) {
  445. if (User->getOpcode() == ISD::CopyToReg &&
  446. User->getOperand(2).getNode() == Node) {
  447. unsigned DestReg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
  448. if (Register::isVirtualRegister(DestReg)) {
  449. VRBase = DestReg;
  450. break;
  451. }
  452. }
  453. }
  454. if (Opc == TargetOpcode::EXTRACT_SUBREG) {
  455. // EXTRACT_SUBREG is lowered as %dst = COPY %src:sub. There are no
  456. // constraints on the %dst register, COPY can target all legal register
  457. // classes.
  458. unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
  459. const TargetRegisterClass *TRC =
  460. TLI->getRegClassFor(Node->getSimpleValueType(0), Node->isDivergent());
  461. unsigned Reg;
  462. MachineInstr *DefMI;
  463. RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(0));
  464. if (R && Register::isPhysicalRegister(R->getReg())) {
  465. Reg = R->getReg();
  466. DefMI = nullptr;
  467. } else {
  468. Reg = R ? R->getReg() : getVR(Node->getOperand(0), VRBaseMap);
  469. DefMI = MRI->getVRegDef(Reg);
  470. }
  471. unsigned SrcReg, DstReg, DefSubIdx;
  472. if (DefMI &&
  473. TII->isCoalescableExtInstr(*DefMI, SrcReg, DstReg, DefSubIdx) &&
  474. SubIdx == DefSubIdx &&
  475. TRC == MRI->getRegClass(SrcReg)) {
  476. // Optimize these:
  477. // r1025 = s/zext r1024, 4
  478. // r1026 = extract_subreg r1025, 4
  479. // to a copy
  480. // r1026 = copy r1024
  481. VRBase = MRI->createVirtualRegister(TRC);
  482. BuildMI(*MBB, InsertPos, Node->getDebugLoc(),
  483. TII->get(TargetOpcode::COPY), VRBase).addReg(SrcReg);
  484. MRI->clearKillFlags(SrcReg);
  485. } else {
  486. // Reg may not support a SubIdx sub-register, and we may need to
  487. // constrain its register class or issue a COPY to a compatible register
  488. // class.
  489. if (Register::isVirtualRegister(Reg))
  490. Reg = ConstrainForSubReg(Reg, SubIdx,
  491. Node->getOperand(0).getSimpleValueType(),
  492. Node->isDivergent(), Node->getDebugLoc());
  493. // Create the destreg if it is missing.
  494. if (VRBase == 0)
  495. VRBase = MRI->createVirtualRegister(TRC);
  496. // Create the extract_subreg machine instruction.
  497. MachineInstrBuilder CopyMI =
  498. BuildMI(*MBB, InsertPos, Node->getDebugLoc(),
  499. TII->get(TargetOpcode::COPY), VRBase);
  500. if (Register::isVirtualRegister(Reg))
  501. CopyMI.addReg(Reg, 0, SubIdx);
  502. else
  503. CopyMI.addReg(TRI->getSubReg(Reg, SubIdx));
  504. }
  505. } else if (Opc == TargetOpcode::INSERT_SUBREG ||
  506. Opc == TargetOpcode::SUBREG_TO_REG) {
  507. SDValue N0 = Node->getOperand(0);
  508. SDValue N1 = Node->getOperand(1);
  509. SDValue N2 = Node->getOperand(2);
  510. unsigned SubIdx = cast<ConstantSDNode>(N2)->getZExtValue();
  511. // Figure out the register class to create for the destreg. It should be
  512. // the largest legal register class supporting SubIdx sub-registers.
  513. // RegisterCoalescer will constrain it further if it decides to eliminate
  514. // the INSERT_SUBREG instruction.
  515. //
  516. // %dst = INSERT_SUBREG %src, %sub, SubIdx
  517. //
  518. // is lowered by TwoAddressInstructionPass to:
  519. //
  520. // %dst = COPY %src
  521. // %dst:SubIdx = COPY %sub
  522. //
  523. // There is no constraint on the %src register class.
  524. //
  525. const TargetRegisterClass *SRC =
  526. TLI->getRegClassFor(Node->getSimpleValueType(0), Node->isDivergent());
  527. SRC = TRI->getSubClassWithSubReg(SRC, SubIdx);
  528. assert(SRC && "No register class supports VT and SubIdx for INSERT_SUBREG");
  529. if (VRBase == 0 || !SRC->hasSubClassEq(MRI->getRegClass(VRBase)))
  530. VRBase = MRI->createVirtualRegister(SRC);
  531. // Create the insert_subreg or subreg_to_reg machine instruction.
  532. MachineInstrBuilder MIB =
  533. BuildMI(*MF, Node->getDebugLoc(), TII->get(Opc), VRBase);
  534. // If creating a subreg_to_reg, then the first input operand
  535. // is an implicit value immediate, otherwise it's a register
  536. if (Opc == TargetOpcode::SUBREG_TO_REG) {
  537. const ConstantSDNode *SD = cast<ConstantSDNode>(N0);
  538. MIB.addImm(SD->getZExtValue());
  539. } else
  540. AddOperand(MIB, N0, 0, nullptr, VRBaseMap, /*IsDebug=*/false,
  541. IsClone, IsCloned);
  542. // Add the subregister being inserted
  543. AddOperand(MIB, N1, 0, nullptr, VRBaseMap, /*IsDebug=*/false,
  544. IsClone, IsCloned);
  545. MIB.addImm(SubIdx);
  546. MBB->insert(InsertPos, MIB);
  547. } else
  548. llvm_unreachable("Node is not insert_subreg, extract_subreg, or subreg_to_reg");
  549. SDValue Op(Node, 0);
  550. bool isNew = VRBaseMap.insert(std::make_pair(Op, VRBase)).second;
  551. (void)isNew; // Silence compiler warning.
  552. assert(isNew && "Node emitted out of order - early");
  553. }
  554. /// EmitCopyToRegClassNode - Generate machine code for COPY_TO_REGCLASS nodes.
  555. /// COPY_TO_REGCLASS is just a normal copy, except that the destination
  556. /// register is constrained to be in a particular register class.
  557. ///
  558. void
  559. InstrEmitter::EmitCopyToRegClassNode(SDNode *Node,
  560. DenseMap<SDValue, unsigned> &VRBaseMap) {
  561. unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
  562. // Create the new VReg in the destination class and emit a copy.
  563. unsigned DstRCIdx = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
  564. const TargetRegisterClass *DstRC =
  565. TRI->getAllocatableClass(TRI->getRegClass(DstRCIdx));
  566. Register NewVReg = MRI->createVirtualRegister(DstRC);
  567. BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY),
  568. NewVReg).addReg(VReg);
  569. SDValue Op(Node, 0);
  570. bool isNew = VRBaseMap.insert(std::make_pair(Op, NewVReg)).second;
  571. (void)isNew; // Silence compiler warning.
  572. assert(isNew && "Node emitted out of order - early");
  573. }
  574. /// EmitRegSequence - Generate machine code for REG_SEQUENCE nodes.
  575. ///
  576. void InstrEmitter::EmitRegSequence(SDNode *Node,
  577. DenseMap<SDValue, unsigned> &VRBaseMap,
  578. bool IsClone, bool IsCloned) {
  579. unsigned DstRCIdx = cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue();
  580. const TargetRegisterClass *RC = TRI->getRegClass(DstRCIdx);
  581. Register NewVReg = MRI->createVirtualRegister(TRI->getAllocatableClass(RC));
  582. const MCInstrDesc &II = TII->get(TargetOpcode::REG_SEQUENCE);
  583. MachineInstrBuilder MIB = BuildMI(*MF, Node->getDebugLoc(), II, NewVReg);
  584. unsigned NumOps = Node->getNumOperands();
  585. // If the input pattern has a chain, then the root of the corresponding
  586. // output pattern will get a chain as well. This can happen to be a
  587. // REG_SEQUENCE (which is not "guarded" by countOperands/CountResults).
  588. if (NumOps && Node->getOperand(NumOps-1).getValueType() == MVT::Other)
  589. --NumOps; // Ignore chain if it exists.
  590. assert((NumOps & 1) == 1 &&
  591. "REG_SEQUENCE must have an odd number of operands!");
  592. for (unsigned i = 1; i != NumOps; ++i) {
  593. SDValue Op = Node->getOperand(i);
  594. if ((i & 1) == 0) {
  595. RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(i-1));
  596. // Skip physical registers as they don't have a vreg to get and we'll
  597. // insert copies for them in TwoAddressInstructionPass anyway.
  598. if (!R || !Register::isPhysicalRegister(R->getReg())) {
  599. unsigned SubIdx = cast<ConstantSDNode>(Op)->getZExtValue();
  600. unsigned SubReg = getVR(Node->getOperand(i-1), VRBaseMap);
  601. const TargetRegisterClass *TRC = MRI->getRegClass(SubReg);
  602. const TargetRegisterClass *SRC =
  603. TRI->getMatchingSuperRegClass(RC, TRC, SubIdx);
  604. if (SRC && SRC != RC) {
  605. MRI->setRegClass(NewVReg, SRC);
  606. RC = SRC;
  607. }
  608. }
  609. }
  610. AddOperand(MIB, Op, i+1, &II, VRBaseMap, /*IsDebug=*/false,
  611. IsClone, IsCloned);
  612. }
  613. MBB->insert(InsertPos, MIB);
  614. SDValue Op(Node, 0);
  615. bool isNew = VRBaseMap.insert(std::make_pair(Op, NewVReg)).second;
  616. (void)isNew; // Silence compiler warning.
  617. assert(isNew && "Node emitted out of order - early");
  618. }
  619. /// EmitDbgValue - Generate machine instruction for a dbg_value node.
  620. ///
  621. MachineInstr *
  622. InstrEmitter::EmitDbgValue(SDDbgValue *SD,
  623. DenseMap<SDValue, unsigned> &VRBaseMap) {
  624. MDNode *Var = SD->getVariable();
  625. MDNode *Expr = SD->getExpression();
  626. DebugLoc DL = SD->getDebugLoc();
  627. assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
  628. "Expected inlined-at fields to agree");
  629. SD->setIsEmitted();
  630. if (SD->isInvalidated()) {
  631. // An invalidated SDNode must generate an undef DBG_VALUE: although the
  632. // original value is no longer computed, earlier DBG_VALUEs live ranges
  633. // must not leak into later code.
  634. auto MIB = BuildMI(*MF, DL, TII->get(TargetOpcode::DBG_VALUE));
  635. MIB.addReg(0U);
  636. MIB.addReg(0U, RegState::Debug);
  637. MIB.addMetadata(Var);
  638. MIB.addMetadata(Expr);
  639. return &*MIB;
  640. }
  641. if (SD->getKind() == SDDbgValue::FRAMEIX) {
  642. // Stack address; this needs to be lowered in target-dependent fashion.
  643. // EmitTargetCodeForFrameDebugValue is responsible for allocation.
  644. auto FrameMI = BuildMI(*MF, DL, TII->get(TargetOpcode::DBG_VALUE))
  645. .addFrameIndex(SD->getFrameIx());
  646. if (SD->isIndirect())
  647. // Push [fi + 0] onto the DIExpression stack.
  648. FrameMI.addImm(0);
  649. else
  650. // Push fi onto the DIExpression stack.
  651. FrameMI.addReg(0);
  652. return FrameMI.addMetadata(Var).addMetadata(Expr);
  653. }
  654. // Otherwise, we're going to create an instruction here.
  655. const MCInstrDesc &II = TII->get(TargetOpcode::DBG_VALUE);
  656. MachineInstrBuilder MIB = BuildMI(*MF, DL, II);
  657. if (SD->getKind() == SDDbgValue::SDNODE) {
  658. SDNode *Node = SD->getSDNode();
  659. SDValue Op = SDValue(Node, SD->getResNo());
  660. // It's possible we replaced this SDNode with other(s) and therefore
  661. // didn't generate code for it. It's better to catch these cases where
  662. // they happen and transfer the debug info, but trying to guarantee that
  663. // in all cases would be very fragile; this is a safeguard for any
  664. // that were missed.
  665. DenseMap<SDValue, unsigned>::iterator I = VRBaseMap.find(Op);
  666. if (I==VRBaseMap.end())
  667. MIB.addReg(0U); // undef
  668. else
  669. AddOperand(MIB, Op, (*MIB).getNumOperands(), &II, VRBaseMap,
  670. /*IsDebug=*/true, /*IsClone=*/false, /*IsCloned=*/false);
  671. } else if (SD->getKind() == SDDbgValue::VREG) {
  672. MIB.addReg(SD->getVReg(), RegState::Debug);
  673. } else if (SD->getKind() == SDDbgValue::CONST) {
  674. const Value *V = SD->getConst();
  675. if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
  676. if (CI->getBitWidth() > 64)
  677. MIB.addCImm(CI);
  678. else
  679. MIB.addImm(CI->getSExtValue());
  680. } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
  681. MIB.addFPImm(CF);
  682. } else if (isa<ConstantPointerNull>(V)) {
  683. // Note: This assumes that all nullptr constants are zero-valued.
  684. MIB.addImm(0);
  685. } else {
  686. // Could be an Undef. In any case insert an Undef so we can see what we
  687. // dropped.
  688. MIB.addReg(0U);
  689. }
  690. } else {
  691. // Insert an Undef so we can see what we dropped.
  692. MIB.addReg(0U);
  693. }
  694. // Indirect addressing is indicated by an Imm as the second parameter.
  695. if (SD->isIndirect())
  696. MIB.addImm(0U);
  697. else
  698. MIB.addReg(0U, RegState::Debug);
  699. MIB.addMetadata(Var);
  700. MIB.addMetadata(Expr);
  701. return &*MIB;
  702. }
  703. MachineInstr *
  704. InstrEmitter::EmitDbgLabel(SDDbgLabel *SD) {
  705. MDNode *Label = SD->getLabel();
  706. DebugLoc DL = SD->getDebugLoc();
  707. assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
  708. "Expected inlined-at fields to agree");
  709. const MCInstrDesc &II = TII->get(TargetOpcode::DBG_LABEL);
  710. MachineInstrBuilder MIB = BuildMI(*MF, DL, II);
  711. MIB.addMetadata(Label);
  712. return &*MIB;
  713. }
  714. /// EmitMachineNode - Generate machine code for a target-specific node and
  715. /// needed dependencies.
  716. ///
  717. void InstrEmitter::
  718. EmitMachineNode(SDNode *Node, bool IsClone, bool IsCloned,
  719. DenseMap<SDValue, unsigned> &VRBaseMap) {
  720. unsigned Opc = Node->getMachineOpcode();
  721. // Handle subreg insert/extract specially
  722. if (Opc == TargetOpcode::EXTRACT_SUBREG ||
  723. Opc == TargetOpcode::INSERT_SUBREG ||
  724. Opc == TargetOpcode::SUBREG_TO_REG) {
  725. EmitSubregNode(Node, VRBaseMap, IsClone, IsCloned);
  726. return;
  727. }
  728. // Handle COPY_TO_REGCLASS specially.
  729. if (Opc == TargetOpcode::COPY_TO_REGCLASS) {
  730. EmitCopyToRegClassNode(Node, VRBaseMap);
  731. return;
  732. }
  733. // Handle REG_SEQUENCE specially.
  734. if (Opc == TargetOpcode::REG_SEQUENCE) {
  735. EmitRegSequence(Node, VRBaseMap, IsClone, IsCloned);
  736. return;
  737. }
  738. if (Opc == TargetOpcode::IMPLICIT_DEF)
  739. // We want a unique VR for each IMPLICIT_DEF use.
  740. return;
  741. const MCInstrDesc &II = TII->get(Opc);
  742. unsigned NumResults = CountResults(Node);
  743. unsigned NumDefs = II.getNumDefs();
  744. const MCPhysReg *ScratchRegs = nullptr;
  745. // Handle STACKMAP and PATCHPOINT specially and then use the generic code.
  746. if (Opc == TargetOpcode::STACKMAP || Opc == TargetOpcode::PATCHPOINT) {
  747. // Stackmaps do not have arguments and do not preserve their calling
  748. // convention. However, to simplify runtime support, they clobber the same
  749. // scratch registers as AnyRegCC.
  750. unsigned CC = CallingConv::AnyReg;
  751. if (Opc == TargetOpcode::PATCHPOINT) {
  752. CC = Node->getConstantOperandVal(PatchPointOpers::CCPos);
  753. NumDefs = NumResults;
  754. }
  755. ScratchRegs = TLI->getScratchRegisters((CallingConv::ID) CC);
  756. }
  757. unsigned NumImpUses = 0;
  758. unsigned NodeOperands =
  759. countOperands(Node, II.getNumOperands() - NumDefs, NumImpUses);
  760. bool HasPhysRegOuts = NumResults > NumDefs && II.getImplicitDefs()!=nullptr;
  761. #ifndef NDEBUG
  762. unsigned NumMIOperands = NodeOperands + NumResults;
  763. if (II.isVariadic())
  764. assert(NumMIOperands >= II.getNumOperands() &&
  765. "Too few operands for a variadic node!");
  766. else
  767. assert(NumMIOperands >= II.getNumOperands() &&
  768. NumMIOperands <= II.getNumOperands() + II.getNumImplicitDefs() +
  769. NumImpUses &&
  770. "#operands for dag node doesn't match .td file!");
  771. #endif
  772. // Create the new machine instruction.
  773. MachineInstrBuilder MIB = BuildMI(*MF, Node->getDebugLoc(), II);
  774. // Add result register values for things that are defined by this
  775. // instruction.
  776. if (NumResults) {
  777. CreateVirtualRegisters(Node, MIB, II, IsClone, IsCloned, VRBaseMap);
  778. // Transfer any IR flags from the SDNode to the MachineInstr
  779. MachineInstr *MI = MIB.getInstr();
  780. const SDNodeFlags Flags = Node->getFlags();
  781. if (Flags.hasNoSignedZeros())
  782. MI->setFlag(MachineInstr::MIFlag::FmNsz);
  783. if (Flags.hasAllowReciprocal())
  784. MI->setFlag(MachineInstr::MIFlag::FmArcp);
  785. if (Flags.hasNoNaNs())
  786. MI->setFlag(MachineInstr::MIFlag::FmNoNans);
  787. if (Flags.hasNoInfs())
  788. MI->setFlag(MachineInstr::MIFlag::FmNoInfs);
  789. if (Flags.hasAllowContract())
  790. MI->setFlag(MachineInstr::MIFlag::FmContract);
  791. if (Flags.hasApproximateFuncs())
  792. MI->setFlag(MachineInstr::MIFlag::FmAfn);
  793. if (Flags.hasAllowReassociation())
  794. MI->setFlag(MachineInstr::MIFlag::FmReassoc);
  795. if (Flags.hasNoUnsignedWrap())
  796. MI->setFlag(MachineInstr::MIFlag::NoUWrap);
  797. if (Flags.hasNoSignedWrap())
  798. MI->setFlag(MachineInstr::MIFlag::NoSWrap);
  799. if (Flags.hasExact())
  800. MI->setFlag(MachineInstr::MIFlag::IsExact);
  801. if (Flags.hasFPExcept())
  802. MI->setFlag(MachineInstr::MIFlag::FPExcept);
  803. }
  804. // Emit all of the actual operands of this instruction, adding them to the
  805. // instruction as appropriate.
  806. bool HasOptPRefs = NumDefs > NumResults;
  807. assert((!HasOptPRefs || !HasPhysRegOuts) &&
  808. "Unable to cope with optional defs and phys regs defs!");
  809. unsigned NumSkip = HasOptPRefs ? NumDefs - NumResults : 0;
  810. for (unsigned i = NumSkip; i != NodeOperands; ++i)
  811. AddOperand(MIB, Node->getOperand(i), i-NumSkip+NumDefs, &II,
  812. VRBaseMap, /*IsDebug=*/false, IsClone, IsCloned);
  813. // Add scratch registers as implicit def and early clobber
  814. if (ScratchRegs)
  815. for (unsigned i = 0; ScratchRegs[i]; ++i)
  816. MIB.addReg(ScratchRegs[i], RegState::ImplicitDefine |
  817. RegState::EarlyClobber);
  818. // Set the memory reference descriptions of this instruction now that it is
  819. // part of the function.
  820. MIB.setMemRefs(cast<MachineSDNode>(Node)->memoperands());
  821. // Insert the instruction into position in the block. This needs to
  822. // happen before any custom inserter hook is called so that the
  823. // hook knows where in the block to insert the replacement code.
  824. MBB->insert(InsertPos, MIB);
  825. // The MachineInstr may also define physregs instead of virtregs. These
  826. // physreg values can reach other instructions in different ways:
  827. //
  828. // 1. When there is a use of a Node value beyond the explicitly defined
  829. // virtual registers, we emit a CopyFromReg for one of the implicitly
  830. // defined physregs. This only happens when HasPhysRegOuts is true.
  831. //
  832. // 2. A CopyFromReg reading a physreg may be glued to this instruction.
  833. //
  834. // 3. A glued instruction may implicitly use a physreg.
  835. //
  836. // 4. A glued instruction may use a RegisterSDNode operand.
  837. //
  838. // Collect all the used physreg defs, and make sure that any unused physreg
  839. // defs are marked as dead.
  840. SmallVector<Register, 8> UsedRegs;
  841. // Additional results must be physical register defs.
  842. if (HasPhysRegOuts) {
  843. for (unsigned i = NumDefs; i < NumResults; ++i) {
  844. Register Reg = II.getImplicitDefs()[i - NumDefs];
  845. if (!Node->hasAnyUseOfValue(i))
  846. continue;
  847. // This implicitly defined physreg has a use.
  848. UsedRegs.push_back(Reg);
  849. EmitCopyFromReg(Node, i, IsClone, IsCloned, Reg, VRBaseMap);
  850. }
  851. }
  852. // Scan the glue chain for any used physregs.
  853. if (Node->getValueType(Node->getNumValues()-1) == MVT::Glue) {
  854. for (SDNode *F = Node->getGluedUser(); F; F = F->getGluedUser()) {
  855. if (F->getOpcode() == ISD::CopyFromReg) {
  856. UsedRegs.push_back(cast<RegisterSDNode>(F->getOperand(1))->getReg());
  857. continue;
  858. } else if (F->getOpcode() == ISD::CopyToReg) {
  859. // Skip CopyToReg nodes that are internal to the glue chain.
  860. continue;
  861. }
  862. // Collect declared implicit uses.
  863. const MCInstrDesc &MCID = TII->get(F->getMachineOpcode());
  864. UsedRegs.append(MCID.getImplicitUses(),
  865. MCID.getImplicitUses() + MCID.getNumImplicitUses());
  866. // In addition to declared implicit uses, we must also check for
  867. // direct RegisterSDNode operands.
  868. for (unsigned i = 0, e = F->getNumOperands(); i != e; ++i)
  869. if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(F->getOperand(i))) {
  870. Register Reg = R->getReg();
  871. if (Reg.isPhysical())
  872. UsedRegs.push_back(Reg);
  873. }
  874. }
  875. }
  876. // Finally mark unused registers as dead.
  877. if (!UsedRegs.empty() || II.getImplicitDefs() || II.hasOptionalDef())
  878. MIB->setPhysRegsDeadExcept(UsedRegs, *TRI);
  879. // Run post-isel target hook to adjust this instruction if needed.
  880. if (II.hasPostISelHook())
  881. TLI->AdjustInstrPostInstrSelection(*MIB, Node);
  882. }
  883. /// EmitSpecialNode - Generate machine code for a target-independent node and
  884. /// needed dependencies.
  885. void InstrEmitter::
  886. EmitSpecialNode(SDNode *Node, bool IsClone, bool IsCloned,
  887. DenseMap<SDValue, unsigned> &VRBaseMap) {
  888. switch (Node->getOpcode()) {
  889. default:
  890. #ifndef NDEBUG
  891. Node->dump();
  892. #endif
  893. llvm_unreachable("This target-independent node should have been selected!");
  894. case ISD::EntryToken:
  895. llvm_unreachable("EntryToken should have been excluded from the schedule!");
  896. case ISD::MERGE_VALUES:
  897. case ISD::TokenFactor: // fall thru
  898. break;
  899. case ISD::CopyToReg: {
  900. unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
  901. SDValue SrcVal = Node->getOperand(2);
  902. if (Register::isVirtualRegister(DestReg) && SrcVal.isMachineOpcode() &&
  903. SrcVal.getMachineOpcode() == TargetOpcode::IMPLICIT_DEF) {
  904. // Instead building a COPY to that vreg destination, build an
  905. // IMPLICIT_DEF instruction instead.
  906. BuildMI(*MBB, InsertPos, Node->getDebugLoc(),
  907. TII->get(TargetOpcode::IMPLICIT_DEF), DestReg);
  908. break;
  909. }
  910. unsigned SrcReg;
  911. if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
  912. SrcReg = R->getReg();
  913. else
  914. SrcReg = getVR(SrcVal, VRBaseMap);
  915. if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
  916. break;
  917. BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TargetOpcode::COPY),
  918. DestReg).addReg(SrcReg);
  919. break;
  920. }
  921. case ISD::CopyFromReg: {
  922. unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
  923. EmitCopyFromReg(Node, 0, IsClone, IsCloned, SrcReg, VRBaseMap);
  924. break;
  925. }
  926. case ISD::EH_LABEL:
  927. case ISD::ANNOTATION_LABEL: {
  928. unsigned Opc = (Node->getOpcode() == ISD::EH_LABEL)
  929. ? TargetOpcode::EH_LABEL
  930. : TargetOpcode::ANNOTATION_LABEL;
  931. MCSymbol *S = cast<LabelSDNode>(Node)->getLabel();
  932. BuildMI(*MBB, InsertPos, Node->getDebugLoc(),
  933. TII->get(Opc)).addSym(S);
  934. break;
  935. }
  936. case ISD::LIFETIME_START:
  937. case ISD::LIFETIME_END: {
  938. unsigned TarOp = (Node->getOpcode() == ISD::LIFETIME_START) ?
  939. TargetOpcode::LIFETIME_START : TargetOpcode::LIFETIME_END;
  940. FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Node->getOperand(1));
  941. BuildMI(*MBB, InsertPos, Node->getDebugLoc(), TII->get(TarOp))
  942. .addFrameIndex(FI->getIndex());
  943. break;
  944. }
  945. case ISD::INLINEASM:
  946. case ISD::INLINEASM_BR: {
  947. unsigned NumOps = Node->getNumOperands();
  948. if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
  949. --NumOps; // Ignore the glue operand.
  950. // Create the inline asm machine instruction.
  951. unsigned TgtOpc = Node->getOpcode() == ISD::INLINEASM_BR
  952. ? TargetOpcode::INLINEASM_BR
  953. : TargetOpcode::INLINEASM;
  954. MachineInstrBuilder MIB =
  955. BuildMI(*MF, Node->getDebugLoc(), TII->get(TgtOpc));
  956. // Add the asm string as an external symbol operand.
  957. SDValue AsmStrV = Node->getOperand(InlineAsm::Op_AsmString);
  958. const char *AsmStr = cast<ExternalSymbolSDNode>(AsmStrV)->getSymbol();
  959. MIB.addExternalSymbol(AsmStr);
  960. // Add the HasSideEffect, isAlignStack, AsmDialect, MayLoad and MayStore
  961. // bits.
  962. int64_t ExtraInfo =
  963. cast<ConstantSDNode>(Node->getOperand(InlineAsm::Op_ExtraInfo))->
  964. getZExtValue();
  965. MIB.addImm(ExtraInfo);
  966. // Remember to operand index of the group flags.
  967. SmallVector<unsigned, 8> GroupIdx;
  968. // Remember registers that are part of early-clobber defs.
  969. SmallVector<unsigned, 8> ECRegs;
  970. // Add all of the operand registers to the instruction.
  971. for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
  972. unsigned Flags =
  973. cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
  974. const unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
  975. GroupIdx.push_back(MIB->getNumOperands());
  976. MIB.addImm(Flags);
  977. ++i; // Skip the ID value.
  978. switch (InlineAsm::getKind(Flags)) {
  979. default: llvm_unreachable("Bad flags!");
  980. case InlineAsm::Kind_RegDef:
  981. for (unsigned j = 0; j != NumVals; ++j, ++i) {
  982. unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
  983. // FIXME: Add dead flags for physical and virtual registers defined.
  984. // For now, mark physical register defs as implicit to help fast
  985. // regalloc. This makes inline asm look a lot like calls.
  986. MIB.addReg(Reg,
  987. RegState::Define |
  988. getImplRegState(Register::isPhysicalRegister(Reg)));
  989. }
  990. break;
  991. case InlineAsm::Kind_RegDefEarlyClobber:
  992. case InlineAsm::Kind_Clobber:
  993. for (unsigned j = 0; j != NumVals; ++j, ++i) {
  994. unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
  995. MIB.addReg(Reg,
  996. RegState::Define | RegState::EarlyClobber |
  997. getImplRegState(Register::isPhysicalRegister(Reg)));
  998. ECRegs.push_back(Reg);
  999. }
  1000. break;
  1001. case InlineAsm::Kind_RegUse: // Use of register.
  1002. case InlineAsm::Kind_Imm: // Immediate.
  1003. case InlineAsm::Kind_Mem: // Addressing mode.
  1004. // The addressing mode has been selected, just add all of the
  1005. // operands to the machine instruction.
  1006. for (unsigned j = 0; j != NumVals; ++j, ++i)
  1007. AddOperand(MIB, Node->getOperand(i), 0, nullptr, VRBaseMap,
  1008. /*IsDebug=*/false, IsClone, IsCloned);
  1009. // Manually set isTied bits.
  1010. if (InlineAsm::getKind(Flags) == InlineAsm::Kind_RegUse) {
  1011. unsigned DefGroup = 0;
  1012. if (InlineAsm::isUseOperandTiedToDef(Flags, DefGroup)) {
  1013. unsigned DefIdx = GroupIdx[DefGroup] + 1;
  1014. unsigned UseIdx = GroupIdx.back() + 1;
  1015. for (unsigned j = 0; j != NumVals; ++j)
  1016. MIB->tieOperands(DefIdx + j, UseIdx + j);
  1017. }
  1018. }
  1019. break;
  1020. }
  1021. }
  1022. // GCC inline assembly allows input operands to also be early-clobber
  1023. // output operands (so long as the operand is written only after it's
  1024. // used), but this does not match the semantics of our early-clobber flag.
  1025. // If an early-clobber operand register is also an input operand register,
  1026. // then remove the early-clobber flag.
  1027. for (unsigned Reg : ECRegs) {
  1028. if (MIB->readsRegister(Reg, TRI)) {
  1029. MachineOperand *MO =
  1030. MIB->findRegisterDefOperand(Reg, false, false, TRI);
  1031. assert(MO && "No def operand for clobbered register?");
  1032. MO->setIsEarlyClobber(false);
  1033. }
  1034. }
  1035. // Get the mdnode from the asm if it exists and add it to the instruction.
  1036. SDValue MDV = Node->getOperand(InlineAsm::Op_MDNode);
  1037. const MDNode *MD = cast<MDNodeSDNode>(MDV)->getMD();
  1038. if (MD)
  1039. MIB.addMetadata(MD);
  1040. MBB->insert(InsertPos, MIB);
  1041. break;
  1042. }
  1043. }
  1044. }
  1045. /// InstrEmitter - Construct an InstrEmitter and set it to start inserting
  1046. /// at the given position in the given block.
  1047. InstrEmitter::InstrEmitter(MachineBasicBlock *mbb,
  1048. MachineBasicBlock::iterator insertpos)
  1049. : MF(mbb->getParent()), MRI(&MF->getRegInfo()),
  1050. TII(MF->getSubtarget().getInstrInfo()),
  1051. TRI(MF->getSubtarget().getRegisterInfo()),
  1052. TLI(MF->getSubtarget().getTargetLowering()), MBB(mbb),
  1053. InsertPos(insertpos) {}