MachineInstrBuilder.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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->isDebugValue() ? 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. /// Copy all the implicit operands from OtherMI onto this one.
  274. const MachineInstrBuilder &
  275. copyImplicitOps(const MachineInstr &OtherMI) const {
  276. MI->copyImplicitOps(*MF, OtherMI);
  277. return *this;
  278. }
  279. bool constrainAllUses(const TargetInstrInfo &TII,
  280. const TargetRegisterInfo &TRI,
  281. const RegisterBankInfo &RBI) const {
  282. return constrainSelectedInstRegOperands(*MI, TII, TRI, RBI);
  283. }
  284. };
  285. /// Builder interface. Specify how to create the initial instruction itself.
  286. inline MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
  287. const MCInstrDesc &MCID) {
  288. return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
  289. }
  290. /// This version of the builder sets up the first operand as a
  291. /// destination virtual register.
  292. inline MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
  293. const MCInstrDesc &MCID, Register DestReg) {
  294. return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
  295. .addReg(DestReg, RegState::Define);
  296. }
  297. /// This version of the builder inserts the newly-built instruction before
  298. /// the given position in the given MachineBasicBlock, and sets up the first
  299. /// operand as a destination virtual register.
  300. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  301. MachineBasicBlock::iterator I,
  302. const DebugLoc &DL, const MCInstrDesc &MCID,
  303. Register DestReg) {
  304. MachineFunction &MF = *BB.getParent();
  305. MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
  306. BB.insert(I, MI);
  307. return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
  308. }
  309. /// This version of the builder inserts the newly-built instruction before
  310. /// the given position in the given MachineBasicBlock, and sets up the first
  311. /// operand as a destination virtual register.
  312. ///
  313. /// If \c I is inside a bundle, then the newly inserted \a MachineInstr is
  314. /// added to the same bundle.
  315. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  316. MachineBasicBlock::instr_iterator I,
  317. const DebugLoc &DL, const MCInstrDesc &MCID,
  318. Register DestReg) {
  319. MachineFunction &MF = *BB.getParent();
  320. MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
  321. BB.insert(I, MI);
  322. return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
  323. }
  324. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
  325. const DebugLoc &DL, const MCInstrDesc &MCID,
  326. Register DestReg) {
  327. // Calling the overload for instr_iterator is always correct. However, the
  328. // definition is not available in headers, so inline the check.
  329. if (I.isInsideBundle())
  330. return BuildMI(BB, MachineBasicBlock::instr_iterator(I), DL, MCID, DestReg);
  331. return BuildMI(BB, MachineBasicBlock::iterator(I), DL, MCID, DestReg);
  332. }
  333. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
  334. const DebugLoc &DL, const MCInstrDesc &MCID,
  335. Register DestReg) {
  336. return BuildMI(BB, *I, DL, MCID, DestReg);
  337. }
  338. /// This version of the builder inserts the newly-built instruction before the
  339. /// given position in the given MachineBasicBlock, and does NOT take a
  340. /// destination register.
  341. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  342. MachineBasicBlock::iterator I,
  343. const DebugLoc &DL,
  344. const MCInstrDesc &MCID) {
  345. MachineFunction &MF = *BB.getParent();
  346. MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
  347. BB.insert(I, MI);
  348. return MachineInstrBuilder(MF, MI);
  349. }
  350. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  351. MachineBasicBlock::instr_iterator I,
  352. const DebugLoc &DL,
  353. const MCInstrDesc &MCID) {
  354. MachineFunction &MF = *BB.getParent();
  355. MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
  356. BB.insert(I, MI);
  357. return MachineInstrBuilder(MF, MI);
  358. }
  359. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
  360. const DebugLoc &DL,
  361. const MCInstrDesc &MCID) {
  362. // Calling the overload for instr_iterator is always correct. However, the
  363. // definition is not available in headers, so inline the check.
  364. if (I.isInsideBundle())
  365. return BuildMI(BB, MachineBasicBlock::instr_iterator(I), DL, MCID);
  366. return BuildMI(BB, MachineBasicBlock::iterator(I), DL, MCID);
  367. }
  368. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
  369. const DebugLoc &DL,
  370. const MCInstrDesc &MCID) {
  371. return BuildMI(BB, *I, DL, MCID);
  372. }
  373. /// This version of the builder inserts the newly-built instruction at the end
  374. /// of the given MachineBasicBlock, and does NOT take a destination register.
  375. inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, const DebugLoc &DL,
  376. const MCInstrDesc &MCID) {
  377. return BuildMI(*BB, BB->end(), DL, MCID);
  378. }
  379. /// This version of the builder inserts the newly-built instruction at the
  380. /// end of the given MachineBasicBlock, and sets up the first operand as a
  381. /// destination virtual register.
  382. inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, const DebugLoc &DL,
  383. const MCInstrDesc &MCID, Register DestReg) {
  384. return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
  385. }
  386. /// This version of the builder builds a DBG_VALUE intrinsic
  387. /// for either a value in a register or a register-indirect
  388. /// address. The convention is that a DBG_VALUE is indirect iff the
  389. /// second operand is an immediate.
  390. MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
  391. const MCInstrDesc &MCID, bool IsIndirect,
  392. Register Reg, const MDNode *Variable,
  393. const MDNode *Expr);
  394. /// This version of the builder builds a DBG_VALUE intrinsic
  395. /// for a MachineOperand.
  396. MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
  397. const MCInstrDesc &MCID, bool IsIndirect,
  398. const MachineOperand &MO, const MDNode *Variable,
  399. const MDNode *Expr);
  400. /// This version of the builder builds a DBG_VALUE or DBG_VALUE_LIST intrinsic
  401. /// for a MachineOperand.
  402. MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
  403. const MCInstrDesc &MCID, bool IsIndirect,
  404. ArrayRef<MachineOperand> MOs,
  405. const MDNode *Variable, const MDNode *Expr);
  406. /// This version of the builder builds a DBG_VALUE intrinsic
  407. /// for either a value in a register or a register-indirect
  408. /// address and inserts it at position I.
  409. MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  410. MachineBasicBlock::iterator I, const DebugLoc &DL,
  411. const MCInstrDesc &MCID, bool IsIndirect,
  412. Register Reg, const MDNode *Variable,
  413. const MDNode *Expr);
  414. /// This version of the builder builds a DBG_VALUE intrinsic
  415. /// for a machine operand and inserts it at position I.
  416. MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  417. MachineBasicBlock::iterator I, const DebugLoc &DL,
  418. const MCInstrDesc &MCID, bool IsIndirect,
  419. MachineOperand &MO, const MDNode *Variable,
  420. const MDNode *Expr);
  421. /// This version of the builder builds a DBG_VALUE or DBG_VALUE_LIST intrinsic
  422. /// for a machine operand and inserts it at position I.
  423. MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  424. MachineBasicBlock::iterator I, const DebugLoc &DL,
  425. const MCInstrDesc &MCID, bool IsIndirect,
  426. ArrayRef<MachineOperand> MOs,
  427. const MDNode *Variable, const MDNode *Expr);
  428. /// Clone a DBG_VALUE whose value has been spilled to FrameIndex.
  429. MachineInstr *buildDbgValueForSpill(MachineBasicBlock &BB,
  430. MachineBasicBlock::iterator I,
  431. const MachineInstr &Orig, int FrameIndex,
  432. Register SpillReg);
  433. MachineInstr *
  434. buildDbgValueForSpill(MachineBasicBlock &BB, MachineBasicBlock::iterator I,
  435. const MachineInstr &Orig, int FrameIndex,
  436. SmallVectorImpl<const MachineOperand *> &SpilledOperands);
  437. /// Update a DBG_VALUE whose value has been spilled to FrameIndex. Useful when
  438. /// modifying an instruction in place while iterating over a basic block.
  439. void updateDbgValueForSpill(MachineInstr &Orig, int FrameIndex, Register Reg);
  440. inline unsigned getDefRegState(bool B) {
  441. return B ? RegState::Define : 0;
  442. }
  443. inline unsigned getImplRegState(bool B) {
  444. return B ? RegState::Implicit : 0;
  445. }
  446. inline unsigned getKillRegState(bool B) {
  447. return B ? RegState::Kill : 0;
  448. }
  449. inline unsigned getDeadRegState(bool B) {
  450. return B ? RegState::Dead : 0;
  451. }
  452. inline unsigned getUndefRegState(bool B) {
  453. return B ? RegState::Undef : 0;
  454. }
  455. inline unsigned getInternalReadRegState(bool B) {
  456. return B ? RegState::InternalRead : 0;
  457. }
  458. inline unsigned getDebugRegState(bool B) {
  459. return B ? RegState::Debug : 0;
  460. }
  461. inline unsigned getRenamableRegState(bool B) {
  462. return B ? RegState::Renamable : 0;
  463. }
  464. /// Get all register state flags from machine operand \p RegOp.
  465. inline unsigned getRegState(const MachineOperand &RegOp) {
  466. assert(RegOp.isReg() && "Not a register operand");
  467. return getDefRegState(RegOp.isDef()) | getImplRegState(RegOp.isImplicit()) |
  468. getKillRegState(RegOp.isKill()) | getDeadRegState(RegOp.isDead()) |
  469. getUndefRegState(RegOp.isUndef()) |
  470. getInternalReadRegState(RegOp.isInternalRead()) |
  471. getDebugRegState(RegOp.isDebug()) |
  472. getRenamableRegState(Register::isPhysicalRegister(RegOp.getReg()) &&
  473. RegOp.isRenamable());
  474. }
  475. /// Helper class for constructing bundles of MachineInstrs.
  476. ///
  477. /// MIBundleBuilder can create a bundle from scratch by inserting new
  478. /// MachineInstrs one at a time, or it can create a bundle from a sequence of
  479. /// existing MachineInstrs in a basic block.
  480. class MIBundleBuilder {
  481. MachineBasicBlock &MBB;
  482. MachineBasicBlock::instr_iterator Begin;
  483. MachineBasicBlock::instr_iterator End;
  484. public:
  485. /// Create an MIBundleBuilder that inserts instructions into a new bundle in
  486. /// BB above the bundle or instruction at Pos.
  487. MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator Pos)
  488. : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
  489. /// Create a bundle from the sequence of instructions between B and E.
  490. MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator B,
  491. MachineBasicBlock::iterator E)
  492. : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
  493. assert(B != E && "No instructions to bundle");
  494. ++B;
  495. while (B != E) {
  496. MachineInstr &MI = *B;
  497. ++B;
  498. MI.bundleWithPred();
  499. }
  500. }
  501. /// Create an MIBundleBuilder representing an existing instruction or bundle
  502. /// that has MI as its head.
  503. explicit MIBundleBuilder(MachineInstr *MI)
  504. : MBB(*MI->getParent()), Begin(MI),
  505. End(getBundleEnd(MI->getIterator())) {}
  506. /// Return a reference to the basic block containing this bundle.
  507. MachineBasicBlock &getMBB() const { return MBB; }
  508. /// Return true if no instructions have been inserted in this bundle yet.
  509. /// Empty bundles aren't representable in a MachineBasicBlock.
  510. bool empty() const { return Begin == End; }
  511. /// Return an iterator to the first bundled instruction.
  512. MachineBasicBlock::instr_iterator begin() const { return Begin; }
  513. /// Return an iterator beyond the last bundled instruction.
  514. MachineBasicBlock::instr_iterator end() const { return End; }
  515. /// Insert MI into this bundle before I which must point to an instruction in
  516. /// the bundle, or end().
  517. MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
  518. MachineInstr *MI) {
  519. MBB.insert(I, MI);
  520. if (I == Begin) {
  521. if (!empty())
  522. MI->bundleWithSucc();
  523. Begin = MI->getIterator();
  524. return *this;
  525. }
  526. if (I == End) {
  527. MI->bundleWithPred();
  528. return *this;
  529. }
  530. // MI was inserted in the middle of the bundle, so its neighbors' flags are
  531. // already fine. Update MI's bundle flags manually.
  532. MI->setFlag(MachineInstr::BundledPred);
  533. MI->setFlag(MachineInstr::BundledSucc);
  534. return *this;
  535. }
  536. /// Insert MI into MBB by prepending it to the instructions in the bundle.
  537. /// MI will become the first instruction in the bundle.
  538. MIBundleBuilder &prepend(MachineInstr *MI) {
  539. return insert(begin(), MI);
  540. }
  541. /// Insert MI into MBB by appending it to the instructions in the bundle.
  542. /// MI will become the last instruction in the bundle.
  543. MIBundleBuilder &append(MachineInstr *MI) {
  544. return insert(end(), MI);
  545. }
  546. };
  547. } // end namespace llvm
  548. #endif // LLVM_CODEGEN_MACHINEINSTRBUILDER_H
  549. #ifdef __GNUC__
  550. #pragma GCC diagnostic pop
  551. #endif