MachineInstrBuilder.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. //===-- CodeGen/MachineInstBuilder.h - Simplify creation of MIs -*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file exposes a function named BuildMI, which is useful for dramatically
  11. // simplifying how MachineInstr's are created. It allows use of code like this:
  12. //
  13. // M = BuildMI(X86::ADDrr8, 2).addReg(argVal1).addReg(argVal2);
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H
  17. #define LLVM_CODEGEN_MACHINEINSTRBUILDER_H
  18. #include "llvm/CodeGen/MachineFunction.h"
  19. #include "llvm/CodeGen/MachineInstrBundle.h"
  20. #include "llvm/Support/ErrorHandling.h"
  21. namespace llvm {
  22. class MCInstrDesc;
  23. class MDNode;
  24. namespace RegState {
  25. enum {
  26. Define = 0x2,
  27. Implicit = 0x4,
  28. Kill = 0x8,
  29. Dead = 0x10,
  30. Undef = 0x20,
  31. EarlyClobber = 0x40,
  32. Debug = 0x80,
  33. InternalRead = 0x100,
  34. DefineNoRead = Define | Undef,
  35. ImplicitDefine = Implicit | Define,
  36. ImplicitKill = Implicit | Kill
  37. };
  38. }
  39. class MachineInstrBuilder {
  40. MachineFunction *MF;
  41. MachineInstr *MI;
  42. public:
  43. MachineInstrBuilder() : MF(nullptr), MI(nullptr) {}
  44. /// Create a MachineInstrBuilder for manipulating an existing instruction.
  45. /// F must be the machine function that was used to allocate I.
  46. MachineInstrBuilder(MachineFunction &F, MachineInstr *I) : MF(&F), MI(I) {}
  47. /// Allow automatic conversion to the machine instruction we are working on.
  48. ///
  49. operator MachineInstr*() const { return MI; }
  50. MachineInstr *operator->() const { return MI; }
  51. operator MachineBasicBlock::iterator() const { return MI; }
  52. /// If conversion operators fail, use this method to get the MachineInstr
  53. /// explicitly.
  54. MachineInstr *getInstr() const { return MI; }
  55. /// addReg - Add a new virtual register operand...
  56. ///
  57. const
  58. MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
  59. unsigned SubReg = 0) const {
  60. assert((flags & 0x1) == 0 &&
  61. "Passing in 'true' to addReg is forbidden! Use enums instead.");
  62. MI->addOperand(*MF, MachineOperand::CreateReg(RegNo,
  63. flags & RegState::Define,
  64. flags & RegState::Implicit,
  65. flags & RegState::Kill,
  66. flags & RegState::Dead,
  67. flags & RegState::Undef,
  68. flags & RegState::EarlyClobber,
  69. SubReg,
  70. flags & RegState::Debug,
  71. flags & RegState::InternalRead));
  72. return *this;
  73. }
  74. /// addImm - Add a new immediate operand.
  75. ///
  76. const MachineInstrBuilder &addImm(int64_t Val) const {
  77. MI->addOperand(*MF, MachineOperand::CreateImm(Val));
  78. return *this;
  79. }
  80. const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
  81. MI->addOperand(*MF, MachineOperand::CreateCImm(Val));
  82. return *this;
  83. }
  84. const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
  85. MI->addOperand(*MF, MachineOperand::CreateFPImm(Val));
  86. return *this;
  87. }
  88. const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
  89. unsigned char TargetFlags = 0) const {
  90. MI->addOperand(*MF, MachineOperand::CreateMBB(MBB, TargetFlags));
  91. return *this;
  92. }
  93. const MachineInstrBuilder &addFrameIndex(int Idx) const {
  94. MI->addOperand(*MF, MachineOperand::CreateFI(Idx));
  95. return *this;
  96. }
  97. const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
  98. int Offset = 0,
  99. unsigned char TargetFlags = 0) const {
  100. MI->addOperand(*MF, MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
  101. return *this;
  102. }
  103. const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0,
  104. unsigned char TargetFlags = 0) const {
  105. MI->addOperand(*MF, MachineOperand::CreateTargetIndex(Idx, Offset,
  106. TargetFlags));
  107. return *this;
  108. }
  109. const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
  110. unsigned char TargetFlags = 0) const {
  111. MI->addOperand(*MF, MachineOperand::CreateJTI(Idx, TargetFlags));
  112. return *this;
  113. }
  114. const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
  115. int64_t Offset = 0,
  116. unsigned char TargetFlags = 0) const {
  117. MI->addOperand(*MF, MachineOperand::CreateGA(GV, Offset, TargetFlags));
  118. return *this;
  119. }
  120. const MachineInstrBuilder &addExternalSymbol(const char *FnName,
  121. unsigned char TargetFlags = 0) const {
  122. MI->addOperand(*MF, MachineOperand::CreateES(FnName, TargetFlags));
  123. return *this;
  124. }
  125. const MachineInstrBuilder &addBlockAddress(const BlockAddress *BA,
  126. int64_t Offset = 0,
  127. unsigned char TargetFlags = 0) const {
  128. MI->addOperand(*MF, MachineOperand::CreateBA(BA, Offset, TargetFlags));
  129. return *this;
  130. }
  131. const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
  132. MI->addOperand(*MF, MachineOperand::CreateRegMask(Mask));
  133. return *this;
  134. }
  135. const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
  136. MI->addMemOperand(*MF, MMO);
  137. return *this;
  138. }
  139. const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
  140. MachineInstr::mmo_iterator e) const {
  141. MI->setMemRefs(b, e);
  142. return *this;
  143. }
  144. const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
  145. MI->addOperand(*MF, MO);
  146. return *this;
  147. }
  148. const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
  149. MI->addOperand(*MF, MachineOperand::CreateMetadata(MD));
  150. assert((MI->isDebugValue() ? static_cast<bool>(MI->getDebugVariable())
  151. : true) &&
  152. "first MDNode argument of a DBG_VALUE not a variable");
  153. return *this;
  154. }
  155. const MachineInstrBuilder &addCFIIndex(unsigned CFIIndex) const {
  156. MI->addOperand(*MF, MachineOperand::CreateCFIIndex(CFIIndex));
  157. return *this;
  158. }
  159. const MachineInstrBuilder &addSym(MCSymbol *Sym,
  160. unsigned char TargetFlags = 0) const {
  161. MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym, TargetFlags));
  162. return *this;
  163. }
  164. const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
  165. MI->setFlags(Flags);
  166. return *this;
  167. }
  168. const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
  169. MI->setFlag(Flag);
  170. return *this;
  171. }
  172. // Add a displacement from an existing MachineOperand with an added offset.
  173. const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off,
  174. unsigned char TargetFlags = 0) const {
  175. switch (Disp.getType()) {
  176. default:
  177. llvm_unreachable("Unhandled operand type in addDisp()");
  178. case MachineOperand::MO_Immediate:
  179. return addImm(Disp.getImm() + off);
  180. case MachineOperand::MO_GlobalAddress: {
  181. // If caller specifies new TargetFlags then use it, otherwise the
  182. // default behavior is to copy the target flags from the existing
  183. // MachineOperand. This means if the caller wants to clear the
  184. // target flags it needs to do so explicitly.
  185. if (TargetFlags)
  186. return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
  187. TargetFlags);
  188. return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
  189. Disp.getTargetFlags());
  190. }
  191. }
  192. }
  193. /// Copy all the implicit operands from OtherMI onto this one.
  194. const MachineInstrBuilder &copyImplicitOps(const MachineInstr *OtherMI) {
  195. MI->copyImplicitOps(*MF, OtherMI);
  196. return *this;
  197. }
  198. };
  199. /// BuildMI - Builder interface. Specify how to create the initial instruction
  200. /// itself.
  201. ///
  202. inline MachineInstrBuilder BuildMI(MachineFunction &MF,
  203. DebugLoc DL,
  204. const MCInstrDesc &MCID) {
  205. return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
  206. }
  207. /// BuildMI - This version of the builder sets up the first operand as a
  208. /// destination virtual register.
  209. ///
  210. inline MachineInstrBuilder BuildMI(MachineFunction &MF,
  211. DebugLoc DL,
  212. const MCInstrDesc &MCID,
  213. unsigned DestReg) {
  214. return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
  215. .addReg(DestReg, RegState::Define);
  216. }
  217. /// BuildMI - This version of the builder inserts the newly-built
  218. /// instruction before the given position in the given MachineBasicBlock, and
  219. /// sets up the first operand as a destination virtual register.
  220. ///
  221. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  222. MachineBasicBlock::iterator I,
  223. DebugLoc DL,
  224. const MCInstrDesc &MCID,
  225. unsigned DestReg) {
  226. MachineFunction &MF = *BB.getParent();
  227. MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
  228. BB.insert(I, MI);
  229. return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
  230. }
  231. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  232. MachineBasicBlock::instr_iterator I,
  233. DebugLoc DL,
  234. const MCInstrDesc &MCID,
  235. unsigned DestReg) {
  236. MachineFunction &MF = *BB.getParent();
  237. MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
  238. BB.insert(I, MI);
  239. return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
  240. }
  241. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  242. MachineInstr *I,
  243. DebugLoc DL,
  244. const MCInstrDesc &MCID,
  245. unsigned DestReg) {
  246. if (I->isInsideBundle()) {
  247. MachineBasicBlock::instr_iterator MII = I;
  248. return BuildMI(BB, MII, DL, MCID, DestReg);
  249. }
  250. MachineBasicBlock::iterator MII = I;
  251. return BuildMI(BB, MII, DL, MCID, DestReg);
  252. }
  253. /// BuildMI - This version of the builder inserts the newly-built
  254. /// instruction before the given position in the given MachineBasicBlock, and
  255. /// does NOT take a destination register.
  256. ///
  257. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  258. MachineBasicBlock::iterator I,
  259. DebugLoc DL,
  260. const MCInstrDesc &MCID) {
  261. MachineFunction &MF = *BB.getParent();
  262. MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
  263. BB.insert(I, MI);
  264. return MachineInstrBuilder(MF, MI);
  265. }
  266. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  267. MachineBasicBlock::instr_iterator I,
  268. DebugLoc DL,
  269. const MCInstrDesc &MCID) {
  270. MachineFunction &MF = *BB.getParent();
  271. MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
  272. BB.insert(I, MI);
  273. return MachineInstrBuilder(MF, MI);
  274. }
  275. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  276. MachineInstr *I,
  277. DebugLoc DL,
  278. const MCInstrDesc &MCID) {
  279. if (I->isInsideBundle()) {
  280. MachineBasicBlock::instr_iterator MII = I;
  281. return BuildMI(BB, MII, DL, MCID);
  282. }
  283. MachineBasicBlock::iterator MII = I;
  284. return BuildMI(BB, MII, DL, MCID);
  285. }
  286. /// BuildMI - This version of the builder inserts the newly-built
  287. /// instruction at the end of the given MachineBasicBlock, and does NOT take a
  288. /// destination register.
  289. ///
  290. inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
  291. DebugLoc DL,
  292. const MCInstrDesc &MCID) {
  293. return BuildMI(*BB, BB->end(), DL, MCID);
  294. }
  295. /// BuildMI - This version of the builder inserts the newly-built
  296. /// instruction at the end of the given MachineBasicBlock, and sets up the first
  297. /// operand as a destination virtual register.
  298. ///
  299. inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
  300. DebugLoc DL,
  301. const MCInstrDesc &MCID,
  302. unsigned DestReg) {
  303. return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
  304. }
  305. /// BuildMI - This version of the builder builds a DBG_VALUE intrinsic
  306. /// for either a value in a register or a register-indirect+offset
  307. /// address. The convention is that a DBG_VALUE is indirect iff the
  308. /// second operand is an immediate.
  309. ///
  310. inline MachineInstrBuilder BuildMI(MachineFunction &MF, DebugLoc DL,
  311. const MCInstrDesc &MCID, bool IsIndirect,
  312. unsigned Reg, unsigned Offset,
  313. const MDNode *Variable, const MDNode *Expr) {
  314. assert(isa<DILocalVariable>(Variable) && "not a variable");
  315. assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
  316. assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
  317. "Expected inlined-at fields to agree");
  318. if (IsIndirect)
  319. return BuildMI(MF, DL, MCID)
  320. .addReg(Reg, RegState::Debug)
  321. .addImm(Offset)
  322. .addMetadata(Variable)
  323. .addMetadata(Expr);
  324. else {
  325. assert(Offset == 0 && "A direct address cannot have an offset.");
  326. return BuildMI(MF, DL, MCID)
  327. .addReg(Reg, RegState::Debug)
  328. .addReg(0U, RegState::Debug)
  329. .addMetadata(Variable)
  330. .addMetadata(Expr);
  331. }
  332. }
  333. /// BuildMI - This version of the builder builds a DBG_VALUE intrinsic
  334. /// for either a value in a register or a register-indirect+offset
  335. /// address and inserts it at position I.
  336. ///
  337. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  338. MachineBasicBlock::iterator I, DebugLoc DL,
  339. const MCInstrDesc &MCID, bool IsIndirect,
  340. unsigned Reg, unsigned Offset,
  341. const MDNode *Variable, const MDNode *Expr) {
  342. assert(isa<DILocalVariable>(Variable) && "not a variable");
  343. assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
  344. MachineFunction &MF = *BB.getParent();
  345. MachineInstr *MI =
  346. BuildMI(MF, DL, MCID, IsIndirect, Reg, Offset, Variable, Expr);
  347. BB.insert(I, MI);
  348. return MachineInstrBuilder(MF, MI);
  349. }
  350. inline unsigned getDefRegState(bool B) {
  351. return B ? RegState::Define : 0;
  352. }
  353. inline unsigned getImplRegState(bool B) {
  354. return B ? RegState::Implicit : 0;
  355. }
  356. inline unsigned getKillRegState(bool B) {
  357. return B ? RegState::Kill : 0;
  358. }
  359. inline unsigned getDeadRegState(bool B) {
  360. return B ? RegState::Dead : 0;
  361. }
  362. inline unsigned getUndefRegState(bool B) {
  363. return B ? RegState::Undef : 0;
  364. }
  365. inline unsigned getInternalReadRegState(bool B) {
  366. return B ? RegState::InternalRead : 0;
  367. }
  368. inline unsigned getDebugRegState(bool B) {
  369. return B ? RegState::Debug : 0;
  370. }
  371. /// Helper class for constructing bundles of MachineInstrs.
  372. ///
  373. /// MIBundleBuilder can create a bundle from scratch by inserting new
  374. /// MachineInstrs one at a time, or it can create a bundle from a sequence of
  375. /// existing MachineInstrs in a basic block.
  376. class MIBundleBuilder {
  377. MachineBasicBlock &MBB;
  378. MachineBasicBlock::instr_iterator Begin;
  379. MachineBasicBlock::instr_iterator End;
  380. public:
  381. /// Create an MIBundleBuilder that inserts instructions into a new bundle in
  382. /// BB above the bundle or instruction at Pos.
  383. MIBundleBuilder(MachineBasicBlock &BB,
  384. MachineBasicBlock::iterator Pos)
  385. : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
  386. /// Create a bundle from the sequence of instructions between B and E.
  387. MIBundleBuilder(MachineBasicBlock &BB,
  388. MachineBasicBlock::iterator B,
  389. MachineBasicBlock::iterator E)
  390. : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
  391. assert(B != E && "No instructions to bundle");
  392. ++B;
  393. while (B != E) {
  394. MachineInstr *MI = B;
  395. ++B;
  396. MI->bundleWithPred();
  397. }
  398. }
  399. /// Create an MIBundleBuilder representing an existing instruction or bundle
  400. /// that has MI as its head.
  401. explicit MIBundleBuilder(MachineInstr *MI)
  402. : MBB(*MI->getParent()), Begin(MI), End(getBundleEnd(MI)) {}
  403. /// Return a reference to the basic block containing this bundle.
  404. MachineBasicBlock &getMBB() const { return MBB; }
  405. /// Return true if no instructions have been inserted in this bundle yet.
  406. /// Empty bundles aren't representable in a MachineBasicBlock.
  407. bool empty() const { return Begin == End; }
  408. /// Return an iterator to the first bundled instruction.
  409. MachineBasicBlock::instr_iterator begin() const { return Begin; }
  410. /// Return an iterator beyond the last bundled instruction.
  411. MachineBasicBlock::instr_iterator end() const { return End; }
  412. /// Insert MI into this bundle before I which must point to an instruction in
  413. /// the bundle, or end().
  414. MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
  415. MachineInstr *MI) {
  416. MBB.insert(I, MI);
  417. if (I == Begin) {
  418. if (!empty())
  419. MI->bundleWithSucc();
  420. Begin = MI;
  421. return *this;
  422. }
  423. if (I == End) {
  424. MI->bundleWithPred();
  425. return *this;
  426. }
  427. // MI was inserted in the middle of the bundle, so its neighbors' flags are
  428. // already fine. Update MI's bundle flags manually.
  429. MI->setFlag(MachineInstr::BundledPred);
  430. MI->setFlag(MachineInstr::BundledSucc);
  431. return *this;
  432. }
  433. /// Insert MI into MBB by prepending it to the instructions in the bundle.
  434. /// MI will become the first instruction in the bundle.
  435. MIBundleBuilder &prepend(MachineInstr *MI) {
  436. return insert(begin(), MI);
  437. }
  438. /// Insert MI into MBB by appending it to the instructions in the bundle.
  439. /// MI will become the last instruction in the bundle.
  440. MIBundleBuilder &append(MachineInstr *MI) {
  441. return insert(end(), MI);
  442. }
  443. };
  444. } // End llvm namespace
  445. #endif