LLVM  5.0.0
Instruction.cpp
Go to the documentation of this file.
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 
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 
58  return getParent()->getModule();
59 }
60 
62  return getParent()->getParent();
63 }
64 
67 }
68 
70  return getParent()->getInstList().erase(getIterator());
71 }
72 
73 /// Insert an unlinked instruction into a basic block immediately before the
74 /// specified instruction.
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.
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.
89  moveBefore(*MovePos->getParent(), MovePos->getIterator());
90 }
91 
94  assert(I == BB.end() || I->getParent() == &BB);
95  BB.getInstList().splice(I, getParent()->getInstList(), getIterator());
96 }
97 
99  cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
100 }
101 
103  cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
104 }
105 
107  cast<PossiblyExactOperator>(this)->setIsExact(b);
108 }
109 
111  return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
112 }
113 
115  return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
116 }
117 
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 
146  assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
147  cast<FPMathOperator>(this)->setHasUnsafeAlgebra(B);
148 }
149 
151  assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
152  cast<FPMathOperator>(this)->setHasNoNaNs(B);
153 }
154 
156  assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
157  cast<FPMathOperator>(this)->setHasNoInfs(B);
158 }
159 
161  assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
162  cast<FPMathOperator>(this)->setHasNoSignedZeros(B);
163 }
164 
166  assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
167  cast<FPMathOperator>(this)->setHasAllowReciprocal(B);
168 }
169 
171  assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
172  cast<FPMathOperator>(this)->setFastMathFlags(FMF);
173 }
174 
176  assert(isa<FPMathOperator>(this) && "copying fast-math flag on invalid op");
177  cast<FPMathOperator>(this)->copyFastMathFlags(FMF);
178 }
179 
181  assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
182  return cast<FPMathOperator>(this)->hasUnsafeAlgebra();
183 }
184 
186  assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
187  return cast<FPMathOperator>(this)->hasNoNaNs();
188 }
189 
191  assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
192  return cast<FPMathOperator>(this)->hasNoInfs();
193 }
194 
196  assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
197  return cast<FPMathOperator>(this)->hasNoSignedZeros();
198 }
199 
201  assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
202  return cast<FPMathOperator>(this)->hasAllowReciprocal();
203 }
204 
206  assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
207  return cast<FPMathOperator>(this)->hasAllowContract();
208 }
209 
211  assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
212  return cast<FPMathOperator>(this)->getFastMathFlags();
213 }
214 
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 
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)) {
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 
409  return isIdenticalToWhenDefined(I) &&
411 }
412 
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.
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 
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 
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 
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 
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 
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 
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 
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)};
602  MDNode::get(ProfileData->getContext(), Ops));
603 }
604 
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.
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 
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 
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());
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(
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(
683  Val.udiv(APT).getLimitedValue())));
684  }
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());
695 }
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:69
void copyFastMathFlags(FastMathFlags FMF)
Convenience function for transferring all fast-math flag values to this instruction, which must be an operator which supports these flags.
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:850
void setFastMathFlags(FastMathFlags FMF)
Convenience function for setting multiple fast-math flags on this instruction, which must be an opera...
iterator_range< use_iterator > uses()
Definition: Value.h:350
This instruction extracts a struct member or array element value from an aggregate value...
iterator erase(iterator where)
Definition: ilist.h:280
ConstantAsMetadata * createConstant(Constant *C)
Return the given constant as metadata.
Definition: MDBuilder.cpp:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
An instruction for ordering other memory operations.
Definition: Instructions.h:440
an instruction that atomically checks whether a specified value is in a memory location, and, if it is, stores a new value there.
Definition: Instructions.h:514
unsigned getNumOperands() const
Definition: User.h:176
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1073
void setProfWeight(uint64_t W)
Sets the branch_weights metadata to W for CallInst.
This class represents a function call, abstracting a target machine's calling convention.
void setHasNoNaNs(bool B)
Set or clear the no-nans flag on this instruction, which must be an operator which supports this flag...
bool isUsedOutsideOfBlock(const BasicBlock *BB) const
Return true if there are any uses of this instruction in blocks other than the specified block...
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:301
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:108
Metadata node.
Definition: Metadata.h:862
An instruction for reading from memory.
Definition: Instructions.h:164
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:177
an instruction that atomically reads a memory location, combines it with another value, and then stores the result back.
Definition: Instructions.h:677
void copyIRFlags(const Value *V, bool IncludeWrapFlags=true)
Convenience method to copy supported exact, fast-math, and (optionally) wrapping flags from V to this...
op_iterator op_begin()
Definition: User.h:214
static uint32_t getAlignment(const MCSectionCOFF &Sec)
bool hasNoNaNs() const
Determine whether the no-NaNs flag is set.
bool isIdenticalTo(const Instruction *I) const
Return true if the specified instruction is exactly identical to the current one. ...
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr it the function does no...
Definition: BasicBlock.cpp:116
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
static bool haveSameSpecialState(const Instruction *I1, const Instruction *I2, bool IgnoreAlignment=false)
Return true if both instructions have the same special state.
void dropPoisonGeneratingFlags()
Drops flags that may cause this instruction to evaluate to poison despite having non-poison inputs...
bool hasAllowReciprocal() const
Determine whether the allow-reciprocal flag is set.
Check for equivalence ignoring load/store alignment.
Definition: Instruction.h:536
Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following: ...
block_iterator block_begin()
AttributeList getAttributes(LLVMContext &C, ID id)
Return the attributes for an intrinsic.
void setIsExact(bool b=true)
Set or clear the exact flag on this instruction, which must be an operator which supports this flag...
Check for equivalence treating a type and a vector of that type as equivalent.
Definition: Instruction.h:539
bool mayReadFromMemory() const
Return true if this instruction may read memory.
bool hasAllowContract() const
Determine whether the allow-contract flag is set.
bool hasAtomicLoad() const
Return true if this atomic instruction loads from memory.
void andIRFlags(const Value *V)
Logical 'and' of any supported wrapping, exact, and fast-math flags of V and this instruction...
void setHasUnsafeAlgebra(bool B)
Set or clear the unsafe-algebra flag on this instruction, which must be an operator which supports th...
bool hasUnsafeAlgebra() const
Determine whether the unsafe-algebra flag is set.
AttributeSet getAttributes(unsigned Index) const
The attributes for the specified index are returned.
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:61
An instruction for storing to memory.
Definition: Instructions.h:306
const char * getOpcodeName() const
Definition: Instruction.h:123
int Switch(int a)
Definition: Switch2Test.cpp:11
bool isAtomic() const
Return true if this instruction has an AtomicOrdering of unordered or higher.
#define P(N)
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:277
bool isIdenticalToWhenDefined(const Instruction *I) const
This is like isIdenticalTo, except that it ignores the SubclassOptionalData flags, which may specify conditions under which the instruction's result is undefined.
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction...
Definition: Instruction.cpp:75
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
bool isExact() const
Determine whether the exact flag is set.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
void getAllMetadataOtherThanDebugLoc(SmallVectorImpl< std::pair< unsigned, MDNode * >> &MDs) const
This does the same thing as getAllMetadata, except that it filters out the debug location.
Definition: Instruction.h:213
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:187
bool hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
bool hasMetadata() const
Return true if this instruction has any metadata attached to it.
Definition: Instruction.h:180
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:280
bool isAssociative() const LLVM_READONLY
Return true if the instruction is associative:
op_iterator op_end()
Definition: User.h:216
void splice(iterator where, iplist_impl &L2)
Definition: ilist.h:342
const InstListType & getInstList() const
Return the underlying instruction list container.
Definition: BasicBlock.h:317
Value * getOperand(unsigned i) const
Definition: User.h:154
self_iterator getIterator()
Definition: ilist_node.h:82
void setHasNoInfs(bool B)
Set or clear the no-infs flag on this instruction, which must be an operator which supports this flag...
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:697
MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight)
Return metadata containing two branch weights.
Definition: MDBuilder.cpp:37
void swapProfMetadata()
If the instruction has "branch_weights" MD_prof metadata and the MDNode has three operands (including...
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1214
unsigned char SubclassOptionalData
Hold subclass data that can be dropped.
Definition: Value.h:91
bool mayWriteToMemory() const
Return true if this instruction may modify memory.
StringRef getString() const
Definition: Metadata.cpp:456
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1067
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
Iterator for intrusive lists based on ilist_node.
#define B
Definition: LargeTest.cpp:24
bool hasNoUnsignedWrap() const
Determine whether the no unsigned wrap flag is set.
iterator end()
Definition: BasicBlock.h:254
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:57
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:190
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:560
Class for arbitrary precision integers.
Definition: APInt.h:69
void push_back(pointer val)
Definition: ilist.h:326
static bool isWeak(const MCSymbolELF &Sym)
bool hasAtomicStore() const
Return true if this atomic instruction stores to memory.
void removeFromParent()
This method unlinks 'this' from the containing basic block, but does not delete it.
Definition: Instruction.cpp:65
llvm_unreachable("Unknown RegBank!")
bool mayThrow() const
Return true if this instruction may throw an exception.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1164
pointer remove(iterator &IT)
Definition: ilist.h:264
List that automatically updates parent links and symbol tables.
iterator insert(iterator where, pointer New)
Definition: ilist.h:241
void updateProfWeight(uint64_t S, uint64_t T)
Updates branch_weights metadata by scaling it by S / T.
void insertAfter(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately after the specified instruction...
Definition: Instruction.cpp:81
#define I(x, y, z)
Definition: MD5.cpp:58
bool hasNoInfs() const
Determine whether the no-infs flag is set.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
bool hasNoSignedZeros() const
Determine whether the no-signed-zeros flag is set.
LLVMContext & getContext() const
Definition: Metadata.h:922
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:91
Instruction(const Instruction &)=delete
void setHasNoUnsignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
Definition: Instruction.cpp:98
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:121
void copyMetadata(const Instruction &SrcInst, ArrayRef< unsigned > WL=ArrayRef< unsigned >())
Copy metadata from SrcInst to this instruction.
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
Definition: Instruction.cpp:88
Invoke instruction.
Convenience struct for specifying and reasoning about fast-math flags.
Definition: Operator.h:160
A single uniqued string.
Definition: Metadata.h:602
bool isSameOperationAs(const Instruction *I, unsigned flags=0) const
This function determines if the specified instruction executes the same operation as the current one...
static bool isVolatile(Instruction *Inst)
void setHasNoSignedZeros(bool B)
Set or clear the no-signed-zeros flag on this instruction, which must be an operator which supports t...
iterator insertAfter(iterator where, pointer New)
Definition: ilist.h:250
Root of the metadata hierarchy.
Definition: Metadata.h:58
void setHasAllowReciprocal(bool B)
Set or clear the allow-reciprocal flag on this instruction, which must be an operator which supports ...
const BasicBlock * getParent() const
Definition: Instruction.h:66
an instruction to allocate memory on the stack
Definition: Instructions.h:60
This instruction inserts a struct field of array element value into an aggregate value.