MachineInstrBuilder.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- CodeGen/MachineInstrBuilder.h - Simplify creation of MIs --*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file exposes a function named BuildMI, which is useful for dramatically
  15. // simplifying how MachineInstr's are created. It allows use of code like this:
  16. //
  17. // M = BuildMI(MBB, MI, DL, TII.get(X86::ADD8rr), Dst)
  18. // .addReg(argVal1)
  19. // .addReg(argVal2);
  20. //
  21. //===----------------------------------------------------------------------===//
  22. #ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H
  23. #define LLVM_CODEGEN_MACHINEINSTRBUILDER_H
  24. #include "llvm/ADT/ArrayRef.h"
  25. #include "llvm/CodeGen/GlobalISel/Utils.h"
  26. #include "llvm/CodeGen/MachineBasicBlock.h"
  27. #include "llvm/CodeGen/MachineFunction.h"
  28. #include "llvm/CodeGen/MachineInstr.h"
  29. #include "llvm/CodeGen/MachineInstrBundle.h"
  30. #include "llvm/CodeGen/MachineOperand.h"
  31. #include "llvm/CodeGen/TargetRegisterInfo.h"
  32. #include "llvm/IR/InstrTypes.h"
  33. #include "llvm/IR/Intrinsics.h"
  34. #include "llvm/Support/ErrorHandling.h"
  35. #include <cassert>
  36. #include <cstdint>
  37. namespace llvm {
  38. class MCInstrDesc;
  39. class MDNode;
  40. namespace RegState {
  41. enum {
  42. /// Register definition.
  43. Define = 0x2,
  44. /// Not emitted register (e.g. carry, or temporary result).
  45. Implicit = 0x4,
  46. /// The last use of a register.
  47. Kill = 0x8,
  48. /// Unused definition.
  49. Dead = 0x10,
  50. /// Value of the register doesn't matter.
  51. Undef = 0x20,
  52. /// Register definition happens before uses.
  53. EarlyClobber = 0x40,
  54. /// Register 'use' is for debugging purpose.
  55. Debug = 0x80,
  56. /// Register reads a value that is defined inside the same instruction or
  57. /// bundle.
  58. InternalRead = 0x100,
  59. /// Register that may be renamed.
  60. Renamable = 0x200,
  61. DefineNoRead = Define | Undef,
  62. ImplicitDefine = Implicit | Define,
  63. ImplicitKill = Implicit | Kill
  64. };
  65. } // end namespace RegState
  66. class MachineInstrBuilder {
  67. MachineFunction *MF = nullptr;
  68. MachineInstr *MI = nullptr;
  69. public:
  70. MachineInstrBuilder() = default;
  71. /// Create a MachineInstrBuilder for manipulating an existing instruction.
  72. /// F must be the machine function that was used to allocate I.
  73. MachineInstrBuilder(MachineFunction &F, MachineInstr *I) : MF(&F), MI(I) {}
  74. MachineInstrBuilder(MachineFunction &F, MachineBasicBlock::iterator I)
  75. : MF(&F), MI(&*I) {}
  76. /// Allow automatic conversion to the machine instruction we are working on.
  77. operator MachineInstr*() const { return MI; }
  78. MachineInstr *operator->() const { return MI; }
  79. operator MachineBasicBlock::iterator() const { return MI; }
  80. /// If conversion operators fail, use this method to get the MachineInstr
  81. /// explicitly.
  82. MachineInstr *getInstr() const { return MI; }
  83. /// Get the register for the operand index.
  84. /// The operand at the index should be a register (asserted by
  85. /// MachineOperand).
  86. Register getReg(unsigned Idx) const { return MI->getOperand(Idx).getReg(); }
  87. /// Add a new virtual register operand.
  88. const MachineInstrBuilder &addReg(Register RegNo, unsigned flags = 0,
  89. unsigned SubReg = 0) const {
  90. assert((flags & 0x1) == 0 &&
  91. "Passing in 'true' to addReg is forbidden! Use enums instead.");
  92. MI->addOperand(*MF, MachineOperand::CreateReg(RegNo,
  93. flags & RegState::Define,
  94. flags & RegState::Implicit,
  95. flags & RegState::Kill,
  96. flags & RegState::Dead,
  97. flags & RegState::Undef,
  98. flags & RegState::EarlyClobber,
  99. SubReg,
  100. flags & RegState::Debug,
  101. flags & RegState::InternalRead,
  102. flags & RegState::Renamable));
  103. return *this;
  104. }
  105. /// Add a virtual register definition operand.
  106. const MachineInstrBuilder &addDef(Register RegNo, unsigned Flags = 0,
  107. unsigned SubReg = 0) const {
  108. return addReg(RegNo, Flags | RegState::Define, SubReg);
  109. }
  110. /// Add a virtual register use operand. It is an error for Flags to contain
  111. /// `RegState::Define` when calling this function.
  112. const MachineInstrBuilder &addUse(Register RegNo, unsigned Flags = 0,
  113. unsigned SubReg = 0) const {
  114. assert(!(Flags & RegState::Define) &&
  115. "Misleading addUse defines register, use addReg instead.");
  116. return addReg(RegNo, Flags, SubReg);
  117. }
  118. /// Add a new immediate operand.
  119. const MachineInstrBuilder &addImm(int64_t Val) const {
  120. MI->addOperand(*MF, MachineOperand::CreateImm(Val));
  121. return *this;
  122. }
  123. const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
  124. MI->addOperand(*MF, MachineOperand::CreateCImm(Val));
  125. return *this;
  126. }
  127. const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
  128. MI->addOperand(*MF, MachineOperand::CreateFPImm(Val));
  129. return *this;
  130. }
  131. const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
  132. unsigned TargetFlags = 0) const {
  133. MI->addOperand(*MF, MachineOperand::CreateMBB(MBB, TargetFlags));
  134. return *this;
  135. }
  136. const MachineInstrBuilder &addFrameIndex(int Idx) const {
  137. MI->addOperand(*MF, MachineOperand::CreateFI(Idx));
  138. return *this;
  139. }
  140. const MachineInstrBuilder &
  141. addConstantPoolIndex(unsigned Idx, int Offset = 0,
  142. unsigned TargetFlags = 0) const {
  143. MI->addOperand(*MF, MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
  144. return *this;
  145. }
  146. const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0,
  147. unsigned TargetFlags = 0) const {
  148. MI->addOperand(*MF, MachineOperand::CreateTargetIndex(Idx, Offset,
  149. TargetFlags));
  150. return *this;
  151. }
  152. const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
  153. unsigned TargetFlags = 0) const {
  154. MI->addOperand(*MF, MachineOperand::CreateJTI(Idx, TargetFlags));
  155. return *this;
  156. }
  157. const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
  158. int64_t Offset = 0,
  159. unsigned TargetFlags = 0) const {
  160. MI->addOperand(*MF, MachineOperand::CreateGA(GV, Offset, TargetFlags));
  161. return *this;
  162. }
  163. const MachineInstrBuilder &addExternalSymbol(const char *FnName,
  164. unsigned TargetFlags = 0) const {
  165. MI->addOperand(*MF, MachineOperand::CreateES(FnName, TargetFlags));
  166. return *this;
  167. }
  168. const MachineInstrBuilder &addBlockAddress(const BlockAddress *BA,
  169. int64_t Offset = 0,
  170. unsigned TargetFlags = 0) const {
  171. MI->addOperand(*MF, MachineOperand::CreateBA(BA, Offset, TargetFlags));
  172. return *this;
  173. }
  174. const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
  175. MI->addOperand(*MF, MachineOperand::CreateRegMask(Mask));
  176. return *this;
  177. }
  178. const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
  179. MI->addMemOperand(*MF, MMO);
  180. return *this;
  181. }
  182. const MachineInstrBuilder &
  183. setMemRefs(ArrayRef<MachineMemOperand *> MMOs) const {
  184. MI->setMemRefs(*MF, MMOs);
  185. return *this;
  186. }
  187. const MachineInstrBuilder &cloneMemRefs(const MachineInstr &OtherMI) const {
  188. MI->cloneMemRefs(*MF, OtherMI);
  189. return *this;
  190. }
  191. const MachineInstrBuilder &
  192. cloneMergedMemRefs(ArrayRef<const MachineInstr *> OtherMIs) const {
  193. MI->cloneMergedMemRefs(*MF, OtherMIs);
  194. return *this;
  195. }
  196. const MachineInstrBuilder &add(const MachineOperand &MO) const {
  197. MI->addOperand(*MF, MO);
  198. return *this;
  199. }
  200. const MachineInstrBuilder &add(ArrayRef<MachineOperand> MOs) const {
  201. for (const MachineOperand &MO : MOs) {
  202. MI->addOperand(*MF, MO);
  203. }
  204. return *this;
  205. }
  206. const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
  207. MI->addOperand(*MF, MachineOperand::CreateMetadata(MD));
  208. assert((MI->isDebugValueLike() ? static_cast<bool>(MI->getDebugVariable())
  209. : true) &&
  210. "first MDNode argument of a DBG_VALUE not a variable");
  211. assert((MI->isDebugLabel() ? static_cast<bool>(MI->getDebugLabel())
  212. : true) &&
  213. "first MDNode argument of a DBG_LABEL not a label");
  214. return *this;
  215. }
  216. const MachineInstrBuilder &addCFIIndex(unsigned CFIIndex) const {
  217. MI->addOperand(*MF, MachineOperand::CreateCFIIndex(CFIIndex));
  218. return *this;
  219. }
  220. const MachineInstrBuilder &addIntrinsicID(Intrinsic::ID ID) const {
  221. MI->addOperand(*MF, MachineOperand::CreateIntrinsicID(ID));
  222. return *this;
  223. }
  224. const MachineInstrBuilder &addPredicate(CmpInst::Predicate Pred) const {
  225. MI->addOperand(*MF, MachineOperand::CreatePredicate(Pred));
  226. return *this;
  227. }
  228. const MachineInstrBuilder &addShuffleMask(ArrayRef<int> Val) const {
  229. MI->addOperand(*MF, MachineOperand::CreateShuffleMask(Val));
  230. return *this;
  231. }
  232. const MachineInstrBuilder &addSym(MCSymbol *Sym,
  233. unsigned char TargetFlags = 0) const {
  234. MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym, TargetFlags));
  235. return *this;
  236. }
  237. const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
  238. MI->setFlags(Flags);
  239. return *this;
  240. }
  241. const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
  242. MI->setFlag(Flag);
  243. return *this;
  244. }
  245. // Add a displacement from an existing MachineOperand with an added offset.
  246. const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off,
  247. unsigned char TargetFlags = 0) const {
  248. // If caller specifies new TargetFlags then use it, otherwise the
  249. // default behavior is to copy the target flags from the existing
  250. // MachineOperand. This means if the caller wants to clear the
  251. // target flags it needs to do so explicitly.
  252. if (0 == TargetFlags)
  253. TargetFlags = Disp.getTargetFlags();
  254. switch (Disp.getType()) {
  255. default:
  256. llvm_unreachable("Unhandled operand type in addDisp()");
  257. case MachineOperand::MO_Immediate:
  258. return addImm(Disp.getImm() + off);
  259. case MachineOperand::MO_ConstantPoolIndex:
  260. return addConstantPoolIndex(Disp.getIndex(), Disp.getOffset() + off,
  261. TargetFlags);
  262. case MachineOperand::MO_GlobalAddress:
  263. return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
  264. TargetFlags);
  265. case MachineOperand::MO_BlockAddress:
  266. return addBlockAddress(Disp.getBlockAddress(), Disp.getOffset() + off,
  267. TargetFlags);
  268. case MachineOperand::MO_JumpTableIndex:
  269. assert(off == 0 && "cannot create offset into jump tables");
  270. return addJumpTableIndex(Disp.getIndex(), TargetFlags);
  271. }
  272. }
  273. const MachineInstrBuilder &setPCSections(MDNode *MD) const {
  274. if (MD)
  275. MI->setPCSections(*MF, MD);
  276. return *this;
  277. }
  278. /// Copy all the implicit operands from OtherMI onto this one.
  279. const MachineInstrBuilder &
  280. copyImplicitOps(const MachineInstr &OtherMI) const {
  281. MI->copyImplicitOps(*MF, OtherMI);
  282. return *this;
  283. }
  284. bool constrainAllUses(const TargetInstrInfo &TII,
  285. const TargetRegisterInfo &TRI,
  286. const RegisterBankInfo &RBI) const {
  287. return constrainSelectedInstRegOperands(*MI, TII, TRI, RBI);
  288. }
  289. };
  290. /// Set of metadata that should be preserved when using BuildMI(). This provides
  291. /// a more convenient way of preserving DebugLoc and PCSections.
  292. class MIMetadata {
  293. public:
  294. MIMetadata() = default;
  295. MIMetadata(DebugLoc DL, MDNode *PCSections = nullptr)
  296. : DL(std::move(DL)), PCSections(PCSections) {}
  297. MIMetadata(const DILocation *DI, MDNode *PCSections = nullptr)
  298. : DL(DI), PCSections(PCSections) {}
  299. explicit MIMetadata(const Instruction &From)
  300. : DL(From.getDebugLoc()),
  301. PCSections(From.getMetadata(LLVMContext::MD_pcsections)) {}
  302. explicit MIMetadata(const MachineInstr &From)
  303. : DL(From.getDebugLoc()), PCSections(From.getPCSections()) {}
  304. const DebugLoc &getDL() const { return DL; }
  305. MDNode *getPCSections() const { return PCSections; }
  306. private:
  307. DebugLoc DL;
  308. MDNode *PCSections = nullptr;
  309. };
  310. /// Builder interface. Specify how to create the initial instruction itself.
  311. inline MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD,
  312. const MCInstrDesc &MCID) {
  313. return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, MIMD.getDL()))
  314. .setPCSections(MIMD.getPCSections());
  315. }
  316. /// This version of the builder sets up the first operand as a
  317. /// destination virtual register.
  318. inline MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD,
  319. const MCInstrDesc &MCID, Register DestReg) {
  320. return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, MIMD.getDL()))
  321. .setPCSections(MIMD.getPCSections())
  322. .addReg(DestReg, RegState::Define);
  323. }
  324. /// This version of the builder inserts the newly-built instruction before
  325. /// the given position in the given MachineBasicBlock, and sets up the first
  326. /// operand as a destination virtual register.
  327. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  328. MachineBasicBlock::iterator I,
  329. const MIMetadata &MIMD,
  330. const MCInstrDesc &MCID, Register DestReg) {
  331. MachineFunction &MF = *BB.getParent();
  332. MachineInstr *MI = MF.CreateMachineInstr(MCID, MIMD.getDL());
  333. BB.insert(I, MI);
  334. return MachineInstrBuilder(MF, MI)
  335. .setPCSections(MIMD.getPCSections())
  336. .addReg(DestReg, RegState::Define);
  337. }
  338. /// This version of the builder inserts the newly-built instruction before
  339. /// the given position in the given MachineBasicBlock, and sets up the first
  340. /// operand as a destination virtual register.
  341. ///
  342. /// If \c I is inside a bundle, then the newly inserted \a MachineInstr is
  343. /// added to the same bundle.
  344. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  345. MachineBasicBlock::instr_iterator I,
  346. const MIMetadata &MIMD,
  347. const MCInstrDesc &MCID, Register DestReg) {
  348. MachineFunction &MF = *BB.getParent();
  349. MachineInstr *MI = MF.CreateMachineInstr(MCID, MIMD.getDL());
  350. BB.insert(I, MI);
  351. return MachineInstrBuilder(MF, MI)
  352. .setPCSections(MIMD.getPCSections())
  353. .addReg(DestReg, RegState::Define);
  354. }
  355. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
  356. const MIMetadata &MIMD,
  357. const MCInstrDesc &MCID, Register DestReg) {
  358. // Calling the overload for instr_iterator is always correct. However, the
  359. // definition is not available in headers, so inline the check.
  360. if (I.isInsideBundle())
  361. return BuildMI(BB, MachineBasicBlock::instr_iterator(I), MIMD, MCID,
  362. DestReg);
  363. return BuildMI(BB, MachineBasicBlock::iterator(I), MIMD, MCID, DestReg);
  364. }
  365. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
  366. const MIMetadata &MIMD,
  367. const MCInstrDesc &MCID, Register DestReg) {
  368. return BuildMI(BB, *I, MIMD, MCID, DestReg);
  369. }
  370. /// This version of the builder inserts the newly-built instruction before the
  371. /// given position in the given MachineBasicBlock, and does NOT take a
  372. /// destination register.
  373. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  374. MachineBasicBlock::iterator I,
  375. const MIMetadata &MIMD,
  376. const MCInstrDesc &MCID) {
  377. MachineFunction &MF = *BB.getParent();
  378. MachineInstr *MI = MF.CreateMachineInstr(MCID, MIMD.getDL());
  379. BB.insert(I, MI);
  380. return MachineInstrBuilder(MF, MI).setPCSections(MIMD.getPCSections());
  381. }
  382. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  383. MachineBasicBlock::instr_iterator I,
  384. const MIMetadata &MIMD,
  385. const MCInstrDesc &MCID) {
  386. MachineFunction &MF = *BB.getParent();
  387. MachineInstr *MI = MF.CreateMachineInstr(MCID, MIMD.getDL());
  388. BB.insert(I, MI);
  389. return MachineInstrBuilder(MF, MI).setPCSections(MIMD.getPCSections());
  390. }
  391. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
  392. const MIMetadata &MIMD,
  393. const MCInstrDesc &MCID) {
  394. // Calling the overload for instr_iterator is always correct. However, the
  395. // definition is not available in headers, so inline the check.
  396. if (I.isInsideBundle())
  397. return BuildMI(BB, MachineBasicBlock::instr_iterator(I), MIMD, MCID);
  398. return BuildMI(BB, MachineBasicBlock::iterator(I), MIMD, MCID);
  399. }
  400. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
  401. const MIMetadata &MIMD,
  402. const MCInstrDesc &MCID) {
  403. return BuildMI(BB, *I, MIMD, MCID);
  404. }
  405. /// This version of the builder inserts the newly-built instruction at the end
  406. /// of the given MachineBasicBlock, and does NOT take a destination register.
  407. inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
  408. const MIMetadata &MIMD,
  409. const MCInstrDesc &MCID) {
  410. return BuildMI(*BB, BB->end(), MIMD, MCID);
  411. }
  412. /// This version of the builder inserts the newly-built instruction at the
  413. /// end of the given MachineBasicBlock, and sets up the first operand as a
  414. /// destination virtual register.
  415. inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
  416. const MIMetadata &MIMD,
  417. const MCInstrDesc &MCID, Register DestReg) {
  418. return BuildMI(*BB, BB->end(), MIMD, MCID, DestReg);
  419. }
  420. /// This version of the builder builds a DBG_VALUE intrinsic
  421. /// for either a value in a register or a register-indirect
  422. /// address. The convention is that a DBG_VALUE is indirect iff the
  423. /// second operand is an immediate.
  424. MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
  425. const MCInstrDesc &MCID, bool IsIndirect,
  426. Register Reg, const MDNode *Variable,
  427. const MDNode *Expr);
  428. /// This version of the builder builds a DBG_VALUE or DBG_VALUE_LIST intrinsic
  429. /// for a MachineOperand.
  430. MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
  431. const MCInstrDesc &MCID, bool IsIndirect,
  432. ArrayRef<MachineOperand> MOs,
  433. const MDNode *Variable, const MDNode *Expr);
  434. /// This version of the builder builds a DBG_VALUE intrinsic
  435. /// for either a value in a register or a register-indirect
  436. /// address and inserts it at position I.
  437. MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  438. MachineBasicBlock::iterator I, const DebugLoc &DL,
  439. const MCInstrDesc &MCID, bool IsIndirect,
  440. Register Reg, const MDNode *Variable,
  441. const MDNode *Expr);
  442. /// This version of the builder builds a DBG_VALUE, DBG_INSTR_REF, or
  443. /// DBG_VALUE_LIST intrinsic for a machine operand and inserts it at position I.
  444. MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  445. MachineBasicBlock::iterator I, const DebugLoc &DL,
  446. const MCInstrDesc &MCID, bool IsIndirect,
  447. ArrayRef<MachineOperand> MOs,
  448. const MDNode *Variable, const MDNode *Expr);
  449. /// Clone a DBG_VALUE whose value has been spilled to FrameIndex.
  450. MachineInstr *buildDbgValueForSpill(MachineBasicBlock &BB,
  451. MachineBasicBlock::iterator I,
  452. const MachineInstr &Orig, int FrameIndex,
  453. Register SpillReg);
  454. MachineInstr *
  455. buildDbgValueForSpill(MachineBasicBlock &BB, MachineBasicBlock::iterator I,
  456. const MachineInstr &Orig, int FrameIndex,
  457. SmallVectorImpl<const MachineOperand *> &SpilledOperands);
  458. /// Update a DBG_VALUE whose value has been spilled to FrameIndex. Useful when
  459. /// modifying an instruction in place while iterating over a basic block.
  460. void updateDbgValueForSpill(MachineInstr &Orig, int FrameIndex, Register Reg);
  461. inline unsigned getDefRegState(bool B) {
  462. return B ? RegState::Define : 0;
  463. }
  464. inline unsigned getImplRegState(bool B) {
  465. return B ? RegState::Implicit : 0;
  466. }
  467. inline unsigned getKillRegState(bool B) {
  468. return B ? RegState::Kill : 0;
  469. }
  470. inline unsigned getDeadRegState(bool B) {
  471. return B ? RegState::Dead : 0;
  472. }
  473. inline unsigned getUndefRegState(bool B) {
  474. return B ? RegState::Undef : 0;
  475. }
  476. inline unsigned getInternalReadRegState(bool B) {
  477. return B ? RegState::InternalRead : 0;
  478. }
  479. inline unsigned getDebugRegState(bool B) {
  480. return B ? RegState::Debug : 0;
  481. }
  482. inline unsigned getRenamableRegState(bool B) {
  483. return B ? RegState::Renamable : 0;
  484. }
  485. /// Get all register state flags from machine operand \p RegOp.
  486. inline unsigned getRegState(const MachineOperand &RegOp) {
  487. assert(RegOp.isReg() && "Not a register operand");
  488. return getDefRegState(RegOp.isDef()) | getImplRegState(RegOp.isImplicit()) |
  489. getKillRegState(RegOp.isKill()) | getDeadRegState(RegOp.isDead()) |
  490. getUndefRegState(RegOp.isUndef()) |
  491. getInternalReadRegState(RegOp.isInternalRead()) |
  492. getDebugRegState(RegOp.isDebug()) |
  493. getRenamableRegState(RegOp.getReg().isPhysical() &&
  494. RegOp.isRenamable());
  495. }
  496. /// Helper class for constructing bundles of MachineInstrs.
  497. ///
  498. /// MIBundleBuilder can create a bundle from scratch by inserting new
  499. /// MachineInstrs one at a time, or it can create a bundle from a sequence of
  500. /// existing MachineInstrs in a basic block.
  501. class MIBundleBuilder {
  502. MachineBasicBlock &MBB;
  503. MachineBasicBlock::instr_iterator Begin;
  504. MachineBasicBlock::instr_iterator End;
  505. public:
  506. /// Create an MIBundleBuilder that inserts instructions into a new bundle in
  507. /// BB above the bundle or instruction at Pos.
  508. MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator Pos)
  509. : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
  510. /// Create a bundle from the sequence of instructions between B and E.
  511. MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator B,
  512. MachineBasicBlock::iterator E)
  513. : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
  514. assert(B != E && "No instructions to bundle");
  515. ++B;
  516. while (B != E) {
  517. MachineInstr &MI = *B;
  518. ++B;
  519. MI.bundleWithPred();
  520. }
  521. }
  522. /// Create an MIBundleBuilder representing an existing instruction or bundle
  523. /// that has MI as its head.
  524. explicit MIBundleBuilder(MachineInstr *MI)
  525. : MBB(*MI->getParent()), Begin(MI),
  526. End(getBundleEnd(MI->getIterator())) {}
  527. /// Return a reference to the basic block containing this bundle.
  528. MachineBasicBlock &getMBB() const { return MBB; }
  529. /// Return true if no instructions have been inserted in this bundle yet.
  530. /// Empty bundles aren't representable in a MachineBasicBlock.
  531. bool empty() const { return Begin == End; }
  532. /// Return an iterator to the first bundled instruction.
  533. MachineBasicBlock::instr_iterator begin() const { return Begin; }
  534. /// Return an iterator beyond the last bundled instruction.
  535. MachineBasicBlock::instr_iterator end() const { return End; }
  536. /// Insert MI into this bundle before I which must point to an instruction in
  537. /// the bundle, or end().
  538. MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
  539. MachineInstr *MI) {
  540. MBB.insert(I, MI);
  541. if (I == Begin) {
  542. if (!empty())
  543. MI->bundleWithSucc();
  544. Begin = MI->getIterator();
  545. return *this;
  546. }
  547. if (I == End) {
  548. MI->bundleWithPred();
  549. return *this;
  550. }
  551. // MI was inserted in the middle of the bundle, so its neighbors' flags are
  552. // already fine. Update MI's bundle flags manually.
  553. MI->setFlag(MachineInstr::BundledPred);
  554. MI->setFlag(MachineInstr::BundledSucc);
  555. return *this;
  556. }
  557. /// Insert MI into MBB by prepending it to the instructions in the bundle.
  558. /// MI will become the first instruction in the bundle.
  559. MIBundleBuilder &prepend(MachineInstr *MI) {
  560. return insert(begin(), MI);
  561. }
  562. /// Insert MI into MBB by appending it to the instructions in the bundle.
  563. /// MI will become the last instruction in the bundle.
  564. MIBundleBuilder &append(MachineInstr *MI) {
  565. return insert(end(), MI);
  566. }
  567. };
  568. } // end namespace llvm
  569. #endif // LLVM_CODEGEN_MACHINEINSTRBUILDER_H
  570. #ifdef __GNUC__
  571. #pragma GCC diagnostic pop
  572. #endif