Instruction.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. //===-- Instruction.cpp - Implement the Instruction class -----------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the Instruction class for the IR library.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/IR/Instruction.h"
  14. #include "llvm/IR/CallSite.h"
  15. #include "llvm/IR/Constants.h"
  16. #include "llvm/IR/Instructions.h"
  17. #include "llvm/IR/Module.h"
  18. #include "llvm/IR/Operator.h"
  19. #include "llvm/IR/Type.h"
  20. using namespace llvm;
  21. Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
  22. Instruction *InsertBefore)
  23. : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {
  24. // If requested, insert this instruction into a basic block...
  25. if (InsertBefore) {
  26. BasicBlock *BB = InsertBefore->getParent();
  27. assert(BB && "Instruction to insert before is not in a basic block!");
  28. BB->getInstList().insert(InsertBefore, this);
  29. }
  30. }
  31. Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
  32. BasicBlock *InsertAtEnd)
  33. : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {
  34. // append this instruction into the basic block
  35. assert(InsertAtEnd && "Basic block to append to may not be NULL!");
  36. InsertAtEnd->getInstList().push_back(this);
  37. }
  38. // Out of line virtual method, so the vtable, etc has a home.
  39. Instruction::~Instruction() {
  40. assert(!Parent && "Instruction still linked in the program!");
  41. if (hasMetadataHashEntry())
  42. clearMetadataHashEntries();
  43. }
  44. void Instruction::setParent(BasicBlock *P) {
  45. Parent = P;
  46. }
  47. const Module *Instruction::getModule() const {
  48. return getParent()->getModule();
  49. }
  50. Module *Instruction::getModule() {
  51. return getParent()->getModule();
  52. }
  53. void Instruction::removeFromParent() {
  54. getParent()->getInstList().remove(this);
  55. }
  56. iplist<Instruction>::iterator Instruction::eraseFromParent() {
  57. return getParent()->getInstList().erase(this);
  58. }
  59. /// insertBefore - Insert an unlinked instructions into a basic block
  60. /// immediately before the specified instruction.
  61. void Instruction::insertBefore(Instruction *InsertPos) {
  62. InsertPos->getParent()->getInstList().insert(InsertPos, this);
  63. }
  64. /// insertAfter - Insert an unlinked instructions into a basic block
  65. /// immediately after the specified instruction.
  66. void Instruction::insertAfter(Instruction *InsertPos) {
  67. InsertPos->getParent()->getInstList().insertAfter(InsertPos, this);
  68. }
  69. /// moveBefore - Unlink this instruction from its current basic block and
  70. /// insert it into the basic block that MovePos lives in, right before
  71. /// MovePos.
  72. void Instruction::moveBefore(Instruction *MovePos) {
  73. MovePos->getParent()->getInstList().splice(MovePos,getParent()->getInstList(),
  74. this);
  75. }
  76. /// Set or clear the unsafe-algebra flag on this instruction, which must be an
  77. /// operator which supports this flag. See LangRef.html for the meaning of this
  78. /// flag.
  79. void Instruction::setHasUnsafeAlgebra(bool B) {
  80. assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
  81. cast<FPMathOperator>(this)->setHasUnsafeAlgebra(B);
  82. }
  83. /// Set or clear the NoNaNs flag on this instruction, which must be an operator
  84. /// which supports this flag. See LangRef.html for the meaning of this flag.
  85. void Instruction::setHasNoNaNs(bool B) {
  86. assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
  87. cast<FPMathOperator>(this)->setHasNoNaNs(B);
  88. }
  89. /// Set or clear the no-infs flag on this instruction, which must be an operator
  90. /// which supports this flag. See LangRef.html for the meaning of this flag.
  91. void Instruction::setHasNoInfs(bool B) {
  92. assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
  93. cast<FPMathOperator>(this)->setHasNoInfs(B);
  94. }
  95. /// Set or clear the no-signed-zeros flag on this instruction, which must be an
  96. /// operator which supports this flag. See LangRef.html for the meaning of this
  97. /// flag.
  98. void Instruction::setHasNoSignedZeros(bool B) {
  99. assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
  100. cast<FPMathOperator>(this)->setHasNoSignedZeros(B);
  101. }
  102. /// Set or clear the allow-reciprocal flag on this instruction, which must be an
  103. /// operator which supports this flag. See LangRef.html for the meaning of this
  104. /// flag.
  105. void Instruction::setHasAllowReciprocal(bool B) {
  106. assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
  107. cast<FPMathOperator>(this)->setHasAllowReciprocal(B);
  108. }
  109. /// Convenience function for setting all the fast-math flags on this
  110. /// instruction, which must be an operator which supports these flags. See
  111. /// LangRef.html for the meaning of these flats.
  112. void Instruction::setFastMathFlags(FastMathFlags FMF) {
  113. assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
  114. cast<FPMathOperator>(this)->setFastMathFlags(FMF);
  115. }
  116. void Instruction::copyFastMathFlags(FastMathFlags FMF) {
  117. assert(isa<FPMathOperator>(this) && "copying fast-math flag on invalid op");
  118. cast<FPMathOperator>(this)->copyFastMathFlags(FMF);
  119. }
  120. /// Determine whether the unsafe-algebra flag is set.
  121. bool Instruction::hasUnsafeAlgebra() const {
  122. assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
  123. return cast<FPMathOperator>(this)->hasUnsafeAlgebra();
  124. }
  125. /// Determine whether the no-NaNs flag is set.
  126. bool Instruction::hasNoNaNs() const {
  127. assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
  128. return cast<FPMathOperator>(this)->hasNoNaNs();
  129. }
  130. /// Determine whether the no-infs flag is set.
  131. bool Instruction::hasNoInfs() const {
  132. assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
  133. return cast<FPMathOperator>(this)->hasNoInfs();
  134. }
  135. /// Determine whether the no-signed-zeros flag is set.
  136. bool Instruction::hasNoSignedZeros() const {
  137. assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
  138. return cast<FPMathOperator>(this)->hasNoSignedZeros();
  139. }
  140. /// Determine whether the allow-reciprocal flag is set.
  141. bool Instruction::hasAllowReciprocal() const {
  142. assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
  143. return cast<FPMathOperator>(this)->hasAllowReciprocal();
  144. }
  145. /// Convenience function for getting all the fast-math flags, which must be an
  146. /// operator which supports these flags. See LangRef.html for the meaning of
  147. /// these flags.
  148. FastMathFlags Instruction::getFastMathFlags() const {
  149. assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
  150. return cast<FPMathOperator>(this)->getFastMathFlags();
  151. }
  152. /// Copy I's fast-math flags
  153. void Instruction::copyFastMathFlags(const Instruction *I) {
  154. copyFastMathFlags(I->getFastMathFlags());
  155. }
  156. const char *Instruction::getOpcodeName(unsigned OpCode) {
  157. switch (OpCode) {
  158. // Terminators
  159. case Ret: return "ret";
  160. case Br: return "br";
  161. case Switch: return "switch";
  162. case IndirectBr: return "indirectbr";
  163. case Invoke: return "invoke";
  164. case Resume: return "resume";
  165. case Unreachable: return "unreachable";
  166. // Standard binary operators...
  167. case Add: return "add";
  168. case FAdd: return "fadd";
  169. case Sub: return "sub";
  170. case FSub: return "fsub";
  171. case Mul: return "mul";
  172. case FMul: return "fmul";
  173. case UDiv: return "udiv";
  174. case SDiv: return "sdiv";
  175. case FDiv: return "fdiv";
  176. case URem: return "urem";
  177. case SRem: return "srem";
  178. case FRem: return "frem";
  179. // Logical operators...
  180. case And: return "and";
  181. case Or : return "or";
  182. case Xor: return "xor";
  183. // Memory instructions...
  184. case Alloca: return "alloca";
  185. case Load: return "load";
  186. case Store: return "store";
  187. case AtomicCmpXchg: return "cmpxchg";
  188. case AtomicRMW: return "atomicrmw";
  189. case Fence: return "fence";
  190. case GetElementPtr: return "getelementptr";
  191. // Convert instructions...
  192. case Trunc: return "trunc";
  193. case ZExt: return "zext";
  194. case SExt: return "sext";
  195. case FPTrunc: return "fptrunc";
  196. case FPExt: return "fpext";
  197. case FPToUI: return "fptoui";
  198. case FPToSI: return "fptosi";
  199. case UIToFP: return "uitofp";
  200. case SIToFP: return "sitofp";
  201. case IntToPtr: return "inttoptr";
  202. case PtrToInt: return "ptrtoint";
  203. case BitCast: return "bitcast";
  204. case AddrSpaceCast: return "addrspacecast";
  205. // Other instructions...
  206. case ICmp: return "icmp";
  207. case FCmp: return "fcmp";
  208. case PHI: return "phi";
  209. case Select: return "select";
  210. case Call: return "call";
  211. case Shl: return "shl";
  212. case LShr: return "lshr";
  213. case AShr: return "ashr";
  214. case VAArg: return "va_arg";
  215. case ExtractElement: return "extractelement";
  216. case InsertElement: return "insertelement";
  217. case ShuffleVector: return "shufflevector";
  218. case ExtractValue: return "extractvalue";
  219. case InsertValue: return "insertvalue";
  220. case LandingPad: return "landingpad";
  221. default: return "<Invalid operator> ";
  222. }
  223. }
  224. /// Return true if both instructions have the same special state
  225. /// This must be kept in sync with lib/Transforms/IPO/MergeFunctions.cpp.
  226. static bool haveSameSpecialState(const Instruction *I1, const Instruction *I2,
  227. bool IgnoreAlignment = false) {
  228. assert(I1->getOpcode() == I2->getOpcode() &&
  229. "Can not compare special state of different instructions");
  230. if (const LoadInst *LI = dyn_cast<LoadInst>(I1))
  231. return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() &&
  232. (LI->getAlignment() == cast<LoadInst>(I2)->getAlignment() ||
  233. IgnoreAlignment) &&
  234. LI->getOrdering() == cast<LoadInst>(I2)->getOrdering() &&
  235. LI->getSynchScope() == cast<LoadInst>(I2)->getSynchScope();
  236. if (const StoreInst *SI = dyn_cast<StoreInst>(I1))
  237. return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() &&
  238. (SI->getAlignment() == cast<StoreInst>(I2)->getAlignment() ||
  239. IgnoreAlignment) &&
  240. SI->getOrdering() == cast<StoreInst>(I2)->getOrdering() &&
  241. SI->getSynchScope() == cast<StoreInst>(I2)->getSynchScope();
  242. if (const CmpInst *CI = dyn_cast<CmpInst>(I1))
  243. return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate();
  244. if (const CallInst *CI = dyn_cast<CallInst>(I1))
  245. return CI->isTailCall() == cast<CallInst>(I2)->isTailCall() &&
  246. CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() &&
  247. CI->getAttributes() == cast<CallInst>(I2)->getAttributes();
  248. if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1))
  249. return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() &&
  250. CI->getAttributes() ==
  251. cast<InvokeInst>(I2)->getAttributes();
  252. if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1))
  253. return IVI->getIndices() == cast<InsertValueInst>(I2)->getIndices();
  254. if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1))
  255. return EVI->getIndices() == cast<ExtractValueInst>(I2)->getIndices();
  256. if (const FenceInst *FI = dyn_cast<FenceInst>(I1))
  257. return FI->getOrdering() == cast<FenceInst>(I2)->getOrdering() &&
  258. FI->getSynchScope() == cast<FenceInst>(I2)->getSynchScope();
  259. if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I1))
  260. return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I2)->isVolatile() &&
  261. CXI->isWeak() == cast<AtomicCmpXchgInst>(I2)->isWeak() &&
  262. CXI->getSuccessOrdering() ==
  263. cast<AtomicCmpXchgInst>(I2)->getSuccessOrdering() &&
  264. CXI->getFailureOrdering() ==
  265. cast<AtomicCmpXchgInst>(I2)->getFailureOrdering() &&
  266. CXI->getSynchScope() == cast<AtomicCmpXchgInst>(I2)->getSynchScope();
  267. if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I1))
  268. return RMWI->getOperation() == cast<AtomicRMWInst>(I2)->getOperation() &&
  269. RMWI->isVolatile() == cast<AtomicRMWInst>(I2)->isVolatile() &&
  270. RMWI->getOrdering() == cast<AtomicRMWInst>(I2)->getOrdering() &&
  271. RMWI->getSynchScope() == cast<AtomicRMWInst>(I2)->getSynchScope();
  272. return true;
  273. }
  274. /// isIdenticalTo - Return true if the specified instruction is exactly
  275. /// identical to the current one. This means that all operands match and any
  276. /// extra information (e.g. load is volatile) agree.
  277. bool Instruction::isIdenticalTo(const Instruction *I) const {
  278. return isIdenticalToWhenDefined(I) &&
  279. SubclassOptionalData == I->SubclassOptionalData;
  280. }
  281. /// isIdenticalToWhenDefined - This is like isIdenticalTo, except that it
  282. /// ignores the SubclassOptionalData flags, which specify conditions
  283. /// under which the instruction's result is undefined.
  284. bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const {
  285. if (getOpcode() != I->getOpcode() ||
  286. getNumOperands() != I->getNumOperands() ||
  287. getType() != I->getType())
  288. return false;
  289. // If both instructions have no operands, they are identical.
  290. if (getNumOperands() == 0 && I->getNumOperands() == 0)
  291. return haveSameSpecialState(this, I);
  292. // We have two instructions of identical opcode and #operands. Check to see
  293. // if all operands are the same.
  294. if (!std::equal(op_begin(), op_end(), I->op_begin()))
  295. return false;
  296. if (const PHINode *thisPHI = dyn_cast<PHINode>(this)) {
  297. const PHINode *otherPHI = cast<PHINode>(I);
  298. return std::equal(thisPHI->block_begin(), thisPHI->block_end(),
  299. otherPHI->block_begin());
  300. }
  301. return haveSameSpecialState(this, I);
  302. }
  303. // isSameOperationAs
  304. // This should be kept in sync with isEquivalentOperation in
  305. // lib/Transforms/IPO/MergeFunctions.cpp.
  306. bool Instruction::isSameOperationAs(const Instruction *I,
  307. unsigned flags) const {
  308. bool IgnoreAlignment = flags & CompareIgnoringAlignment;
  309. bool UseScalarTypes = flags & CompareUsingScalarTypes;
  310. if (getOpcode() != I->getOpcode() ||
  311. getNumOperands() != I->getNumOperands() ||
  312. (UseScalarTypes ?
  313. getType()->getScalarType() != I->getType()->getScalarType() :
  314. getType() != I->getType()))
  315. return false;
  316. // We have two instructions of identical opcode and #operands. Check to see
  317. // if all operands are the same type
  318. for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
  319. if (UseScalarTypes ?
  320. getOperand(i)->getType()->getScalarType() !=
  321. I->getOperand(i)->getType()->getScalarType() :
  322. getOperand(i)->getType() != I->getOperand(i)->getType())
  323. return false;
  324. return haveSameSpecialState(this, I, IgnoreAlignment);
  325. }
  326. /// isUsedOutsideOfBlock - Return true if there are any uses of I outside of the
  327. /// specified block. Note that PHI nodes are considered to evaluate their
  328. /// operands in the corresponding predecessor block.
  329. bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
  330. for (const Use &U : uses()) {
  331. // PHI nodes uses values in the corresponding predecessor block. For other
  332. // instructions, just check to see whether the parent of the use matches up.
  333. const Instruction *I = cast<Instruction>(U.getUser());
  334. const PHINode *PN = dyn_cast<PHINode>(I);
  335. if (!PN) {
  336. if (I->getParent() != BB)
  337. return true;
  338. continue;
  339. }
  340. if (PN->getIncomingBlock(U) != BB)
  341. return true;
  342. }
  343. return false;
  344. }
  345. /// mayReadFromMemory - Return true if this instruction may read memory.
  346. ///
  347. bool Instruction::mayReadFromMemory() const {
  348. switch (getOpcode()) {
  349. default: return false;
  350. case Instruction::VAArg:
  351. case Instruction::Load:
  352. case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory
  353. case Instruction::AtomicCmpXchg:
  354. case Instruction::AtomicRMW:
  355. return true;
  356. case Instruction::Call:
  357. return !cast<CallInst>(this)->doesNotAccessMemory();
  358. case Instruction::Invoke:
  359. return !cast<InvokeInst>(this)->doesNotAccessMemory();
  360. case Instruction::Store:
  361. return !cast<StoreInst>(this)->isUnordered();
  362. }
  363. }
  364. /// mayWriteToMemory - Return true if this instruction may modify memory.
  365. ///
  366. bool Instruction::mayWriteToMemory() const {
  367. switch (getOpcode()) {
  368. default: return false;
  369. case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory
  370. case Instruction::Store:
  371. case Instruction::VAArg:
  372. case Instruction::AtomicCmpXchg:
  373. case Instruction::AtomicRMW:
  374. return true;
  375. case Instruction::Call:
  376. return !cast<CallInst>(this)->onlyReadsMemory();
  377. case Instruction::Invoke:
  378. return !cast<InvokeInst>(this)->onlyReadsMemory();
  379. case Instruction::Load:
  380. return !cast<LoadInst>(this)->isUnordered();
  381. }
  382. }
  383. bool Instruction::isAtomic() const {
  384. switch (getOpcode()) {
  385. default:
  386. return false;
  387. case Instruction::AtomicCmpXchg:
  388. case Instruction::AtomicRMW:
  389. case Instruction::Fence:
  390. return true;
  391. case Instruction::Load:
  392. return cast<LoadInst>(this)->getOrdering() != NotAtomic;
  393. case Instruction::Store:
  394. return cast<StoreInst>(this)->getOrdering() != NotAtomic;
  395. }
  396. }
  397. bool Instruction::mayThrow() const {
  398. if (const CallInst *CI = dyn_cast<CallInst>(this))
  399. return !CI->doesNotThrow();
  400. return isa<ResumeInst>(this);
  401. }
  402. bool Instruction::mayReturn() const {
  403. if (const CallInst *CI = dyn_cast<CallInst>(this))
  404. return !CI->doesNotReturn();
  405. return true;
  406. }
  407. /// isAssociative - Return true if the instruction is associative:
  408. ///
  409. /// Associative operators satisfy: x op (y op z) === (x op y) op z
  410. ///
  411. /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative.
  412. ///
  413. bool Instruction::isAssociative(unsigned Opcode) {
  414. return Opcode == And || Opcode == Or || Opcode == Xor ||
  415. Opcode == Add || Opcode == Mul;
  416. }
  417. bool Instruction::isAssociative() const {
  418. unsigned Opcode = getOpcode();
  419. if (isAssociative(Opcode))
  420. return true;
  421. switch (Opcode) {
  422. case FMul:
  423. case FAdd:
  424. return cast<FPMathOperator>(this)->hasUnsafeAlgebra();
  425. default:
  426. return false;
  427. }
  428. }
  429. /// isCommutative - Return true if the instruction is commutative:
  430. ///
  431. /// Commutative operators satisfy: (x op y) === (y op x)
  432. ///
  433. /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
  434. /// applied to any type.
  435. ///
  436. bool Instruction::isCommutative(unsigned op) {
  437. switch (op) {
  438. case Add:
  439. case FAdd:
  440. case Mul:
  441. case FMul:
  442. case And:
  443. case Or:
  444. case Xor:
  445. return true;
  446. default:
  447. return false;
  448. }
  449. }
  450. /// isIdempotent - Return true if the instruction is idempotent:
  451. ///
  452. /// Idempotent operators satisfy: x op x === x
  453. ///
  454. /// In LLVM, the And and Or operators are idempotent.
  455. ///
  456. bool Instruction::isIdempotent(unsigned Opcode) {
  457. return Opcode == And || Opcode == Or;
  458. }
  459. /// isNilpotent - Return true if the instruction is nilpotent:
  460. ///
  461. /// Nilpotent operators satisfy: x op x === Id,
  462. ///
  463. /// where Id is the identity for the operator, i.e. a constant such that
  464. /// x op Id === x and Id op x === x for all x.
  465. ///
  466. /// In LLVM, the Xor operator is nilpotent.
  467. ///
  468. bool Instruction::isNilpotent(unsigned Opcode) {
  469. return Opcode == Xor;
  470. }
  471. Instruction *Instruction::cloneImpl() const {
  472. llvm_unreachable("Subclass of Instruction failed to implement cloneImpl");
  473. }
  474. Instruction *Instruction::clone() const {
  475. Instruction *New = nullptr;
  476. switch (getOpcode()) {
  477. default:
  478. llvm_unreachable("Unhandled Opcode.");
  479. #define HANDLE_INST(num, opc, clas) \
  480. case Instruction::opc: \
  481. New = cast<clas>(this)->cloneImpl(); \
  482. break;
  483. #include "llvm/IR/Instruction.def"
  484. #undef HANDLE_INST
  485. }
  486. New->SubclassOptionalData = SubclassOptionalData;
  487. if (!hasMetadata())
  488. return New;
  489. // Otherwise, enumerate and copy over metadata from the old instruction to the
  490. // new one.
  491. SmallVector<std::pair<unsigned, MDNode *>, 4> TheMDs;
  492. getAllMetadataOtherThanDebugLoc(TheMDs);
  493. for (const auto &MD : TheMDs)
  494. New->setMetadata(MD.first, MD.second);
  495. New->setDebugLoc(getDebugLoc());
  496. return New;
  497. }