Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

447 lines
16 KiB

  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(0), MI(0) {}
  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. /// addReg - Add a new virtual register operand...
  53. ///
  54. const
  55. MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
  56. unsigned SubReg = 0) const {
  57. assert((flags & 0x1) == 0 &&
  58. "Passing in 'true' to addReg is forbidden! Use enums instead.");
  59. MI->addOperand(*MF, MachineOperand::CreateReg(RegNo,
  60. flags & RegState::Define,
  61. flags & RegState::Implicit,
  62. flags & RegState::Kill,
  63. flags & RegState::Dead,
  64. flags & RegState::Undef,
  65. flags & RegState::EarlyClobber,
  66. SubReg,
  67. flags & RegState::Debug,
  68. flags & RegState::InternalRead));
  69. return *this;
  70. }
  71. /// addImm - Add a new immediate operand.
  72. ///
  73. const MachineInstrBuilder &addImm(int64_t Val) const {
  74. MI->addOperand(*MF, MachineOperand::CreateImm(Val));
  75. return *this;
  76. }
  77. const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
  78. MI->addOperand(*MF, MachineOperand::CreateCImm(Val));
  79. return *this;
  80. }
  81. const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
  82. MI->addOperand(*MF, MachineOperand::CreateFPImm(Val));
  83. return *this;
  84. }
  85. const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
  86. unsigned char TargetFlags = 0) const {
  87. MI->addOperand(*MF, MachineOperand::CreateMBB(MBB, TargetFlags));
  88. return *this;
  89. }
  90. const MachineInstrBuilder &addFrameIndex(int Idx) const {
  91. MI->addOperand(*MF, MachineOperand::CreateFI(Idx));
  92. return *this;
  93. }
  94. const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
  95. int Offset = 0,
  96. unsigned char TargetFlags = 0) const {
  97. MI->addOperand(*MF, MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
  98. return *this;
  99. }
  100. const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0,
  101. unsigned char TargetFlags = 0) const {
  102. MI->addOperand(*MF, MachineOperand::CreateTargetIndex(Idx, Offset,
  103. TargetFlags));
  104. return *this;
  105. }
  106. const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
  107. unsigned char TargetFlags = 0) const {
  108. MI->addOperand(*MF, MachineOperand::CreateJTI(Idx, TargetFlags));
  109. return *this;
  110. }
  111. const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
  112. int64_t Offset = 0,
  113. unsigned char TargetFlags = 0) const {
  114. MI->addOperand(*MF, MachineOperand::CreateGA(GV, Offset, TargetFlags));
  115. return *this;
  116. }
  117. const MachineInstrBuilder &addExternalSymbol(const char *FnName,
  118. unsigned char TargetFlags = 0) const {
  119. MI->addOperand(*MF, MachineOperand::CreateES(FnName, TargetFlags));
  120. return *this;
  121. }
  122. const MachineInstrBuilder &addBlockAddress(const BlockAddress *BA,
  123. int64_t Offset = 0,
  124. unsigned char TargetFlags = 0) const {
  125. MI->addOperand(*MF, MachineOperand::CreateBA(BA, Offset, TargetFlags));
  126. return *this;
  127. }
  128. const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
  129. MI->addOperand(*MF, MachineOperand::CreateRegMask(Mask));
  130. return *this;
  131. }
  132. const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
  133. MI->addMemOperand(*MF, MMO);
  134. return *this;
  135. }
  136. const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
  137. MachineInstr::mmo_iterator e) const {
  138. MI->setMemRefs(b, e);
  139. return *this;
  140. }
  141. const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
  142. MI->addOperand(*MF, MO);
  143. return *this;
  144. }
  145. const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
  146. MI->addOperand(*MF, MachineOperand::CreateMetadata(MD));
  147. return *this;
  148. }
  149. const MachineInstrBuilder &addSym(MCSymbol *Sym) const {
  150. MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym));
  151. return *this;
  152. }
  153. const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
  154. MI->setFlags(Flags);
  155. return *this;
  156. }
  157. const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
  158. MI->setFlag(Flag);
  159. return *this;
  160. }
  161. // Add a displacement from an existing MachineOperand with an added offset.
  162. const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off,
  163. unsigned char TargetFlags = 0) const {
  164. switch (Disp.getType()) {
  165. default:
  166. llvm_unreachable("Unhandled operand type in addDisp()");
  167. case MachineOperand::MO_Immediate:
  168. return addImm(Disp.getImm() + off);
  169. case MachineOperand::MO_GlobalAddress: {
  170. // If caller specifies new TargetFlags then use it, otherwise the
  171. // default behavior is to copy the target flags from the existing
  172. // MachineOperand. This means if the caller wants to clear the
  173. // target flags it needs to do so explicitly.
  174. if (TargetFlags)
  175. return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
  176. TargetFlags);
  177. return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
  178. Disp.getTargetFlags());
  179. }
  180. }
  181. }
  182. /// Copy all the implicit operands from OtherMI onto this one.
  183. const MachineInstrBuilder &copyImplicitOps(const MachineInstr *OtherMI) {
  184. MI->copyImplicitOps(*MF, OtherMI);
  185. return *this;
  186. }
  187. };
  188. /// BuildMI - Builder interface. Specify how to create the initial instruction
  189. /// itself.
  190. ///
  191. inline MachineInstrBuilder BuildMI(MachineFunction &MF,
  192. DebugLoc DL,
  193. const MCInstrDesc &MCID) {
  194. return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
  195. }
  196. /// BuildMI - This version of the builder sets up the first operand as a
  197. /// destination virtual register.
  198. ///
  199. inline MachineInstrBuilder BuildMI(MachineFunction &MF,
  200. DebugLoc DL,
  201. const MCInstrDesc &MCID,
  202. unsigned DestReg) {
  203. return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
  204. .addReg(DestReg, RegState::Define);
  205. }
  206. /// BuildMI - This version of the builder inserts the newly-built
  207. /// instruction before the given position in the given MachineBasicBlock, and
  208. /// sets up the first operand as a destination virtual register.
  209. ///
  210. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  211. MachineBasicBlock::iterator I,
  212. DebugLoc DL,
  213. const MCInstrDesc &MCID,
  214. unsigned DestReg) {
  215. MachineFunction &MF = *BB.getParent();
  216. MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
  217. BB.insert(I, MI);
  218. return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
  219. }
  220. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  221. MachineBasicBlock::instr_iterator I,
  222. DebugLoc DL,
  223. const MCInstrDesc &MCID,
  224. unsigned DestReg) {
  225. MachineFunction &MF = *BB.getParent();
  226. MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
  227. BB.insert(I, MI);
  228. return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
  229. }
  230. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  231. MachineInstr *I,
  232. DebugLoc DL,
  233. const MCInstrDesc &MCID,
  234. unsigned DestReg) {
  235. if (I->isInsideBundle()) {
  236. MachineBasicBlock::instr_iterator MII = I;
  237. return BuildMI(BB, MII, DL, MCID, DestReg);
  238. }
  239. MachineBasicBlock::iterator MII = I;
  240. return BuildMI(BB, MII, DL, MCID, DestReg);
  241. }
  242. /// BuildMI - This version of the builder inserts the newly-built
  243. /// instruction before the given position in the given MachineBasicBlock, and
  244. /// does NOT take a destination register.
  245. ///
  246. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  247. MachineBasicBlock::iterator I,
  248. DebugLoc DL,
  249. const MCInstrDesc &MCID) {
  250. MachineFunction &MF = *BB.getParent();
  251. MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
  252. BB.insert(I, MI);
  253. return MachineInstrBuilder(MF, MI);
  254. }
  255. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  256. MachineBasicBlock::instr_iterator I,
  257. DebugLoc DL,
  258. const MCInstrDesc &MCID) {
  259. MachineFunction &MF = *BB.getParent();
  260. MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
  261. BB.insert(I, MI);
  262. return MachineInstrBuilder(MF, MI);
  263. }
  264. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
  265. MachineInstr *I,
  266. DebugLoc DL,
  267. const MCInstrDesc &MCID) {
  268. if (I->isInsideBundle()) {
  269. MachineBasicBlock::instr_iterator MII = I;
  270. return BuildMI(BB, MII, DL, MCID);
  271. }
  272. MachineBasicBlock::iterator MII = I;
  273. return BuildMI(BB, MII, DL, MCID);
  274. }
  275. /// BuildMI - This version of the builder inserts the newly-built
  276. /// instruction at the end of the given MachineBasicBlock, and does NOT take a
  277. /// destination register.
  278. ///
  279. inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
  280. DebugLoc DL,
  281. const MCInstrDesc &MCID) {
  282. return BuildMI(*BB, BB->end(), DL, MCID);
  283. }
  284. /// BuildMI - This version of the builder inserts the newly-built
  285. /// instruction at the end of the given MachineBasicBlock, and sets up the first
  286. /// operand as a destination virtual register.
  287. ///
  288. inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
  289. DebugLoc DL,
  290. const MCInstrDesc &MCID,
  291. unsigned DestReg) {
  292. return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
  293. }
  294. inline unsigned getDefRegState(bool B) {
  295. return B ? RegState::Define : 0;
  296. }
  297. inline unsigned getImplRegState(bool B) {
  298. return B ? RegState::Implicit : 0;
  299. }
  300. inline unsigned getKillRegState(bool B) {
  301. return B ? RegState::Kill : 0;
  302. }
  303. inline unsigned getDeadRegState(bool B) {
  304. return B ? RegState::Dead : 0;
  305. }
  306. inline unsigned getUndefRegState(bool B) {
  307. return B ? RegState::Undef : 0;
  308. }
  309. inline unsigned getInternalReadRegState(bool B) {
  310. return B ? RegState::InternalRead : 0;
  311. }
  312. inline unsigned getDebugRegState(bool B) {
  313. return B ? RegState::Debug : 0;
  314. }
  315. /// Helper class for constructing bundles of MachineInstrs.
  316. ///
  317. /// MIBundleBuilder can create a bundle from scratch by inserting new
  318. /// MachineInstrs one at a time, or it can create a bundle from a sequence of
  319. /// existing MachineInstrs in a basic block.
  320. class MIBundleBuilder {
  321. MachineBasicBlock &MBB;
  322. MachineBasicBlock::instr_iterator Begin;
  323. MachineBasicBlock::instr_iterator End;
  324. public:
  325. /// Create an MIBundleBuilder that inserts instructions into a new bundle in
  326. /// BB above the bundle or instruction at Pos.
  327. MIBundleBuilder(MachineBasicBlock &BB,
  328. MachineBasicBlock::iterator Pos)
  329. : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
  330. /// Create a bundle from the sequence of instructions between B and E.
  331. MIBundleBuilder(MachineBasicBlock &BB,
  332. MachineBasicBlock::iterator B,
  333. MachineBasicBlock::iterator E)
  334. : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
  335. assert(B != E && "No instructions to bundle");
  336. ++B;
  337. while (B != E) {
  338. MachineInstr *MI = B;
  339. ++B;
  340. MI->bundleWithPred();
  341. }
  342. }
  343. /// Create an MIBundleBuilder representing an existing instruction or bundle
  344. /// that has MI as its head.
  345. explicit MIBundleBuilder(MachineInstr *MI)
  346. : MBB(*MI->getParent()), Begin(MI), End(getBundleEnd(MI)) {}
  347. /// Return a reference to the basic block containing this bundle.
  348. MachineBasicBlock &getMBB() const { return MBB; }
  349. /// Return true if no instructions have been inserted in this bundle yet.
  350. /// Empty bundles aren't representable in a MachineBasicBlock.
  351. bool empty() const { return Begin == End; }
  352. /// Return an iterator to the first bundled instruction.
  353. MachineBasicBlock::instr_iterator begin() const { return Begin; }
  354. /// Return an iterator beyond the last bundled instruction.
  355. MachineBasicBlock::instr_iterator end() const { return End; }
  356. /// Insert MI into this bundle before I which must point to an instruction in
  357. /// the bundle, or end().
  358. MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
  359. MachineInstr *MI) {
  360. MBB.insert(I, MI);
  361. if (I == Begin) {
  362. if (!empty())
  363. MI->bundleWithSucc();
  364. Begin = MI;
  365. return *this;
  366. }
  367. if (I == End) {
  368. MI->bundleWithPred();
  369. return *this;
  370. }
  371. // MI was inserted in the middle of the bundle, so its neighbors' flags are
  372. // already fine. Update MI's bundle flags manually.
  373. MI->setFlag(MachineInstr::BundledPred);
  374. MI->setFlag(MachineInstr::BundledSucc);
  375. return *this;
  376. }
  377. /// Insert MI into MBB by prepending it to the instructions in the bundle.
  378. /// MI will become the first instruction in the bundle.
  379. MIBundleBuilder &prepend(MachineInstr *MI) {
  380. return insert(begin(), MI);
  381. }
  382. /// Insert MI into MBB by appending it to the instructions in the bundle.
  383. /// MI will become the last instruction in the bundle.
  384. MIBundleBuilder &append(MachineInstr *MI) {
  385. return insert(end(), MI);
  386. }
  387. };
  388. } // End llvm namespace
  389. #endif