MachineInstrBuilder.h 24 KB

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