LLVM  7.0.0svn
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/IR/IntrinsicInst.h"
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/Instructions.h"
19 #include "llvm/IR/MDBuilder.h"
20 #include "llvm/IR/Operator.h"
21 #include "llvm/IR/Type.h"
22 using namespace llvm;
23 
24 Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
25  Instruction *InsertBefore)
26  : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {
27 
28  // If requested, insert this instruction into a basic block...
29  if (InsertBefore) {
30  BasicBlock *BB = InsertBefore->getParent();
31  assert(BB && "Instruction to insert before is not in a basic block!");
32  BB->getInstList().insert(InsertBefore->getIterator(), this);
33  }
34 }
35 
36 Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
37  BasicBlock *InsertAtEnd)
38  : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) {
39 
40  // append this instruction into the basic block
41  assert(InsertAtEnd && "Basic block to append to may not be NULL!");
42  InsertAtEnd->getInstList().push_back(this);
43 }
44 
46  assert(!Parent && "Instruction still linked in the program!");
47  if (hasMetadataHashEntry())
48  clearMetadataHashEntries();
49 }
50 
51 
52 void Instruction::setParent(BasicBlock *P) {
53  Parent = P;
54 }
55 
57  return getParent()->getModule();
58 }
59 
61  return getParent()->getParent();
62 }
63 
66 }
67 
69  return getParent()->getInstList().erase(getIterator());
70 }
71 
72 /// Insert an unlinked instruction into a basic block immediately before the
73 /// specified instruction.
75  InsertPos->getParent()->getInstList().insert(InsertPos->getIterator(), this);
76 }
77 
78 /// Insert an unlinked instruction into a basic block immediately after the
79 /// specified instruction.
81  InsertPos->getParent()->getInstList().insertAfter(InsertPos->getIterator(),
82  this);
83 }
84 
85 /// Unlink this instruction from its current basic block and insert it into the
86 /// basic block that MovePos lives in, right before MovePos.
88  moveBefore(*MovePos->getParent(), MovePos->getIterator());
89 }
90 
92  moveBefore(*MovePos->getParent(), ++MovePos->getIterator());
93 }
94 
97  assert(I == BB.end() || I->getParent() == &BB);
98  BB.getInstList().splice(I, getParent()->getInstList(), getIterator());
99 }
100 
102  cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
103 }
104 
106  cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
107 }
108 
110  cast<PossiblyExactOperator>(this)->setIsExact(b);
111 }
112 
114  return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
115 }
116 
118  return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
119 }
120 
122  switch (getOpcode()) {
123  case Instruction::Add:
124  case Instruction::Sub:
125  case Instruction::Mul:
126  case Instruction::Shl:
127  cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(false);
128  cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(false);
129  break;
130 
131  case Instruction::UDiv:
132  case Instruction::SDiv:
133  case Instruction::AShr:
134  case Instruction::LShr:
135  cast<PossiblyExactOperator>(this)->setIsExact(false);
136  break;
137 
138  case Instruction::GetElementPtr:
139  cast<GetElementPtrInst>(this)->setIsInBounds(false);
140  break;
141  }
142 }
143 
144 bool Instruction::isExact() const {
145  return cast<PossiblyExactOperator>(this)->isExact();
146 }
147 
149  assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
150  cast<FPMathOperator>(this)->setFast(B);
151 }
152 
154  assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
155  cast<FPMathOperator>(this)->setHasAllowReassoc(B);
156 }
157 
159  assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
160  cast<FPMathOperator>(this)->setHasNoNaNs(B);
161 }
162 
164  assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
165  cast<FPMathOperator>(this)->setHasNoInfs(B);
166 }
167 
169  assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
170  cast<FPMathOperator>(this)->setHasNoSignedZeros(B);
171 }
172 
174  assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
175  cast<FPMathOperator>(this)->setHasAllowReciprocal(B);
176 }
177 
179  assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
180  cast<FPMathOperator>(this)->setHasApproxFunc(B);
181 }
182 
184  assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
185  cast<FPMathOperator>(this)->setFastMathFlags(FMF);
186 }
187 
189  assert(isa<FPMathOperator>(this) && "copying fast-math flag on invalid op");
190  cast<FPMathOperator>(this)->copyFastMathFlags(FMF);
191 }
192 
193 bool Instruction::isFast() const {
194  assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
195  return cast<FPMathOperator>(this)->isFast();
196 }
197 
199  assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
200  return cast<FPMathOperator>(this)->hasAllowReassoc();
201 }
202 
204  assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
205  return cast<FPMathOperator>(this)->hasNoNaNs();
206 }
207 
209  assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
210  return cast<FPMathOperator>(this)->hasNoInfs();
211 }
212 
214  assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
215  return cast<FPMathOperator>(this)->hasNoSignedZeros();
216 }
217 
219  assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
220  return cast<FPMathOperator>(this)->hasAllowReciprocal();
221 }
222 
224  assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
225  return cast<FPMathOperator>(this)->hasAllowContract();
226 }
227 
229  assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
230  return cast<FPMathOperator>(this)->hasApproxFunc();
231 }
232 
234  assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
235  return cast<FPMathOperator>(this)->getFastMathFlags();
236 }
237 
240 }
241 
242 void Instruction::copyIRFlags(const Value *V, bool IncludeWrapFlags) {
243  // Copy the wrapping flags.
244  if (IncludeWrapFlags && isa<OverflowingBinaryOperator>(this)) {
245  if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
246  setHasNoSignedWrap(OB->hasNoSignedWrap());
247  setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
248  }
249  }
250 
251  // Copy the exact flag.
252  if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
253  if (isa<PossiblyExactOperator>(this))
254  setIsExact(PE->isExact());
255 
256  // Copy the fast-math flags.
257  if (auto *FP = dyn_cast<FPMathOperator>(V))
258  if (isa<FPMathOperator>(this))
259  copyFastMathFlags(FP->getFastMathFlags());
260 
261  if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V))
262  if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this))
263  DestGEP->setIsInBounds(SrcGEP->isInBounds() | DestGEP->isInBounds());
264 }
265 
267  if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
268  if (isa<OverflowingBinaryOperator>(this)) {
269  setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
270  setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
271  }
272  }
273 
274  if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
275  if (isa<PossiblyExactOperator>(this))
276  setIsExact(isExact() & PE->isExact());
277 
278  if (auto *FP = dyn_cast<FPMathOperator>(V)) {
279  if (isa<FPMathOperator>(this)) {
281  FM &= FP->getFastMathFlags();
282  copyFastMathFlags(FM);
283  }
284  }
285 
286  if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V))
287  if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this))
288  DestGEP->setIsInBounds(SrcGEP->isInBounds() & DestGEP->isInBounds());
289 }
290 
291 const char *Instruction::getOpcodeName(unsigned OpCode) {
292  switch (OpCode) {
293  // Terminators
294  case Ret: return "ret";
295  case Br: return "br";
296  case Switch: return "switch";
297  case IndirectBr: return "indirectbr";
298  case Invoke: return "invoke";
299  case Resume: return "resume";
300  case Unreachable: return "unreachable";
301  case CleanupRet: return "cleanupret";
302  case CatchRet: return "catchret";
303  case CatchPad: return "catchpad";
304  case CatchSwitch: return "catchswitch";
305 
306  // Standard binary operators...
307  case Add: return "add";
308  case FAdd: return "fadd";
309  case Sub: return "sub";
310  case FSub: return "fsub";
311  case Mul: return "mul";
312  case FMul: return "fmul";
313  case UDiv: return "udiv";
314  case SDiv: return "sdiv";
315  case FDiv: return "fdiv";
316  case URem: return "urem";
317  case SRem: return "srem";
318  case FRem: return "frem";
319 
320  // Logical operators...
321  case And: return "and";
322  case Or : return "or";
323  case Xor: return "xor";
324 
325  // Memory instructions...
326  case Alloca: return "alloca";
327  case Load: return "load";
328  case Store: return "store";
329  case AtomicCmpXchg: return "cmpxchg";
330  case AtomicRMW: return "atomicrmw";
331  case Fence: return "fence";
332  case GetElementPtr: return "getelementptr";
333 
334  // Convert instructions...
335  case Trunc: return "trunc";
336  case ZExt: return "zext";
337  case SExt: return "sext";
338  case FPTrunc: return "fptrunc";
339  case FPExt: return "fpext";
340  case FPToUI: return "fptoui";
341  case FPToSI: return "fptosi";
342  case UIToFP: return "uitofp";
343  case SIToFP: return "sitofp";
344  case IntToPtr: return "inttoptr";
345  case PtrToInt: return "ptrtoint";
346  case BitCast: return "bitcast";
347  case AddrSpaceCast: return "addrspacecast";
348 
349  // Other instructions...
350  case ICmp: return "icmp";
351  case FCmp: return "fcmp";
352  case PHI: return "phi";
353  case Select: return "select";
354  case Call: return "call";
355  case Shl: return "shl";
356  case LShr: return "lshr";
357  case AShr: return "ashr";
358  case VAArg: return "va_arg";
359  case ExtractElement: return "extractelement";
360  case InsertElement: return "insertelement";
361  case ShuffleVector: return "shufflevector";
362  case ExtractValue: return "extractvalue";
363  case InsertValue: return "insertvalue";
364  case LandingPad: return "landingpad";
365  case CleanupPad: return "cleanuppad";
366 
367  default: return "<Invalid operator> ";
368  }
369 }
370 
371 /// Return true if both instructions have the same special state. This must be
372 /// kept in sync with FunctionComparator::cmpOperations in
373 /// lib/Transforms/IPO/MergeFunctions.cpp.
374 static bool haveSameSpecialState(const Instruction *I1, const Instruction *I2,
375  bool IgnoreAlignment = false) {
376  assert(I1->getOpcode() == I2->getOpcode() &&
377  "Can not compare special state of different instructions");
378 
379  if (const AllocaInst *AI = dyn_cast<AllocaInst>(I1))
380  return AI->getAllocatedType() == cast<AllocaInst>(I2)->getAllocatedType() &&
381  (AI->getAlignment() == cast<AllocaInst>(I2)->getAlignment() ||
382  IgnoreAlignment);
383  if (const LoadInst *LI = dyn_cast<LoadInst>(I1))
384  return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() &&
385  (LI->getAlignment() == cast<LoadInst>(I2)->getAlignment() ||
386  IgnoreAlignment) &&
387  LI->getOrdering() == cast<LoadInst>(I2)->getOrdering() &&
388  LI->getSyncScopeID() == cast<LoadInst>(I2)->getSyncScopeID();
389  if (const StoreInst *SI = dyn_cast<StoreInst>(I1))
390  return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() &&
391  (SI->getAlignment() == cast<StoreInst>(I2)->getAlignment() ||
392  IgnoreAlignment) &&
393  SI->getOrdering() == cast<StoreInst>(I2)->getOrdering() &&
394  SI->getSyncScopeID() == cast<StoreInst>(I2)->getSyncScopeID();
395  if (const CmpInst *CI = dyn_cast<CmpInst>(I1))
396  return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate();
397  if (const CallInst *CI = dyn_cast<CallInst>(I1))
398  return CI->isTailCall() == cast<CallInst>(I2)->isTailCall() &&
399  CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() &&
400  CI->getAttributes() == cast<CallInst>(I2)->getAttributes() &&
401  CI->hasIdenticalOperandBundleSchema(*cast<CallInst>(I2));
402  if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1))
403  return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() &&
404  CI->getAttributes() == cast<InvokeInst>(I2)->getAttributes() &&
405  CI->hasIdenticalOperandBundleSchema(*cast<InvokeInst>(I2));
406  if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1))
407  return IVI->getIndices() == cast<InsertValueInst>(I2)->getIndices();
408  if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1))
409  return EVI->getIndices() == cast<ExtractValueInst>(I2)->getIndices();
410  if (const FenceInst *FI = dyn_cast<FenceInst>(I1))
411  return FI->getOrdering() == cast<FenceInst>(I2)->getOrdering() &&
412  FI->getSyncScopeID() == cast<FenceInst>(I2)->getSyncScopeID();
413  if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I1))
414  return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I2)->isVolatile() &&
415  CXI->isWeak() == cast<AtomicCmpXchgInst>(I2)->isWeak() &&
416  CXI->getSuccessOrdering() ==
417  cast<AtomicCmpXchgInst>(I2)->getSuccessOrdering() &&
418  CXI->getFailureOrdering() ==
419  cast<AtomicCmpXchgInst>(I2)->getFailureOrdering() &&
420  CXI->getSyncScopeID() ==
421  cast<AtomicCmpXchgInst>(I2)->getSyncScopeID();
422  if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I1))
423  return RMWI->getOperation() == cast<AtomicRMWInst>(I2)->getOperation() &&
424  RMWI->isVolatile() == cast<AtomicRMWInst>(I2)->isVolatile() &&
425  RMWI->getOrdering() == cast<AtomicRMWInst>(I2)->getOrdering() &&
426  RMWI->getSyncScopeID() == cast<AtomicRMWInst>(I2)->getSyncScopeID();
427 
428  return true;
429 }
430 
432  return isIdenticalToWhenDefined(I) &&
434 }
435 
437  if (getOpcode() != I->getOpcode() ||
438  getNumOperands() != I->getNumOperands() ||
439  getType() != I->getType())
440  return false;
441 
442  // If both instructions have no operands, they are identical.
443  if (getNumOperands() == 0 && I->getNumOperands() == 0)
444  return haveSameSpecialState(this, I);
445 
446  // We have two instructions of identical opcode and #operands. Check to see
447  // if all operands are the same.
448  if (!std::equal(op_begin(), op_end(), I->op_begin()))
449  return false;
450 
451  if (const PHINode *thisPHI = dyn_cast<PHINode>(this)) {
452  const PHINode *otherPHI = cast<PHINode>(I);
453  return std::equal(thisPHI->block_begin(), thisPHI->block_end(),
454  otherPHI->block_begin());
455  }
456 
457  return haveSameSpecialState(this, I);
458 }
459 
460 // Keep this in sync with FunctionComparator::cmpOperations in
461 // lib/Transforms/IPO/MergeFunctions.cpp.
463  unsigned flags) const {
464  bool IgnoreAlignment = flags & CompareIgnoringAlignment;
465  bool UseScalarTypes = flags & CompareUsingScalarTypes;
466 
467  if (getOpcode() != I->getOpcode() ||
468  getNumOperands() != I->getNumOperands() ||
469  (UseScalarTypes ?
470  getType()->getScalarType() != I->getType()->getScalarType() :
471  getType() != I->getType()))
472  return false;
473 
474  // We have two instructions of identical opcode and #operands. Check to see
475  // if all operands are the same type
476  for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
477  if (UseScalarTypes ?
478  getOperand(i)->getType()->getScalarType() !=
479  I->getOperand(i)->getType()->getScalarType() :
480  getOperand(i)->getType() != I->getOperand(i)->getType())
481  return false;
482 
483  return haveSameSpecialState(this, I, IgnoreAlignment);
484 }
485 
487  for (const Use &U : uses()) {
488  // PHI nodes uses values in the corresponding predecessor block. For other
489  // instructions, just check to see whether the parent of the use matches up.
490  const Instruction *I = cast<Instruction>(U.getUser());
491  const PHINode *PN = dyn_cast<PHINode>(I);
492  if (!PN) {
493  if (I->getParent() != BB)
494  return true;
495  continue;
496  }
497 
498  if (PN->getIncomingBlock(U) != BB)
499  return true;
500  }
501  return false;
502 }
503 
505  switch (getOpcode()) {
506  default: return false;
507  case Instruction::VAArg:
508  case Instruction::Load:
509  case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory
510  case Instruction::AtomicCmpXchg:
511  case Instruction::AtomicRMW:
512  case Instruction::CatchPad:
513  case Instruction::CatchRet:
514  return true;
515  case Instruction::Call:
516  return !cast<CallInst>(this)->doesNotAccessMemory();
517  case Instruction::Invoke:
518  return !cast<InvokeInst>(this)->doesNotAccessMemory();
519  case Instruction::Store:
520  return !cast<StoreInst>(this)->isUnordered();
521  }
522 }
523 
525  switch (getOpcode()) {
526  default: return false;
527  case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory
528  case Instruction::Store:
529  case Instruction::VAArg:
530  case Instruction::AtomicCmpXchg:
531  case Instruction::AtomicRMW:
532  case Instruction::CatchPad:
533  case Instruction::CatchRet:
534  return true;
535  case Instruction::Call:
536  return !cast<CallInst>(this)->onlyReadsMemory();
537  case Instruction::Invoke:
538  return !cast<InvokeInst>(this)->onlyReadsMemory();
539  case Instruction::Load:
540  return !cast<LoadInst>(this)->isUnordered();
541  }
542 }
543 
544 bool Instruction::isAtomic() const {
545  switch (getOpcode()) {
546  default:
547  return false;
548  case Instruction::AtomicCmpXchg:
549  case Instruction::AtomicRMW:
550  case Instruction::Fence:
551  return true;
552  case Instruction::Load:
553  return cast<LoadInst>(this)->getOrdering() != AtomicOrdering::NotAtomic;
554  case Instruction::Store:
555  return cast<StoreInst>(this)->getOrdering() != AtomicOrdering::NotAtomic;
556  }
557 }
558 
560  assert(isAtomic());
561  switch (getOpcode()) {
562  default:
563  return false;
564  case Instruction::AtomicCmpXchg:
565  case Instruction::AtomicRMW:
566  case Instruction::Load:
567  return true;
568  }
569 }
570 
572  assert(isAtomic());
573  switch (getOpcode()) {
574  default:
575  return false;
576  case Instruction::AtomicCmpXchg:
577  case Instruction::AtomicRMW:
578  case Instruction::Store:
579  return true;
580  }
581 }
582 
583 bool Instruction::mayThrow() const {
584  if (const CallInst *CI = dyn_cast<CallInst>(this))
585  return !CI->doesNotThrow();
586  if (const auto *CRI = dyn_cast<CleanupReturnInst>(this))
587  return CRI->unwindsToCaller();
588  if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(this))
589  return CatchSwitch->unwindsToCaller();
590  return isa<ResumeInst>(this);
591 }
592 
594  return (!isa<CallInst>(this) || !this->mayHaveSideEffects()) &&
595  !isa<TerminatorInst>(this);
596 }
597 
599  for (const Instruction *I = getNextNode(); I; I = I->getNextNode())
600  if (!isa<DbgInfoIntrinsic>(I))
601  return I;
602  return nullptr;
603 }
604 
606  unsigned Opcode = getOpcode();
607  if (isAssociative(Opcode))
608  return true;
609 
610  switch (Opcode) {
611  case FMul:
612  case FAdd:
613  return cast<FPMathOperator>(this)->hasAllowReassoc() &&
614  cast<FPMathOperator>(this)->hasNoSignedZeros();
615  default:
616  return false;
617  }
618 }
619 
620 Instruction *Instruction::cloneImpl() const {
621  llvm_unreachable("Subclass of Instruction failed to implement cloneImpl");
622 }
623 
625  MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
626  if (!ProfileData || ProfileData->getNumOperands() != 3 ||
627  !isa<MDString>(ProfileData->getOperand(0)))
628  return;
629 
630  MDString *MDName = cast<MDString>(ProfileData->getOperand(0));
631  if (MDName->getString() != "branch_weights")
632  return;
633 
634  // The first operand is the name. Fetch them backwards and build a new one.
635  Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
636  ProfileData->getOperand(1)};
638  MDNode::get(ProfileData->getContext(), Ops));
639 }
640 
642  ArrayRef<unsigned> WL) {
643  if (!SrcInst.hasMetadata())
644  return;
645 
646  DenseSet<unsigned> WLS;
647  for (unsigned M : WL)
648  WLS.insert(M);
649 
650  // Otherwise, enumerate and copy over metadata from the old instruction to the
651  // new one.
653  SrcInst.getAllMetadataOtherThanDebugLoc(TheMDs);
654  for (const auto &MD : TheMDs) {
655  if (WL.empty() || WLS.count(MD.first))
656  setMetadata(MD.first, MD.second);
657  }
658  if (WL.empty() || WLS.count(LLVMContext::MD_dbg))
659  setDebugLoc(SrcInst.getDebugLoc());
660 }
661 
663  Instruction *New = nullptr;
664  switch (getOpcode()) {
665  default:
666  llvm_unreachable("Unhandled Opcode.");
667 #define HANDLE_INST(num, opc, clas) \
668  case Instruction::opc: \
669  New = cast<clas>(this)->cloneImpl(); \
670  break;
671 #include "llvm/IR/Instruction.def"
672 #undef HANDLE_INST
673  }
674 
676  New->copyMetadata(*this);
677  return New;
678 }
679 
680 void Instruction::updateProfWeight(uint64_t S, uint64_t T) {
681  auto *ProfileData = getMetadata(LLVMContext::MD_prof);
682  if (ProfileData == nullptr)
683  return;
684 
685  auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
686  if (!ProfDataName || (!ProfDataName->getString().equals("branch_weights") &&
687  !ProfDataName->getString().equals("VP")))
688  return;
689 
690  MDBuilder MDB(getContext());
692  Vals.push_back(ProfileData->getOperand(0));
693  APInt APS(128, S), APT(128, T);
694  if (ProfDataName->getString().equals("branch_weights"))
695  for (unsigned i = 1; i < ProfileData->getNumOperands(); i++) {
696  // Using APInt::div may be expensive, but most cases should fit 64 bits.
697  APInt Val(128,
698  mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i))
699  ->getValue()
700  .getZExtValue());
701  Val *= APS;
702  Vals.push_back(MDB.createConstant(
704  Val.udiv(APT).getLimitedValue())));
705  }
706  else if (ProfDataName->getString().equals("VP"))
707  for (unsigned i = 1; i < ProfileData->getNumOperands(); i += 2) {
708  // The first value is the key of the value profile, which will not change.
709  Vals.push_back(ProfileData->getOperand(i));
710  // Using APInt::div may be expensive, but most cases should fit 64 bits.
711  APInt Val(128,
712  mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i + 1))
713  ->getValue()
714  .getZExtValue());
715  Val *= APS;
716  Vals.push_back(MDB.createConstant(
718  Val.udiv(APT).getLimitedValue())));
719  }
721 }
722 
724  assert((isa<CallInst>(this) || isa<InvokeInst>(this)) &&
725  "Can only set weights for call and invoke instrucitons");
726  SmallVector<uint32_t, 1> Weights;
727  Weights.push_back(W);
728  MDBuilder MDB(getContext());
730 }
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:223
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:68
bool hasNoInfs() const
Determine whether the no-infs flag is set.
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:875
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:354
bool hasNoSignedZeros() const
Determine whether the no-signed-zeros flag is set.
This instruction extracts a struct member or array element value from an aggregate value...
bool isSameOperationAs(const Instruction *I, unsigned flags=0) const
This function determines if the specified instruction executes the same operation as the current one...
Instruction * getNextNode()
Get the next node, or nullptr for the list tail.
Definition: ilist_node.h:289
iterator erase(iterator where)
Definition: ilist.h:267
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
bool isAtomic() const
Return true if this instruction has an AtomicOrdering of unordered or higher.
ConstantAsMetadata * createConstant(Constant *C)
Return the given constant as metadata.
Definition: MDBuilder.cpp:25
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
bool isSafeToRemove() const
Return true if the instruction can be removed if the result is unused.
void setProfWeight(uint64_t W)
Sets the branch_weights metadata to W for CallInst.
This class represents a function call, abstracting a target machine&#39;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 mayWriteToMemory() const
Return true if this instruction may modify memory.
bool hasAtomicLoad() const
Return true if this atomic instruction loads from memory.
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:713
Metadata node.
Definition: Metadata.h:862
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1067
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...
bool hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
op_iterator op_begin()
Definition: User.h:230
bool isIdenticalTo(const Instruction *I) const
Return true if the specified instruction is exactly identical to the current one. ...
bool hasAllowContract() const
Determine whether the allow-contract flag is set.
void setHasApproxFunc(bool B)
Set or clear the approximate-math-functions flag on this instruction, which must be an operator which...
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
Definition: BasicBlock.cpp:134
static uint32_t getAlignment(const MCSectionCOFF &Sec)
bool hasAllowReciprocal() const
Determine whether the allow-reciprocal flag is set.
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 hasApproxFunc() const
Determine whether the approximate-math-functions flag is set.
bool hasMetadata() const
Return true if this instruction has any metadata attached to it.
Definition: Instruction.h:190
Check for equivalence ignoring load/store alignment.
Definition: Instruction.h:593
block_iterator block_begin()
AttributeList getAttributes(LLVMContext &C, ID id)
Return the attributes for an intrinsic.
Instruction * clone() const
Create a copy of &#39;this&#39; instruction that is identical in all ways except the following: ...
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
void setIsExact(bool b=true)
Set or clear the exact flag on this instruction, which must be an operator which supports this flag...
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
Check for equivalence treating a type and a vector of that type as equivalent.
Definition: Instruction.h:596
LLVMContext & getContext() const
Definition: Metadata.h:922
void andIRFlags(const Value *V)
Logical &#39;and&#39; of any supported wrapping, exact, and fast-math flags of V and this instruction...
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:200
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:126
An instruction for storing to memory.
Definition: Instructions.h:306
Value * getOperand(unsigned i) const
Definition: User.h:170
void setFast(bool B)
Set or clear all fast-math-flags on this instruction, which must be an operator which supports this f...
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return &#39;this&#39;.
Definition: Type.h:301
AttributeSet getAttributes(unsigned Index) const
The attributes for the specified index are returned.
StringRef getString() const
Definition: Metadata.cpp:464
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1164
#define P(N)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:287
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&#39;s result is undefined.
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction...
Definition: Instruction.cpp:74
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
const char * getOpcodeName() const
Definition: Instruction.h:128
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 mayHaveSideEffects() const
Return true if the instruction may have side effects.
Definition: Instruction.h:541
bool mayThrow() const
Return true if this instruction may throw an exception.
const Instruction * getNextNonDebugInstruction() const
Return a pointer to the next non-debug instruction in the same basic block as &#39;this&#39;, or nullptr if no such instruction exists.
bool isAssociative() const LLVM_READONLY
Return true if the instruction is associative:
op_iterator op_end()
Definition: User.h:232
void splice(iterator where, iplist_impl &L2)
Definition: ilist.h:329
bool isFast() const
Determine whether all fast-math-flags are set.
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...
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:60
MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight)
Return metadata containing two branch weights.
Definition: MDBuilder.cpp:38
bool isExact() const
Determine whether the exact flag is set.
void swapProfMetadata()
If the instruction has "branch_weights" MD_prof metadata and the MDNode has three operands (including...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1226
unsigned char SubclassOptionalData
Hold subclass data that can be dropped.
Definition: Value.h:91
const InstListType & getInstList() const
Return the underlying instruction list container.
Definition: BasicBlock.h:329
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
Predicate getPredicate(unsigned Condition, unsigned Hint)
Return predicate consisting of specified condition and hint bits.
Definition: PPCPredicates.h:85
Iterator for intrusive lists based on ilist_node.
unsigned getNumOperands() const
Definition: User.h:192
void setHasAllowReassoc(bool B)
Set or clear the reassociation flag on this instruction, which must be an operator which supports thi...
iterator end()
Definition: BasicBlock.h:266
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
bool isUsedOutsideOfBlock(const BasicBlock *BB) const
Return true if there are any uses of this instruction in blocks other than the specified block...
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:611
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:56
Class for arbitrary precision integers.
Definition: APInt.h:69
void push_back(pointer val)
Definition: ilist.h:313
static bool isWeak(const MCSymbolELF &Sym)
void removeFromParent()
This method unlinks &#39;this&#39; from the containing basic block, but does not delete it.
Definition: Instruction.cpp:64
pointer remove(iterator &IT)
Definition: ilist.h:251
List that automatically updates parent links and symbol tables.
iterator insert(iterator where, pointer New)
Definition: ilist.h:228
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:290
void updateProfWeight(uint64_t S, uint64_t T)
Updates branch_weights metadata by scaling it by S / T.
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:108
void insertAfter(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately after the specified instruction...
Definition: Instruction.cpp:80
#define I(x, y, z)
Definition: MD5.cpp:58
bool mayReadFromMemory() const
Return true if this instruction may read memory.
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
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
bool hasNoUnsignedWrap() const
Determine whether the no unsigned wrap flag is set.
Instruction(const Instruction &)=delete
void setHasNoUnsignedWrap(bool b=true)
Set or clear the nuw flag on this instruction, which must be an operator which supports this flag...
bool hasAtomicStore() const
Return true if this atomic instruction stores to memory.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void moveAfter(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
Definition: Instruction.cpp:91
LLVM Value Representation.
Definition: Value.h:73
bool hasAllowReassoc() const
Determine whether the allow-reassociation flag is set.
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:87
Invoke instruction.
bool hasNoNaNs() const
Determine whether the no-NaNs flag is set.
Convenience struct for specifying and reasoning about fast-math flags.
Definition: Operator.h:160
A single uniqued string.
Definition: Metadata.h:602
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...
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1073
iterator insertAfter(iterator where, pointer New)
Definition: ilist.h:237
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:67
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.