MachineInstr.cpp 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  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/LLVMContext.h"
  18. #include "llvm/Metadata.h"
  19. #include "llvm/Module.h"
  20. #include "llvm/Type.h"
  21. #include "llvm/Value.h"
  22. #include "llvm/Assembly/Writer.h"
  23. #include "llvm/CodeGen/MachineConstantPool.h"
  24. #include "llvm/CodeGen/MachineFunction.h"
  25. #include "llvm/CodeGen/MachineMemOperand.h"
  26. #include "llvm/CodeGen/MachineModuleInfo.h"
  27. #include "llvm/CodeGen/MachineRegisterInfo.h"
  28. #include "llvm/CodeGen/PseudoSourceValue.h"
  29. #include "llvm/MC/MCInstrDesc.h"
  30. #include "llvm/MC/MCSymbol.h"
  31. #include "llvm/Target/TargetMachine.h"
  32. #include "llvm/Target/TargetInstrInfo.h"
  33. #include "llvm/Target/TargetRegisterInfo.h"
  34. #include "llvm/Analysis/AliasAnalysis.h"
  35. #include "llvm/Analysis/DebugInfo.h"
  36. #include "llvm/Support/Debug.h"
  37. #include "llvm/Support/ErrorHandling.h"
  38. #include "llvm/Support/LeakDetector.h"
  39. #include "llvm/Support/MathExtras.h"
  40. #include "llvm/Support/raw_ostream.h"
  41. #include "llvm/ADT/FoldingSet.h"
  42. using namespace llvm;
  43. //===----------------------------------------------------------------------===//
  44. // MachineOperand Implementation
  45. //===----------------------------------------------------------------------===//
  46. /// AddRegOperandToRegInfo - Add this register operand to the specified
  47. /// MachineRegisterInfo. If it is null, then the next/prev fields should be
  48. /// explicitly nulled out.
  49. void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) {
  50. assert(isReg() && "Can only add reg operand to use lists");
  51. // If the reginfo pointer is null, just explicitly null out or next/prev
  52. // pointers, to ensure they are not garbage.
  53. if (RegInfo == 0) {
  54. Contents.Reg.Prev = 0;
  55. Contents.Reg.Next = 0;
  56. return;
  57. }
  58. // Otherwise, add this operand to the head of the registers use/def list.
  59. MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg());
  60. // For SSA values, we prefer to keep the definition at the start of the list.
  61. // we do this by skipping over the definition if it is at the head of the
  62. // list.
  63. if (*Head && (*Head)->isDef())
  64. Head = &(*Head)->Contents.Reg.Next;
  65. Contents.Reg.Next = *Head;
  66. if (Contents.Reg.Next) {
  67. assert(getReg() == Contents.Reg.Next->getReg() &&
  68. "Different regs on the same list!");
  69. Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next;
  70. }
  71. Contents.Reg.Prev = Head;
  72. *Head = this;
  73. }
  74. /// RemoveRegOperandFromRegInfo - Remove this register operand from the
  75. /// MachineRegisterInfo it is linked with.
  76. void MachineOperand::RemoveRegOperandFromRegInfo() {
  77. assert(isOnRegUseList() && "Reg operand is not on a use list");
  78. // Unlink this from the doubly linked list of operands.
  79. MachineOperand *NextOp = Contents.Reg.Next;
  80. *Contents.Reg.Prev = NextOp;
  81. if (NextOp) {
  82. assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!");
  83. NextOp->Contents.Reg.Prev = Contents.Reg.Prev;
  84. }
  85. Contents.Reg.Prev = 0;
  86. Contents.Reg.Next = 0;
  87. }
  88. void MachineOperand::setReg(unsigned Reg) {
  89. if (getReg() == Reg) return; // No change.
  90. // Otherwise, we have to change the register. If this operand is embedded
  91. // into a machine function, we need to update the old and new register's
  92. // use/def lists.
  93. if (MachineInstr *MI = getParent())
  94. if (MachineBasicBlock *MBB = MI->getParent())
  95. if (MachineFunction *MF = MBB->getParent()) {
  96. RemoveRegOperandFromRegInfo();
  97. SmallContents.RegNo = Reg;
  98. AddRegOperandToRegInfo(&MF->getRegInfo());
  99. return;
  100. }
  101. // Otherwise, just change the register, no problem. :)
  102. SmallContents.RegNo = Reg;
  103. }
  104. void MachineOperand::substVirtReg(unsigned Reg, unsigned SubIdx,
  105. const TargetRegisterInfo &TRI) {
  106. assert(TargetRegisterInfo::isVirtualRegister(Reg));
  107. if (SubIdx && getSubReg())
  108. SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg());
  109. setReg(Reg);
  110. if (SubIdx)
  111. setSubReg(SubIdx);
  112. }
  113. void MachineOperand::substPhysReg(unsigned Reg, const TargetRegisterInfo &TRI) {
  114. assert(TargetRegisterInfo::isPhysicalRegister(Reg));
  115. if (getSubReg()) {
  116. Reg = TRI.getSubReg(Reg, getSubReg());
  117. // Note that getSubReg() may return 0 if the sub-register doesn't exist.
  118. // That won't happen in legal code.
  119. setSubReg(0);
  120. }
  121. setReg(Reg);
  122. }
  123. /// ChangeToImmediate - Replace this operand with a new immediate operand of
  124. /// the specified value. If an operand is known to be an immediate already,
  125. /// the setImm method should be used.
  126. void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
  127. // If this operand is currently a register operand, and if this is in a
  128. // function, deregister the operand from the register's use/def list.
  129. if (isReg() && getParent() && getParent()->getParent() &&
  130. getParent()->getParent()->getParent())
  131. RemoveRegOperandFromRegInfo();
  132. OpKind = MO_Immediate;
  133. Contents.ImmVal = ImmVal;
  134. }
  135. /// ChangeToRegister - Replace this operand with a new register operand of
  136. /// the specified value. If an operand is known to be an register already,
  137. /// the setReg method should be used.
  138. void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
  139. bool isKill, bool isDead, bool isUndef,
  140. bool isDebug) {
  141. // If this operand is already a register operand, use setReg to update the
  142. // register's use/def lists.
  143. if (isReg()) {
  144. assert(!isEarlyClobber());
  145. setReg(Reg);
  146. } else {
  147. // Otherwise, change this to a register and set the reg#.
  148. OpKind = MO_Register;
  149. SmallContents.RegNo = Reg;
  150. // If this operand is embedded in a function, add the operand to the
  151. // register's use/def list.
  152. if (MachineInstr *MI = getParent())
  153. if (MachineBasicBlock *MBB = MI->getParent())
  154. if (MachineFunction *MF = MBB->getParent())
  155. AddRegOperandToRegInfo(&MF->getRegInfo());
  156. }
  157. IsDef = isDef;
  158. IsImp = isImp;
  159. IsKill = isKill;
  160. IsDead = isDead;
  161. IsUndef = isUndef;
  162. IsInternalRead = false;
  163. IsEarlyClobber = false;
  164. IsDebug = isDebug;
  165. SubReg = 0;
  166. }
  167. /// isIdenticalTo - Return true if this operand is identical to the specified
  168. /// operand.
  169. bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
  170. if (getType() != Other.getType() ||
  171. getTargetFlags() != Other.getTargetFlags())
  172. return false;
  173. switch (getType()) {
  174. default: llvm_unreachable("Unrecognized operand type");
  175. case MachineOperand::MO_Register:
  176. return getReg() == Other.getReg() && isDef() == Other.isDef() &&
  177. getSubReg() == Other.getSubReg();
  178. case MachineOperand::MO_Immediate:
  179. return getImm() == Other.getImm();
  180. case MachineOperand::MO_CImmediate:
  181. return getCImm() == Other.getCImm();
  182. case MachineOperand::MO_FPImmediate:
  183. return getFPImm() == Other.getFPImm();
  184. case MachineOperand::MO_MachineBasicBlock:
  185. return getMBB() == Other.getMBB();
  186. case MachineOperand::MO_FrameIndex:
  187. return getIndex() == Other.getIndex();
  188. case MachineOperand::MO_ConstantPoolIndex:
  189. return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
  190. case MachineOperand::MO_JumpTableIndex:
  191. return getIndex() == Other.getIndex();
  192. case MachineOperand::MO_GlobalAddress:
  193. return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
  194. case MachineOperand::MO_ExternalSymbol:
  195. return !strcmp(getSymbolName(), Other.getSymbolName()) &&
  196. getOffset() == Other.getOffset();
  197. case MachineOperand::MO_BlockAddress:
  198. return getBlockAddress() == Other.getBlockAddress();
  199. case MachineOperand::MO_MCSymbol:
  200. return getMCSymbol() == Other.getMCSymbol();
  201. case MachineOperand::MO_Metadata:
  202. return getMetadata() == Other.getMetadata();
  203. }
  204. }
  205. /// print - Print the specified machine operand.
  206. ///
  207. void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const {
  208. // If the instruction is embedded into a basic block, we can find the
  209. // target info for the instruction.
  210. if (!TM)
  211. if (const MachineInstr *MI = getParent())
  212. if (const MachineBasicBlock *MBB = MI->getParent())
  213. if (const MachineFunction *MF = MBB->getParent())
  214. TM = &MF->getTarget();
  215. const TargetRegisterInfo *TRI = TM ? TM->getRegisterInfo() : 0;
  216. switch (getType()) {
  217. case MachineOperand::MO_Register:
  218. OS << PrintReg(getReg(), TRI, getSubReg());
  219. if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
  220. isInternalRead() || isEarlyClobber()) {
  221. OS << '<';
  222. bool NeedComma = false;
  223. if (isDef()) {
  224. if (NeedComma) OS << ',';
  225. if (isEarlyClobber())
  226. OS << "earlyclobber,";
  227. if (isImplicit())
  228. OS << "imp-";
  229. OS << "def";
  230. NeedComma = true;
  231. } else if (isImplicit()) {
  232. OS << "imp-use";
  233. NeedComma = true;
  234. }
  235. if (isKill() || isDead() || isUndef() || isInternalRead()) {
  236. if (NeedComma) OS << ',';
  237. NeedComma = false;
  238. if (isKill()) {
  239. OS << "kill";
  240. NeedComma = true;
  241. }
  242. if (isDead()) {
  243. OS << "dead";
  244. NeedComma = true;
  245. }
  246. if (isUndef()) {
  247. if (NeedComma) OS << ',';
  248. OS << "undef";
  249. NeedComma = true;
  250. }
  251. if (isInternalRead()) {
  252. if (NeedComma) OS << ',';
  253. OS << "internal";
  254. NeedComma = true;
  255. }
  256. }
  257. OS << '>';
  258. }
  259. break;
  260. case MachineOperand::MO_Immediate:
  261. OS << getImm();
  262. break;
  263. case MachineOperand::MO_CImmediate:
  264. getCImm()->getValue().print(OS, false);
  265. break;
  266. case MachineOperand::MO_FPImmediate:
  267. if (getFPImm()->getType()->isFloatTy())
  268. OS << getFPImm()->getValueAPF().convertToFloat();
  269. else
  270. OS << getFPImm()->getValueAPF().convertToDouble();
  271. break;
  272. case MachineOperand::MO_MachineBasicBlock:
  273. OS << "<BB#" << getMBB()->getNumber() << ">";
  274. break;
  275. case MachineOperand::MO_FrameIndex:
  276. OS << "<fi#" << getIndex() << '>';
  277. break;
  278. case MachineOperand::MO_ConstantPoolIndex:
  279. OS << "<cp#" << getIndex();
  280. if (getOffset()) OS << "+" << getOffset();
  281. OS << '>';
  282. break;
  283. case MachineOperand::MO_JumpTableIndex:
  284. OS << "<jt#" << getIndex() << '>';
  285. break;
  286. case MachineOperand::MO_GlobalAddress:
  287. OS << "<ga:";
  288. WriteAsOperand(OS, getGlobal(), /*PrintType=*/false);
  289. if (getOffset()) OS << "+" << getOffset();
  290. OS << '>';
  291. break;
  292. case MachineOperand::MO_ExternalSymbol:
  293. OS << "<es:" << getSymbolName();
  294. if (getOffset()) OS << "+" << getOffset();
  295. OS << '>';
  296. break;
  297. case MachineOperand::MO_BlockAddress:
  298. OS << '<';
  299. WriteAsOperand(OS, getBlockAddress(), /*PrintType=*/false);
  300. OS << '>';
  301. break;
  302. case MachineOperand::MO_Metadata:
  303. OS << '<';
  304. WriteAsOperand(OS, getMetadata(), /*PrintType=*/false);
  305. OS << '>';
  306. break;
  307. case MachineOperand::MO_MCSymbol:
  308. OS << "<MCSym=" << *getMCSymbol() << '>';
  309. break;
  310. default:
  311. llvm_unreachable("Unrecognized operand type");
  312. }
  313. if (unsigned TF = getTargetFlags())
  314. OS << "[TF=" << TF << ']';
  315. }
  316. //===----------------------------------------------------------------------===//
  317. // MachineMemOperand Implementation
  318. //===----------------------------------------------------------------------===//
  319. /// getAddrSpace - Return the LLVM IR address space number that this pointer
  320. /// points into.
  321. unsigned MachinePointerInfo::getAddrSpace() const {
  322. if (V == 0) return 0;
  323. return cast<PointerType>(V->getType())->getAddressSpace();
  324. }
  325. /// getConstantPool - Return a MachinePointerInfo record that refers to the
  326. /// constant pool.
  327. MachinePointerInfo MachinePointerInfo::getConstantPool() {
  328. return MachinePointerInfo(PseudoSourceValue::getConstantPool());
  329. }
  330. /// getFixedStack - Return a MachinePointerInfo record that refers to the
  331. /// the specified FrameIndex.
  332. MachinePointerInfo MachinePointerInfo::getFixedStack(int FI, int64_t offset) {
  333. return MachinePointerInfo(PseudoSourceValue::getFixedStack(FI), offset);
  334. }
  335. MachinePointerInfo MachinePointerInfo::getJumpTable() {
  336. return MachinePointerInfo(PseudoSourceValue::getJumpTable());
  337. }
  338. MachinePointerInfo MachinePointerInfo::getGOT() {
  339. return MachinePointerInfo(PseudoSourceValue::getGOT());
  340. }
  341. MachinePointerInfo MachinePointerInfo::getStack(int64_t Offset) {
  342. return MachinePointerInfo(PseudoSourceValue::getStack(), Offset);
  343. }
  344. MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, unsigned f,
  345. uint64_t s, unsigned int a,
  346. const MDNode *TBAAInfo)
  347. : PtrInfo(ptrinfo), Size(s),
  348. Flags((f & ((1 << MOMaxBits) - 1)) | ((Log2_32(a) + 1) << MOMaxBits)),
  349. TBAAInfo(TBAAInfo) {
  350. assert((PtrInfo.V == 0 || isa<PointerType>(PtrInfo.V->getType())) &&
  351. "invalid pointer value");
  352. assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
  353. assert((isLoad() || isStore()) && "Not a load/store!");
  354. }
  355. /// Profile - Gather unique data for the object.
  356. ///
  357. void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
  358. ID.AddInteger(getOffset());
  359. ID.AddInteger(Size);
  360. ID.AddPointer(getValue());
  361. ID.AddInteger(Flags);
  362. }
  363. void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
  364. // The Value and Offset may differ due to CSE. But the flags and size
  365. // should be the same.
  366. assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
  367. assert(MMO->getSize() == getSize() && "Size mismatch!");
  368. if (MMO->getBaseAlignment() >= getBaseAlignment()) {
  369. // Update the alignment value.
  370. Flags = (Flags & ((1 << MOMaxBits) - 1)) |
  371. ((Log2_32(MMO->getBaseAlignment()) + 1) << MOMaxBits);
  372. // Also update the base and offset, because the new alignment may
  373. // not be applicable with the old ones.
  374. PtrInfo = MMO->PtrInfo;
  375. }
  376. }
  377. /// getAlignment - Return the minimum known alignment in bytes of the
  378. /// actual memory reference.
  379. uint64_t MachineMemOperand::getAlignment() const {
  380. return MinAlign(getBaseAlignment(), getOffset());
  381. }
  382. raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) {
  383. assert((MMO.isLoad() || MMO.isStore()) &&
  384. "SV has to be a load, store or both.");
  385. if (MMO.isVolatile())
  386. OS << "Volatile ";
  387. if (MMO.isLoad())
  388. OS << "LD";
  389. if (MMO.isStore())
  390. OS << "ST";
  391. OS << MMO.getSize();
  392. // Print the address information.
  393. OS << "[";
  394. if (!MMO.getValue())
  395. OS << "<unknown>";
  396. else
  397. WriteAsOperand(OS, MMO.getValue(), /*PrintType=*/false);
  398. // If the alignment of the memory reference itself differs from the alignment
  399. // of the base pointer, print the base alignment explicitly, next to the base
  400. // pointer.
  401. if (MMO.getBaseAlignment() != MMO.getAlignment())
  402. OS << "(align=" << MMO.getBaseAlignment() << ")";
  403. if (MMO.getOffset() != 0)
  404. OS << "+" << MMO.getOffset();
  405. OS << "]";
  406. // Print the alignment of the reference.
  407. if (MMO.getBaseAlignment() != MMO.getAlignment() ||
  408. MMO.getBaseAlignment() != MMO.getSize())
  409. OS << "(align=" << MMO.getAlignment() << ")";
  410. // Print TBAA info.
  411. if (const MDNode *TBAAInfo = MMO.getTBAAInfo()) {
  412. OS << "(tbaa=";
  413. if (TBAAInfo->getNumOperands() > 0)
  414. WriteAsOperand(OS, TBAAInfo->getOperand(0), /*PrintType=*/false);
  415. else
  416. OS << "<unknown>";
  417. OS << ")";
  418. }
  419. // Print nontemporal info.
  420. if (MMO.isNonTemporal())
  421. OS << "(nontemporal)";
  422. return OS;
  423. }
  424. //===----------------------------------------------------------------------===//
  425. // MachineInstr Implementation
  426. //===----------------------------------------------------------------------===//
  427. /// MachineInstr ctor - This constructor creates a dummy MachineInstr with
  428. /// MCID NULL and no operands.
  429. MachineInstr::MachineInstr()
  430. : MCID(0), Flags(0), AsmPrinterFlags(0),
  431. MemRefs(0), MemRefsEnd(0),
  432. Parent(0) {
  433. // Make sure that we get added to a machine basicblock
  434. LeakDetector::addGarbageObject(this);
  435. }
  436. void MachineInstr::addImplicitDefUseOperands() {
  437. if (MCID->ImplicitDefs)
  438. for (const unsigned *ImpDefs = MCID->ImplicitDefs; *ImpDefs; ++ImpDefs)
  439. addOperand(MachineOperand::CreateReg(*ImpDefs, true, true));
  440. if (MCID->ImplicitUses)
  441. for (const unsigned *ImpUses = MCID->ImplicitUses; *ImpUses; ++ImpUses)
  442. addOperand(MachineOperand::CreateReg(*ImpUses, false, true));
  443. }
  444. /// MachineInstr ctor - This constructor creates a MachineInstr and adds the
  445. /// implicit operands. It reserves space for the number of operands specified by
  446. /// the MCInstrDesc.
  447. MachineInstr::MachineInstr(const MCInstrDesc &tid, bool NoImp)
  448. : MCID(&tid), Flags(0), AsmPrinterFlags(0),
  449. MemRefs(0), MemRefsEnd(0), Parent(0) {
  450. unsigned NumImplicitOps = 0;
  451. if (!NoImp)
  452. NumImplicitOps = MCID->getNumImplicitDefs() + MCID->getNumImplicitUses();
  453. Operands.reserve(NumImplicitOps + MCID->getNumOperands());
  454. if (!NoImp)
  455. addImplicitDefUseOperands();
  456. // Make sure that we get added to a machine basicblock
  457. LeakDetector::addGarbageObject(this);
  458. }
  459. /// MachineInstr ctor - As above, but with a DebugLoc.
  460. MachineInstr::MachineInstr(const MCInstrDesc &tid, const DebugLoc dl,
  461. bool NoImp)
  462. : MCID(&tid), Flags(0), AsmPrinterFlags(0),
  463. MemRefs(0), MemRefsEnd(0), Parent(0), debugLoc(dl) {
  464. unsigned NumImplicitOps = 0;
  465. if (!NoImp)
  466. NumImplicitOps = MCID->getNumImplicitDefs() + MCID->getNumImplicitUses();
  467. Operands.reserve(NumImplicitOps + MCID->getNumOperands());
  468. if (!NoImp)
  469. addImplicitDefUseOperands();
  470. // Make sure that we get added to a machine basicblock
  471. LeakDetector::addGarbageObject(this);
  472. }
  473. /// MachineInstr ctor - Work exactly the same as the ctor two above, except
  474. /// that the MachineInstr is created and added to the end of the specified
  475. /// basic block.
  476. MachineInstr::MachineInstr(MachineBasicBlock *MBB, const MCInstrDesc &tid)
  477. : MCID(&tid), Flags(0), AsmPrinterFlags(0),
  478. MemRefs(0), MemRefsEnd(0), Parent(0) {
  479. assert(MBB && "Cannot use inserting ctor with null basic block!");
  480. unsigned NumImplicitOps =
  481. MCID->getNumImplicitDefs() + MCID->getNumImplicitUses();
  482. Operands.reserve(NumImplicitOps + MCID->getNumOperands());
  483. addImplicitDefUseOperands();
  484. // Make sure that we get added to a machine basicblock
  485. LeakDetector::addGarbageObject(this);
  486. MBB->push_back(this); // Add instruction to end of basic block!
  487. }
  488. /// MachineInstr ctor - As above, but with a DebugLoc.
  489. ///
  490. MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl,
  491. const MCInstrDesc &tid)
  492. : MCID(&tid), Flags(0), AsmPrinterFlags(0),
  493. MemRefs(0), MemRefsEnd(0), Parent(0), debugLoc(dl) {
  494. assert(MBB && "Cannot use inserting ctor with null basic block!");
  495. unsigned NumImplicitOps =
  496. MCID->getNumImplicitDefs() + MCID->getNumImplicitUses();
  497. Operands.reserve(NumImplicitOps + MCID->getNumOperands());
  498. addImplicitDefUseOperands();
  499. // Make sure that we get added to a machine basicblock
  500. LeakDetector::addGarbageObject(this);
  501. MBB->push_back(this); // Add instruction to end of basic block!
  502. }
  503. /// MachineInstr ctor - Copies MachineInstr arg exactly
  504. ///
  505. MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
  506. : MCID(&MI.getDesc()), Flags(0), AsmPrinterFlags(0),
  507. MemRefs(MI.MemRefs), MemRefsEnd(MI.MemRefsEnd),
  508. Parent(0), debugLoc(MI.getDebugLoc()) {
  509. Operands.reserve(MI.getNumOperands());
  510. // Add operands
  511. for (unsigned i = 0; i != MI.getNumOperands(); ++i)
  512. addOperand(MI.getOperand(i));
  513. // Copy all the flags.
  514. Flags = MI.Flags;
  515. // Set parent to null.
  516. Parent = 0;
  517. LeakDetector::addGarbageObject(this);
  518. }
  519. MachineInstr::~MachineInstr() {
  520. LeakDetector::removeGarbageObject(this);
  521. #ifndef NDEBUG
  522. for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
  523. assert(Operands[i].ParentMI == this && "ParentMI mismatch!");
  524. assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) &&
  525. "Reg operand def/use list corrupted");
  526. }
  527. #endif
  528. }
  529. /// getRegInfo - If this instruction is embedded into a MachineFunction,
  530. /// return the MachineRegisterInfo object for the current function, otherwise
  531. /// return null.
  532. MachineRegisterInfo *MachineInstr::getRegInfo() {
  533. if (MachineBasicBlock *MBB = getParent())
  534. return &MBB->getParent()->getRegInfo();
  535. return 0;
  536. }
  537. /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
  538. /// this instruction from their respective use lists. This requires that the
  539. /// operands already be on their use lists.
  540. void MachineInstr::RemoveRegOperandsFromUseLists() {
  541. for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
  542. if (Operands[i].isReg())
  543. Operands[i].RemoveRegOperandFromRegInfo();
  544. }
  545. }
  546. /// AddRegOperandsToUseLists - Add all of the register operands in
  547. /// this instruction from their respective use lists. This requires that the
  548. /// operands not be on their use lists yet.
  549. void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) {
  550. for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
  551. if (Operands[i].isReg())
  552. Operands[i].AddRegOperandToRegInfo(&RegInfo);
  553. }
  554. }
  555. /// addOperand - Add the specified operand to the instruction. If it is an
  556. /// implicit operand, it is added to the end of the operand list. If it is
  557. /// an explicit operand it is added at the end of the explicit operand list
  558. /// (before the first implicit operand).
  559. void MachineInstr::addOperand(const MachineOperand &Op) {
  560. assert(MCID && "Cannot add operands before providing an instr descriptor");
  561. bool isImpReg = Op.isReg() && Op.isImplicit();
  562. MachineRegisterInfo *RegInfo = getRegInfo();
  563. // If the Operands backing store is reallocated, all register operands must
  564. // be removed and re-added to RegInfo. It is storing pointers to operands.
  565. bool Reallocate = RegInfo &&
  566. !Operands.empty() && Operands.size() == Operands.capacity();
  567. // Find the insert location for the new operand. Implicit registers go at
  568. // the end, everything goes before the implicit regs.
  569. unsigned OpNo = Operands.size();
  570. // Remove all the implicit operands from RegInfo if they need to be shifted.
  571. // FIXME: Allow mixed explicit and implicit operands on inline asm.
  572. // InstrEmitter::EmitSpecialNode() is marking inline asm clobbers as
  573. // implicit-defs, but they must not be moved around. See the FIXME in
  574. // InstrEmitter.cpp.
  575. if (!isImpReg && !isInlineAsm()) {
  576. while (OpNo && Operands[OpNo-1].isReg() && Operands[OpNo-1].isImplicit()) {
  577. --OpNo;
  578. if (RegInfo)
  579. Operands[OpNo].RemoveRegOperandFromRegInfo();
  580. }
  581. }
  582. // OpNo now points as the desired insertion point. Unless this is a variadic
  583. // instruction, only implicit regs are allowed beyond MCID->getNumOperands().
  584. assert((isImpReg || MCID->isVariadic() || OpNo < MCID->getNumOperands()) &&
  585. "Trying to add an operand to a machine instr that is already done!");
  586. // All operands from OpNo have been removed from RegInfo. If the Operands
  587. // backing store needs to be reallocated, we also need to remove any other
  588. // register operands.
  589. if (Reallocate)
  590. for (unsigned i = 0; i != OpNo; ++i)
  591. if (Operands[i].isReg())
  592. Operands[i].RemoveRegOperandFromRegInfo();
  593. // Insert the new operand at OpNo.
  594. Operands.insert(Operands.begin() + OpNo, Op);
  595. Operands[OpNo].ParentMI = this;
  596. // The Operands backing store has now been reallocated, so we can re-add the
  597. // operands before OpNo.
  598. if (Reallocate)
  599. for (unsigned i = 0; i != OpNo; ++i)
  600. if (Operands[i].isReg())
  601. Operands[i].AddRegOperandToRegInfo(RegInfo);
  602. // When adding a register operand, tell RegInfo about it.
  603. if (Operands[OpNo].isReg()) {
  604. // Add the new operand to RegInfo, even when RegInfo is NULL.
  605. // This will initialize the linked list pointers.
  606. Operands[OpNo].AddRegOperandToRegInfo(RegInfo);
  607. // If the register operand is flagged as early, mark the operand as such.
  608. if (MCID->getOperandConstraint(OpNo, MCOI::EARLY_CLOBBER) != -1)
  609. Operands[OpNo].setIsEarlyClobber(true);
  610. }
  611. // Re-add all the implicit ops.
  612. if (RegInfo) {
  613. for (unsigned i = OpNo + 1, e = Operands.size(); i != e; ++i) {
  614. assert(Operands[i].isReg() && "Should only be an implicit reg!");
  615. Operands[i].AddRegOperandToRegInfo(RegInfo);
  616. }
  617. }
  618. }
  619. /// RemoveOperand - Erase an operand from an instruction, leaving it with one
  620. /// fewer operand than it started with.
  621. ///
  622. void MachineInstr::RemoveOperand(unsigned OpNo) {
  623. assert(OpNo < Operands.size() && "Invalid operand number");
  624. // Special case removing the last one.
  625. if (OpNo == Operands.size()-1) {
  626. // If needed, remove from the reg def/use list.
  627. if (Operands.back().isReg() && Operands.back().isOnRegUseList())
  628. Operands.back().RemoveRegOperandFromRegInfo();
  629. Operands.pop_back();
  630. return;
  631. }
  632. // Otherwise, we are removing an interior operand. If we have reginfo to
  633. // update, remove all operands that will be shifted down from their reg lists,
  634. // move everything down, then re-add them.
  635. MachineRegisterInfo *RegInfo = getRegInfo();
  636. if (RegInfo) {
  637. for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
  638. if (Operands[i].isReg())
  639. Operands[i].RemoveRegOperandFromRegInfo();
  640. }
  641. }
  642. Operands.erase(Operands.begin()+OpNo);
  643. if (RegInfo) {
  644. for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) {
  645. if (Operands[i].isReg())
  646. Operands[i].AddRegOperandToRegInfo(RegInfo);
  647. }
  648. }
  649. }
  650. /// addMemOperand - Add a MachineMemOperand to the machine instruction.
  651. /// This function should be used only occasionally. The setMemRefs function
  652. /// is the primary method for setting up a MachineInstr's MemRefs list.
  653. void MachineInstr::addMemOperand(MachineFunction &MF,
  654. MachineMemOperand *MO) {
  655. mmo_iterator OldMemRefs = MemRefs;
  656. mmo_iterator OldMemRefsEnd = MemRefsEnd;
  657. size_t NewNum = (MemRefsEnd - MemRefs) + 1;
  658. mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum);
  659. mmo_iterator NewMemRefsEnd = NewMemRefs + NewNum;
  660. std::copy(OldMemRefs, OldMemRefsEnd, NewMemRefs);
  661. NewMemRefs[NewNum - 1] = MO;
  662. MemRefs = NewMemRefs;
  663. MemRefsEnd = NewMemRefsEnd;
  664. }
  665. bool
  666. MachineInstr::hasProperty(unsigned MCFlag, QueryType Type) const {
  667. if (Type == IgnoreBundle || !isBundle())
  668. return getDesc().getFlags() & (1 << MCFlag);
  669. const MachineBasicBlock *MBB = getParent();
  670. MachineBasicBlock::const_instr_iterator MII = *this; ++MII;
  671. while (MII != MBB->end() && MII->isInsideBundle()) {
  672. if (MII->getDesc().getFlags() & (1 << MCFlag)) {
  673. if (Type == AnyInBundle)
  674. return true;
  675. } else {
  676. if (Type == AllInBundle)
  677. return false;
  678. }
  679. ++MII;
  680. }
  681. return Type == AllInBundle;
  682. }
  683. bool MachineInstr::isIdenticalTo(const MachineInstr *Other,
  684. MICheckType Check) const {
  685. // If opcodes or number of operands are not the same then the two
  686. // instructions are obviously not identical.
  687. if (Other->getOpcode() != getOpcode() ||
  688. Other->getNumOperands() != getNumOperands())
  689. return false;
  690. if (isBundle()) {
  691. // Both instructions are bundles, compare MIs inside the bundle.
  692. MachineBasicBlock::const_instr_iterator I1 = *this;
  693. MachineBasicBlock::const_instr_iterator E1 = getParent()->instr_end();
  694. MachineBasicBlock::const_instr_iterator I2 = *Other;
  695. MachineBasicBlock::const_instr_iterator E2= Other->getParent()->instr_end();
  696. while (++I1 != E1 && I1->isInsideBundle()) {
  697. ++I2;
  698. if (I2 == E2 || !I2->isInsideBundle() || !I1->isIdenticalTo(I2, Check))
  699. return false;
  700. }
  701. }
  702. // Check operands to make sure they match.
  703. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  704. const MachineOperand &MO = getOperand(i);
  705. const MachineOperand &OMO = Other->getOperand(i);
  706. if (!MO.isReg()) {
  707. if (!MO.isIdenticalTo(OMO))
  708. return false;
  709. continue;
  710. }
  711. // Clients may or may not want to ignore defs when testing for equality.
  712. // For example, machine CSE pass only cares about finding common
  713. // subexpressions, so it's safe to ignore virtual register defs.
  714. if (MO.isDef()) {
  715. if (Check == IgnoreDefs)
  716. continue;
  717. else if (Check == IgnoreVRegDefs) {
  718. if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) ||
  719. TargetRegisterInfo::isPhysicalRegister(OMO.getReg()))
  720. if (MO.getReg() != OMO.getReg())
  721. return false;
  722. } else {
  723. if (!MO.isIdenticalTo(OMO))
  724. return false;
  725. if (Check == CheckKillDead && MO.isDead() != OMO.isDead())
  726. return false;
  727. }
  728. } else {
  729. if (!MO.isIdenticalTo(OMO))
  730. return false;
  731. if (Check == CheckKillDead && MO.isKill() != OMO.isKill())
  732. return false;
  733. }
  734. }
  735. // If DebugLoc does not match then two dbg.values are not identical.
  736. if (isDebugValue())
  737. if (!getDebugLoc().isUnknown() && !Other->getDebugLoc().isUnknown()
  738. && getDebugLoc() != Other->getDebugLoc())
  739. return false;
  740. return true;
  741. }
  742. /// removeFromParent - This method unlinks 'this' from the containing basic
  743. /// block, and returns it, but does not delete it.
  744. MachineInstr *MachineInstr::removeFromParent() {
  745. assert(getParent() && "Not embedded in a basic block!");
  746. // If it's a bundle then remove the MIs inside the bundle as well.
  747. if (isBundle()) {
  748. MachineBasicBlock *MBB = getParent();
  749. MachineBasicBlock::instr_iterator MII = *this; ++MII;
  750. MachineBasicBlock::instr_iterator E = MBB->instr_end();
  751. while (MII != E && MII->isInsideBundle()) {
  752. MachineInstr *MI = &*MII;
  753. ++MII;
  754. MBB->remove(MI);
  755. }
  756. }
  757. getParent()->remove(this);
  758. return this;
  759. }
  760. /// eraseFromParent - This method unlinks 'this' from the containing basic
  761. /// block, and deletes it.
  762. void MachineInstr::eraseFromParent() {
  763. assert(getParent() && "Not embedded in a basic block!");
  764. // If it's a bundle then remove the MIs inside the bundle as well.
  765. if (isBundle()) {
  766. MachineBasicBlock *MBB = getParent();
  767. MachineBasicBlock::instr_iterator MII = *this; ++MII;
  768. MachineBasicBlock::instr_iterator E = MBB->instr_end();
  769. while (MII != E && MII->isInsideBundle()) {
  770. MachineInstr *MI = &*MII;
  771. ++MII;
  772. MBB->erase(MI);
  773. }
  774. }
  775. getParent()->erase(this);
  776. }
  777. /// getNumExplicitOperands - Returns the number of non-implicit operands.
  778. ///
  779. unsigned MachineInstr::getNumExplicitOperands() const {
  780. unsigned NumOperands = MCID->getNumOperands();
  781. if (!MCID->isVariadic())
  782. return NumOperands;
  783. for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
  784. const MachineOperand &MO = getOperand(i);
  785. if (!MO.isReg() || !MO.isImplicit())
  786. NumOperands++;
  787. }
  788. return NumOperands;
  789. }
  790. bool MachineInstr::isStackAligningInlineAsm() const {
  791. if (isInlineAsm()) {
  792. unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
  793. if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
  794. return true;
  795. }
  796. return false;
  797. }
  798. int MachineInstr::findInlineAsmFlagIdx(unsigned OpIdx,
  799. unsigned *GroupNo) const {
  800. assert(isInlineAsm() && "Expected an inline asm instruction");
  801. assert(OpIdx < getNumOperands() && "OpIdx out of range");
  802. // Ignore queries about the initial operands.
  803. if (OpIdx < InlineAsm::MIOp_FirstOperand)
  804. return -1;
  805. unsigned Group = 0;
  806. unsigned NumOps;
  807. for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
  808. i += NumOps) {
  809. const MachineOperand &FlagMO = getOperand(i);
  810. // If we reach the implicit register operands, stop looking.
  811. if (!FlagMO.isImm())
  812. return -1;
  813. NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
  814. if (i + NumOps > OpIdx) {
  815. if (GroupNo)
  816. *GroupNo = Group;
  817. return i;
  818. }
  819. ++Group;
  820. }
  821. return -1;
  822. }
  823. const TargetRegisterClass*
  824. MachineInstr::getRegClassConstraint(unsigned OpIdx,
  825. const TargetInstrInfo *TII,
  826. const TargetRegisterInfo *TRI) const {
  827. // Most opcodes have fixed constraints in their MCInstrDesc.
  828. if (!isInlineAsm())
  829. return TII->getRegClass(getDesc(), OpIdx, TRI);
  830. if (!getOperand(OpIdx).isReg())
  831. return NULL;
  832. // For tied uses on inline asm, get the constraint from the def.
  833. unsigned DefIdx;
  834. if (getOperand(OpIdx).isUse() && isRegTiedToDefOperand(OpIdx, &DefIdx))
  835. OpIdx = DefIdx;
  836. // Inline asm stores register class constraints in the flag word.
  837. int FlagIdx = findInlineAsmFlagIdx(OpIdx);
  838. if (FlagIdx < 0)
  839. return NULL;
  840. unsigned Flag = getOperand(FlagIdx).getImm();
  841. unsigned RCID;
  842. if (InlineAsm::hasRegClassConstraint(Flag, RCID))
  843. return TRI->getRegClass(RCID);
  844. // Assume that all registers in a memory operand are pointers.
  845. if (InlineAsm::getKind(Flag) == InlineAsm::Kind_Mem)
  846. return TRI->getPointerRegClass();
  847. return NULL;
  848. }
  849. /// getBundleSize - Return the number of instructions inside the MI bundle.
  850. unsigned MachineInstr::getBundleSize() const {
  851. assert(isBundle() && "Expecting a bundle");
  852. MachineBasicBlock::const_instr_iterator I = *this;
  853. unsigned Size = 0;
  854. while ((++I)->isInsideBundle()) {
  855. ++Size;
  856. }
  857. assert(Size > 1 && "Malformed bundle");
  858. return Size;
  859. }
  860. /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
  861. /// the specific register or -1 if it is not found. It further tightens
  862. /// the search criteria to a use that kills the register if isKill is true.
  863. int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill,
  864. const TargetRegisterInfo *TRI) const {
  865. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  866. const MachineOperand &MO = getOperand(i);
  867. if (!MO.isReg() || !MO.isUse())
  868. continue;
  869. unsigned MOReg = MO.getReg();
  870. if (!MOReg)
  871. continue;
  872. if (MOReg == Reg ||
  873. (TRI &&
  874. TargetRegisterInfo::isPhysicalRegister(MOReg) &&
  875. TargetRegisterInfo::isPhysicalRegister(Reg) &&
  876. TRI->isSubRegister(MOReg, Reg)))
  877. if (!isKill || MO.isKill())
  878. return i;
  879. }
  880. return -1;
  881. }
  882. /// readsWritesVirtualRegister - Return a pair of bools (reads, writes)
  883. /// indicating if this instruction reads or writes Reg. This also considers
  884. /// partial defines.
  885. std::pair<bool,bool>
  886. MachineInstr::readsWritesVirtualRegister(unsigned Reg,
  887. SmallVectorImpl<unsigned> *Ops) const {
  888. bool PartDef = false; // Partial redefine.
  889. bool FullDef = false; // Full define.
  890. bool Use = false;
  891. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  892. const MachineOperand &MO = getOperand(i);
  893. if (!MO.isReg() || MO.getReg() != Reg)
  894. continue;
  895. if (Ops)
  896. Ops->push_back(i);
  897. if (MO.isUse())
  898. Use |= !MO.isUndef();
  899. else if (MO.getSubReg() && !MO.isUndef())
  900. // A partial <def,undef> doesn't count as reading the register.
  901. PartDef = true;
  902. else
  903. FullDef = true;
  904. }
  905. // A partial redefine uses Reg unless there is also a full define.
  906. return std::make_pair(Use || (PartDef && !FullDef), PartDef || FullDef);
  907. }
  908. /// findRegisterDefOperandIdx() - Returns the operand index that is a def of
  909. /// the specified register or -1 if it is not found. If isDead is true, defs
  910. /// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
  911. /// also checks if there is a def of a super-register.
  912. int
  913. MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, bool Overlap,
  914. const TargetRegisterInfo *TRI) const {
  915. bool isPhys = TargetRegisterInfo::isPhysicalRegister(Reg);
  916. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  917. const MachineOperand &MO = getOperand(i);
  918. if (!MO.isReg() || !MO.isDef())
  919. continue;
  920. unsigned MOReg = MO.getReg();
  921. bool Found = (MOReg == Reg);
  922. if (!Found && TRI && isPhys &&
  923. TargetRegisterInfo::isPhysicalRegister(MOReg)) {
  924. if (Overlap)
  925. Found = TRI->regsOverlap(MOReg, Reg);
  926. else
  927. Found = TRI->isSubRegister(MOReg, Reg);
  928. }
  929. if (Found && (!isDead || MO.isDead()))
  930. return i;
  931. }
  932. return -1;
  933. }
  934. /// findFirstPredOperandIdx() - Find the index of the first operand in the
  935. /// operand list that is used to represent the predicate. It returns -1 if
  936. /// none is found.
  937. int MachineInstr::findFirstPredOperandIdx() const {
  938. // Don't call MCID.findFirstPredOperandIdx() because this variant
  939. // is sometimes called on an instruction that's not yet complete, and
  940. // so the number of operands is less than the MCID indicates. In
  941. // particular, the PTX target does this.
  942. const MCInstrDesc &MCID = getDesc();
  943. if (MCID.isPredicable()) {
  944. for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
  945. if (MCID.OpInfo[i].isPredicate())
  946. return i;
  947. }
  948. return -1;
  949. }
  950. /// isRegTiedToUseOperand - Given the index of a register def operand,
  951. /// check if the register def is tied to a source operand, due to either
  952. /// two-address elimination or inline assembly constraints. Returns the
  953. /// first tied use operand index by reference is UseOpIdx is not null.
  954. bool MachineInstr::
  955. isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx) const {
  956. if (isInlineAsm()) {
  957. assert(DefOpIdx > InlineAsm::MIOp_FirstOperand);
  958. const MachineOperand &MO = getOperand(DefOpIdx);
  959. if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0)
  960. return false;
  961. // Determine the actual operand index that corresponds to this index.
  962. unsigned DefNo = 0;
  963. int FlagIdx = findInlineAsmFlagIdx(DefOpIdx, &DefNo);
  964. if (FlagIdx < 0)
  965. return false;
  966. // Which part of the group is DefOpIdx?
  967. unsigned DefPart = DefOpIdx - (FlagIdx + 1);
  968. for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands();
  969. i != e; ++i) {
  970. const MachineOperand &FMO = getOperand(i);
  971. if (!FMO.isImm())
  972. continue;
  973. if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse())
  974. continue;
  975. unsigned Idx;
  976. if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) &&
  977. Idx == DefNo) {
  978. if (UseOpIdx)
  979. *UseOpIdx = (unsigned)i + 1 + DefPart;
  980. return true;
  981. }
  982. }
  983. return false;
  984. }
  985. assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!");
  986. const MCInstrDesc &MCID = getDesc();
  987. for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i) {
  988. const MachineOperand &MO = getOperand(i);
  989. if (MO.isReg() && MO.isUse() &&
  990. MCID.getOperandConstraint(i, MCOI::TIED_TO) == (int)DefOpIdx) {
  991. if (UseOpIdx)
  992. *UseOpIdx = (unsigned)i;
  993. return true;
  994. }
  995. }
  996. return false;
  997. }
  998. /// isRegTiedToDefOperand - Return true if the operand of the specified index
  999. /// is a register use and it is tied to an def operand. It also returns the def
  1000. /// operand index by reference.
  1001. bool MachineInstr::
  1002. isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx) const {
  1003. if (isInlineAsm()) {
  1004. const MachineOperand &MO = getOperand(UseOpIdx);
  1005. if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0)
  1006. return false;
  1007. // Find the flag operand corresponding to UseOpIdx
  1008. int FlagIdx = findInlineAsmFlagIdx(UseOpIdx);
  1009. if (FlagIdx < 0)
  1010. return false;
  1011. const MachineOperand &UFMO = getOperand(FlagIdx);
  1012. unsigned DefNo;
  1013. if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) {
  1014. if (!DefOpIdx)
  1015. return true;
  1016. unsigned DefIdx = InlineAsm::MIOp_FirstOperand;
  1017. // Remember to adjust the index. First operand is asm string, second is
  1018. // the HasSideEffects and AlignStack bits, then there is a flag for each.
  1019. while (DefNo) {
  1020. const MachineOperand &FMO = getOperand(DefIdx);
  1021. assert(FMO.isImm());
  1022. // Skip over this def.
  1023. DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1;
  1024. --DefNo;
  1025. }
  1026. *DefOpIdx = DefIdx + UseOpIdx - FlagIdx;
  1027. return true;
  1028. }
  1029. return false;
  1030. }
  1031. const MCInstrDesc &MCID = getDesc();
  1032. if (UseOpIdx >= MCID.getNumOperands())
  1033. return false;
  1034. const MachineOperand &MO = getOperand(UseOpIdx);
  1035. if (!MO.isReg() || !MO.isUse())
  1036. return false;
  1037. int DefIdx = MCID.getOperandConstraint(UseOpIdx, MCOI::TIED_TO);
  1038. if (DefIdx == -1)
  1039. return false;
  1040. if (DefOpIdx)
  1041. *DefOpIdx = (unsigned)DefIdx;
  1042. return true;
  1043. }
  1044. /// clearKillInfo - Clears kill flags on all operands.
  1045. ///
  1046. void MachineInstr::clearKillInfo() {
  1047. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  1048. MachineOperand &MO = getOperand(i);
  1049. if (MO.isReg() && MO.isUse())
  1050. MO.setIsKill(false);
  1051. }
  1052. }
  1053. /// copyKillDeadInfo - Copies kill / dead operand properties from MI.
  1054. ///
  1055. void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) {
  1056. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  1057. const MachineOperand &MO = MI->getOperand(i);
  1058. if (!MO.isReg() || (!MO.isKill() && !MO.isDead()))
  1059. continue;
  1060. for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) {
  1061. MachineOperand &MOp = getOperand(j);
  1062. if (!MOp.isIdenticalTo(MO))
  1063. continue;
  1064. if (MO.isKill())
  1065. MOp.setIsKill();
  1066. else
  1067. MOp.setIsDead();
  1068. break;
  1069. }
  1070. }
  1071. }
  1072. /// copyPredicates - Copies predicate operand(s) from MI.
  1073. void MachineInstr::copyPredicates(const MachineInstr *MI) {
  1074. assert(!isBundle() && "MachineInstr::copyPredicates() can't handle bundles");
  1075. const MCInstrDesc &MCID = MI->getDesc();
  1076. if (!MCID.isPredicable())
  1077. return;
  1078. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  1079. if (MCID.OpInfo[i].isPredicate()) {
  1080. // Predicated operands must be last operands.
  1081. addOperand(MI->getOperand(i));
  1082. }
  1083. }
  1084. }
  1085. void MachineInstr::substituteRegister(unsigned FromReg,
  1086. unsigned ToReg,
  1087. unsigned SubIdx,
  1088. const TargetRegisterInfo &RegInfo) {
  1089. if (TargetRegisterInfo::isPhysicalRegister(ToReg)) {
  1090. if (SubIdx)
  1091. ToReg = RegInfo.getSubReg(ToReg, SubIdx);
  1092. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  1093. MachineOperand &MO = getOperand(i);
  1094. if (!MO.isReg() || MO.getReg() != FromReg)
  1095. continue;
  1096. MO.substPhysReg(ToReg, RegInfo);
  1097. }
  1098. } else {
  1099. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  1100. MachineOperand &MO = getOperand(i);
  1101. if (!MO.isReg() || MO.getReg() != FromReg)
  1102. continue;
  1103. MO.substVirtReg(ToReg, SubIdx, RegInfo);
  1104. }
  1105. }
  1106. }
  1107. /// isSafeToMove - Return true if it is safe to move this instruction. If
  1108. /// SawStore is set to true, it means that there is a store (or call) between
  1109. /// the instruction's location and its intended destination.
  1110. bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII,
  1111. AliasAnalysis *AA,
  1112. bool &SawStore) const {
  1113. // Ignore stuff that we obviously can't move.
  1114. if (mayStore() || isCall()) {
  1115. SawStore = true;
  1116. return false;
  1117. }
  1118. if (isLabel() || isDebugValue() ||
  1119. isTerminator() || hasUnmodeledSideEffects())
  1120. return false;
  1121. // See if this instruction does a load. If so, we have to guarantee that the
  1122. // loaded value doesn't change between the load and the its intended
  1123. // destination. The check for isInvariantLoad gives the targe the chance to
  1124. // classify the load as always returning a constant, e.g. a constant pool
  1125. // load.
  1126. if (mayLoad() && !isInvariantLoad(AA))
  1127. // Otherwise, this is a real load. If there is a store between the load and
  1128. // end of block, or if the load is volatile, we can't move it.
  1129. return !SawStore && !hasVolatileMemoryRef();
  1130. return true;
  1131. }
  1132. /// isSafeToReMat - Return true if it's safe to rematerialize the specified
  1133. /// instruction which defined the specified register instead of copying it.
  1134. bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII,
  1135. AliasAnalysis *AA,
  1136. unsigned DstReg) const {
  1137. bool SawStore = false;
  1138. if (!TII->isTriviallyReMaterializable(this, AA) ||
  1139. !isSafeToMove(TII, AA, SawStore))
  1140. return false;
  1141. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  1142. const MachineOperand &MO = getOperand(i);
  1143. if (!MO.isReg())
  1144. continue;
  1145. // FIXME: For now, do not remat any instruction with register operands.
  1146. // Later on, we can loosen the restriction is the register operands have
  1147. // not been modified between the def and use. Note, this is different from
  1148. // MachineSink because the code is no longer in two-address form (at least
  1149. // partially).
  1150. if (MO.isUse())
  1151. return false;
  1152. else if (!MO.isDead() && MO.getReg() != DstReg)
  1153. return false;
  1154. }
  1155. return true;
  1156. }
  1157. /// hasVolatileMemoryRef - Return true if this instruction may have a
  1158. /// volatile memory reference, or if the information describing the
  1159. /// memory reference is not available. Return false if it is known to
  1160. /// have no volatile memory references.
  1161. bool MachineInstr::hasVolatileMemoryRef() const {
  1162. // An instruction known never to access memory won't have a volatile access.
  1163. if (!mayStore() &&
  1164. !mayLoad() &&
  1165. !isCall() &&
  1166. !hasUnmodeledSideEffects())
  1167. return false;
  1168. // Otherwise, if the instruction has no memory reference information,
  1169. // conservatively assume it wasn't preserved.
  1170. if (memoperands_empty())
  1171. return true;
  1172. // Check the memory reference information for volatile references.
  1173. for (mmo_iterator I = memoperands_begin(), E = memoperands_end(); I != E; ++I)
  1174. if ((*I)->isVolatile())
  1175. return true;
  1176. return false;
  1177. }
  1178. /// isInvariantLoad - Return true if this instruction is loading from a
  1179. /// location whose value is invariant across the function. For example,
  1180. /// loading a value from the constant pool or from the argument area
  1181. /// of a function if it does not change. This should only return true of
  1182. /// *all* loads the instruction does are invariant (if it does multiple loads).
  1183. bool MachineInstr::isInvariantLoad(AliasAnalysis *AA) const {
  1184. // If the instruction doesn't load at all, it isn't an invariant load.
  1185. if (!mayLoad())
  1186. return false;
  1187. // If the instruction has lost its memoperands, conservatively assume that
  1188. // it may not be an invariant load.
  1189. if (memoperands_empty())
  1190. return false;
  1191. const MachineFrameInfo *MFI = getParent()->getParent()->getFrameInfo();
  1192. for (mmo_iterator I = memoperands_begin(),
  1193. E = memoperands_end(); I != E; ++I) {
  1194. if ((*I)->isVolatile()) return false;
  1195. if ((*I)->isStore()) return false;
  1196. if ((*I)->isInvariant()) return true;
  1197. if (const Value *V = (*I)->getValue()) {
  1198. // A load from a constant PseudoSourceValue is invariant.
  1199. if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V))
  1200. if (PSV->isConstant(MFI))
  1201. continue;
  1202. // If we have an AliasAnalysis, ask it whether the memory is constant.
  1203. if (AA && AA->pointsToConstantMemory(
  1204. AliasAnalysis::Location(V, (*I)->getSize(),
  1205. (*I)->getTBAAInfo())))
  1206. continue;
  1207. }
  1208. // Otherwise assume conservatively.
  1209. return false;
  1210. }
  1211. // Everything checks out.
  1212. return true;
  1213. }
  1214. /// isConstantValuePHI - If the specified instruction is a PHI that always
  1215. /// merges together the same virtual register, return the register, otherwise
  1216. /// return 0.
  1217. unsigned MachineInstr::isConstantValuePHI() const {
  1218. if (!isPHI())
  1219. return 0;
  1220. assert(getNumOperands() >= 3 &&
  1221. "It's illegal to have a PHI without source operands");
  1222. unsigned Reg = getOperand(1).getReg();
  1223. for (unsigned i = 3, e = getNumOperands(); i < e; i += 2)
  1224. if (getOperand(i).getReg() != Reg)
  1225. return 0;
  1226. return Reg;
  1227. }
  1228. bool MachineInstr::hasUnmodeledSideEffects() const {
  1229. if (hasProperty(MCID::UnmodeledSideEffects))
  1230. return true;
  1231. if (isInlineAsm()) {
  1232. unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
  1233. if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
  1234. return true;
  1235. }
  1236. return false;
  1237. }
  1238. /// allDefsAreDead - Return true if all the defs of this instruction are dead.
  1239. ///
  1240. bool MachineInstr::allDefsAreDead() const {
  1241. for (unsigned i = 0, e = getNumOperands(); i < e; ++i) {
  1242. const MachineOperand &MO = getOperand(i);
  1243. if (!MO.isReg() || MO.isUse())
  1244. continue;
  1245. if (!MO.isDead())
  1246. return false;
  1247. }
  1248. return true;
  1249. }
  1250. /// copyImplicitOps - Copy implicit register operands from specified
  1251. /// instruction to this instruction.
  1252. void MachineInstr::copyImplicitOps(const MachineInstr *MI) {
  1253. for (unsigned i = MI->getDesc().getNumOperands(), e = MI->getNumOperands();
  1254. i != e; ++i) {
  1255. const MachineOperand &MO = MI->getOperand(i);
  1256. if (MO.isReg() && MO.isImplicit())
  1257. addOperand(MO);
  1258. }
  1259. }
  1260. void MachineInstr::dump() const {
  1261. dbgs() << " " << *this;
  1262. }
  1263. static void printDebugLoc(DebugLoc DL, const MachineFunction *MF,
  1264. raw_ostream &CommentOS) {
  1265. const LLVMContext &Ctx = MF->getFunction()->getContext();
  1266. if (!DL.isUnknown()) { // Print source line info.
  1267. DIScope Scope(DL.getScope(Ctx));
  1268. // Omit the directory, because it's likely to be long and uninteresting.
  1269. if (Scope.Verify())
  1270. CommentOS << Scope.getFilename();
  1271. else
  1272. CommentOS << "<unknown>";
  1273. CommentOS << ':' << DL.getLine();
  1274. if (DL.getCol() != 0)
  1275. CommentOS << ':' << DL.getCol();
  1276. DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
  1277. if (!InlinedAtDL.isUnknown()) {
  1278. CommentOS << " @[ ";
  1279. printDebugLoc(InlinedAtDL, MF, CommentOS);
  1280. CommentOS << " ]";
  1281. }
  1282. }
  1283. }
  1284. void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const {
  1285. // We can be a bit tidier if we know the TargetMachine and/or MachineFunction.
  1286. const MachineFunction *MF = 0;
  1287. const MachineRegisterInfo *MRI = 0;
  1288. if (const MachineBasicBlock *MBB = getParent()) {
  1289. MF = MBB->getParent();
  1290. if (!TM && MF)
  1291. TM = &MF->getTarget();
  1292. if (MF)
  1293. MRI = &MF->getRegInfo();
  1294. }
  1295. // Save a list of virtual registers.
  1296. SmallVector<unsigned, 8> VirtRegs;
  1297. // Print explicitly defined operands on the left of an assignment syntax.
  1298. unsigned StartOp = 0, e = getNumOperands();
  1299. for (; StartOp < e && getOperand(StartOp).isReg() &&
  1300. getOperand(StartOp).isDef() &&
  1301. !getOperand(StartOp).isImplicit();
  1302. ++StartOp) {
  1303. if (StartOp != 0) OS << ", ";
  1304. getOperand(StartOp).print(OS, TM);
  1305. unsigned Reg = getOperand(StartOp).getReg();
  1306. if (TargetRegisterInfo::isVirtualRegister(Reg))
  1307. VirtRegs.push_back(Reg);
  1308. }
  1309. if (StartOp != 0)
  1310. OS << " = ";
  1311. // Print the opcode name.
  1312. OS << getDesc().getName();
  1313. // Print the rest of the operands.
  1314. bool OmittedAnyCallClobbers = false;
  1315. bool FirstOp = true;
  1316. unsigned AsmDescOp = ~0u;
  1317. unsigned AsmOpCount = 0;
  1318. if (isInlineAsm() && e >= InlineAsm::MIOp_FirstOperand) {
  1319. // Print asm string.
  1320. OS << " ";
  1321. getOperand(InlineAsm::MIOp_AsmString).print(OS, TM);
  1322. // Print HasSideEffects, IsAlignStack
  1323. unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
  1324. if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
  1325. OS << " [sideeffect]";
  1326. if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
  1327. OS << " [alignstack]";
  1328. StartOp = AsmDescOp = InlineAsm::MIOp_FirstOperand;
  1329. FirstOp = false;
  1330. }
  1331. for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
  1332. const MachineOperand &MO = getOperand(i);
  1333. if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg()))
  1334. VirtRegs.push_back(MO.getReg());
  1335. // Omit call-clobbered registers which aren't used anywhere. This makes
  1336. // call instructions much less noisy on targets where calls clobber lots
  1337. // of registers. Don't rely on MO.isDead() because we may be called before
  1338. // LiveVariables is run, or we may be looking at a non-allocatable reg.
  1339. if (MF && isCall() &&
  1340. MO.isReg() && MO.isImplicit() && MO.isDef()) {
  1341. unsigned Reg = MO.getReg();
  1342. if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
  1343. const MachineRegisterInfo &MRI = MF->getRegInfo();
  1344. if (MRI.use_empty(Reg) && !MRI.isLiveOut(Reg)) {
  1345. bool HasAliasLive = false;
  1346. for (const unsigned *Alias = TM->getRegisterInfo()->getAliasSet(Reg);
  1347. unsigned AliasReg = *Alias; ++Alias)
  1348. if (!MRI.use_empty(AliasReg) || MRI.isLiveOut(AliasReg)) {
  1349. HasAliasLive = true;
  1350. break;
  1351. }
  1352. if (!HasAliasLive) {
  1353. OmittedAnyCallClobbers = true;
  1354. continue;
  1355. }
  1356. }
  1357. }
  1358. }
  1359. if (FirstOp) FirstOp = false; else OS << ",";
  1360. OS << " ";
  1361. if (i < getDesc().NumOperands) {
  1362. const MCOperandInfo &MCOI = getDesc().OpInfo[i];
  1363. if (MCOI.isPredicate())
  1364. OS << "pred:";
  1365. if (MCOI.isOptionalDef())
  1366. OS << "opt:";
  1367. }
  1368. if (isDebugValue() && MO.isMetadata()) {
  1369. // Pretty print DBG_VALUE instructions.
  1370. const MDNode *MD = MO.getMetadata();
  1371. if (const MDString *MDS = dyn_cast<MDString>(MD->getOperand(2)))
  1372. OS << "!\"" << MDS->getString() << '\"';
  1373. else
  1374. MO.print(OS, TM);
  1375. } else if (TM && (isInsertSubreg() || isRegSequence()) && MO.isImm()) {
  1376. OS << TM->getRegisterInfo()->getSubRegIndexName(MO.getImm());
  1377. } else if (i == AsmDescOp && MO.isImm()) {
  1378. // Pretty print the inline asm operand descriptor.
  1379. OS << '$' << AsmOpCount++;
  1380. unsigned Flag = MO.getImm();
  1381. switch (InlineAsm::getKind(Flag)) {
  1382. case InlineAsm::Kind_RegUse: OS << ":[reguse"; break;
  1383. case InlineAsm::Kind_RegDef: OS << ":[regdef"; break;
  1384. case InlineAsm::Kind_RegDefEarlyClobber: OS << ":[regdef-ec"; break;
  1385. case InlineAsm::Kind_Clobber: OS << ":[clobber"; break;
  1386. case InlineAsm::Kind_Imm: OS << ":[imm"; break;
  1387. case InlineAsm::Kind_Mem: OS << ":[mem"; break;
  1388. default: OS << ":[??" << InlineAsm::getKind(Flag); break;
  1389. }
  1390. unsigned RCID = 0;
  1391. if (InlineAsm::hasRegClassConstraint(Flag, RCID)) {
  1392. if (TM)
  1393. OS << ':' << TM->getRegisterInfo()->getRegClass(RCID)->getName();
  1394. else
  1395. OS << ":RC" << RCID;
  1396. }
  1397. unsigned TiedTo = 0;
  1398. if (InlineAsm::isUseOperandTiedToDef(Flag, TiedTo))
  1399. OS << " tiedto:$" << TiedTo;
  1400. OS << ']';
  1401. // Compute the index of the next operand descriptor.
  1402. AsmDescOp += 1 + InlineAsm::getNumOperandRegisters(Flag);
  1403. } else
  1404. MO.print(OS, TM);
  1405. }
  1406. // Briefly indicate whether any call clobbers were omitted.
  1407. if (OmittedAnyCallClobbers) {
  1408. if (!FirstOp) OS << ",";
  1409. OS << " ...";
  1410. }
  1411. bool HaveSemi = false;
  1412. if (Flags) {
  1413. if (!HaveSemi) OS << ";"; HaveSemi = true;
  1414. OS << " flags: ";
  1415. if (Flags & FrameSetup)
  1416. OS << "FrameSetup";
  1417. }
  1418. if (!memoperands_empty()) {
  1419. if (!HaveSemi) OS << ";"; HaveSemi = true;
  1420. OS << " mem:";
  1421. for (mmo_iterator i = memoperands_begin(), e = memoperands_end();
  1422. i != e; ++i) {
  1423. OS << **i;
  1424. if (llvm::next(i) != e)
  1425. OS << " ";
  1426. }
  1427. }
  1428. // Print the regclass of any virtual registers encountered.
  1429. if (MRI && !VirtRegs.empty()) {
  1430. if (!HaveSemi) OS << ";"; HaveSemi = true;
  1431. for (unsigned i = 0; i != VirtRegs.size(); ++i) {
  1432. const TargetRegisterClass *RC = MRI->getRegClass(VirtRegs[i]);
  1433. OS << " " << RC->getName() << ':' << PrintReg(VirtRegs[i]);
  1434. for (unsigned j = i+1; j != VirtRegs.size();) {
  1435. if (MRI->getRegClass(VirtRegs[j]) != RC) {
  1436. ++j;
  1437. continue;
  1438. }
  1439. if (VirtRegs[i] != VirtRegs[j])
  1440. OS << "," << PrintReg(VirtRegs[j]);
  1441. VirtRegs.erase(VirtRegs.begin()+j);
  1442. }
  1443. }
  1444. }
  1445. // Print debug location information.
  1446. if (isDebugValue() && getOperand(e - 1).isMetadata()) {
  1447. if (!HaveSemi) OS << ";"; HaveSemi = true;
  1448. DIVariable DV(getOperand(e - 1).getMetadata());
  1449. OS << " line no:" << DV.getLineNumber();
  1450. if (MDNode *InlinedAt = DV.getInlinedAt()) {
  1451. DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
  1452. if (!InlinedAtDL.isUnknown()) {
  1453. OS << " inlined @[ ";
  1454. printDebugLoc(InlinedAtDL, MF, OS);
  1455. OS << " ]";
  1456. }
  1457. }
  1458. } else if (!debugLoc.isUnknown() && MF) {
  1459. if (!HaveSemi) OS << ";"; HaveSemi = true;
  1460. OS << " dbg:";
  1461. printDebugLoc(debugLoc, MF, OS);
  1462. }
  1463. OS << '\n';
  1464. }
  1465. bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
  1466. const TargetRegisterInfo *RegInfo,
  1467. bool AddIfNotFound) {
  1468. bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
  1469. bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
  1470. bool Found = false;
  1471. SmallVector<unsigned,4> DeadOps;
  1472. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  1473. MachineOperand &MO = getOperand(i);
  1474. if (!MO.isReg() || !MO.isUse() || MO.isUndef())
  1475. continue;
  1476. unsigned Reg = MO.getReg();
  1477. if (!Reg)
  1478. continue;
  1479. if (Reg == IncomingReg) {
  1480. if (!Found) {
  1481. if (MO.isKill())
  1482. // The register is already marked kill.
  1483. return true;
  1484. if (isPhysReg && isRegTiedToDefOperand(i))
  1485. // Two-address uses of physregs must not be marked kill.
  1486. return true;
  1487. MO.setIsKill();
  1488. Found = true;
  1489. }
  1490. } else if (hasAliases && MO.isKill() &&
  1491. TargetRegisterInfo::isPhysicalRegister(Reg)) {
  1492. // A super-register kill already exists.
  1493. if (RegInfo->isSuperRegister(IncomingReg, Reg))
  1494. return true;
  1495. if (RegInfo->isSubRegister(IncomingReg, Reg))
  1496. DeadOps.push_back(i);
  1497. }
  1498. }
  1499. // Trim unneeded kill operands.
  1500. while (!DeadOps.empty()) {
  1501. unsigned OpIdx = DeadOps.back();
  1502. if (getOperand(OpIdx).isImplicit())
  1503. RemoveOperand(OpIdx);
  1504. else
  1505. getOperand(OpIdx).setIsKill(false);
  1506. DeadOps.pop_back();
  1507. }
  1508. // If not found, this means an alias of one of the operands is killed. Add a
  1509. // new implicit operand if required.
  1510. if (!Found && AddIfNotFound) {
  1511. addOperand(MachineOperand::CreateReg(IncomingReg,
  1512. false /*IsDef*/,
  1513. true /*IsImp*/,
  1514. true /*IsKill*/));
  1515. return true;
  1516. }
  1517. return Found;
  1518. }
  1519. bool MachineInstr::addRegisterDead(unsigned IncomingReg,
  1520. const TargetRegisterInfo *RegInfo,
  1521. bool AddIfNotFound) {
  1522. bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
  1523. bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg);
  1524. bool Found = false;
  1525. SmallVector<unsigned,4> DeadOps;
  1526. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  1527. MachineOperand &MO = getOperand(i);
  1528. if (!MO.isReg() || !MO.isDef())
  1529. continue;
  1530. unsigned Reg = MO.getReg();
  1531. if (!Reg)
  1532. continue;
  1533. if (Reg == IncomingReg) {
  1534. MO.setIsDead();
  1535. Found = true;
  1536. } else if (hasAliases && MO.isDead() &&
  1537. TargetRegisterInfo::isPhysicalRegister(Reg)) {
  1538. // There exists a super-register that's marked dead.
  1539. if (RegInfo->isSuperRegister(IncomingReg, Reg))
  1540. return true;
  1541. if (RegInfo->getSubRegisters(IncomingReg) &&
  1542. RegInfo->getSuperRegisters(Reg) &&
  1543. RegInfo->isSubRegister(IncomingReg, Reg))
  1544. DeadOps.push_back(i);
  1545. }
  1546. }
  1547. // Trim unneeded dead operands.
  1548. while (!DeadOps.empty()) {
  1549. unsigned OpIdx = DeadOps.back();
  1550. if (getOperand(OpIdx).isImplicit())
  1551. RemoveOperand(OpIdx);
  1552. else
  1553. getOperand(OpIdx).setIsDead(false);
  1554. DeadOps.pop_back();
  1555. }
  1556. // If not found, this means an alias of one of the operands is dead. Add a
  1557. // new implicit operand if required.
  1558. if (Found || !AddIfNotFound)
  1559. return Found;
  1560. addOperand(MachineOperand::CreateReg(IncomingReg,
  1561. true /*IsDef*/,
  1562. true /*IsImp*/,
  1563. false /*IsKill*/,
  1564. true /*IsDead*/));
  1565. return true;
  1566. }
  1567. void MachineInstr::addRegisterDefined(unsigned IncomingReg,
  1568. const TargetRegisterInfo *RegInfo) {
  1569. if (TargetRegisterInfo::isPhysicalRegister(IncomingReg)) {
  1570. MachineOperand *MO = findRegisterDefOperand(IncomingReg, false, RegInfo);
  1571. if (MO)
  1572. return;
  1573. } else {
  1574. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  1575. const MachineOperand &MO = getOperand(i);
  1576. if (MO.isReg() && MO.getReg() == IncomingReg && MO.isDef() &&
  1577. MO.getSubReg() == 0)
  1578. return;
  1579. }
  1580. }
  1581. addOperand(MachineOperand::CreateReg(IncomingReg,
  1582. true /*IsDef*/,
  1583. true /*IsImp*/));
  1584. }
  1585. void MachineInstr::setPhysRegsDeadExcept(const SmallVectorImpl<unsigned> &UsedRegs,
  1586. const TargetRegisterInfo &TRI) {
  1587. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  1588. MachineOperand &MO = getOperand(i);
  1589. if (!MO.isReg() || !MO.isDef()) continue;
  1590. unsigned Reg = MO.getReg();
  1591. if (Reg == 0) continue;
  1592. bool Dead = true;
  1593. for (SmallVectorImpl<unsigned>::const_iterator I = UsedRegs.begin(),
  1594. E = UsedRegs.end(); I != E; ++I)
  1595. if (TRI.regsOverlap(*I, Reg)) {
  1596. Dead = false;
  1597. break;
  1598. }
  1599. // If there are no uses, including partial uses, the def is dead.
  1600. if (Dead) MO.setIsDead();
  1601. }
  1602. }
  1603. unsigned
  1604. MachineInstrExpressionTrait::getHashValue(const MachineInstr* const &MI) {
  1605. unsigned Hash = MI->getOpcode() * 37;
  1606. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  1607. const MachineOperand &MO = MI->getOperand(i);
  1608. uint64_t Key = (uint64_t)MO.getType() << 32;
  1609. switch (MO.getType()) {
  1610. default: break;
  1611. case MachineOperand::MO_Register:
  1612. if (MO.isDef() && TargetRegisterInfo::isVirtualRegister(MO.getReg()))
  1613. continue; // Skip virtual register defs.
  1614. Key |= MO.getReg();
  1615. break;
  1616. case MachineOperand::MO_Immediate:
  1617. Key |= MO.getImm();
  1618. break;
  1619. case MachineOperand::MO_FrameIndex:
  1620. case MachineOperand::MO_ConstantPoolIndex:
  1621. case MachineOperand::MO_JumpTableIndex:
  1622. Key |= MO.getIndex();
  1623. break;
  1624. case MachineOperand::MO_MachineBasicBlock:
  1625. Key |= DenseMapInfo<void*>::getHashValue(MO.getMBB());
  1626. break;
  1627. case MachineOperand::MO_GlobalAddress:
  1628. Key |= DenseMapInfo<void*>::getHashValue(MO.getGlobal());
  1629. break;
  1630. case MachineOperand::MO_BlockAddress:
  1631. Key |= DenseMapInfo<void*>::getHashValue(MO.getBlockAddress());
  1632. break;
  1633. case MachineOperand::MO_MCSymbol:
  1634. Key |= DenseMapInfo<void*>::getHashValue(MO.getMCSymbol());
  1635. break;
  1636. }
  1637. Key += ~(Key << 32);
  1638. Key ^= (Key >> 22);
  1639. Key += ~(Key << 13);
  1640. Key ^= (Key >> 8);
  1641. Key += (Key << 3);
  1642. Key ^= (Key >> 15);
  1643. Key += ~(Key << 27);
  1644. Key ^= (Key >> 31);
  1645. Hash = (unsigned)Key + Hash * 37;
  1646. }
  1647. return Hash;
  1648. }
  1649. void MachineInstr::emitError(StringRef Msg) const {
  1650. // Find the source location cookie.
  1651. unsigned LocCookie = 0;
  1652. const MDNode *LocMD = 0;
  1653. for (unsigned i = getNumOperands(); i != 0; --i) {
  1654. if (getOperand(i-1).isMetadata() &&
  1655. (LocMD = getOperand(i-1).getMetadata()) &&
  1656. LocMD->getNumOperands() != 0) {
  1657. if (const ConstantInt *CI = dyn_cast<ConstantInt>(LocMD->getOperand(0))) {
  1658. LocCookie = CI->getZExtValue();
  1659. break;
  1660. }
  1661. }
  1662. }
  1663. if (const MachineBasicBlock *MBB = getParent())
  1664. if (const MachineFunction *MF = MBB->getParent())
  1665. return MF->getMMI().getModule()->getContext().emitError(LocCookie, Msg);
  1666. report_fatal_error(Msg);
  1667. }