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 
14 #include "llvm/IR/Instruction.h"
15 #include "llvm/ADT/DenseSet.h"
16 #include "llvm/IR/CallSite.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/Instructions.h"
19 #include "llvm/IR/MDBuilder.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/IR/Operator.h"
22 #include "llvm/IR/Type.h"
23 using namespace llvm;
24 
25 Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
26                          Instruction *InsertBefore)
27   : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {
28 
29   // If requested, insert this instruction into a basic block...
30   if (InsertBefore) {
31     BasicBlock *BB = InsertBefore->getParent();
32     assert(BB && "Instruction to insert before is not in a basic block!");
33     BB->getInstList().insert(InsertBefore->getIterator(), this);
34   }
35 }
36 
37 Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
38                          BasicBlock *InsertAtEnd)
39   : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {
40 
41   // append this instruction into the basic block
42   assert(InsertAtEnd && "Basic block to append to may not be NULL!");
43   InsertAtEnd->getInstList().push_back(this);
44 }
45 
46 Instruction::~Instruction() {
47   assert(!Parent && "Instruction still linked in the program!");
48   if (hasMetadataHashEntry())
49     clearMetadataHashEntries();
50 }
51 
52 
53 void Instruction::setParent(BasicBlock *P) {
54   Parent = P;
55 }
56 
57 const Module *Instruction::getModule() const {
58   return getParent()->getModule();
59 }
60 
61 const Function *Instruction::getFunction() const {
62   return getParent()->getParent();
63 }
64 
65 void Instruction::removeFromParent() {
66   getParent()->getInstList().remove(getIterator());
67 }
68 
69 iplist<Instruction>::iterator Instruction::eraseFromParent() {
70   return getParent()->getInstList().erase(getIterator());
71 }
72 
73 /// Insert an unlinked instruction into a basic block immediately before the
74 /// specified instruction.
75 void Instruction::insertBefore(Instruction *InsertPos) {
76   InsertPos->getParent()->getInstList().insert(InsertPos->getIterator(), this);
77 }
78 
79 /// Insert an unlinked instruction into a basic block immediately after the
80 /// specified instruction.
81 void Instruction::insertAfter(Instruction *InsertPos) {
82   InsertPos->getParent()->getInstList().insertAfter(InsertPos->getIterator(),
83                                                     this);
84 }
85 
86 /// Unlink this instruction from its current basic block and insert it into the
87 /// basic block that MovePos lives in, right before MovePos.
88 void Instruction::moveBefore(Instruction *MovePos) {
89   moveBefore(*MovePos->getParent(), MovePos->getIterator());
90 }
91 
92 void Instruction::moveBefore(BasicBlock &BB,
93                              SymbolTableList<Instruction>::iterator I) {
94   assert(I == BB.end() || I->getParent() == &BB);
95   BB.getInstList().splice(I, getParent()->getInstList(), getIterator());
96 }
97 
98 void Instruction::setHasNoUnsignedWrap(bool b) {
99   cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
100 }
101 
102 void Instruction::setHasNoSignedWrap(bool b) {
103   cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
104 }
105 
106 void Instruction::setIsExact(bool b) {
107   cast<PossiblyExactOperator>(this)->setIsExact(b);
108 }
109 
110 bool Instruction::hasNoUnsignedWrap() const {
111   return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
112 }
113 
114 bool Instruction::hasNoSignedWrap() const {
115   return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
116 }
117 
118 void Instruction::dropPoisonGeneratingFlags() {
119   switch (getOpcode()) {
120   case Instruction::Add:
121   case Instruction::Sub:
122   case Instruction::Mul:
123   case Instruction::Shl:
124     cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(false);
125     cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(false);
126     break;
127 
128   case Instruction::UDiv:
129   case Instruction::SDiv:
130   case Instruction::AShr:
131   case Instruction::LShr:
132     cast<PossiblyExactOperator>(this)->setIsExact(false);
133     break;
134 
135   case Instruction::GetElementPtr:
136     cast<GetElementPtrInst>(this)->setIsInBounds(false);
137     break;
138   }
139 }
140 
141 bool Instruction::isExact() const {
142   return cast<PossiblyExactOperator>(this)->isExact();
143 }
144 
145 void Instruction::setHasUnsafeAlgebra(bool B) {
146   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
147   cast<FPMathOperator>(this)->setHasUnsafeAlgebra(B);
148 }
149 
150 void Instruction::setHasNoNaNs(bool B) {
151   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
152   cast<FPMathOperator>(this)->setHasNoNaNs(B);
153 }
154 
155 void Instruction::setHasNoInfs(bool B) {
156   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
157   cast<FPMathOperator>(this)->setHasNoInfs(B);
158 }
159 
160 void Instruction::setHasNoSignedZeros(bool B) {
161   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
162   cast<FPMathOperator>(this)->setHasNoSignedZeros(B);
163 }
164 
165 void Instruction::setHasAllowReciprocal(bool B) {
166   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
167   cast<FPMathOperator>(this)->setHasAllowReciprocal(B);
168 }
169 
170 void Instruction::setFastMathFlags(FastMathFlags FMF) {
171   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
172   cast<FPMathOperator>(this)->setFastMathFlags(FMF);
173 }
174 
175 void Instruction::copyFastMathFlags(FastMathFlags FMF) {
176   assert(isa<FPMathOperator>(this) && "copying fast-math flag on invalid op");
177   cast<FPMathOperator>(this)->copyFastMathFlags(FMF);
178 }
179 
180 bool Instruction::hasUnsafeAlgebra() const {
181   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
182   return cast<FPMathOperator>(this)->hasUnsafeAlgebra();
183 }
184 
185 bool Instruction::hasNoNaNs() const {
186   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
187   return cast<FPMathOperator>(this)->hasNoNaNs();
188 }
189 
190 bool Instruction::hasNoInfs() const {
191   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
192   return cast<FPMathOperator>(this)->hasNoInfs();
193 }
194 
195 bool Instruction::hasNoSignedZeros() const {
196   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
197   return cast<FPMathOperator>(this)->hasNoSignedZeros();
198 }
199 
200 bool Instruction::hasAllowReciprocal() const {
201   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
202   return cast<FPMathOperator>(this)->hasAllowReciprocal();
203 }
204 
205 bool Instruction::hasAllowContract() const {
206   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
207   return cast<FPMathOperator>(this)->hasAllowContract();
208 }
209 
210 FastMathFlags Instruction::getFastMathFlags() const {
211   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
212   return cast<FPMathOperator>(this)->getFastMathFlags();
213 }
214 
215 void Instruction::copyFastMathFlags(const Instruction *I) {
216   copyFastMathFlags(I->getFastMathFlags());
217 }
218 
219 void Instruction::copyIRFlags(const Value *V, bool IncludeWrapFlags) {
220   // Copy the wrapping flags.
221   if (IncludeWrapFlags && isa<OverflowingBinaryOperator>(this)) {
222     if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
223       setHasNoSignedWrap(OB->hasNoSignedWrap());
224       setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
225     }
226   }
227 
228   // Copy the exact flag.
229   if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
230     if (isa<PossiblyExactOperator>(this))
231       setIsExact(PE->isExact());
232 
233   // Copy the fast-math flags.
234   if (auto *FP = dyn_cast<FPMathOperator>(V))
235     if (isa<FPMathOperator>(this))
236       copyFastMathFlags(FP->getFastMathFlags());
237 
238   if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V))
239     if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this))
240       DestGEP->setIsInBounds(SrcGEP->isInBounds() | DestGEP->isInBounds());
241 }
242 
243 void Instruction::andIRFlags(const Value *V) {
244   if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
245     if (isa<OverflowingBinaryOperator>(this)) {
246       setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
247       setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
248     }
249   }
250 
251   if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
252     if (isa<PossiblyExactOperator>(this))
253       setIsExact(isExact() & PE->isExact());
254 
255   if (auto *FP = dyn_cast<FPMathOperator>(V)) {
256     if (isa<FPMathOperator>(this)) {
257       FastMathFlags FM = getFastMathFlags();
258       FM &= FP->getFastMathFlags();
259       copyFastMathFlags(FM);
260     }
261   }
262 
263   if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V))
264     if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this))
265       DestGEP->setIsInBounds(SrcGEP->isInBounds() & DestGEP->isInBounds());
266 }
267 
268 const char *Instruction::getOpcodeName(unsigned OpCode) {
269   switch (OpCode) {
270   // Terminators
271   case Ret:    return "ret";
272   case Br:     return "br";
273   case Switch: return "switch";
274   case IndirectBr: return "indirectbr";
275   case Invoke: return "invoke";
276   case Resume: return "resume";
277   case Unreachable: return "unreachable";
278   case CleanupRet: return "cleanupret";
279   case CatchRet: return "catchret";
280   case CatchPad: return "catchpad";
281   case CatchSwitch: return "catchswitch";
282 
283   // Standard binary operators...
284   case Add: return "add";
285   case FAdd: return "fadd";
286   case Sub: return "sub";
287   case FSub: return "fsub";
288   case Mul: return "mul";
289   case FMul: return "fmul";
290   case UDiv: return "udiv";
291   case SDiv: return "sdiv";
292   case FDiv: return "fdiv";
293   case URem: return "urem";
294   case SRem: return "srem";
295   case FRem: return "frem";
296 
297   // Logical operators...
298   case And: return "and";
299   case Or : return "or";
300   case Xor: return "xor";
301 
302   // Memory instructions...
303   case Alloca:        return "alloca";
304   case Load:          return "load";
305   case Store:         return "store";
306   case AtomicCmpXchg: return "cmpxchg";
307   case AtomicRMW:     return "atomicrmw";
308   case Fence:         return "fence";
309   case GetElementPtr: return "getelementptr";
310 
311   // Convert instructions...
312   case Trunc:         return "trunc";
313   case ZExt:          return "zext";
314   case SExt:          return "sext";
315   case FPTrunc:       return "fptrunc";
316   case FPExt:         return "fpext";
317   case FPToUI:        return "fptoui";
318   case FPToSI:        return "fptosi";
319   case UIToFP:        return "uitofp";
320   case SIToFP:        return "sitofp";
321   case IntToPtr:      return "inttoptr";
322   case PtrToInt:      return "ptrtoint";
323   case BitCast:       return "bitcast";
324   case AddrSpaceCast: return "addrspacecast";
325 
326   // Other instructions...
327   case ICmp:           return "icmp";
328   case FCmp:           return "fcmp";
329   case PHI:            return "phi";
330   case Select:         return "select";
331   case Call:           return "call";
332   case Shl:            return "shl";
333   case LShr:           return "lshr";
334   case AShr:           return "ashr";
335   case VAArg:          return "va_arg";
336   case ExtractElement: return "extractelement";
337   case InsertElement:  return "insertelement";
338   case ShuffleVector:  return "shufflevector";
339   case ExtractValue:   return "extractvalue";
340   case InsertValue:    return "insertvalue";
341   case LandingPad:     return "landingpad";
342   case CleanupPad:     return "cleanuppad";
343 
344   default: return "<Invalid operator> ";
345   }
346 }
347 
348 /// Return true if both instructions have the same special state. This must be
349 /// kept in sync with FunctionComparator::cmpOperations in
350 /// lib/Transforms/IPO/MergeFunctions.cpp.
351 static bool haveSameSpecialState(const Instruction *I1, const Instruction *I2,
352                                  bool IgnoreAlignment = false) {
353   assert(I1->getOpcode() == I2->getOpcode() &&
354          "Can not compare special state of different instructions");
355 
356   if (const AllocaInst *AI = dyn_cast<AllocaInst>(I1))
357     return AI->getAllocatedType() == cast<AllocaInst>(I2)->getAllocatedType() &&
358            (AI->getAlignment() == cast<AllocaInst>(I2)->getAlignment() ||
359             IgnoreAlignment);
360   if (const LoadInst *LI = dyn_cast<LoadInst>(I1))
361     return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() &&
362            (LI->getAlignment() == cast<LoadInst>(I2)->getAlignment() ||
363             IgnoreAlignment) &&
364            LI->getOrdering() == cast<LoadInst>(I2)->getOrdering() &&
365            LI->getSynchScope() == cast<LoadInst>(I2)->getSynchScope();
366   if (const StoreInst *SI = dyn_cast<StoreInst>(I1))
367     return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() &&
368            (SI->getAlignment() == cast<StoreInst>(I2)->getAlignment() ||
369             IgnoreAlignment) &&
370            SI->getOrdering() == cast<StoreInst>(I2)->getOrdering() &&
371            SI->getSynchScope() == cast<StoreInst>(I2)->getSynchScope();
372   if (const CmpInst *CI = dyn_cast<CmpInst>(I1))
373     return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate();
374   if (const CallInst *CI = dyn_cast<CallInst>(I1))
375     return CI->isTailCall() == cast<CallInst>(I2)->isTailCall() &&
376            CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() &&
377            CI->getAttributes() == cast<CallInst>(I2)->getAttributes() &&
378            CI->hasIdenticalOperandBundleSchema(*cast<CallInst>(I2));
379   if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1))
380     return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() &&
381            CI->getAttributes() == cast<InvokeInst>(I2)->getAttributes() &&
382            CI->hasIdenticalOperandBundleSchema(*cast<InvokeInst>(I2));
383   if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1))
384     return IVI->getIndices() == cast<InsertValueInst>(I2)->getIndices();
385   if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1))
386     return EVI->getIndices() == cast<ExtractValueInst>(I2)->getIndices();
387   if (const FenceInst *FI = dyn_cast<FenceInst>(I1))
388     return FI->getOrdering() == cast<FenceInst>(I2)->getOrdering() &&
389            FI->getSynchScope() == cast<FenceInst>(I2)->getSynchScope();
390   if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I1))
391     return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I2)->isVolatile() &&
392            CXI->isWeak() == cast<AtomicCmpXchgInst>(I2)->isWeak() &&
393            CXI->getSuccessOrdering() ==
394                cast<AtomicCmpXchgInst>(I2)->getSuccessOrdering() &&
395            CXI->getFailureOrdering() ==
396                cast<AtomicCmpXchgInst>(I2)->getFailureOrdering() &&
397            CXI->getSynchScope() == cast<AtomicCmpXchgInst>(I2)->getSynchScope();
398   if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I1))
399     return RMWI->getOperation() == cast<AtomicRMWInst>(I2)->getOperation() &&
400            RMWI->isVolatile() == cast<AtomicRMWInst>(I2)->isVolatile() &&
401            RMWI->getOrdering() == cast<AtomicRMWInst>(I2)->getOrdering() &&
402            RMWI->getSynchScope() == cast<AtomicRMWInst>(I2)->getSynchScope();
403 
404   return true;
405 }
406 
407 bool Instruction::isIdenticalTo(const Instruction *I) const {
408   return isIdenticalToWhenDefined(I) &&
409          SubclassOptionalData == I->SubclassOptionalData;
410 }
411 
412 bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const {
413   if (getOpcode() != I->getOpcode() ||
414       getNumOperands() != I->getNumOperands() ||
415       getType() != I->getType())
416     return false;
417 
418   // If both instructions have no operands, they are identical.
419   if (getNumOperands() == 0 && I->getNumOperands() == 0)
420     return haveSameSpecialState(this, I);
421 
422   // We have two instructions of identical opcode and #operands.  Check to see
423   // if all operands are the same.
424   if (!std::equal(op_begin(), op_end(), I->op_begin()))
425     return false;
426 
427   if (const PHINode *thisPHI = dyn_cast<PHINode>(this)) {
428     const PHINode *otherPHI = cast<PHINode>(I);
429     return std::equal(thisPHI->block_begin(), thisPHI->block_end(),
430                       otherPHI->block_begin());
431   }
432 
433   return haveSameSpecialState(this, I);
434 }
435 
436 // Keep this in sync with FunctionComparator::cmpOperations in
437 // lib/Transforms/IPO/MergeFunctions.cpp.
438 bool Instruction::isSameOperationAs(const Instruction *I,
439                                     unsigned flags) const {
440   bool IgnoreAlignment = flags & CompareIgnoringAlignment;
441   bool UseScalarTypes  = flags & CompareUsingScalarTypes;
442 
443   if (getOpcode() != I->getOpcode() ||
444       getNumOperands() != I->getNumOperands() ||
445       (UseScalarTypes ?
446        getType()->getScalarType() != I->getType()->getScalarType() :
447        getType() != I->getType()))
448     return false;
449 
450   // We have two instructions of identical opcode and #operands.  Check to see
451   // if all operands are the same type
452   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
453     if (UseScalarTypes ?
454         getOperand(i)->getType()->getScalarType() !=
455           I->getOperand(i)->getType()->getScalarType() :
456         getOperand(i)->getType() != I->getOperand(i)->getType())
457       return false;
458 
459   return haveSameSpecialState(this, I, IgnoreAlignment);
460 }
461 
462 bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
463   for (const Use &U : uses()) {
464     // PHI nodes uses values in the corresponding predecessor block.  For other
465     // instructions, just check to see whether the parent of the use matches up.
466     const Instruction *I = cast<Instruction>(U.getUser());
467     const PHINode *PN = dyn_cast<PHINode>(I);
468     if (!PN) {
469       if (I->getParent() != BB)
470         return true;
471       continue;
472     }
473 
474     if (PN->getIncomingBlock(U) != BB)
475       return true;
476   }
477   return false;
478 }
479 
480 bool Instruction::mayReadFromMemory() const {
481   switch (getOpcode()) {
482   default: return false;
483   case Instruction::VAArg:
484   case Instruction::Load:
485   case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory
486   case Instruction::AtomicCmpXchg:
487   case Instruction::AtomicRMW:
488   case Instruction::CatchPad:
489   case Instruction::CatchRet:
490     return true;
491   case Instruction::Call:
492     return !cast<CallInst>(this)->doesNotAccessMemory();
493   case Instruction::Invoke:
494     return !cast<InvokeInst>(this)->doesNotAccessMemory();
495   case Instruction::Store:
496     return !cast<StoreInst>(this)->isUnordered();
497   }
498 }
499 
500 bool Instruction::mayWriteToMemory() const {
501   switch (getOpcode()) {
502   default: return false;
503   case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory
504   case Instruction::Store:
505   case Instruction::VAArg:
506   case Instruction::AtomicCmpXchg:
507   case Instruction::AtomicRMW:
508   case Instruction::CatchPad:
509   case Instruction::CatchRet:
510     return true;
511   case Instruction::Call:
512     return !cast<CallInst>(this)->onlyReadsMemory();
513   case Instruction::Invoke:
514     return !cast<InvokeInst>(this)->onlyReadsMemory();
515   case Instruction::Load:
516     return !cast<LoadInst>(this)->isUnordered();
517   }
518 }
519 
520 bool Instruction::isAtomic() const {
521   switch (getOpcode()) {
522   default:
523     return false;
524   case Instruction::AtomicCmpXchg:
525   case Instruction::AtomicRMW:
526   case Instruction::Fence:
527     return true;
528   case Instruction::Load:
529     return cast<LoadInst>(this)->getOrdering() != AtomicOrdering::NotAtomic;
530   case Instruction::Store:
531     return cast<StoreInst>(this)->getOrdering() != AtomicOrdering::NotAtomic;
532   }
533 }
534 
535 bool Instruction::hasAtomicLoad() const {
536   assert(isAtomic());
537   switch (getOpcode()) {
538   default:
539     return false;
540   case Instruction::AtomicCmpXchg:
541   case Instruction::AtomicRMW:
542   case Instruction::Load:
543     return true;
544   }
545 }
546 
547 bool Instruction::hasAtomicStore() const {
548   assert(isAtomic());
549   switch (getOpcode()) {
550   default:
551     return false;
552   case Instruction::AtomicCmpXchg:
553   case Instruction::AtomicRMW:
554   case Instruction::Store:
555     return true;
556   }
557 }
558 
559 bool Instruction::mayThrow() const {
560   if (const CallInst *CI = dyn_cast<CallInst>(this))
561     return !CI->doesNotThrow();
562   if (const auto *CRI = dyn_cast<CleanupReturnInst>(this))
563     return CRI->unwindsToCaller();
564   if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(this))
565     return CatchSwitch->unwindsToCaller();
566   return isa<ResumeInst>(this);
567 }
568 
569 bool Instruction::isAssociative() const {
570   unsigned Opcode = getOpcode();
571   if (isAssociative(Opcode))
572     return true;
573 
574   switch (Opcode) {
575   case FMul:
576   case FAdd:
577     return cast<FPMathOperator>(this)->hasUnsafeAlgebra();
578   default:
579     return false;
580   }
581 }
582 
583 Instruction *Instruction::cloneImpl() const {
584   llvm_unreachable("Subclass of Instruction failed to implement cloneImpl");
585 }
586 
587 void Instruction::swapProfMetadata() {
588   MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
589   if (!ProfileData || ProfileData->getNumOperands() != 3 ||
590       !isa<MDString>(ProfileData->getOperand(0)))
591     return;
592 
593   MDString *MDName = cast<MDString>(ProfileData->getOperand(0));
594   if (MDName->getString() != "branch_weights")
595     return;
596 
597   // The first operand is the name. Fetch them backwards and build a new one.
598   Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
599                      ProfileData->getOperand(1)};
600   setMetadata(LLVMContext::MD_prof,
601               MDNode::get(ProfileData->getContext(), Ops));
602 }
603 
604 void Instruction::copyMetadata(const Instruction &SrcInst,
605                                ArrayRef<unsigned> WL) {
606   if (!SrcInst.hasMetadata())
607     return;
608 
609   DenseSet<unsigned> WLS;
610   for (unsigned M : WL)
611     WLS.insert(M);
612 
613   // Otherwise, enumerate and copy over metadata from the old instruction to the
614   // new one.
615   SmallVector<std::pair<unsigned, MDNode *>, 4> TheMDs;
616   SrcInst.getAllMetadataOtherThanDebugLoc(TheMDs);
617   for (const auto &MD : TheMDs) {
618     if (WL.empty() || WLS.count(MD.first))
619       setMetadata(MD.first, MD.second);
620   }
621   if (WL.empty() || WLS.count(LLVMContext::MD_dbg))
622     setDebugLoc(SrcInst.getDebugLoc());
623   return;
624 }
625 
626 Instruction *Instruction::clone() const {
627   Instruction *New = nullptr;
628   switch (getOpcode()) {
629   default:
630     llvm_unreachable("Unhandled Opcode.");
631 #define HANDLE_INST(num, opc, clas)                                            \
632   case Instruction::opc:                                                       \
633     New = cast<clas>(this)->cloneImpl();                                       \
634     break;
635 #include "llvm/IR/Instruction.def"
636 #undef HANDLE_INST
637   }
638 
639   New->SubclassOptionalData = SubclassOptionalData;
640   New->copyMetadata(*this);
641   return New;
642 }
643 
644 void Instruction::updateProfWeight(uint64_t S, uint64_t T) {
645   auto *ProfileData = getMetadata(LLVMContext::MD_prof);
646   if (ProfileData == nullptr)
647     return;
648 
649   auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
650   if (!ProfDataName || (!ProfDataName->getString().equals("branch_weights") &&
651                         !ProfDataName->getString().equals("VP")))
652     return;
653 
654   MDBuilder MDB(getContext());
655   SmallVector<Metadata *, 3> Vals;
656   Vals.push_back(ProfileData->getOperand(0));
657   APInt APS(128, S), APT(128, T);
658   if (ProfDataName->getString().equals("branch_weights"))
659     for (unsigned i = 1; i < ProfileData->getNumOperands(); i++) {
660       // Using APInt::div may be expensive, but most cases should fit 64 bits.
661       APInt Val(128,
662                 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i))
663                     ->getValue()
664                     .getZExtValue());
665       Val *= APS;
666       Vals.push_back(MDB.createConstant(
667           ConstantInt::get(Type::getInt64Ty(getContext()),
668                            Val.udiv(APT).getLimitedValue())));
669     }
670   else if (ProfDataName->getString().equals("VP"))
671     for (unsigned i = 1; i < ProfileData->getNumOperands(); i += 2) {
672       // The first value is the key of the value profile, which will not change.
673       Vals.push_back(ProfileData->getOperand(i));
674       // Using APInt::div may be expensive, but most cases should fit 64 bits.
675       APInt Val(128,
676                 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i + 1))
677                     ->getValue()
678                     .getZExtValue());
679       Val *= APS;
680       Vals.push_back(MDB.createConstant(
681           ConstantInt::get(Type::getInt64Ty(getContext()),
682                            Val.udiv(APT).getLimitedValue())));
683     }
684   setMetadata(LLVMContext::MD_prof, MDNode::get(getContext(), Vals));
685 }
686 
687 void Instruction::setProfWeight(uint64_t W) {
688   assert((isa<CallInst>(this) || isa<InvokeInst>(this)) &&
689          "Can only set weights for call and invoke instrucitons");
690   SmallVector<uint32_t, 1> Weights;
691   Weights.push_back(W);
692   MDBuilder MDB(getContext());
693   setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights));
694 }
695