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->getSyncScopeID() == cast<LoadInst>(I2)->getSyncScopeID();
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->getSyncScopeID() == cast<StoreInst>(I2)->getSyncScopeID();
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->getSyncScopeID() == cast<FenceInst>(I2)->getSyncScopeID();
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->getSyncScopeID() ==
398                cast<AtomicCmpXchgInst>(I2)->getSyncScopeID();
399   if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I1))
400     return RMWI->getOperation() == cast<AtomicRMWInst>(I2)->getOperation() &&
401            RMWI->isVolatile() == cast<AtomicRMWInst>(I2)->isVolatile() &&
402            RMWI->getOrdering() == cast<AtomicRMWInst>(I2)->getOrdering() &&
403            RMWI->getSyncScopeID() == cast<AtomicRMWInst>(I2)->getSyncScopeID();
404 
405   return true;
406 }
407 
408 bool Instruction::isIdenticalTo(const Instruction *I) const {
409   return isIdenticalToWhenDefined(I) &&
410          SubclassOptionalData == I->SubclassOptionalData;
411 }
412 
413 bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const {
414   if (getOpcode() != I->getOpcode() ||
415       getNumOperands() != I->getNumOperands() ||
416       getType() != I->getType())
417     return false;
418 
419   // If both instructions have no operands, they are identical.
420   if (getNumOperands() == 0 && I->getNumOperands() == 0)
421     return haveSameSpecialState(this, I);
422 
423   // We have two instructions of identical opcode and #operands.  Check to see
424   // if all operands are the same.
425   if (!std::equal(op_begin(), op_end(), I->op_begin()))
426     return false;
427 
428   if (const PHINode *thisPHI = dyn_cast<PHINode>(this)) {
429     const PHINode *otherPHI = cast<PHINode>(I);
430     return std::equal(thisPHI->block_begin(), thisPHI->block_end(),
431                       otherPHI->block_begin());
432   }
433 
434   return haveSameSpecialState(this, I);
435 }
436 
437 // Keep this in sync with FunctionComparator::cmpOperations in
438 // lib/Transforms/IPO/MergeFunctions.cpp.
439 bool Instruction::isSameOperationAs(const Instruction *I,
440                                     unsigned flags) const {
441   bool IgnoreAlignment = flags & CompareIgnoringAlignment;
442   bool UseScalarTypes  = flags & CompareUsingScalarTypes;
443 
444   if (getOpcode() != I->getOpcode() ||
445       getNumOperands() != I->getNumOperands() ||
446       (UseScalarTypes ?
447        getType()->getScalarType() != I->getType()->getScalarType() :
448        getType() != I->getType()))
449     return false;
450 
451   // We have two instructions of identical opcode and #operands.  Check to see
452   // if all operands are the same type
453   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
454     if (UseScalarTypes ?
455         getOperand(i)->getType()->getScalarType() !=
456           I->getOperand(i)->getType()->getScalarType() :
457         getOperand(i)->getType() != I->getOperand(i)->getType())
458       return false;
459 
460   return haveSameSpecialState(this, I, IgnoreAlignment);
461 }
462 
463 bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
464   for (const Use &U : uses()) {
465     // PHI nodes uses values in the corresponding predecessor block.  For other
466     // instructions, just check to see whether the parent of the use matches up.
467     const Instruction *I = cast<Instruction>(U.getUser());
468     const PHINode *PN = dyn_cast<PHINode>(I);
469     if (!PN) {
470       if (I->getParent() != BB)
471         return true;
472       continue;
473     }
474 
475     if (PN->getIncomingBlock(U) != BB)
476       return true;
477   }
478   return false;
479 }
480 
481 bool Instruction::mayReadFromMemory() const {
482   switch (getOpcode()) {
483   default: return false;
484   case Instruction::VAArg:
485   case Instruction::Load:
486   case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory
487   case Instruction::AtomicCmpXchg:
488   case Instruction::AtomicRMW:
489   case Instruction::CatchPad:
490   case Instruction::CatchRet:
491     return true;
492   case Instruction::Call:
493     return !cast<CallInst>(this)->doesNotAccessMemory();
494   case Instruction::Invoke:
495     return !cast<InvokeInst>(this)->doesNotAccessMemory();
496   case Instruction::Store:
497     return !cast<StoreInst>(this)->isUnordered();
498   }
499 }
500 
501 bool Instruction::mayWriteToMemory() const {
502   switch (getOpcode()) {
503   default: return false;
504   case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory
505   case Instruction::Store:
506   case Instruction::VAArg:
507   case Instruction::AtomicCmpXchg:
508   case Instruction::AtomicRMW:
509   case Instruction::CatchPad:
510   case Instruction::CatchRet:
511     return true;
512   case Instruction::Call:
513     return !cast<CallInst>(this)->onlyReadsMemory();
514   case Instruction::Invoke:
515     return !cast<InvokeInst>(this)->onlyReadsMemory();
516   case Instruction::Load:
517     return !cast<LoadInst>(this)->isUnordered();
518   }
519 }
520 
521 bool Instruction::isAtomic() const {
522   switch (getOpcode()) {
523   default:
524     return false;
525   case Instruction::AtomicCmpXchg:
526   case Instruction::AtomicRMW:
527   case Instruction::Fence:
528     return true;
529   case Instruction::Load:
530     return cast<LoadInst>(this)->getOrdering() != AtomicOrdering::NotAtomic;
531   case Instruction::Store:
532     return cast<StoreInst>(this)->getOrdering() != AtomicOrdering::NotAtomic;
533   }
534 }
535 
536 bool Instruction::hasAtomicLoad() const {
537   assert(isAtomic());
538   switch (getOpcode()) {
539   default:
540     return false;
541   case Instruction::AtomicCmpXchg:
542   case Instruction::AtomicRMW:
543   case Instruction::Load:
544     return true;
545   }
546 }
547 
548 bool Instruction::hasAtomicStore() const {
549   assert(isAtomic());
550   switch (getOpcode()) {
551   default:
552     return false;
553   case Instruction::AtomicCmpXchg:
554   case Instruction::AtomicRMW:
555   case Instruction::Store:
556     return true;
557   }
558 }
559 
560 bool Instruction::mayThrow() const {
561   if (const CallInst *CI = dyn_cast<CallInst>(this))
562     return !CI->doesNotThrow();
563   if (const auto *CRI = dyn_cast<CleanupReturnInst>(this))
564     return CRI->unwindsToCaller();
565   if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(this))
566     return CatchSwitch->unwindsToCaller();
567   return isa<ResumeInst>(this);
568 }
569 
570 bool Instruction::isAssociative() const {
571   unsigned Opcode = getOpcode();
572   if (isAssociative(Opcode))
573     return true;
574 
575   switch (Opcode) {
576   case FMul:
577   case FAdd:
578     return cast<FPMathOperator>(this)->hasUnsafeAlgebra();
579   default:
580     return false;
581   }
582 }
583 
584 Instruction *Instruction::cloneImpl() const {
585   llvm_unreachable("Subclass of Instruction failed to implement cloneImpl");
586 }
587 
588 void Instruction::swapProfMetadata() {
589   MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
590   if (!ProfileData || ProfileData->getNumOperands() != 3 ||
591       !isa<MDString>(ProfileData->getOperand(0)))
592     return;
593 
594   MDString *MDName = cast<MDString>(ProfileData->getOperand(0));
595   if (MDName->getString() != "branch_weights")
596     return;
597 
598   // The first operand is the name. Fetch them backwards and build a new one.
599   Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
600                      ProfileData->getOperand(1)};
601   setMetadata(LLVMContext::MD_prof,
602               MDNode::get(ProfileData->getContext(), Ops));
603 }
604 
605 void Instruction::copyMetadata(const Instruction &SrcInst,
606                                ArrayRef<unsigned> WL) {
607   if (!SrcInst.hasMetadata())
608     return;
609 
610   DenseSet<unsigned> WLS;
611   for (unsigned M : WL)
612     WLS.insert(M);
613 
614   // Otherwise, enumerate and copy over metadata from the old instruction to the
615   // new one.
616   SmallVector<std::pair<unsigned, MDNode *>, 4> TheMDs;
617   SrcInst.getAllMetadataOtherThanDebugLoc(TheMDs);
618   for (const auto &MD : TheMDs) {
619     if (WL.empty() || WLS.count(MD.first))
620       setMetadata(MD.first, MD.second);
621   }
622   if (WL.empty() || WLS.count(LLVMContext::MD_dbg))
623     setDebugLoc(SrcInst.getDebugLoc());
624   return;
625 }
626 
627 Instruction *Instruction::clone() const {
628   Instruction *New = nullptr;
629   switch (getOpcode()) {
630   default:
631     llvm_unreachable("Unhandled Opcode.");
632 #define HANDLE_INST(num, opc, clas)                                            \
633   case Instruction::opc:                                                       \
634     New = cast<clas>(this)->cloneImpl();                                       \
635     break;
636 #include "llvm/IR/Instruction.def"
637 #undef HANDLE_INST
638   }
639 
640   New->SubclassOptionalData = SubclassOptionalData;
641   New->copyMetadata(*this);
642   return New;
643 }
644 
645 void Instruction::updateProfWeight(uint64_t S, uint64_t T) {
646   auto *ProfileData = getMetadata(LLVMContext::MD_prof);
647   if (ProfileData == nullptr)
648     return;
649 
650   auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
651   if (!ProfDataName || (!ProfDataName->getString().equals("branch_weights") &&
652                         !ProfDataName->getString().equals("VP")))
653     return;
654 
655   MDBuilder MDB(getContext());
656   SmallVector<Metadata *, 3> Vals;
657   Vals.push_back(ProfileData->getOperand(0));
658   APInt APS(128, S), APT(128, T);
659   if (ProfDataName->getString().equals("branch_weights"))
660     for (unsigned i = 1; i < ProfileData->getNumOperands(); i++) {
661       // Using APInt::div may be expensive, but most cases should fit 64 bits.
662       APInt Val(128,
663                 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i))
664                     ->getValue()
665                     .getZExtValue());
666       Val *= APS;
667       Vals.push_back(MDB.createConstant(
668           ConstantInt::get(Type::getInt64Ty(getContext()),
669                            Val.udiv(APT).getLimitedValue())));
670     }
671   else if (ProfDataName->getString().equals("VP"))
672     for (unsigned i = 1; i < ProfileData->getNumOperands(); i += 2) {
673       // The first value is the key of the value profile, which will not change.
674       Vals.push_back(ProfileData->getOperand(i));
675       // Using APInt::div may be expensive, but most cases should fit 64 bits.
676       APInt Val(128,
677                 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i + 1))
678                     ->getValue()
679                     .getZExtValue());
680       Val *= APS;
681       Vals.push_back(MDB.createConstant(
682           ConstantInt::get(Type::getInt64Ty(getContext()),
683                            Val.udiv(APT).getLimitedValue())));
684     }
685   setMetadata(LLVMContext::MD_prof, MDNode::get(getContext(), Vals));
686 }
687 
688 void Instruction::setProfWeight(uint64_t W) {
689   assert((isa<CallInst>(this) || isa<InvokeInst>(this)) &&
690          "Can only set weights for call and invoke instrucitons");
691   SmallVector<uint32_t, 1> Weights;
692   Weights.push_back(W);
693   MDBuilder MDB(getContext());
694   setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights));
695 }
696