MachineInstr.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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/Function.h"
  16. #include "llvm/InlineAsm.h"
  17. #include "llvm/Type.h"
  18. #include "llvm/Value.h"
  19. #include "llvm/Assembly/Writer.h"
  20. #include "llvm/CodeGen/MachineFunction.h"
  21. #include "llvm/CodeGen/MachineMemOperand.h"
  22. #include "llvm/CodeGen/MachineRegisterInfo.h"
  23. #include "llvm/CodeGen/PseudoSourceValue.h"
  24. #include "llvm/Target/TargetMachine.h"
  25. #include "llvm/Target/TargetInstrInfo.h"
  26. #include "llvm/Target/TargetInstrDesc.h"
  27. #include "llvm/Target/TargetRegisterInfo.h"
  28. #include "llvm/Analysis/AliasAnalysis.h"
  29. #include "llvm/Analysis/DebugInfo.h"
  30. #include "llvm/Support/Debug.h"
  31. #include "llvm/Support/ErrorHandling.h"
  32. #include "llvm/Support/LeakDetector.h"
  33. #include "llvm/Support/MathExtras.h"
  34. #include "llvm/Support/raw_ostream.h"
  35. #include "llvm/ADT/FoldingSet.h"
  36. #include "llvm/Metadata.h"
  37. using namespace llvm;
  38. //===----------------------------------------------------------------------===//
  39. // MachineOperand Implementation
  40. //===----------------------------------------------------------------------===//
  41. /// AddRegOperandToRegInfo - Add this register operand to the specified
  42. /// MachineRegisterInfo. If it is null, then the next/prev fields should be
  43. /// explicitly nulled out.
  44. void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) {
  45. assert(isReg() && "Can only add reg operand to use lists");
  46. // If the reginfo pointer is null, just explicitly null out or next/prev
  47. // pointers, to ensure they are not garbage.
  48. if (RegInfo == 0) {
  49. Contents.Reg.Prev = 0;
  50. Contents.Reg.Next = 0;
  51. return;
  52. }
  53. // Otherwise, add this operand to the head of the registers use/def list.
  54. MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg());
  55. // For SSA values, we prefer to keep the definition at the start of the list.
  56. // we do this by skipping over the definition if it is at the head of the
  57. // list.
  58. if (*Head && (*Head)->isDef())
  59. Head = &(*Head)->Contents.Reg.Next;
  60. Contents.Reg.Next = *Head;
  61. if (Contents.Reg.Next) {
  62. assert(getReg() == Contents.Reg.Next->getReg() &&
  63. "Different regs on the same list!");
  64. Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next;
  65. }
  66. Contents.Reg.Prev = Head;
  67. *Head = this;
  68. }
  69. /// RemoveRegOperandFromRegInfo - Remove this register operand from the
  70. /// MachineRegisterInfo it is linked with.
  71. void MachineOperand::RemoveRegOperandFromRegInfo() {
  72. assert(isOnRegUseList() && "Reg operand is not on a use list");
  73. // Unlink this from the doubly linked list of operands.
  74. MachineOperand *NextOp = Contents.Reg.Next;
  75. *Contents.Reg.Prev = NextOp;
  76. if (NextOp) {
  77. assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!");
  78. NextOp->Contents.Reg.Prev = Contents.Reg.Prev;
  79. }
  80. Contents.Reg.Prev = 0;
  81. Contents.Reg.Next = 0;
  82. }
  83. void MachineOperand::setReg(unsigned Reg) {
  84. if (getReg() == Reg) return; // No change.
  85. // Otherwise, we have to change the register. If this operand is embedded
  86. // into a machine function, we need to update the old and new register's
  87. // use/def lists.
  88. if (MachineInstr *MI = getParent())
  89. if (MachineBasicBlock *MBB = MI->getParent())
  90. if (MachineFunction *MF = MBB->getParent()) {
  91. RemoveRegOperandFromRegInfo();
  92. Contents.Reg.RegNo = Reg;
  93. AddRegOperandToRegInfo(&MF->getRegInfo());
  94. return;
  95. }
  96. // Otherwise, just change the register, no problem. :)
  97. Contents.Reg.RegNo = Reg;
  98. }
  99. /// ChangeToImmediate - Replace this operand with a new immediate operand of
  100. /// the specified value. If an operand is known to be an immediate already,
  101. /// the setImm method should be used.
  102. void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
  103. // If this operand is currently a register operand, and if this is in a
  104. // function, deregister the operand from the register's use/def list.
  105. if (isReg() && getParent() && getParent()->getParent() &&
  106. getParent()->getParent()->getParent())
  107. RemoveRegOperandFromRegInfo();
  108. OpKind = MO_Immediate;
  109. Contents.ImmVal = ImmVal;
  110. }
  111. /// ChangeToRegister - Replace this operand with a new register operand of
  112. /// the specified value. If an operand is known to be an register already,
  113. /// the setReg method should be used.
  114. void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
  115. bool isKill, bool isDead, bool isUndef) {
  116. // If this operand is already a register operand, use setReg to update the
  117. // register's use/def lists.
  118. if (isReg()) {
  119. assert(!isEarlyClobber());
  120. setReg(Reg);
  121. } else {
  122. // Otherwise, change this to a register and set the reg#.
  123. OpKind = MO_Register;
  124. Contents.Reg.RegNo = Reg;
  125. // If this operand is embedded in a function, add the operand to the
  126. // register's use/def list.
  127. if (MachineInstr *MI = getParent())
  128. if (MachineBasicBlock *MBB = MI->getParent())
  129. if (MachineFunction *MF = MBB->getParent())
  130. AddRegOperandToRegInfo(&MF->getRegInfo());
  131. }
  132. IsDef = isDef;
  133. IsImp = isImp;
  134. IsKill = isKill;
  135. IsDead = isDead;
  136. IsUndef = isUndef;
  137. IsEarlyClobber = false;
  138. SubReg = 0;
  139. }
  140. /// isIdenticalTo - Return true if this operand is identical to the specified
  141. /// operand.
  142. bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
  143. if (getType() != Other.getType() ||
  144. getTargetFlags() != Other.getTargetFlags())
  145. return false;
  146. switch (getType()) {
  147. default: llvm_unreachable("Unrecognized operand type");
  148. case MachineOperand::MO_Register:
  149. return getReg() == Other.getReg() && isDef() == Other.isDef() &&
  150. getSubReg() == Other.getSubReg();
  151. case MachineOperand::MO_Immediate:
  152. return getImm() == Other.getImm();
  153. case MachineOperand::MO_FPImmediate:
  154. return getFPImm() == Other.getFPImm();
  155. case MachineOperand::MO_MachineBasicBlock:
  156. return getMBB() == Other.getMBB();
  157. case MachineOperand::MO_FrameIndex:
  158. return getIndex() == Other.getIndex();
  159. case MachineOperand::MO_ConstantPoolIndex:
  160. return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
  161. case MachineOperand::MO_JumpTableIndex:
  162. return getIndex() == Other.getIndex();
  163. case MachineOperand::MO_GlobalAddress:
  164. return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
  165. case MachineOperand::MO_ExternalSymbol:
  166. return !strcmp(getSymbolName(), Other.getSymbolName()) &&
  167. getOffset() == Other.getOffset();
  168. case MachineOperand::MO_BlockAddress:
  169. return getBlockAddress() == Other.getBlockAddress();
  170. }
  171. }
  172. /// print - Print the specified machine operand.
  173. ///
  174. void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const {
  175. // If the instruction is embedded into a basic block, we can find the
  176. // target info for the instruction.
  177. if (!TM)
  178. if (const MachineInstr *MI = getParent())
  179. if (const MachineBasicBlock *MBB = MI->getParent())
  180. if (const MachineFunction *MF = MBB->getParent())
  181. TM = &MF->getTarget();
  182. switch (getType()) {
  183. case MachineOperand::MO_Register:
  184. if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) {
  185. OS << "%reg" << getReg();
  186. } else {
  187. if (TM)
  188. OS << "%" << TM->getRegisterInfo()->get(getReg()).Name;
  189. else
  190. OS << "%physreg" << getReg();
  191. }
  192. if (getSubReg() != 0)
  193. OS << ':' << getSubReg();
  194. if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
  195. isEarlyClobber()) {
  196. OS << '<';
  197. bool NeedComma = false;
  198. if (isDef()) {
  199. if (NeedComma) OS << ',';
  200. if (isEarlyClobber())
  201. OS << "earlyclobber,";
  202. if (isImplicit())
  203. OS << "imp-";
  204. OS << "def";
  205. NeedComma = true;
  206. } else if (isImplicit()) {
  207. OS << "imp-use";
  208. NeedComma = true;
  209. }
  210. if (isKill() || isDead() || isUndef()) {
  211. if (NeedComma) OS << ',';
  212. if (isKill()) OS << "kill";
  213. if (isDead()) OS << "dead";
  214. if (isUndef()) {
  215. if (isKill() || isDead())
  216. OS << ',';
  217. OS << "undef";
  218. }
  219. }
  220. OS << '>';
  221. }
  222. break;
  223. case MachineOperand::MO_Immediate:
  224. OS << getImm();
  225. break;
  226. case MachineOperand::MO_FPImmediate:
  227. if (getFPImm()->getType()->isFloatTy())
  228. OS << getFPImm()->getValueAPF().convertToFloat();
  229. else
  230. OS << getFPImm()->getValueAPF().convertToDouble();
  231. break;
  232. case MachineOperand::MO_MachineBasicBlock:
  233. OS << "<BB#" << getMBB()->getNumber() << ">";
  234. break;
  235. case MachineOperand::MO_FrameIndex:
  236. OS << "<fi#" << getIndex() << '>';
  237. break;
  238. case MachineOperand::MO_ConstantPoolIndex:
  239. OS << "<cp#" << getIndex();
  240. if (getOffset()) OS << "+" << getOffset();
  241. OS << '>';
  242. break;
  243. case MachineOperand::MO_JumpTableIndex:
  244. OS << "<jt#" << getIndex() << '>';
  245. break;
  246. case MachineOperand::MO_GlobalAddress:
  247. OS << "<ga:";
  248. WriteAsOperand(OS, getGlobal(), /*PrintType=*/false);
  249. if (getOffset()) OS << "+" << getOffset();
  250. OS << '>';
  251. break;
  252. case MachineOperand::MO_ExternalSymbol:
  253. OS << "<es:" << getSymbolName();
  254. if (getOffset()) OS << "+" << getOffset();
  255. OS << '>';
  256. break;
  257. case MachineOperand::MO_BlockAddress:
  258. OS << '<';
  259. WriteAsOperand(OS, getBlockAddress(), /*PrintType=*/false);
  260. OS << '>';
  261. break;
  262. case MachineOperand::MO_Metadata:
  263. OS << '<';
  264. WriteAsOperand(OS, getMetadata(), /*PrintType=*/false);
  265. OS << '>';
  266. break;
  267. default:
  268. llvm_unreachable("Unrecognized operand type");
  269. }
  270. if (unsigned TF = getTargetFlags())
  271. OS << "[TF=" << TF << ']';
  272. }
  273. //===----------------------------------------------------------------------===//
  274. // MachineMemOperand Implementation
  275. //===----------------------------------------------------------------------===//
  276. MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f,
  277. int64_t o, uint64_t s, unsigned int a)
  278. : Offset(o), Size(s), V(v),
  279. Flags((f & 7) | ((Log2_32(a) + 1) << 3)) {
  280. assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
  281. assert((isLoad() || isStore()) && "Not a load/store!");
  282. }
  283. /// Profile - Gather unique data for the object.
  284. ///
  285. void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
  286. ID.AddInteger(Offset);
  287. ID.AddInteger(Size);
  288. ID.AddPointer(V);
  289. ID.AddInteger(Flags);
  290. }
  291. void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
  292. // The Value and Offset may differ due to CSE. But the flags and size
  293. // should be the same.
  294. assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
  295. assert(MMO->getSize() == getSize() && "Size mismatch!");
  296. if (MMO->getBaseAlignment() >= getBaseAlignment()) {
  297. // Update the alignment value.
  298. Flags = (Flags & 7) | ((Log2_32(MMO->getBaseAlignment()) + 1) << 3);
  299. // Also update the base and offset, because the new alignment may
  300. // not be applicable with the old ones.
  301. V = MMO->getValue();
  302. Offset = MMO->getOffset();
  303. }
  304. }
  305. /// getAlignment - Return the minimum known alignment in bytes of the
  306. /// actual memory reference.
  307. uint64_t MachineMemOperand::getAlignment() const {
  308. return MinAlign(getBaseAlignment(), getOffset());
  309. }
  310. raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) {
  311. assert((MMO.isLoad() || MMO.isStore()) &&
  312. "SV has to be a load, store or both.");
  313. if (MMO.isVolatile())
  314. OS << "Volatile ";
  315. if (MMO.isLoad())
  316. OS << "LD";
  317. if (MMO.isStore())
  318. OS << "ST";
  319. OS << MMO.getSize();
  320. // Print the address information.
  321. OS << "[";
  322. if (!MMO.getValue())
  323. OS << "<unknown>";
  324. else
  325. WriteAsOperand(OS, MMO.getValue(), /*PrintType=*/false);
  326. // If the alignment of the memory reference itself differs from the alignment
  327. // of the base pointer, print the base alignment explicitly, next to the base
  328. // pointer.
  329. if (MMO.getBaseAlignment() != MMO.getAlignment())
  330. OS << "(align=" << MMO.getBaseAlignment() << ")";
  331. if (MMO.getOffset() != 0)
  332. OS << "+" << MMO.getOffset();
  333. OS << "]";
  334. // Print the alignment of the reference.
  335. if (MMO.getBaseAlignment() != MMO.getAlignment() ||
  336. MMO.getBaseAlignment() != MMO.getSize())
  337. OS << "(align=" << MMO.getAlignment() << ")";
  338. return OS;
  339. }
  340. //===----------------------------------------------------------------------===//
  341. // MachineInstr Implementation
  342. //===----------------------------------------------------------------------===//
  343. /// MachineInstr ctor - This constructor creates a dummy MachineInstr with
  344. /// TID NULL and no operands.
  345. MachineInstr::MachineInstr()
  346. : TID(0), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0),
  347. Parent(0), debugLoc(DebugLoc::getUnknownLoc()) {
  348. // Make sure that we get added to a machine basicblock
  349. LeakDetector::addGarbageObject(this);
  350. }
  351. void MachineInstr::addImplicitDefUseOperands() {
  352. if (TID->ImplicitDefs)
  353. for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs)
  354. addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
  355. if (TID->ImplicitUses)
  356. for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses)
  357. addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
  358. }
  359. /// MachineInstr ctor - This constructor create a MachineInstr and add the
  360. /// implicit operands. It reserves space for number of operands specified by
  361. /// TargetInstrDesc or the numOperands if it is not zero. (for
  362. /// instructions with variable number of operands).
  363. MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp)
  364. : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0),
  365. MemRefs(0), MemRefsEnd(0), Parent(0),
  366. debugLoc(DebugLoc::getUnknownLoc()) {
  367. if (!NoImp && TID->getImplicitDefs())
  368. for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
  369. NumImplicitOps++;
  370. if (!NoImp && TID->getImplicitUses())
  371. for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
  372. NumImplicitOps++;
  373. Operands.reserve(NumImplicitOps + TID->getNumOperands());
  374. if (!NoImp)
  375. addImplicitDefUseOperands();
  376. // Make sure that we get added to a machine basicblock
  377. LeakDetector::addGarbageObject(this);
  378. }
  379. /// MachineInstr ctor - As above, but with a DebugLoc.
  380. MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl,
  381. bool NoImp)
  382. : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0),
  383. Parent(0), debugLoc(dl) {
  384. if (!NoImp && TID->getImplicitDefs())
  385. for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
  386. NumImplicitOps++;
  387. if (!NoImp && TID->getImplicitUses())
  388. for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
  389. NumImplicitOps++;
  390. Operands.reserve(NumImplicitOps + TID->getNumOperands());
  391. if (!NoImp)
  392. addImplicitDefUseOperands();
  393. // Make sure that we get added to a machine basicblock
  394. LeakDetector::addGarbageObject(this);
  395. }
  396. /// MachineInstr ctor - Work exactly the same as the ctor two above, except
  397. /// that the MachineInstr is created and added to the end of the specified
  398. /// basic block.
  399. ///
  400. MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid)
  401. : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0),
  402. MemRefs(0), MemRefsEnd(0), Parent(0),
  403. debugLoc(DebugLoc::getUnknownLoc()) {
  404. assert(MBB && "Cannot use inserting ctor with null basic block!");
  405. if (TID->ImplicitDefs)
  406. for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
  407. NumImplicitOps++;
  408. if (TID->ImplicitUses)
  409. for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
  410. NumImplicitOps++;
  411. Operands.reserve(NumImplicitOps + TID->getNumOperands());
  412. addImplicitDefUseOperands();
  413. // Make sure that we get added to a machine basicblock
  414. LeakDetector::addGarbageObject(this);
  415. MBB->push_back(this); // Add instruction to end of basic block!
  416. }
  417. /// MachineInstr ctor - As above, but with a DebugLoc.
  418. ///
  419. MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl,
  420. const TargetInstrDesc &tid)
  421. : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0),
  422. Parent(0), debugLoc(dl) {
  423. assert(MBB && "Cannot use inserting ctor with null basic block!");
  424. if (TID->ImplicitDefs)
  425. for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs)
  426. NumImplicitOps++;
  427. if (TID->ImplicitUses)
  428. for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses)
  429. NumImplicitOps++;
  430. Operands.reserve(NumImplicitOps + TID->getNumOperands());
  431. addImplicitDefUseOperands();
  432. // Make sure that we get added to a machine basicblock
  433. LeakDetector::addGarbageObject(this);
  434. MBB->push_back(this); // Add instruction to end of basic block!
  435. }
  436. /// MachineInstr ctor - Copies MachineInstr arg exactly
  437. ///
  438. MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
  439. : TID(&MI.getDesc()), NumImplicitOps(0), AsmPrinterFlags(0),
  440. MemRefs(MI.MemRefs), MemRefsEnd(MI.MemRefsEnd),
  441. Parent(0), debugLoc(MI.getDebugLoc()) {
  442. Operands.reserve(MI.getNumOperands());
  443. // Add operands
  444. for (unsigned i = 0; i != MI.getNumOperands(); ++i)
  445. addOperand(MI.getOperand(i));
  446. NumImplicitOps = MI.NumImplicitOps;
  447. // Set parent to null.
  448. Parent = 0;
  449. LeakDetector::addGarbageObject(this);
  450. }
  451. MachineInstr::~MachineInstr() {
  452. LeakDetector::removeGarbageObject(this);
  453. #ifndef NDEBUG
  454. for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
  455. assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
  456. assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
  457. "Reg operand def/use list corrupted");
  458. }
  459. #endif
  460. }
  461. /// getRegInfo - If this instruction is embedded into a MachineFunction,
  462. /// return the MachineRegisterInfo object for the current function, otherwise
  463. /// return null.
  464. MachineRegisterInfo *MachineInstr::getRegInfo() {
  465. if (MachineBasicBlock *MBB = getParent())
  466. return &MBB->getParent()->getRegInfo();
  467. return 0;
  468. }
  469. /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
  470. /// this instruction from their respective use lists. This requires that the
  471. /// operands already be on their use lists.
  472. void MachineInstr::RemoveRegOperandsFromUseLists() {
  473. for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
  474. if (Operands[i].isReg())
  475. Operands[i].RemoveRegOperandFromRegInfo();
  476. }
  477. }
  478. /// AddRegOperandsToUseLists - Add all of the register operands in
  479. /// this instruction from their respective use lists. This requires that the
  480. /// operands not be on their use lists yet.
  481. void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) {
  482. for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
  483. if (Operands[i].isReg())
  484. Operands[i].AddRegOperandToRegInfo(&RegInfo);
  485. }
  486. }
  487. /// addOperand - Add the specified operand to the instruction. If it is an
  488. /// implicit operand, it is added to the end of the operand list. If it is
  489. /// an explicit operand it is added at the end of the explicit operand list
  490. /// (before the first implicit operand).
  491. void MachineInstr::addOperand(const MachineOperand &Op) {
  492. bool isImpReg = Op.isReg() && Op.isImplicit();
  493. assert((isImpReg || !OperandsComplete()) &&
  494. "Trying to add an operand to a machine instr that is already done!");
  495. MachineRegisterInfo *RegInfo = getRegInfo();
  496. // If we are adding the operand to the end of the list, our job is simpler.
  497. // This is true most of the time, so this is a reasonable optimization.
  498. if (isImpReg || NumImplicitOps == 0) {
  499. // We can only do this optimization if we know that the operand list won't
  500. // reallocate.
  501. if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) {
  502. Operands.push_back(Op);
  503. // Set the parent of the operand.
  504. Operands.back().ParentMI = this;
  505. // If the operand is a register, update the operand's use list.
  506. if (Op.isReg()) {
  507. Operands.back().AddRegOperandToRegInfo(RegInfo);
  508. // If the register operand is flagged as early, mark the operand as such
  509. unsigned OpNo = Operands.size() - 1;
  510. if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
  511. Operands[OpNo].setIsEarlyClobber(true);
  512. }
  513. return;
  514. }
  515. }
  516. // Otherwise, we have to insert a real operand before any implicit ones.
  517. unsigned OpNo = Operands.size()-NumImplicitOps;
  518. // If this instruction isn't embedded into a function, then we don't need to
  519. // update any operand lists.
  520. if (RegInfo == 0) {
  521. // Simple insertion, no reginfo update needed for other register operands.
  522. Operands.insert(Operands.begin()+OpNo, Op);
  523. Operands[OpNo].ParentMI = this;
  524. // Do explicitly set the reginfo for this operand though, to ensure the
  525. // next/prev fields are properly nulled out.
  526. if (Operands[OpNo].isReg()) {
  527. Operands[OpNo].AddRegOperandToRegInfo(0);
  528. // If the register operand is flagged as early, mark the operand as such
  529. if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
  530. Operands[OpNo].setIsEarlyClobber(true);
  531. }
  532. } else if (Operands.size()+1 <= Operands.capacity()) {
  533. // Otherwise, we have to remove register operands from their register use
  534. // list, add the operand, then add the register operands back to their use
  535. // list. This also must handle the case when the operand list reallocates
  536. // to somewhere else.
  537. // If insertion of this operand won't cause reallocation of the operand
  538. // list, just remove the implicit operands, add the operand, then re-add all
  539. // the rest of the operands.
  540. for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
  541. assert(Operands[i].isReg() && "Should only be an implicit reg!");
  542. Operands[i].RemoveRegOperandFromRegInfo();
  543. }
  544. // Add the operand. If it is a register, add it to the reg list.
  545. Operands.insert(Operands.begin()+OpNo, Op);
  546. Operands[OpNo].ParentMI = this;
  547. if (Operands[OpNo].isReg()) {
  548. Operands[OpNo].AddRegOperandToRegInfo(RegInfo);
  549. // If the register operand is flagged as early, mark the operand as such
  550. if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
  551. Operands[OpNo].setIsEarlyClobber(true);
  552. }
  553. // Re-add all the implicit ops.
  554. for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) {
  555. assert(Operands[i].isReg() && "Should only be an implicit reg!");
  556. Operands[i].AddRegOperandToRegInfo(RegInfo);
  557. }
  558. } else {
  559. // Otherwise, we will be reallocating the operand list. Remove all reg
  560. // operands from their list, then readd them after the operand list is
  561. // reallocated.
  562. RemoveRegOperandsFromUseLists();
  563. Operands.insert(Operands.begin()+OpNo, Op);
  564. Operands[OpNo].ParentMI = this;
  565. // Re-add all the operands.
  566. AddRegOperandsToUseLists(*RegInfo);
  567. // If the register operand is flagged as early, mark the operand as such
  568. if (Operands[OpNo].isReg()
  569. && TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1)
  570. Operands[OpNo].setIsEarlyClobber(true);
  571. }
  572. }
  573. /// RemoveOperand - Erase an operand from an instruction, leaving it with one
  574. /// fewer operand than it started with.
  575. ///
  576. void MachineInstr::RemoveOperand(unsigned OpNo) {
  577. assert(OpNo < Operands.size() && "Invalid operand number");
  578. // Special case removing the last one.
  579. if (OpNo == Operands.size()-1) {
  580. // If needed, remove from the reg def/use list.
  581. if (Operands.back().isReg() && Operands.back().isOnRegUseList())
  582. Operands.back().RemoveRegOperandFromRegInfo();
  583. Operands.pop_back();
  584. return;
  585. }
  586. // Otherwise, we are removing an interior operand. If we have reginfo to
  587. // update, remove all operands that will be shifted down from their reg lists,
  588. // move everything down, then re-add them.
  589. MachineRegisterInfo *RegInfo = getRegInfo();
  590. if (RegInfo) {
  591. for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
  592. if (Operands[i].isReg())
  593. Operands[i].RemoveRegOperandFromRegInfo();
  594. }
  595. }
  596. Operands.erase(Operands.begin()+OpNo);
  597. if (RegInfo) {
  598. for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
  599. if (Operands[i].isReg())
  600. Operands[i].AddRegOperandToRegInfo(RegInfo);
  601. }
  602. }
  603. }
  604. /// addMemOperand - Add a MachineMemOperand to the machine instruction.
  605. /// This function should be used only occasionally. The setMemRefs function
  606. /// is the primary method for setting up a MachineInstr's MemRefs list.
  607. void MachineInstr::addMemOperand(MachineFunction &MF,
  608. MachineMemOperand *MO) {
  609. mmo_iterator OldMemRefs = MemRefs;
  610. mmo_iterator OldMemRefsEnd = MemRefsEnd;
  611. size_t NewNum = (MemRefsEnd - MemRefs) + 1;
  612. mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum);
  613. mmo_iterator NewMemRefsEnd = NewMemRefs + NewNum;
  614. std::copy(OldMemRefs, OldMemRefsEnd, NewMemRefs);
  615. NewMemRefs[NewNum - 1] = MO;
  616. MemRefs = NewMemRefs;
  617. MemRefsEnd = NewMemRefsEnd;
  618. }
  619. /// removeFromParent - This method unlinks 'this' from the containing basic
  620. /// block, and returns it, but does not delete it.
  621. MachineInstr *MachineInstr::removeFromParent() {
  622. assert(getParent() && "Not embedded in a basic block!");
  623. getParent()->remove(this);
  624. return this;
  625. }
  626. /// eraseFromParent - This method unlinks 'this' from the containing basic
  627. /// block, and deletes it.
  628. void MachineInstr::eraseFromParent() {
  629. assert(getParent() && "Not embedded in a basic block!");
  630. getParent()->erase(this);
  631. }
  632. /// OperandComplete - Return true if it's illegal to add a new operand
  633. ///
  634. bool MachineInstr::OperandsComplete() const {
  635. unsigned short NumOperands = TID->getNumOperands();
  636. if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands)
  637. return true; // Broken: we have all the operands of this instruction!
  638. return false;
  639. }
  640. /// getNumExplicitOperands - Returns the number of non-implicit operands.
  641. ///
  642. unsigned MachineInstr::getNumExplicitOperands() const {
  643. unsigned NumOperands = TID->getNumOperands();
  644. if (!TID->isVariadic())
  645. return NumOperands;
  646. for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
  647. const MachineOperand &MO = getOperand(i);
  648. if (!MO.isReg() || !MO.isImplicit())
  649. NumOperands++;
  650. }
  651. return NumOperands;
  652. }
  653. /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
  654. /// the specific register or -1 if it is not found. It further tightens
  655. /// the search criteria to a use that kills the register if isKill is true.
  656. int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
  657. const TargetRegisterInfo *TRI) const {
  658. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  659. const MachineOperand &MO = getOperand(i);
  660. if (!MO.isReg() || !MO.isUse())
  661. continue;
  662. unsigned MOReg = MO.getReg();
  663. if (!MOReg)
  664. continue;
  665. if (MOReg == Reg ||
  666. (TRI &&
  667. TargetRegisterInfo::isPhysicalRegister(MOReg) &&
  668. TargetRegisterInfo::isPhysicalRegister(Reg) &&
  669. TRI->isSubRegister(MOReg, Reg)))
  670. if (!isKill || MO.isKill())
  671. return i;
  672. }
  673. return -1;
  674. }
  675. /// findRegisterDefOperandIdx() - Returns the operand index that is a def of
  676. /// the specified register or -1 if it is not found. If isDead is true, defs
  677. /// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
  678. /// also checks if there is a def of a super-register.
  679. int MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead,
  680. const TargetRegisterInfo *TRI) const {
  681. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  682. const MachineOperand &MO = getOperand(i);
  683. if (!MO.isReg() || !MO.isDef())
  684. continue;
  685. unsigned MOReg = MO.getReg();
  686. if (MOReg == Reg ||
  687. (TRI &&
  688. TargetRegisterInfo::isPhysicalRegister(MOReg) &&
  689. TargetRegisterInfo::isPhysicalRegister(Reg) &&
  690. TRI->isSubRegister(MOReg, Reg)))
  691. if (!isDead || MO.isDead())
  692. return i;
  693. }
  694. return -1;
  695. }
  696. /// findFirstPredOperandIdx() - Find the index of the first operand in the
  697. /// operand list that is used to represent the predicate. It returns -1 if
  698. /// none is found.
  699. int MachineInstr::findFirstPredOperandIdx() const {
  700. const TargetInstrDesc &TID = getDesc();
  701. if (TID.isPredicable()) {
  702. for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
  703. if (TID.OpInfo[i].isPredicate())
  704. return i;
  705. }
  706. return -1;
  707. }
  708. /// isRegTiedToUseOperand - Given the index of a register def operand,
  709. /// check if the register def is tied to a source operand, due to either
  710. /// two-address elimination or inline assembly constraints. Returns the
  711. /// first tied use operand index by reference is UseOpIdx is not null.
  712. bool MachineInstr::
  713. isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx) const {
  714. if (isInlineAsm()) {
  715. assert(DefOpIdx >= 2);
  716. const MachineOperand &MO = getOperand(DefOpIdx);
  717. if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0)
  718. return false;
  719. // Determine the actual operand index that corresponds to this index.
  720. unsigned DefNo = 0;
  721. unsigned DefPart = 0;
  722. for (unsigned i = 1, e = getNumOperands(); i < e; ) {
  723. const MachineOperand &FMO = getOperand(i);
  724. // After the normal asm operands there may be additional imp-def regs.
  725. if (!FMO.isImm())
  726. return false;
  727. // Skip over this def.
  728. unsigned NumOps = InlineAsm::getNumOperandRegisters(FMO.getImm());
  729. unsigned PrevDef = i + 1;
  730. i = PrevDef + NumOps;
  731. if (i > DefOpIdx) {
  732. DefPart = DefOpIdx - PrevDef;
  733. break;
  734. }
  735. ++DefNo;
  736. }
  737. for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
  738. const MachineOperand &FMO = getOperand(i);
  739. if (!FMO.isImm())
  740. continue;
  741. if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse())
  742. continue;
  743. unsigned Idx;
  744. if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) &&
  745. Idx == DefNo) {
  746. if (UseOpIdx)
  747. *UseOpIdx = (unsigned)i + 1 + DefPart;
  748. return true;
  749. }
  750. }
  751. return false;
  752. }
  753. assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!");
  754. const TargetInstrDesc &TID = getDesc();
  755. for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
  756. const MachineOperand &MO = getOperand(i);
  757. if (MO.isReg() && MO.isUse() &&
  758. TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefOpIdx) {
  759. if (UseOpIdx)
  760. *UseOpIdx = (unsigned)i;
  761. return true;
  762. }
  763. }
  764. return false;
  765. }
  766. /// isRegTiedToDefOperand - Return true if the operand of the specified index
  767. /// is a register use and it is tied to an def operand. It also returns the def
  768. /// operand index by reference.
  769. bool MachineInstr::
  770. isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx) const {
  771. if (isInlineAsm()) {
  772. const MachineOperand &MO = getOperand(UseOpIdx);
  773. if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0)
  774. return false;
  775. // Find the flag operand corresponding to UseOpIdx
  776. unsigned FlagIdx, NumOps=0;
  777. for (FlagIdx = 1; FlagIdx < UseOpIdx; FlagIdx += NumOps+1) {
  778. const MachineOperand &UFMO = getOperand(FlagIdx);
  779. // After the normal asm operands there may be additional imp-def regs.
  780. if (!UFMO.isImm())
  781. return false;
  782. NumOps = InlineAsm::getNumOperandRegisters(UFMO.getImm());
  783. assert(NumOps < getNumOperands() && "Invalid inline asm flag");
  784. if (UseOpIdx < FlagIdx+NumOps+1)
  785. break;
  786. }
  787. if (FlagIdx >= UseOpIdx)
  788. return false;
  789. const MachineOperand &UFMO = getOperand(FlagIdx);
  790. unsigned DefNo;
  791. if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) {
  792. if (!DefOpIdx)
  793. return true;
  794. unsigned DefIdx = 1;
  795. // Remember to adjust the index. First operand is asm string, then there
  796. // is a flag for each.
  797. while (DefNo) {
  798. const MachineOperand &FMO = getOperand(DefIdx);
  799. assert(FMO.isImm());
  800. // Skip over this def.
  801. DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1;
  802. --DefNo;
  803. }
  804. *DefOpIdx = DefIdx + UseOpIdx - FlagIdx;
  805. return true;
  806. }
  807. return false;
  808. }
  809. const TargetInstrDesc &TID = getDesc();
  810. if (UseOpIdx >= TID.getNumOperands())
  811. return false;
  812. const MachineOperand &MO = getOperand(UseOpIdx);
  813. if (!MO.isReg() || !MO.isUse())
  814. return false;
  815. int DefIdx = TID.getOperandConstraint(UseOpIdx, TOI::TIED_TO);
  816. if (DefIdx == -1)
  817. return false;
  818. if (DefOpIdx)
  819. *DefOpIdx = (unsigned)DefIdx;
  820. return true;
  821. }
  822. /// copyKillDeadInfo - Copies kill / dead operand properties from MI.
  823. ///
  824. void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
  825. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  826. const MachineOperand &MO = MI->getOperand(i);
  827. if (!MO.isReg() || (!MO.isKill() && !MO.isDead()))
  828. continue;
  829. for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
  830. MachineOperand &MOp = getOperand(j);
  831. if (!MOp.isIdenticalTo(MO))
  832. continue;
  833. if (MO.isKill())
  834. MOp.setIsKill();
  835. else
  836. MOp.setIsDead();
  837. break;
  838. }
  839. }
  840. }
  841. /// copyPredicates - Copies predicate operand(s) from MI.
  842. void MachineInstr::copyPredicates(const MachineInstr *MI) {
  843. const TargetInstrDesc &TID = MI->getDesc();
  844. if (!TID.isPredicable())
  845. return;
  846. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  847. if (TID.OpInfo[i].isPredicate()) {
  848. // Predicated operands must be last operands.
  849. addOperand(MI->getOperand(i));
  850. }
  851. }
  852. }
  853. /// isSafeToMove - Return true if it is safe to move this instruction. If
  854. /// SawStore is set to true, it means that there is a store (or call) between
  855. /// the instruction's location and its intended destination.
  856. bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII,
  857. bool &SawStore,
  858. AliasAnalysis *AA) const {
  859. // Ignore stuff that we obviously can't move.
  860. if (TID->mayStore() || TID->isCall()) {
  861. SawStore = true;
  862. return false;
  863. }
  864. if (TID->isTerminator() || TID->hasUnmodeledSideEffects())
  865. return false;
  866. // See if this instruction does a load. If so, we have to guarantee that the
  867. // loaded value doesn't change between the load and the its intended
  868. // destination. The check for isInvariantLoad gives the targe the chance to
  869. // classify the load as always returning a constant, e.g. a constant pool
  870. // load.
  871. if (TID->mayLoad() && !isInvariantLoad(AA))
  872. // Otherwise, this is a real load. If there is a store between the load and
  873. // end of block, or if the load is volatile, we can't move it.
  874. return !SawStore && !hasVolatileMemoryRef();
  875. return true;
  876. }
  877. /// isSafeToReMat - Return true if it's safe to rematerialize the specified
  878. /// instruction which defined the specified register instead of copying it.
  879. bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII,
  880. unsigned DstReg,
  881. AliasAnalysis *AA) const {
  882. bool SawStore = false;
  883. if (!TII->isTriviallyReMaterializable(this, AA) ||
  884. !isSafeToMove(TII, SawStore, AA))
  885. return false;
  886. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  887. const MachineOperand &MO = getOperand(i);
  888. if (!MO.isReg())
  889. continue;
  890. // FIXME: For now, do not remat any instruction with register operands.
  891. // Later on, we can loosen the restriction is the register operands have
  892. // not been modified between the def and use. Note, this is different from
  893. // MachineSink because the code is no longer in two-address form (at least
  894. // partially).
  895. if (MO.isUse())
  896. return false;
  897. else if (!MO.isDead() && MO.getReg() != DstReg)
  898. return false;
  899. }
  900. return true;
  901. }
  902. /// hasVolatileMemoryRef - Return true if this instruction may have a
  903. /// volatile memory reference, or if the information describing the
  904. /// memory reference is not available. Return false if it is known to
  905. /// have no volatile memory references.
  906. bool MachineInstr::hasVolatileMemoryRef() const {
  907. // An instruction known never to access memory won't have a volatile access.
  908. if (!TID->mayStore() &&
  909. !TID->mayLoad() &&
  910. !TID->isCall() &&
  911. !TID->hasUnmodeledSideEffects())
  912. return false;
  913. // Otherwise, if the instruction has no memory reference information,
  914. // conservatively assume it wasn't preserved.
  915. if (memoperands_empty())
  916. return true;
  917. // Check the memory reference information for volatile references.
  918. for (mmo_iterator I = memoperands_begin(), E = memoperands_end(); I != E; ++I)
  919. if ((*I)->isVolatile())
  920. return true;
  921. return false;
  922. }
  923. /// isInvariantLoad - Return true if this instruction is loading from a
  924. /// location whose value is invariant across the function. For example,
  925. /// loading a value from the constant pool or from from the argument area
  926. /// of a function if it does not change. This should only return true of
  927. /// *all* loads the instruction does are invariant (if it does multiple loads).
  928. bool MachineInstr::isInvariantLoad(AliasAnalysis *AA) const {
  929. // If the instruction doesn't load at all, it isn't an invariant load.
  930. if (!TID->mayLoad())
  931. return false;
  932. // If the instruction has lost its memoperands, conservatively assume that
  933. // it may not be an invariant load.
  934. if (memoperands_empty())
  935. return false;
  936. const MachineFrameInfo *MFI = getParent()->getParent()->getFrameInfo();
  937. for (mmo_iterator I = memoperands_begin(),
  938. E = memoperands_end(); I != E; ++I) {
  939. if ((*I)->isVolatile()) return false;
  940. if ((*I)->isStore()) return false;
  941. if (const Value *V = (*I)->getValue()) {
  942. // A load from a constant PseudoSourceValue is invariant.
  943. if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V))
  944. if (PSV->isConstant(MFI))
  945. continue;
  946. // If we have an AliasAnalysis, ask it whether the memory is constant.
  947. if (AA && AA->pointsToConstantMemory(V))
  948. continue;
  949. }
  950. // Otherwise assume conservatively.
  951. return false;
  952. }
  953. // Everything checks out.
  954. return true;
  955. }
  956. /// isConstantValuePHI - If the specified instruction is a PHI that always
  957. /// merges together the same virtual register, return the register, otherwise
  958. /// return 0.
  959. unsigned MachineInstr::isConstantValuePHI() const {
  960. if (!isPHI())
  961. return 0;
  962. assert(getNumOperands() >= 3 &&
  963. "It's illegal to have a PHI without source operands");
  964. unsigned Reg = getOperand(1).getReg();
  965. for (unsigned i = 3, e = getNumOperands(); i < e; i += 2)
  966. if (getOperand(i).getReg() != Reg)
  967. return 0;
  968. return Reg;
  969. }
  970. void MachineInstr::dump() const {
  971. dbgs() << " " << *this;
  972. }
  973. void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const {
  974. // We can be a bit tidier if we know the TargetMachine and/or MachineFunction.
  975. const MachineFunction *MF = 0;
  976. if (const MachineBasicBlock *MBB = getParent()) {
  977. MF = MBB->getParent();
  978. if (!TM && MF)
  979. TM = &MF->getTarget();
  980. }
  981. // Print explicitly defined operands on the left of an assignment syntax.
  982. unsigned StartOp = 0, e = getNumOperands();
  983. for (; StartOp < e && getOperand(StartOp).isReg() &&
  984. getOperand(StartOp).isDef() &&
  985. !getOperand(StartOp).isImplicit();
  986. ++StartOp) {
  987. if (StartOp != 0) OS << ", ";
  988. getOperand(StartOp).print(OS, TM);
  989. }
  990. if (StartOp != 0)
  991. OS << " = ";
  992. // Print the opcode name.
  993. OS << getDesc().getName();
  994. // Print the rest of the operands.
  995. bool OmittedAnyCallClobbers = false;
  996. bool FirstOp = true;
  997. for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
  998. const MachineOperand &MO = getOperand(i);
  999. // Omit call-clobbered registers which aren't used anywhere. This makes
  1000. // call instructions much less noisy on targets where calls clobber lots
  1001. // of registers. Don't rely on MO.isDead() because we may be called before
  1002. // LiveVariables is run, or we may be looking at a non-allocatable reg.
  1003. if (MF && getDesc().isCall() &&
  1004. MO.isReg() && MO.isImplicit() && MO.isDef()) {
  1005. unsigned Reg = MO.getReg();
  1006. if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) {
  1007. const MachineRegisterInfo &MRI = MF->getRegInfo();
  1008. if (MRI.use_empty(Reg) && !MRI.isLiveOut(Reg)) {
  1009. bool HasAliasLive = false;
  1010. for (const unsigned *Alias = TM->getRegisterInfo()->getAliasSet(Reg);
  1011. unsigned AliasReg = *Alias; ++Alias)
  1012. if (!MRI.use_empty(AliasReg) || MRI.isLiveOut(AliasReg)) {
  1013. HasAliasLive = true;
  1014. break;
  1015. }
  1016. if (!HasAliasLive) {
  1017. OmittedAnyCallClobbers = true;
  1018. continue;
  1019. }
  1020. }
  1021. }
  1022. }
  1023. if (FirstOp) FirstOp = false; else OS << ",";
  1024. OS << " ";
  1025. if (i < getDesc().NumOperands) {
  1026. const TargetOperandInfo &TOI = getDesc().OpInfo[i];
  1027. if (TOI.isPredicate())
  1028. OS << "pred:";
  1029. if (TOI.isOptionalDef())
  1030. OS << "opt:";
  1031. }
  1032. MO.print(OS, TM);
  1033. }
  1034. // Briefly indicate whether any call clobbers were omitted.
  1035. if (OmittedAnyCallClobbers) {
  1036. if (!FirstOp) OS << ",";
  1037. OS << " ...";
  1038. }
  1039. bool HaveSemi = false;
  1040. if (!memoperands_empty()) {
  1041. if (!HaveSemi) OS << ";"; HaveSemi = true;
  1042. OS << " mem:";
  1043. for (mmo_iterator i = memoperands_begin(), e = memoperands_end();
  1044. i != e; ++i) {
  1045. OS << **i;
  1046. if (next(i) != e)
  1047. OS << " ";
  1048. }
  1049. }
  1050. if (!debugLoc.isUnknown() && MF) {
  1051. if (!HaveSemi) OS << ";";
  1052. // TODO: print InlinedAtLoc information
  1053. DILocation DLT = MF->getDILocation(debugLoc);
  1054. DIScope Scope = DLT.getScope();
  1055. OS << " dbg:";
  1056. // Omit the directory, since it's usually long and uninteresting.
  1057. if (!Scope.isNull())
  1058. OS << Scope.getFilename();
  1059. else
  1060. OS << "<unknown>";
  1061. OS << ':' << DLT.getLineNumber();
  1062. if (DLT.getColumnNumber() != 0)
  1063. OS << ':' << DLT.getColumnNumber();
  1064. }
  1065. OS << "\n";
  1066. }
  1067. bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
  1068. const TargetRegisterInfo *RegInfo,
  1069. bool AddIfNotFound) {
  1070. bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
  1071. bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
  1072. bool Found = false;
  1073. SmallVector<unsigned,4> DeadOps;
  1074. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  1075. MachineOperand &MO = getOperand(i);
  1076. if (!MO.isReg() || !MO.isUse() || MO.isUndef())
  1077. continue;
  1078. unsigned Reg = MO.getReg();
  1079. if (!Reg)
  1080. continue;
  1081. if (Reg == IncomingReg) {
  1082. if (!Found) {
  1083. if (MO.isKill())
  1084. // The register is already marked kill.
  1085. return true;
  1086. if (isPhysReg && isRegTiedToDefOperand(i))
  1087. // Two-address uses of physregs must not be marked kill.
  1088. return true;
  1089. MO.setIsKill();
  1090. Found = true;
  1091. }
  1092. } else if (hasAliases && MO.isKill() &&
  1093. TargetRegisterInfo::isPhysicalRegister(Reg)) {
  1094. // A super-register kill already exists.
  1095. if (RegInfo->isSuperRegister(IncomingReg, Reg))
  1096. return true;
  1097. if (RegInfo->isSubRegister(IncomingReg, Reg))
  1098. DeadOps.push_back(i);
  1099. }
  1100. }
  1101. // Trim unneeded kill operands.
  1102. while (!DeadOps.empty()) {
  1103. unsigned OpIdx = DeadOps.back();
  1104. if (getOperand(OpIdx).isImplicit())
  1105. RemoveOperand(OpIdx);
  1106. else
  1107. getOperand(OpIdx).setIsKill(false);
  1108. DeadOps.pop_back();
  1109. }
  1110. // If not found, this means an alias of one of the operands is killed. Add a
  1111. // new implicit operand if required.
  1112. if (!Found && AddIfNotFound) {
  1113. addOperand(MachineOperand::CreateReg(IncomingReg,
  1114. false /*IsDef*/,
  1115. true /*IsImp*/,
  1116. true /*IsKill*/));
  1117. return true;
  1118. }
  1119. return Found;
  1120. }
  1121. bool MachineInstr::addRegisterDead(unsigned IncomingReg,
  1122. const TargetRegisterInfo *RegInfo,
  1123. bool AddIfNotFound) {
  1124. bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
  1125. bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
  1126. bool Found = false;
  1127. SmallVector<unsigned,4> DeadOps;
  1128. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  1129. MachineOperand &MO = getOperand(i);
  1130. if (!MO.isReg() || !MO.isDef())
  1131. continue;
  1132. unsigned Reg = MO.getReg();
  1133. if (!Reg)
  1134. continue;
  1135. if (Reg == IncomingReg) {
  1136. if (!Found) {
  1137. if (MO.isDead())
  1138. // The register is already marked dead.
  1139. return true;
  1140. MO.setIsDead();
  1141. Found = true;
  1142. }
  1143. } else if (hasAliases && MO.isDead() &&
  1144. TargetRegisterInfo::isPhysicalRegister(Reg)) {
  1145. // There exists a super-register that's marked dead.
  1146. if (RegInfo->isSuperRegister(IncomingReg, Reg))
  1147. return true;
  1148. if (RegInfo->getSubRegisters(IncomingReg) &&
  1149. RegInfo->getSuperRegisters(Reg) &&
  1150. RegInfo->isSubRegister(IncomingReg, Reg))
  1151. DeadOps.push_back(i);
  1152. }
  1153. }
  1154. // Trim unneeded dead operands.
  1155. while (!DeadOps.empty()) {
  1156. unsigned OpIdx = DeadOps.back();
  1157. if (getOperand(OpIdx).isImplicit())
  1158. RemoveOperand(OpIdx);
  1159. else
  1160. getOperand(OpIdx).setIsDead(false);
  1161. DeadOps.pop_back();
  1162. }
  1163. // If not found, this means an alias of one of the operands is dead. Add a
  1164. // new implicit operand if required.
  1165. if (Found || !AddIfNotFound)
  1166. return Found;
  1167. addOperand(MachineOperand::CreateReg(IncomingReg,
  1168. true /*IsDef*/,
  1169. true /*IsImp*/,
  1170. false /*IsKill*/,
  1171. true /*IsDead*/));
  1172. return true;
  1173. }
  1174. void MachineInstr::addRegisterDefined(unsigned IncomingReg,
  1175. const TargetRegisterInfo *RegInfo) {
  1176. MachineOperand *MO = findRegisterDefOperand(IncomingReg, false, RegInfo);
  1177. if (!MO || MO->getSubReg())
  1178. addOperand(MachineOperand::CreateReg(IncomingReg,
  1179. true /*IsDef*/,
  1180. true /*IsImp*/));
  1181. }