MachineInstr.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. //===-- lib/CodeGen/MachineInstr.cpp --------------------------------------===//
  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. // Methods common to all machine instructions.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/MachineInstr.h"
  14. #include "llvm/Constants.h"
  15. #include "llvm/InlineAsm.h"
  16. #include "llvm/Value.h"
  17. #include "llvm/CodeGen/MachineFunction.h"
  18. #include "llvm/CodeGen/MachineRegisterInfo.h"
  19. #include "llvm/CodeGen/PseudoSourceValue.h"
  20. #include "llvm/Target/TargetMachine.h"
  21. #include "llvm/Target/TargetInstrInfo.h"
  22. #include "llvm/Target/TargetInstrDesc.h"
  23. #include "llvm/Target/TargetRegisterInfo.h"
  24. #include "llvm/Support/LeakDetector.h"
  25. #include "llvm/Support/MathExtras.h"
  26. #include "llvm/Support/Streams.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. #include "llvm/ADT/FoldingSet.h"
  29. #include <ostream>
  30. using namespace llvm;
  31. //===----------------------------------------------------------------------===//
  32. // MachineOperand Implementation
  33. //===----------------------------------------------------------------------===//
  34. /// AddRegOperandToRegInfo - Add this register operand to the specified
  35. /// MachineRegisterInfo. If it is null, then the next/prev fields should be
  36. /// explicitly nulled out.
  37. void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) {
  38. assert(isReg() && "Can only add reg operand to use lists");
  39. // If the reginfo pointer is null, just explicitly null out or next/prev
  40. // pointers, to ensure they are not garbage.
  41. if (RegInfo == 0) {
  42. Contents.Reg.Prev = 0;
  43. Contents.Reg.Next = 0;
  44. return;
  45. }
  46. // Otherwise, add this operand to the head of the registers use/def list.
  47. MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg());
  48. // For SSA values, we prefer to keep the definition at the start of the list.
  49. // we do this by skipping over the definition if it is at the head of the
  50. // list.
  51. if (*Head && (*Head)->isDef())
  52. Head = &(*Head)->Contents.Reg.Next;
  53. Contents.Reg.Next = *Head;
  54. if (Contents.Reg.Next) {
  55. assert(getReg() == Contents.Reg.Next->getReg() &&
  56. "Different regs on the same list!");
  57. Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next;
  58. }
  59. Contents.Reg.Prev = Head;
  60. *Head = this;
  61. }
  62. void MachineOperand::setReg(unsigned Reg) {
  63. if (getReg() == Reg) return; // No change.
  64. // Otherwise, we have to change the register. If this operand is embedded
  65. // into a machine function, we need to update the old and new register's
  66. // use/def lists.
  67. if (MachineInstr *MI = getParent())
  68. if (MachineBasicBlock *MBB = MI->getParent())
  69. if (MachineFunction *MF = MBB->getParent()) {
  70. RemoveRegOperandFromRegInfo();
  71. Contents.Reg.RegNo = Reg;
  72. AddRegOperandToRegInfo(&MF->getRegInfo());
  73. return;
  74. }
  75. // Otherwise, just change the register, no problem. :)
  76. Contents.Reg.RegNo = Reg;
  77. }
  78. /// ChangeToImmediate - Replace this operand with a new immediate operand of
  79. /// the specified value. If an operand is known to be an immediate already,
  80. /// the setImm method should be used.
  81. void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
  82. // If this operand is currently a register operand, and if this is in a
  83. // function, deregister the operand from the register's use/def list.
  84. if (isReg() && getParent() && getParent()->getParent() &&
  85. getParent()->getParent()->getParent())
  86. RemoveRegOperandFromRegInfo();
  87. OpKind = MO_Immediate;
  88. Contents.ImmVal = ImmVal;
  89. }
  90. /// ChangeToRegister - Replace this operand with a new register operand of
  91. /// the specified value. If an operand is known to be an register already,
  92. /// the setReg method should be used.
  93. void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
  94. bool isKill, bool isDead) {
  95. // If this operand is already a register operand, use setReg to update the
  96. // register's use/def lists.
  97. if (isReg()) {
  98. assert(!isEarlyClobber());
  99. setReg(Reg);
  100. } else {
  101. // Otherwise, change this to a register and set the reg#.
  102. OpKind = MO_Register;
  103. Contents.Reg.RegNo = Reg;
  104. // If this operand is embedded in a function, add the operand to the
  105. // register's use/def list.
  106. if (MachineInstr *MI = getParent())
  107. if (MachineBasicBlock *MBB = MI->getParent())
  108. if (MachineFunction *MF = MBB->getParent())
  109. AddRegOperandToRegInfo(&MF->getRegInfo());
  110. }
  111. IsDef = isDef;
  112. IsImp = isImp;
  113. IsKill = isKill;
  114. IsDead = isDead;
  115. IsEarlyClobber = false;
  116. SubReg = 0;
  117. }
  118. /// isIdenticalTo - Return true if this operand is identical to the specified
  119. /// operand.
  120. bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
  121. if (getType() != Other.getType()) return false;
  122. switch (getType()) {
  123. default: assert(0 && "Unrecognized operand type");
  124. case MachineOperand::MO_Register:
  125. return getReg() == Other.getReg() && isDef() == Other.isDef() &&
  126. getSubReg() == Other.getSubReg();
  127. case MachineOperand::MO_Immediate:
  128. return getImm() == Other.getImm();
  129. case MachineOperand::MO_FPImmediate:
  130. return getFPImm() == Other.getFPImm();
  131. case MachineOperand::MO_MachineBasicBlock:
  132. return getMBB() == Other.getMBB();
  133. case MachineOperand::MO_FrameIndex:
  134. return getIndex() == Other.getIndex();
  135. case MachineOperand::MO_ConstantPoolIndex:
  136. return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
  137. case MachineOperand::MO_JumpTableIndex:
  138. return getIndex() == Other.getIndex();
  139. case MachineOperand::MO_GlobalAddress:
  140. return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
  141. case MachineOperand::MO_ExternalSymbol:
  142. return !strcmp(getSymbolName(), Other.getSymbolName()) &&
  143. getOffset() == Other.getOffset();
  144. }
  145. }
  146. /// print - Print the specified machine operand.
  147. ///
  148. void MachineOperand::print(std::ostream &OS, const TargetMachine *TM) const {
  149. raw_os_ostream RawOS(OS);
  150. print(RawOS, TM);
  151. }
  152. void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const {
  153. switch (getType()) {
  154. case MachineOperand::MO_Register:
  155. if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) {
  156. OS << "%reg" << getReg();
  157. } else {
  158. // If the instruction is embedded into a basic block, we can find the
  159. // target info for the instruction.
  160. if (TM == 0)
  161. if (const MachineInstr *MI = getParent())
  162. if (const MachineBasicBlock *MBB = MI->getParent())
  163. if (const MachineFunction *MF = MBB->getParent())
  164. TM = &MF->getTarget();
  165. if (TM)
  166. OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
  167. else
  168. OS << "%mreg" << getReg();
  169. }
  170. if (getSubReg() != 0) {
  171. OS << ":" << getSubReg();
  172. }
  173. if (isDef() || isKill() || isDead() || isImplicit() || isEarlyClobber()) {
  174. OS << "<";
  175. bool NeedComma = false;
  176. if (isImplicit()) {
  177. if (NeedComma) OS << ",";
  178. OS << (isDef() ? "imp-def" : "imp-use");
  179. NeedComma = true;
  180. } else if (isDef()) {
  181. if (NeedComma) OS << ",";
  182. if (isEarlyClobber())
  183. OS << "earlyclobber,";
  184. OS << "def";
  185. NeedComma = true;
  186. }
  187. if (isKill() || isDead()) {
  188. if (NeedComma) OS << ",";
  189. if (isKill()) OS << "kill";
  190. if (isDead()) OS << "dead";
  191. }
  192. OS << ">";
  193. }
  194. break;
  195. case MachineOperand::MO_Immediate:
  196. OS << getImm();
  197. break;
  198. case MachineOperand::MO_FPImmediate:
  199. if (getFPImm()->getType() == Type::FloatTy) {
  200. OS << getFPImm()->getValueAPF().convertToFloat();
  201. } else {
  202. OS << getFPImm()->getValueAPF().convertToDouble();
  203. }
  204. break;
  205. case MachineOperand::MO_MachineBasicBlock:
  206. OS << "mbb<"
  207. << ((Value*)getMBB()->getBasicBlock())->getName()
  208. << "," << (void*)getMBB() << ">";
  209. break;
  210. case MachineOperand::MO_FrameIndex:
  211. OS << "<fi#" << getIndex() << ">";
  212. break;
  213. case MachineOperand::MO_ConstantPoolIndex:
  214. OS << "<cp#" << getIndex();
  215. if (getOffset()) OS << "+" << getOffset();
  216. OS << ">";
  217. break;
  218. case MachineOperand::MO_JumpTableIndex:
  219. OS << "<jt#" << getIndex() << ">";
  220. break;
  221. case MachineOperand::MO_GlobalAddress:
  222. OS << "<ga:" << ((Value*)getGlobal())->getName();
  223. if (getOffset()) OS << "+" << getOffset();
  224. OS << ">";
  225. break;
  226. case MachineOperand::MO_ExternalSymbol:
  227. OS << "<es:" << getSymbolName();
  228. if (getOffset()) OS << "+" << getOffset();
  229. OS << ">";
  230. break;
  231. default:
  232. assert(0 && "Unrecognized operand type");
  233. }
  234. }
  235. //===----------------------------------------------------------------------===//
  236. // MachineMemOperand Implementation
  237. //===----------------------------------------------------------------------===//
  238. MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f,
  239. int64_t o, uint64_t s, unsigned int a)
  240. : Offset(o), Size(s), V(v),
  241. Flags((f & 7) | ((Log2_32(a) + 1) << 3)) {
  242. assert(isPowerOf2_32(a) && "Alignment is not a power of 2!");
  243. assert((isLoad() || isStore()) && "Not a load/store!");
  244. }
  245. /// Profile - Gather unique data for the object.
  246. ///
  247. void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
  248. ID.AddInteger(Offset);
  249. ID.AddInteger(Size);
  250. ID.AddPointer(V);
  251. ID.AddInteger(Flags);
  252. }
  253. //===----------------------------------------------------------------------===//
  254. // MachineInstr Implementation
  255. //===----------------------------------------------------------------------===//
  256. /// MachineInstr ctor - This constructor creates a dummy MachineInstr with
  257. /// TID NULL and no operands.
  258. MachineInstr::MachineInstr()
  259. : TID(0), NumImplicitOps(0), Parent(0), debugLoc(DebugLoc::getUnknownLoc()) {
  260. // Make sure that we get added to a machine basicblock
  261. LeakDetector::addGarbageObject(this);
  262. }
  263. void MachineInstr::addImplicitDefUseOperands() {
  264. if (TID->ImplicitDefs)
  265. for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
  266. addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
  267. if (TID->ImplicitUses)
  268. for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
  269. addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
  270. }
  271. /// MachineInstr ctor - This constructor create a MachineInstr and add the
  272. /// implicit operands. It reserves space for number of operands specified by
  273. /// TargetInstrDesc or the numOperands if it is not zero. (for
  274. /// instructions with variable number of operands).
  275. MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp)
  276. : TID(&tid), NumImplicitOps(0), Parent(0),
  277. debugLoc(DebugLoc::getUnknownLoc()) {
  278. if (!NoImp && TID->getImplicitDefs())
  279. for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
  280. NumImplicitOps++;
  281. if (!NoImp && TID->getImplicitUses())
  282. for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
  283. NumImplicitOps++;
  284. Operands.reserve(NumImplicitOps + TID->getNumOperands());
  285. if (!NoImp)
  286. addImplicitDefUseOperands();
  287. // Make sure that we get added to a machine basicblock
  288. LeakDetector::addGarbageObject(this);
  289. }
  290. /// MachineInstr ctor - As above, but with a DebugLoc.
  291. MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl,
  292. bool NoImp)
  293. : TID(&tid), NumImplicitOps(0), Parent(0), debugLoc(dl) {
  294. if (!NoImp && TID->getImplicitDefs())
  295. for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
  296. NumImplicitOps++;
  297. if (!NoImp && TID->getImplicitUses())
  298. for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
  299. NumImplicitOps++;
  300. Operands.reserve(NumImplicitOps + TID->getNumOperands());
  301. if (!NoImp)
  302. addImplicitDefUseOperands();
  303. // Make sure that we get added to a machine basicblock
  304. LeakDetector::addGarbageObject(this);
  305. }
  306. /// MachineInstr ctor - Work exactly the same as the ctor two above, except
  307. /// that the MachineInstr is created and added to the end of the specified
  308. /// basic block.
  309. ///
  310. MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid)
  311. : TID(&tid), NumImplicitOps(0), Parent(0),
  312. debugLoc(DebugLoc::getUnknownLoc()) {
  313. assert(MBB && "Cannot use inserting ctor with null basic block!");
  314. if (TID->ImplicitDefs)
  315. for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
  316. NumImplicitOps++;
  317. if (TID->ImplicitUses)
  318. for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
  319. NumImplicitOps++;
  320. Operands.reserve(NumImplicitOps + TID->getNumOperands());
  321. addImplicitDefUseOperands();
  322. // Make sure that we get added to a machine basicblock
  323. LeakDetector::addGarbageObject(this);
  324. MBB->push_back(this); // Add instruction to end of basic block!
  325. }
  326. /// MachineInstr ctor - As above, but with a DebugLoc.
  327. ///
  328. MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl,
  329. const TargetInstrDesc &tid)
  330. : TID(&tid), NumImplicitOps(0), Parent(0), debugLoc(dl) {
  331. assert(MBB && "Cannot use inserting ctor with null basic block!");
  332. if (TID->ImplicitDefs)
  333. for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
  334. NumImplicitOps++;
  335. if (TID->ImplicitUses)
  336. for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
  337. NumImplicitOps++;
  338. Operands.reserve(NumImplicitOps + TID->getNumOperands());
  339. addImplicitDefUseOperands();
  340. // Make sure that we get added to a machine basicblock
  341. LeakDetector::addGarbageObject(this);
  342. MBB->push_back(this); // Add instruction to end of basic block!
  343. }
  344. /// MachineInstr ctor - Copies MachineInstr arg exactly
  345. ///
  346. MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
  347. : TID(&MI.getDesc()), NumImplicitOps(0), Parent(0),
  348. debugLoc(MI.getDebugLoc()) {
  349. Operands.reserve(MI.getNumOperands());
  350. // Add operands
  351. for (unsigned i = 0; i != MI.getNumOperands(); ++i)
  352. addOperand(MI.getOperand(i));
  353. NumImplicitOps = MI.NumImplicitOps;
  354. // Add memory operands.
  355. for (std::list<MachineMemOperand>::const_iterator i = MI.memoperands_begin(),
  356. j = MI.memoperands_end(); i != j; ++i)
  357. addMemOperand(MF, *i);
  358. // Set parent to null.
  359. Parent = 0;
  360. LeakDetector::addGarbageObject(this);
  361. }
  362. MachineInstr::~MachineInstr() {
  363. LeakDetector::removeGarbageObject(this);
  364. assert(MemOperands.empty() &&
  365. "MachineInstr being deleted with live memoperands!");
  366. #ifndef NDEBUG
  367. for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
  368. assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
  369. assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
  370. "Reg operand def/use list corrupted");
  371. }
  372. #endif
  373. }
  374. /// getRegInfo - If this instruction is embedded into a MachineFunction,
  375. /// return the MachineRegisterInfo object for the current function, otherwise
  376. /// return null.
  377. MachineRegisterInfo *MachineInstr::getRegInfo() {
  378. if (MachineBasicBlock *MBB = getParent())
  379. return &MBB->getParent()->getRegInfo();
  380. return 0;
  381. }
  382. /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
  383. /// this instruction from their respective use lists. This requires that the
  384. /// operands already be on their use lists.
  385. void MachineInstr::RemoveRegOperandsFromUseLists() {
  386. for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
  387. if (Operands[i].isReg())
  388. Operands[i].RemoveRegOperandFromRegInfo();
  389. }
  390. }
  391. /// AddRegOperandsToUseLists - Add all of the register operands in
  392. /// this instruction from their respective use lists. This requires that the
  393. /// operands not be on their use lists yet.
  394. void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) {
  395. for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
  396. if (Operands[i].isReg())
  397. Operands[i].AddRegOperandToRegInfo(&RegInfo);
  398. }
  399. }
  400. /// addOperand - Add the specified operand to the instruction. If it is an
  401. /// implicit operand, it is added to the end of the operand list. If it is
  402. /// an explicit operand it is added at the end of the explicit operand list
  403. /// (before the first implicit operand).
  404. void MachineInstr::addOperand(const MachineOperand &Op) {
  405. bool isImpReg = Op.isReg() && Op.isImplicit();
  406. assert((isImpReg || !OperandsComplete()) &&
  407. "Trying to add an operand to a machine instr that is already done!");
  408. MachineRegisterInfo *RegInfo = getRegInfo();
  409. // If we are adding the operand to the end of the list, our job is simpler.
  410. // This is true most of the time, so this is a reasonable optimization.
  411. if (isImpReg || NumImplicitOps == 0) {
  412. // We can only do this optimization if we know that the operand list won't
  413. // reallocate.
  414. if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) {
  415. Operands.push_back(Op);
  416. // Set the parent of the operand.
  417. Operands.back().ParentMI = this;
  418. // If the operand is a register, update the operand's use list.
  419. if (Op.isReg())
  420. Operands.back().AddRegOperandToRegInfo(RegInfo);
  421. return;
  422. }
  423. }
  424. // Otherwise, we have to insert a real operand before any implicit ones.
  425. unsigned OpNo = Operands.size()-NumImplicitOps;
  426. // If this instruction isn't embedded into a function, then we don't need to
  427. // update any operand lists.
  428. if (RegInfo == 0) {
  429. // Simple insertion, no reginfo update needed for other register operands.
  430. Operands.insert(Operands.begin()+OpNo, Op);
  431. Operands[OpNo].ParentMI = this;
  432. // Do explicitly set the reginfo for this operand though, to ensure the
  433. // next/prev fields are properly nulled out.
  434. if (Operands[OpNo].isReg())
  435. Operands[OpNo].AddRegOperandToRegInfo(0);
  436. } else if (Operands.size()+1 <= Operands.capacity()) {
  437. // Otherwise, we have to remove register operands from their register use
  438. // list, add the operand, then add the register operands back to their use
  439. // list. This also must handle the case when the operand list reallocates
  440. // to somewhere else.
  441. // If insertion of this operand won't cause reallocation of the operand
  442. // list, just remove the implicit operands, add the operand, then re-add all
  443. // the rest of the operands.
  444. for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
  445. assert(Operands[i].isReg() && "Should only be an implicit reg!");
  446. Operands[i].RemoveRegOperandFromRegInfo();
  447. }
  448. // Add the operand. If it is a register, add it to the reg list.
  449. Operands.insert(Operands.begin()+OpNo, Op);
  450. Operands[OpNo].ParentMI = this;
  451. if (Operands[OpNo].isReg())
  452. Operands[OpNo].AddRegOperandToRegInfo(RegInfo);
  453. // Re-add all the implicit ops.
  454. for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) {
  455. assert(Operands[i].isReg() && "Should only be an implicit reg!");
  456. Operands[i].AddRegOperandToRegInfo(RegInfo);
  457. }
  458. } else {
  459. // Otherwise, we will be reallocating the operand list. Remove all reg
  460. // operands from their list, then readd them after the operand list is
  461. // reallocated.
  462. RemoveRegOperandsFromUseLists();
  463. Operands.insert(Operands.begin()+OpNo, Op);
  464. Operands[OpNo].ParentMI = this;
  465. // Re-add all the operands.
  466. AddRegOperandsToUseLists(*RegInfo);
  467. }
  468. }
  469. /// RemoveOperand - Erase an operand from an instruction, leaving it with one
  470. /// fewer operand than it started with.
  471. ///
  472. void MachineInstr::RemoveOperand(unsigned OpNo) {
  473. assert(OpNo < Operands.size() && "Invalid operand number");
  474. // Special case removing the last one.
  475. if (OpNo == Operands.size()-1) {
  476. // If needed, remove from the reg def/use list.
  477. if (Operands.back().isReg() && Operands.back().isOnRegUseList())
  478. Operands.back().RemoveRegOperandFromRegInfo();
  479. Operands.pop_back();
  480. return;
  481. }
  482. // Otherwise, we are removing an interior operand. If we have reginfo to
  483. // update, remove all operands that will be shifted down from their reg lists,
  484. // move everything down, then re-add them.
  485. MachineRegisterInfo *RegInfo = getRegInfo();
  486. if (RegInfo) {
  487. for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
  488. if (Operands[i].isReg())
  489. Operands[i].RemoveRegOperandFromRegInfo();
  490. }
  491. }
  492. Operands.erase(Operands.begin()+OpNo);
  493. if (RegInfo) {
  494. for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
  495. if (Operands[i].isReg())
  496. Operands[i].AddRegOperandToRegInfo(RegInfo);
  497. }
  498. }
  499. }
  500. /// addMemOperand - Add a MachineMemOperand to the machine instruction,
  501. /// referencing arbitrary storage.
  502. void MachineInstr::addMemOperand(MachineFunction &MF,
  503. const MachineMemOperand &MO) {
  504. MemOperands.push_back(MO);
  505. }
  506. /// clearMemOperands - Erase all of this MachineInstr's MachineMemOperands.
  507. void MachineInstr::clearMemOperands(MachineFunction &MF) {
  508. MemOperands.clear();
  509. }
  510. /// removeFromParent - This method unlinks 'this' from the containing basic
  511. /// block, and returns it, but does not delete it.
  512. MachineInstr *MachineInstr::removeFromParent() {
  513. assert(getParent() && "Not embedded in a basic block!");
  514. getParent()->remove(this);
  515. return this;
  516. }
  517. /// eraseFromParent - This method unlinks 'this' from the containing basic
  518. /// block, and deletes it.
  519. void MachineInstr::eraseFromParent() {
  520. assert(getParent() && "Not embedded in a basic block!");
  521. getParent()->erase(this);
  522. }
  523. /// OperandComplete - Return true if it's illegal to add a new operand
  524. ///
  525. bool MachineInstr::OperandsComplete() const {
  526. unsigned short NumOperands = TID->getNumOperands();
  527. if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands)
  528. return true; // Broken: we have all the operands of this instruction!
  529. return false;
  530. }
  531. /// getNumExplicitOperands - Returns the number of non-implicit operands.
  532. ///
  533. unsigned MachineInstr::getNumExplicitOperands() const {
  534. unsigned NumOperands = TID->getNumOperands();
  535. if (!TID->isVariadic())
  536. return NumOperands;
  537. for (unsigned e = getNumOperands(); NumOperands != e; ++NumOperands) {
  538. const MachineOperand &MO = getOperand(NumOperands);
  539. if (!MO.isReg() || !MO.isImplicit())
  540. NumOperands++;
  541. }
  542. return NumOperands;
  543. }
  544. /// isLabel - Returns true if the MachineInstr represents a label.
  545. ///
  546. bool MachineInstr::isLabel() const {
  547. return getOpcode() == TargetInstrInfo::DBG_LABEL ||
  548. getOpcode() == TargetInstrInfo::EH_LABEL ||
  549. getOpcode() == TargetInstrInfo::GC_LABEL;
  550. }
  551. /// isDebugLabel - Returns true if the MachineInstr represents a debug label.
  552. ///
  553. bool MachineInstr::isDebugLabel() const {
  554. return getOpcode() == TargetInstrInfo::DBG_LABEL;
  555. }
  556. /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
  557. /// the specific register or -1 if it is not found. It further tightening
  558. /// the search criteria to a use that kills the register if isKill is true.
  559. int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
  560. const TargetRegisterInfo *TRI) const {
  561. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  562. const MachineOperand &MO = getOperand(i);
  563. if (!MO.isReg() || !MO.isUse())
  564. continue;
  565. unsigned MOReg = MO.getReg();
  566. if (!MOReg)
  567. continue;
  568. if (MOReg == Reg ||
  569. (TRI &&
  570. TargetRegisterInfo::isPhysicalRegister(MOReg) &&
  571. TargetRegisterInfo::isPhysicalRegister(Reg) &&
  572. TRI->isSubRegister(MOReg, Reg)))
  573. if (!isKill || MO.isKill())
  574. return i;
  575. }
  576. return -1;
  577. }
  578. /// findRegisterDefOperandIdx() - Returns the operand index that is a def of
  579. /// the specified register or -1 if it is not found. If isDead is true, defs
  580. /// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
  581. /// also checks if there is a def of a super-register.
  582. int MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead,
  583. const TargetRegisterInfo *TRI) const {
  584. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  585. const MachineOperand &MO = getOperand(i);
  586. if (!MO.isReg() || !MO.isDef())
  587. continue;
  588. unsigned MOReg = MO.getReg();
  589. if (MOReg == Reg ||
  590. (TRI &&
  591. TargetRegisterInfo::isPhysicalRegister(MOReg) &&
  592. TargetRegisterInfo::isPhysicalRegister(Reg) &&
  593. TRI->isSubRegister(MOReg, Reg)))
  594. if (!isDead || MO.isDead())
  595. return i;
  596. }
  597. return -1;
  598. }
  599. /// findFirstPredOperandIdx() - Find the index of the first operand in the
  600. /// operand list that is used to represent the predicate. It returns -1 if
  601. /// none is found.
  602. int MachineInstr::findFirstPredOperandIdx() const {
  603. const TargetInstrDesc &TID = getDesc();
  604. if (TID.isPredicable()) {
  605. for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
  606. if (TID.OpInfo[i].isPredicate())
  607. return i;
  608. }
  609. return -1;
  610. }
  611. /// isRegTiedToUseOperand - Given the index of a register def operand,
  612. /// check if the register def is tied to a source operand, due to either
  613. /// two-address elimination or inline assembly constraints. Returns the
  614. /// first tied use operand index by reference is UseOpIdx is not null.
  615. bool MachineInstr::isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx){
  616. if (getOpcode() == TargetInstrInfo::INLINEASM) {
  617. assert(DefOpIdx >= 2);
  618. const MachineOperand &MO = getOperand(DefOpIdx);
  619. if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0)
  620. return false;
  621. // Determine the actual operand no corresponding to this index.
  622. unsigned DefNo = 0;
  623. for (unsigned i = 1, e = getNumOperands(); i < e; ) {
  624. const MachineOperand &FMO = getOperand(i);
  625. assert(FMO.isImm());
  626. // Skip over this def.
  627. i += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1;
  628. if (i > DefOpIdx)
  629. break;
  630. ++DefNo;
  631. }
  632. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  633. const MachineOperand &FMO = getOperand(i);
  634. if (!FMO.isImm())
  635. continue;
  636. if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse())
  637. continue;
  638. unsigned Idx;
  639. if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) &&
  640. Idx == DefNo) {
  641. if (UseOpIdx)
  642. *UseOpIdx = (unsigned)i + 1;
  643. return true;
  644. }
  645. }
  646. }
  647. assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!");
  648. const TargetInstrDesc &TID = getDesc();
  649. for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
  650. const MachineOperand &MO = getOperand(i);
  651. if (MO.isReg() && MO.isUse() &&
  652. TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefOpIdx) {
  653. if (UseOpIdx)
  654. *UseOpIdx = (unsigned)i;
  655. return true;
  656. }
  657. }
  658. return false;
  659. }
  660. /// isRegTiedToDefOperand - Return true if the operand of the specified index
  661. /// is a register use and it is tied to an def operand. It also returns the def
  662. /// operand index by reference.
  663. bool MachineInstr::isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx){
  664. if (getOpcode() == TargetInstrInfo::INLINEASM) {
  665. const MachineOperand &MO = getOperand(UseOpIdx);
  666. if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0)
  667. return false;
  668. assert(UseOpIdx > 0);
  669. const MachineOperand &UFMO = getOperand(UseOpIdx-1);
  670. if (!UFMO.isImm())
  671. return false; // Must be physreg uses.
  672. unsigned DefNo;
  673. if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) {
  674. if (!DefOpIdx)
  675. return true;
  676. unsigned DefIdx = 1;
  677. // Remember to adjust the index. First operand is asm string, then there
  678. // is a flag for each.
  679. while (DefNo) {
  680. const MachineOperand &FMO = getOperand(DefIdx);
  681. assert(FMO.isImm());
  682. // Skip over this def.
  683. DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1;
  684. --DefNo;
  685. }
  686. *DefOpIdx = DefIdx+1;
  687. return true;
  688. }
  689. return false;
  690. }
  691. const TargetInstrDesc &TID = getDesc();
  692. if (UseOpIdx >= TID.getNumOperands())
  693. return false;
  694. const MachineOperand &MO = getOperand(UseOpIdx);
  695. if (!MO.isReg() || !MO.isUse())
  696. return false;
  697. int DefIdx = TID.getOperandConstraint(UseOpIdx, TOI::TIED_TO);
  698. if (DefIdx == -1)
  699. return false;
  700. if (DefOpIdx)
  701. *DefOpIdx = (unsigned)DefIdx;
  702. return true;
  703. }
  704. /// copyKillDeadInfo - Copies kill / dead operand properties from MI.
  705. ///
  706. void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
  707. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  708. const MachineOperand &MO = MI->getOperand(i);
  709. if (!MO.isReg() || (!MO.isKill() && !MO.isDead()))
  710. continue;
  711. for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
  712. MachineOperand &MOp = getOperand(j);
  713. if (!MOp.isIdenticalTo(MO))
  714. continue;
  715. if (MO.isKill())
  716. MOp.setIsKill();
  717. else
  718. MOp.setIsDead();
  719. break;
  720. }
  721. }
  722. }
  723. /// copyPredicates - Copies predicate operand(s) from MI.
  724. void MachineInstr::copyPredicates(const MachineInstr *MI) {
  725. const TargetInstrDesc &TID = MI->getDesc();
  726. if (!TID.isPredicable())
  727. return;
  728. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  729. if (TID.OpInfo[i].isPredicate()) {
  730. // Predicated operands must be last operands.
  731. addOperand(MI->getOperand(i));
  732. }
  733. }
  734. }
  735. /// isSafeToMove - Return true if it is safe to move this instruction. If
  736. /// SawStore is set to true, it means that there is a store (or call) between
  737. /// the instruction's location and its intended destination.
  738. bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII,
  739. bool &SawStore) const {
  740. // Ignore stuff that we obviously can't move.
  741. if (TID->mayStore() || TID->isCall()) {
  742. SawStore = true;
  743. return false;
  744. }
  745. if (TID->isTerminator() || TID->hasUnmodeledSideEffects())
  746. return false;
  747. // See if this instruction does a load. If so, we have to guarantee that the
  748. // loaded value doesn't change between the load and the its intended
  749. // destination. The check for isInvariantLoad gives the targe the chance to
  750. // classify the load as always returning a constant, e.g. a constant pool
  751. // load.
  752. if (TID->mayLoad() && !TII->isInvariantLoad(this))
  753. // Otherwise, this is a real load. If there is a store between the load and
  754. // end of block, or if the laod is volatile, we can't move it.
  755. return !SawStore && !hasVolatileMemoryRef();
  756. return true;
  757. }
  758. /// isSafeToReMat - Return true if it's safe to rematerialize the specified
  759. /// instruction which defined the specified register instead of copying it.
  760. bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII,
  761. unsigned DstReg) const {
  762. bool SawStore = false;
  763. if (!getDesc().isRematerializable() ||
  764. !TII->isTriviallyReMaterializable(this) ||
  765. !isSafeToMove(TII, SawStore))
  766. return false;
  767. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  768. const MachineOperand &MO = getOperand(i);
  769. if (!MO.isReg())
  770. continue;
  771. // FIXME: For now, do not remat any instruction with register operands.
  772. // Later on, we can loosen the restriction is the register operands have
  773. // not been modified between the def and use. Note, this is different from
  774. // MachineSink because the code is no longer in two-address form (at least
  775. // partially).
  776. if (MO.isUse())
  777. return false;
  778. else if (!MO.isDead() && MO.getReg() != DstReg)
  779. return false;
  780. }
  781. return true;
  782. }
  783. /// hasVolatileMemoryRef - Return true if this instruction may have a
  784. /// volatile memory reference, or if the information describing the
  785. /// memory reference is not available. Return false if it is known to
  786. /// have no volatile memory references.
  787. bool MachineInstr::hasVolatileMemoryRef() const {
  788. // An instruction known never to access memory won't have a volatile access.
  789. if (!TID->mayStore() &&
  790. !TID->mayLoad() &&
  791. !TID->isCall() &&
  792. !TID->hasUnmodeledSideEffects())
  793. return false;
  794. // Otherwise, if the instruction has no memory reference information,
  795. // conservatively assume it wasn't preserved.
  796. if (memoperands_empty())
  797. return true;
  798. // Check the memory reference information for volatile references.
  799. for (std::list<MachineMemOperand>::const_iterator I = memoperands_begin(),
  800. E = memoperands_end(); I != E; ++I)
  801. if (I->isVolatile())
  802. return true;
  803. return false;
  804. }
  805. void MachineInstr::dump() const {
  806. cerr << " " << *this;
  807. }
  808. void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
  809. raw_os_ostream RawOS(OS);
  810. print(RawOS, TM);
  811. }
  812. void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const {
  813. // Specialize printing if op#0 is definition
  814. unsigned StartOp = 0;
  815. if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) {
  816. getOperand(0).print(OS, TM);
  817. OS << " = ";
  818. ++StartOp; // Don't print this operand again!
  819. }
  820. OS << getDesc().getName();
  821. for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
  822. if (i != StartOp)
  823. OS << ",";
  824. OS << " ";
  825. getOperand(i).print(OS, TM);
  826. }
  827. if (!memoperands_empty()) {
  828. OS << ", Mem:";
  829. for (std::list<MachineMemOperand>::const_iterator i = memoperands_begin(),
  830. e = memoperands_end(); i != e; ++i) {
  831. const MachineMemOperand &MRO = *i;
  832. const Value *V = MRO.getValue();
  833. assert((MRO.isLoad() || MRO.isStore()) &&
  834. "SV has to be a load, store or both.");
  835. if (MRO.isVolatile())
  836. OS << "Volatile ";
  837. if (MRO.isLoad())
  838. OS << "LD";
  839. if (MRO.isStore())
  840. OS << "ST";
  841. OS << "(" << MRO.getSize() << "," << MRO.getAlignment() << ") [";
  842. if (!V)
  843. OS << "<unknown>";
  844. else if (!V->getName().empty())
  845. OS << V->getName();
  846. else if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
  847. PSV->print(OS);
  848. } else
  849. OS << V;
  850. OS << " + " << MRO.getOffset() << "]";
  851. }
  852. }
  853. if (!debugLoc.isUnknown()) {
  854. const MachineFunction *MF = getParent()->getParent();
  855. DebugLocTuple DLT = MF->getDebugLocTuple(debugLoc);
  856. OS << " [dbg: "
  857. << DLT.Src << ","
  858. << DLT.Line << ","
  859. << DLT.Col << "]";
  860. }
  861. OS << "\n";
  862. }
  863. bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
  864. const TargetRegisterInfo *RegInfo,
  865. bool AddIfNotFound) {
  866. bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
  867. bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
  868. bool Found = false;
  869. SmallVector<unsigned,4> DeadOps;
  870. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  871. MachineOperand &MO = getOperand(i);
  872. if (!MO.isReg() || !MO.isUse())
  873. continue;
  874. unsigned Reg = MO.getReg();
  875. if (!Reg)
  876. continue;
  877. if (Reg == IncomingReg) {
  878. if (!Found) {
  879. if (MO.isKill())
  880. // The register is already marked kill.
  881. return true;
  882. MO.setIsKill();
  883. Found = true;
  884. }
  885. } else if (hasAliases && MO.isKill() &&
  886. TargetRegisterInfo::isPhysicalRegister(Reg)) {
  887. // A super-register kill already exists.
  888. if (RegInfo->isSuperRegister(IncomingReg, Reg))
  889. return true;
  890. if (RegInfo->isSubRegister(IncomingReg, Reg))
  891. DeadOps.push_back(i);
  892. }
  893. }
  894. // Trim unneeded kill operands.
  895. while (!DeadOps.empty()) {
  896. unsigned OpIdx = DeadOps.back();
  897. if (getOperand(OpIdx).isImplicit())
  898. RemoveOperand(OpIdx);
  899. else
  900. getOperand(OpIdx).setIsKill(false);
  901. DeadOps.pop_back();
  902. }
  903. // If not found, this means an alias of one of the operands is killed. Add a
  904. // new implicit operand if required.
  905. if (!Found && AddIfNotFound) {
  906. addOperand(MachineOperand::CreateReg(IncomingReg,
  907. false /*IsDef*/,
  908. true /*IsImp*/,
  909. true /*IsKill*/));
  910. return true;
  911. }
  912. return Found;
  913. }
  914. bool MachineInstr::addRegisterDead(unsigned IncomingReg,
  915. const TargetRegisterInfo *RegInfo,
  916. bool AddIfNotFound) {
  917. bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
  918. bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
  919. bool Found = false;
  920. SmallVector<unsigned,4> DeadOps;
  921. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  922. MachineOperand &MO = getOperand(i);
  923. if (!MO.isReg() || !MO.isDef())
  924. continue;
  925. unsigned Reg = MO.getReg();
  926. if (!Reg)
  927. continue;
  928. if (Reg == IncomingReg) {
  929. if (!Found) {
  930. if (MO.isDead())
  931. // The register is already marked dead.
  932. return true;
  933. MO.setIsDead();
  934. Found = true;
  935. }
  936. } else if (hasAliases && MO.isDead() &&
  937. TargetRegisterInfo::isPhysicalRegister(Reg)) {
  938. // There exists a super-register that's marked dead.
  939. if (RegInfo->isSuperRegister(IncomingReg, Reg))
  940. return true;
  941. if (RegInfo->getSubRegisters(IncomingReg) &&
  942. RegInfo->getSuperRegisters(Reg) &&
  943. RegInfo->isSubRegister(IncomingReg, Reg))
  944. DeadOps.push_back(i);
  945. }
  946. }
  947. // Trim unneeded dead operands.
  948. while (!DeadOps.empty()) {
  949. unsigned OpIdx = DeadOps.back();
  950. if (getOperand(OpIdx).isImplicit())
  951. RemoveOperand(OpIdx);
  952. else
  953. getOperand(OpIdx).setIsDead(false);
  954. DeadOps.pop_back();
  955. }
  956. // If not found, this means an alias of one of the operands is dead. Add a
  957. // new implicit operand if required.
  958. if (!Found && AddIfNotFound) {
  959. addOperand(MachineOperand::CreateReg(IncomingReg,
  960. true /*IsDef*/,
  961. true /*IsImp*/,
  962. false /*IsKill*/,
  963. true /*IsDead*/));
  964. return true;
  965. }
  966. return Found;
  967. }