1 //===-- Instruction.cpp - Implement the Instruction class -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Instruction class for the IR library.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/DenseSet.h"
15 #include "llvm/IR/Instruction.h"
16 #include "llvm/IR/CallSite.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/Instructions.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/MDBuilder.h"
21 #include "llvm/IR/Operator.h"
22 #include "llvm/IR/Type.h"
25 Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
26 Instruction *InsertBefore)
27 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {
29 // If requested, insert this instruction into a basic block...
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);
37 Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
38 BasicBlock *InsertAtEnd)
39 : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {
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);
47 // Out of line virtual method, so the vtable, etc has a home.
48 Instruction::~Instruction() {
49 assert(!Parent && "Instruction still linked in the program!");
50 if (hasMetadataHashEntry())
51 clearMetadataHashEntries();
55 void Instruction::setParent(BasicBlock *P) {
59 const Module *Instruction::getModule() const {
60 return getParent()->getModule();
63 const Function *Instruction::getFunction() const {
64 return getParent()->getParent();
67 void Instruction::removeFromParent() {
68 getParent()->getInstList().remove(getIterator());
71 iplist<Instruction>::iterator Instruction::eraseFromParent() {
72 return getParent()->getInstList().erase(getIterator());
75 /// Insert an unlinked instruction into a basic block immediately before the
76 /// specified instruction.
77 void Instruction::insertBefore(Instruction *InsertPos) {
78 InsertPos->getParent()->getInstList().insert(InsertPos->getIterator(), this);
81 /// Insert an unlinked instruction into a basic block immediately after the
82 /// specified instruction.
83 void Instruction::insertAfter(Instruction *InsertPos) {
84 InsertPos->getParent()->getInstList().insertAfter(InsertPos->getIterator(),
88 /// Unlink this instruction from its current basic block and insert it into the
89 /// basic block that MovePos lives in, right before MovePos.
90 void Instruction::moveBefore(Instruction *MovePos) {
91 moveBefore(*MovePos->getParent(), MovePos->getIterator());
94 void Instruction::moveBefore(BasicBlock &BB,
95 SymbolTableList<Instruction>::iterator I) {
96 assert(I == BB.end() || I->getParent() == &BB);
97 BB.getInstList().splice(I, getParent()->getInstList(), getIterator());
100 void Instruction::setHasNoUnsignedWrap(bool b) {
101 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
104 void Instruction::setHasNoSignedWrap(bool b) {
105 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
108 void Instruction::setIsExact(bool b) {
109 cast<PossiblyExactOperator>(this)->setIsExact(b);
112 bool Instruction::hasNoUnsignedWrap() const {
113 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
116 bool Instruction::hasNoSignedWrap() const {
117 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
120 void Instruction::dropPoisonGeneratingFlags() {
121 switch (getOpcode()) {
122 case Instruction::Add:
123 case Instruction::Sub:
124 case Instruction::Mul:
125 case Instruction::Shl:
126 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(false);
127 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(false);
130 case Instruction::UDiv:
131 case Instruction::SDiv:
132 case Instruction::AShr:
133 case Instruction::LShr:
134 cast<PossiblyExactOperator>(this)->setIsExact(false);
137 case Instruction::GetElementPtr:
138 cast<GetElementPtrInst>(this)->setIsInBounds(false);
143 bool Instruction::isExact() const {
144 return cast<PossiblyExactOperator>(this)->isExact();
147 void Instruction::setHasUnsafeAlgebra(bool B) {
148 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
149 cast<FPMathOperator>(this)->setHasUnsafeAlgebra(B);
152 void Instruction::setHasNoNaNs(bool B) {
153 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
154 cast<FPMathOperator>(this)->setHasNoNaNs(B);
157 void Instruction::setHasNoInfs(bool B) {
158 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
159 cast<FPMathOperator>(this)->setHasNoInfs(B);
162 void Instruction::setHasNoSignedZeros(bool B) {
163 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
164 cast<FPMathOperator>(this)->setHasNoSignedZeros(B);
167 void Instruction::setHasAllowReciprocal(bool B) {
168 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
169 cast<FPMathOperator>(this)->setHasAllowReciprocal(B);
172 void Instruction::setFastMathFlags(FastMathFlags FMF) {
173 assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
174 cast<FPMathOperator>(this)->setFastMathFlags(FMF);
177 void Instruction::copyFastMathFlags(FastMathFlags FMF) {
178 assert(isa<FPMathOperator>(this) && "copying fast-math flag on invalid op");
179 cast<FPMathOperator>(this)->copyFastMathFlags(FMF);
182 bool Instruction::hasUnsafeAlgebra() const {
183 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
184 return cast<FPMathOperator>(this)->hasUnsafeAlgebra();
187 bool Instruction::hasNoNaNs() const {
188 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
189 return cast<FPMathOperator>(this)->hasNoNaNs();
192 bool Instruction::hasNoInfs() const {
193 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
194 return cast<FPMathOperator>(this)->hasNoInfs();
197 bool Instruction::hasNoSignedZeros() const {
198 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
199 return cast<FPMathOperator>(this)->hasNoSignedZeros();
202 bool Instruction::hasAllowReciprocal() const {
203 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
204 return cast<FPMathOperator>(this)->hasAllowReciprocal();
207 bool Instruction::hasAllowContract() const {
208 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
209 return cast<FPMathOperator>(this)->hasAllowContract();
212 FastMathFlags Instruction::getFastMathFlags() const {
213 assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
214 return cast<FPMathOperator>(this)->getFastMathFlags();
217 void Instruction::copyFastMathFlags(const Instruction *I) {
218 copyFastMathFlags(I->getFastMathFlags());
221 void Instruction::copyIRFlags(const Value *V) {
222 // Copy the wrapping flags.
223 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
224 if (isa<OverflowingBinaryOperator>(this)) {
225 setHasNoSignedWrap(OB->hasNoSignedWrap());
226 setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
230 // Copy the exact flag.
231 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
232 if (isa<PossiblyExactOperator>(this))
233 setIsExact(PE->isExact());
235 // Copy the fast-math flags.
236 if (auto *FP = dyn_cast<FPMathOperator>(V))
237 if (isa<FPMathOperator>(this))
238 copyFastMathFlags(FP->getFastMathFlags());
240 if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V))
241 if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this))
242 DestGEP->setIsInBounds(SrcGEP->isInBounds() | DestGEP->isInBounds());
245 void Instruction::andIRFlags(const Value *V) {
246 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
247 if (isa<OverflowingBinaryOperator>(this)) {
248 setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
249 setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
253 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
254 if (isa<PossiblyExactOperator>(this))
255 setIsExact(isExact() & PE->isExact());
257 if (auto *FP = dyn_cast<FPMathOperator>(V)) {
258 if (isa<FPMathOperator>(this)) {
259 FastMathFlags FM = getFastMathFlags();
260 FM &= FP->getFastMathFlags();
261 copyFastMathFlags(FM);
265 if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V))
266 if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this))
267 DestGEP->setIsInBounds(SrcGEP->isInBounds() & DestGEP->isInBounds());
270 const char *Instruction::getOpcodeName(unsigned OpCode) {
273 case Ret: return "ret";
274 case Br: return "br";
275 case Switch: return "switch";
276 case IndirectBr: return "indirectbr";
277 case Invoke: return "invoke";
278 case Resume: return "resume";
279 case Unreachable: return "unreachable";
280 case CleanupRet: return "cleanupret";
281 case CatchRet: return "catchret";
282 case CatchPad: return "catchpad";
283 case CatchSwitch: return "catchswitch";
285 // Standard binary operators...
286 case Add: return "add";
287 case FAdd: return "fadd";
288 case Sub: return "sub";
289 case FSub: return "fsub";
290 case Mul: return "mul";
291 case FMul: return "fmul";
292 case UDiv: return "udiv";
293 case SDiv: return "sdiv";
294 case FDiv: return "fdiv";
295 case URem: return "urem";
296 case SRem: return "srem";
297 case FRem: return "frem";
299 // Logical operators...
300 case And: return "and";
301 case Or : return "or";
302 case Xor: return "xor";
304 // Memory instructions...
305 case Alloca: return "alloca";
306 case Load: return "load";
307 case Store: return "store";
308 case AtomicCmpXchg: return "cmpxchg";
309 case AtomicRMW: return "atomicrmw";
310 case Fence: return "fence";
311 case GetElementPtr: return "getelementptr";
313 // Convert instructions...
314 case Trunc: return "trunc";
315 case ZExt: return "zext";
316 case SExt: return "sext";
317 case FPTrunc: return "fptrunc";
318 case FPExt: return "fpext";
319 case FPToUI: return "fptoui";
320 case FPToSI: return "fptosi";
321 case UIToFP: return "uitofp";
322 case SIToFP: return "sitofp";
323 case IntToPtr: return "inttoptr";
324 case PtrToInt: return "ptrtoint";
325 case BitCast: return "bitcast";
326 case AddrSpaceCast: return "addrspacecast";
328 // Other instructions...
329 case ICmp: return "icmp";
330 case FCmp: return "fcmp";
331 case PHI: return "phi";
332 case Select: return "select";
333 case Call: return "call";
334 case Shl: return "shl";
335 case LShr: return "lshr";
336 case AShr: return "ashr";
337 case VAArg: return "va_arg";
338 case ExtractElement: return "extractelement";
339 case InsertElement: return "insertelement";
340 case ShuffleVector: return "shufflevector";
341 case ExtractValue: return "extractvalue";
342 case InsertValue: return "insertvalue";
343 case LandingPad: return "landingpad";
344 case CleanupPad: return "cleanuppad";
346 default: return "<Invalid operator> ";
350 /// Return true if both instructions have the same special state. This must be
351 /// kept in sync with FunctionComparator::cmpOperations in
352 /// lib/Transforms/IPO/MergeFunctions.cpp.
353 static bool haveSameSpecialState(const Instruction *I1, const Instruction *I2,
354 bool IgnoreAlignment = false) {
355 assert(I1->getOpcode() == I2->getOpcode() &&
356 "Can not compare special state of different instructions");
358 if (const AllocaInst *AI = dyn_cast<AllocaInst>(I1))
359 return AI->getAllocatedType() == cast<AllocaInst>(I2)->getAllocatedType() &&
360 (AI->getAlignment() == cast<AllocaInst>(I2)->getAlignment() ||
362 if (const LoadInst *LI = dyn_cast<LoadInst>(I1))
363 return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() &&
364 (LI->getAlignment() == cast<LoadInst>(I2)->getAlignment() ||
366 LI->getOrdering() == cast<LoadInst>(I2)->getOrdering() &&
367 LI->getSynchScope() == cast<LoadInst>(I2)->getSynchScope();
368 if (const StoreInst *SI = dyn_cast<StoreInst>(I1))
369 return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() &&
370 (SI->getAlignment() == cast<StoreInst>(I2)->getAlignment() ||
372 SI->getOrdering() == cast<StoreInst>(I2)->getOrdering() &&
373 SI->getSynchScope() == cast<StoreInst>(I2)->getSynchScope();
374 if (const CmpInst *CI = dyn_cast<CmpInst>(I1))
375 return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate();
376 if (const CallInst *CI = dyn_cast<CallInst>(I1))
377 return CI->isTailCall() == cast<CallInst>(I2)->isTailCall() &&
378 CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() &&
379 CI->getAttributes() == cast<CallInst>(I2)->getAttributes() &&
380 CI->hasIdenticalOperandBundleSchema(*cast<CallInst>(I2));
381 if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1))
382 return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() &&
383 CI->getAttributes() == cast<InvokeInst>(I2)->getAttributes() &&
384 CI->hasIdenticalOperandBundleSchema(*cast<InvokeInst>(I2));
385 if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1))
386 return IVI->getIndices() == cast<InsertValueInst>(I2)->getIndices();
387 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1))
388 return EVI->getIndices() == cast<ExtractValueInst>(I2)->getIndices();
389 if (const FenceInst *FI = dyn_cast<FenceInst>(I1))
390 return FI->getOrdering() == cast<FenceInst>(I2)->getOrdering() &&
391 FI->getSynchScope() == cast<FenceInst>(I2)->getSynchScope();
392 if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I1))
393 return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I2)->isVolatile() &&
394 CXI->isWeak() == cast<AtomicCmpXchgInst>(I2)->isWeak() &&
395 CXI->getSuccessOrdering() ==
396 cast<AtomicCmpXchgInst>(I2)->getSuccessOrdering() &&
397 CXI->getFailureOrdering() ==
398 cast<AtomicCmpXchgInst>(I2)->getFailureOrdering() &&
399 CXI->getSynchScope() == cast<AtomicCmpXchgInst>(I2)->getSynchScope();
400 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I1))
401 return RMWI->getOperation() == cast<AtomicRMWInst>(I2)->getOperation() &&
402 RMWI->isVolatile() == cast<AtomicRMWInst>(I2)->isVolatile() &&
403 RMWI->getOrdering() == cast<AtomicRMWInst>(I2)->getOrdering() &&
404 RMWI->getSynchScope() == cast<AtomicRMWInst>(I2)->getSynchScope();
409 bool Instruction::isIdenticalTo(const Instruction *I) const {
410 return isIdenticalToWhenDefined(I) &&
411 SubclassOptionalData == I->SubclassOptionalData;
414 bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const {
415 if (getOpcode() != I->getOpcode() ||
416 getNumOperands() != I->getNumOperands() ||
417 getType() != I->getType())
420 // If both instructions have no operands, they are identical.
421 if (getNumOperands() == 0 && I->getNumOperands() == 0)
422 return haveSameSpecialState(this, I);
424 // We have two instructions of identical opcode and #operands. Check to see
425 // if all operands are the same.
426 if (!std::equal(op_begin(), op_end(), I->op_begin()))
429 if (const PHINode *thisPHI = dyn_cast<PHINode>(this)) {
430 const PHINode *otherPHI = cast<PHINode>(I);
431 return std::equal(thisPHI->block_begin(), thisPHI->block_end(),
432 otherPHI->block_begin());
435 return haveSameSpecialState(this, I);
438 // Keep this in sync with FunctionComparator::cmpOperations in
439 // lib/Transforms/IPO/MergeFunctions.cpp.
440 bool Instruction::isSameOperationAs(const Instruction *I,
441 unsigned flags) const {
442 bool IgnoreAlignment = flags & CompareIgnoringAlignment;
443 bool UseScalarTypes = flags & CompareUsingScalarTypes;
445 if (getOpcode() != I->getOpcode() ||
446 getNumOperands() != I->getNumOperands() ||
448 getType()->getScalarType() != I->getType()->getScalarType() :
449 getType() != I->getType()))
452 // We have two instructions of identical opcode and #operands. Check to see
453 // if all operands are the same type
454 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
456 getOperand(i)->getType()->getScalarType() !=
457 I->getOperand(i)->getType()->getScalarType() :
458 getOperand(i)->getType() != I->getOperand(i)->getType())
461 return haveSameSpecialState(this, I, IgnoreAlignment);
464 bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
465 for (const Use &U : uses()) {
466 // PHI nodes uses values in the corresponding predecessor block. For other
467 // instructions, just check to see whether the parent of the use matches up.
468 const Instruction *I = cast<Instruction>(U.getUser());
469 const PHINode *PN = dyn_cast<PHINode>(I);
471 if (I->getParent() != BB)
476 if (PN->getIncomingBlock(U) != BB)
482 bool Instruction::mayReadFromMemory() const {
483 switch (getOpcode()) {
484 default: return false;
485 case Instruction::VAArg:
486 case Instruction::Load:
487 case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory
488 case Instruction::AtomicCmpXchg:
489 case Instruction::AtomicRMW:
490 case Instruction::CatchPad:
491 case Instruction::CatchRet:
493 case Instruction::Call:
494 return !cast<CallInst>(this)->doesNotAccessMemory();
495 case Instruction::Invoke:
496 return !cast<InvokeInst>(this)->doesNotAccessMemory();
497 case Instruction::Store:
498 return !cast<StoreInst>(this)->isUnordered();
502 bool Instruction::mayWriteToMemory() const {
503 switch (getOpcode()) {
504 default: return false;
505 case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory
506 case Instruction::Store:
507 case Instruction::VAArg:
508 case Instruction::AtomicCmpXchg:
509 case Instruction::AtomicRMW:
510 case Instruction::CatchPad:
511 case Instruction::CatchRet:
513 case Instruction::Call:
514 return !cast<CallInst>(this)->onlyReadsMemory();
515 case Instruction::Invoke:
516 return !cast<InvokeInst>(this)->onlyReadsMemory();
517 case Instruction::Load:
518 return !cast<LoadInst>(this)->isUnordered();
522 bool Instruction::isAtomic() const {
523 switch (getOpcode()) {
526 case Instruction::AtomicCmpXchg:
527 case Instruction::AtomicRMW:
528 case Instruction::Fence:
530 case Instruction::Load:
531 return cast<LoadInst>(this)->getOrdering() != AtomicOrdering::NotAtomic;
532 case Instruction::Store:
533 return cast<StoreInst>(this)->getOrdering() != AtomicOrdering::NotAtomic;
537 bool Instruction::mayThrow() const {
538 if (const CallInst *CI = dyn_cast<CallInst>(this))
539 return !CI->doesNotThrow();
540 if (const auto *CRI = dyn_cast<CleanupReturnInst>(this))
541 return CRI->unwindsToCaller();
542 if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(this))
543 return CatchSwitch->unwindsToCaller();
544 return isa<ResumeInst>(this);
547 bool Instruction::isAssociative() const {
548 unsigned Opcode = getOpcode();
549 if (isAssociative(Opcode))
555 return cast<FPMathOperator>(this)->hasUnsafeAlgebra();
561 Instruction *Instruction::cloneImpl() const {
562 llvm_unreachable("Subclass of Instruction failed to implement cloneImpl");
565 void Instruction::swapProfMetadata() {
566 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
567 if (!ProfileData || ProfileData->getNumOperands() != 3 ||
568 !isa<MDString>(ProfileData->getOperand(0)))
571 MDString *MDName = cast<MDString>(ProfileData->getOperand(0));
572 if (MDName->getString() != "branch_weights")
575 // The first operand is the name. Fetch them backwards and build a new one.
576 Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
577 ProfileData->getOperand(1)};
578 setMetadata(LLVMContext::MD_prof,
579 MDNode::get(ProfileData->getContext(), Ops));
582 void Instruction::copyMetadata(const Instruction &SrcInst,
583 ArrayRef<unsigned> WL) {
584 if (!SrcInst.hasMetadata())
587 DenseSet<unsigned> WLS;
588 for (unsigned M : WL)
591 // Otherwise, enumerate and copy over metadata from the old instruction to the
593 SmallVector<std::pair<unsigned, MDNode *>, 4> TheMDs;
594 SrcInst.getAllMetadataOtherThanDebugLoc(TheMDs);
595 for (const auto &MD : TheMDs) {
596 if (WL.empty() || WLS.count(MD.first))
597 setMetadata(MD.first, MD.second);
599 if (WL.empty() || WLS.count(LLVMContext::MD_dbg))
600 setDebugLoc(SrcInst.getDebugLoc());
604 Instruction *Instruction::clone() const {
605 Instruction *New = nullptr;
606 switch (getOpcode()) {
608 llvm_unreachable("Unhandled Opcode.");
609 #define HANDLE_INST(num, opc, clas) \
610 case Instruction::opc: \
611 New = cast<clas>(this)->cloneImpl(); \
613 #include "llvm/IR/Instruction.def"
617 New->SubclassOptionalData = SubclassOptionalData;
618 New->copyMetadata(*this);
622 void Instruction::updateProfWeight(uint64_t S, uint64_t T) {
623 auto *ProfileData = getMetadata(LLVMContext::MD_prof);
624 if (ProfileData == nullptr)
627 auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
628 if (!ProfDataName || !ProfDataName->getString().equals("branch_weights"))
631 SmallVector<uint32_t, 4> Weights;
632 for (unsigned i = 1; i < ProfileData->getNumOperands(); i++) {
633 // Using APInt::div may be expensive, but most cases should fit in 64 bits.
634 APInt Val(128, mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i))
637 Val *= APInt(128, S);
638 Weights.push_back(Val.udiv(APInt(128, T)).getLimitedValue());
640 MDBuilder MDB(getContext());
641 setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights));
644 void Instruction::setProfWeight(uint64_t W) {
645 assert((isa<CallInst>(this) || isa<InvokeInst>(this)) &&
646 "Can only set weights for call and invoke instrucitons");
647 SmallVector<uint32_t, 1> Weights;
648 Weights.push_back(W);
649 MDBuilder MDB(getContext());
650 setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights));