MachineInstr.cpp 66 KB

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