xref: /freebsd/contrib/llvm-project/llvm/lib/IR/Instruction.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===-- Instruction.cpp - Implement the Instruction class -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the Instruction class for the IR library.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/IR/Instruction.h"
14 #include "llvm/ADT/DenseSet.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/IR/AttributeMask.h"
17 #include "llvm/IR/Attributes.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/InstrTypes.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/IntrinsicInst.h"
22 #include "llvm/IR/Intrinsics.h"
23 #include "llvm/IR/LLVMContext.h"
24 #include "llvm/IR/MemoryModelRelaxationAnnotations.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/IR/Operator.h"
27 #include "llvm/IR/ProfDataUtils.h"
28 #include "llvm/IR/Type.h"
29 #include "llvm/Support/Compiler.h"
30 using namespace llvm;
31 
InsertPosition(Instruction * InsertBefore)32 InsertPosition::InsertPosition(Instruction *InsertBefore)
33     : InsertAt(InsertBefore ? InsertBefore->getIterator()
34                             : InstListType::iterator()) {}
InsertPosition(BasicBlock * InsertAtEnd)35 InsertPosition::InsertPosition(BasicBlock *InsertAtEnd)
36     : InsertAt(InsertAtEnd ? InsertAtEnd->end() : InstListType::iterator()) {}
37 
Instruction(Type * ty,unsigned it,AllocInfo AllocInfo,InsertPosition InsertBefore)38 Instruction::Instruction(Type *ty, unsigned it, AllocInfo AllocInfo,
39                          InsertPosition InsertBefore)
40     : User(ty, Value::InstructionVal + it, AllocInfo) {
41   // When called with an iterator, there must be a block to insert into.
42   if (InstListType::iterator InsertIt = InsertBefore; InsertIt.isValid()) {
43     BasicBlock *BB = InsertIt.getNodeParent();
44     assert(BB && "Instruction to insert before is not in a basic block!");
45     insertInto(BB, InsertBefore);
46   }
47 }
48 
~Instruction()49 Instruction::~Instruction() {
50   assert(!getParent() && "Instruction still linked in the program!");
51 
52   // Replace any extant metadata uses of this instruction with poison to
53   // preserve debug info accuracy. Some alternatives include:
54   // - Treat Instruction like any other Value, and point its extant metadata
55   //   uses to an empty ValueAsMetadata node. This makes extant dbg.value uses
56   //   trivially dead (i.e. fair game for deletion in many passes), leading to
57   //   stale dbg.values being in effect for too long.
58   // - Call salvageDebugInfoOrMarkUndef. Not needed to make instruction removal
59   //   correct. OTOH results in wasted work in some common cases (e.g. when all
60   //   instructions in a BasicBlock are deleted).
61   if (isUsedByMetadata())
62     ValueAsMetadata::handleRAUW(this, PoisonValue::get(getType()));
63 
64   // Explicitly remove DIAssignID metadata to clear up ID -> Instruction(s)
65   // mapping in LLVMContext.
66   setMetadata(LLVMContext::MD_DIAssignID, nullptr);
67 }
68 
getModule() const69 const Module *Instruction::getModule() const {
70   return getParent()->getModule();
71 }
72 
getFunction() const73 const Function *Instruction::getFunction() const {
74   return getParent()->getParent();
75 }
76 
getDataLayout() const77 const DataLayout &Instruction::getDataLayout() const {
78   return getModule()->getDataLayout();
79 }
80 
removeFromParent()81 void Instruction::removeFromParent() {
82   // Perform any debug-info maintenence required.
83   handleMarkerRemoval();
84 
85   getParent()->getInstList().remove(getIterator());
86 }
87 
handleMarkerRemoval()88 void Instruction::handleMarkerRemoval() {
89   if (!DebugMarker)
90     return;
91 
92   DebugMarker->removeMarker();
93 }
94 
eraseFromParent()95 BasicBlock::iterator Instruction::eraseFromParent() {
96   handleMarkerRemoval();
97   return getParent()->getInstList().erase(getIterator());
98 }
99 
insertBefore(Instruction * InsertPos)100 void Instruction::insertBefore(Instruction *InsertPos) {
101   insertBefore(InsertPos->getIterator());
102 }
103 
104 /// Insert an unlinked instruction into a basic block immediately before the
105 /// specified instruction.
insertBefore(BasicBlock::iterator InsertPos)106 void Instruction::insertBefore(BasicBlock::iterator InsertPos) {
107   insertBefore(*InsertPos->getParent(), InsertPos);
108 }
109 
110 /// Insert an unlinked instruction into a basic block immediately after the
111 /// specified instruction.
insertAfter(Instruction * InsertPos)112 void Instruction::insertAfter(Instruction *InsertPos) {
113   BasicBlock *DestParent = InsertPos->getParent();
114 
115   DestParent->getInstList().insertAfter(InsertPos->getIterator(), this);
116 }
117 
insertAfter(BasicBlock::iterator InsertPos)118 void Instruction::insertAfter(BasicBlock::iterator InsertPos) {
119   BasicBlock *DestParent = InsertPos->getParent();
120 
121   DestParent->getInstList().insertAfter(InsertPos, this);
122 }
123 
insertInto(BasicBlock * ParentBB,BasicBlock::iterator It)124 BasicBlock::iterator Instruction::insertInto(BasicBlock *ParentBB,
125                                              BasicBlock::iterator It) {
126   assert(getParent() == nullptr && "Expected detached instruction");
127   assert((It == ParentBB->end() || It->getParent() == ParentBB) &&
128          "It not in ParentBB");
129   insertBefore(*ParentBB, It);
130   return getIterator();
131 }
132 
insertBefore(BasicBlock & BB,InstListType::iterator InsertPos)133 void Instruction::insertBefore(BasicBlock &BB,
134                                InstListType::iterator InsertPos) {
135   assert(!DebugMarker);
136 
137   BB.getInstList().insert(InsertPos, this);
138 
139   // We've inserted "this": if InsertAtHead is set then it comes before any
140   // DbgVariableRecords attached to InsertPos. But if it's not set, then any
141   // DbgRecords should now come before "this".
142   bool InsertAtHead = InsertPos.getHeadBit();
143   if (!InsertAtHead) {
144     DbgMarker *SrcMarker = BB.getMarker(InsertPos);
145     if (SrcMarker && !SrcMarker->empty()) {
146       // If this assertion fires, the calling code is about to insert a PHI
147       // after debug-records, which would form a sequence like:
148       //     %0 = PHI
149       //     #dbg_value
150       //     %1 = PHI
151       // Which is de-normalised and undesired -- hence the assertion. To avoid
152       // this, you must insert at that position using an iterator, and it must
153       // be aquired by calling getFirstNonPHIIt / begin or similar methods on
154       // the block. This will signal to this behind-the-scenes debug-info
155       // maintenence code that you intend the PHI to be ahead of everything,
156       // including any debug-info.
157       assert(!isa<PHINode>(this) && "Inserting PHI after debug-records!");
158       adoptDbgRecords(&BB, InsertPos, false);
159     }
160   }
161 
162   // If we're inserting a terminator, check if we need to flush out
163   // TrailingDbgRecords. Inserting instructions at the end of an incomplete
164   // block is handled by the code block above.
165   if (isTerminator())
166     getParent()->flushTerminatorDbgRecords();
167 }
168 
169 /// Unlink this instruction from its current basic block and insert it into the
170 /// basic block that MovePos lives in, right before MovePos.
moveBefore(Instruction * MovePos)171 void Instruction::moveBefore(Instruction *MovePos) {
172   moveBeforeImpl(*MovePos->getParent(), MovePos->getIterator(), false);
173 }
174 
moveBefore(BasicBlock::iterator MovePos)175 void Instruction::moveBefore(BasicBlock::iterator MovePos) {
176   moveBeforeImpl(*MovePos->getParent(), MovePos, false);
177 }
178 
moveBeforePreserving(Instruction * MovePos)179 void Instruction::moveBeforePreserving(Instruction *MovePos) {
180   moveBeforeImpl(*MovePos->getParent(), MovePos->getIterator(), true);
181 }
182 
moveBeforePreserving(BasicBlock::iterator MovePos)183 void Instruction::moveBeforePreserving(BasicBlock::iterator MovePos) {
184   moveBeforeImpl(*MovePos->getParent(), MovePos, true);
185 }
186 
moveAfter(Instruction * MovePos)187 void Instruction::moveAfter(Instruction *MovePos) {
188   auto NextIt = std::next(MovePos->getIterator());
189   // We want this instruction to be moved to after NextIt in the instruction
190   // list, but before NextIt's debug value range.
191   NextIt.setHeadBit(true);
192   moveBeforeImpl(*MovePos->getParent(), NextIt, false);
193 }
194 
moveAfter(InstListType::iterator MovePos)195 void Instruction::moveAfter(InstListType::iterator MovePos) {
196   // We want this instruction to be moved to after NextIt in the instruction
197   // list, but before NextIt's debug value range.
198   MovePos.setHeadBit(true);
199   moveBeforeImpl(*MovePos->getParent(), MovePos, false);
200 }
201 
moveAfterPreserving(Instruction * MovePos)202 void Instruction::moveAfterPreserving(Instruction *MovePos) {
203   auto NextIt = std::next(MovePos->getIterator());
204   // We want this instruction and its debug range to be moved to after NextIt
205   // in the instruction list, but before NextIt's debug value range.
206   NextIt.setHeadBit(true);
207   moveBeforeImpl(*MovePos->getParent(), NextIt, true);
208 }
209 
moveBefore(BasicBlock & BB,InstListType::iterator I)210 void Instruction::moveBefore(BasicBlock &BB, InstListType::iterator I) {
211   moveBeforeImpl(BB, I, false);
212 }
213 
moveBeforePreserving(BasicBlock & BB,InstListType::iterator I)214 void Instruction::moveBeforePreserving(BasicBlock &BB,
215                                        InstListType::iterator I) {
216   moveBeforeImpl(BB, I, true);
217 }
218 
moveBeforeImpl(BasicBlock & BB,InstListType::iterator I,bool Preserve)219 void Instruction::moveBeforeImpl(BasicBlock &BB, InstListType::iterator I,
220                               bool Preserve) {
221   assert(I == BB.end() || I->getParent() == &BB);
222   bool InsertAtHead = I.getHeadBit();
223 
224   // If we've been given the "Preserve" flag, then just move the DbgRecords with
225   // the instruction, no more special handling needed.
226   if (DebugMarker && !Preserve) {
227     if (I != this->getIterator() || InsertAtHead) {
228       // "this" is definitely moving in the list, or it's moving ahead of its
229       // attached DbgVariableRecords. Detach any existing DbgRecords.
230       handleMarkerRemoval();
231     }
232   }
233 
234   // Move this single instruction. Use the list splice method directly, not
235   // the block splicer, which will do more debug-info things.
236   BB.getInstList().splice(I, getParent()->getInstList(), getIterator());
237 
238   if (!Preserve) {
239     DbgMarker *NextMarker = getParent()->getNextMarker(this);
240 
241     // If we're inserting at point I, and not in front of the DbgRecords
242     // attached there, then we should absorb the DbgRecords attached to I.
243     if (!InsertAtHead && NextMarker && !NextMarker->empty()) {
244       adoptDbgRecords(&BB, I, false);
245     }
246   }
247 
248   if (isTerminator())
249     getParent()->flushTerminatorDbgRecords();
250 }
251 
cloneDebugInfoFrom(const Instruction * From,std::optional<DbgRecord::self_iterator> FromHere,bool InsertAtHead)252 iterator_range<DbgRecord::self_iterator> Instruction::cloneDebugInfoFrom(
253     const Instruction *From, std::optional<DbgRecord::self_iterator> FromHere,
254     bool InsertAtHead) {
255   if (!From->DebugMarker)
256     return DbgMarker::getEmptyDbgRecordRange();
257 
258   if (!DebugMarker)
259     getParent()->createMarker(this);
260 
261   return DebugMarker->cloneDebugInfoFrom(From->DebugMarker, FromHere,
262                                          InsertAtHead);
263 }
264 
265 std::optional<DbgRecord::self_iterator>
getDbgReinsertionPosition()266 Instruction::getDbgReinsertionPosition() {
267   // Is there a marker on the next instruction?
268   DbgMarker *NextMarker = getParent()->getNextMarker(this);
269   if (!NextMarker)
270     return std::nullopt;
271 
272   // Are there any DbgRecords in the next marker?
273   if (NextMarker->StoredDbgRecords.empty())
274     return std::nullopt;
275 
276   return NextMarker->StoredDbgRecords.begin();
277 }
278 
hasDbgRecords() const279 bool Instruction::hasDbgRecords() const { return !getDbgRecordRange().empty(); }
280 
adoptDbgRecords(BasicBlock * BB,BasicBlock::iterator It,bool InsertAtHead)281 void Instruction::adoptDbgRecords(BasicBlock *BB, BasicBlock::iterator It,
282                                   bool InsertAtHead) {
283   DbgMarker *SrcMarker = BB->getMarker(It);
284   auto ReleaseTrailingDbgRecords = [BB, It, SrcMarker]() {
285     if (BB->end() == It) {
286       SrcMarker->eraseFromParent();
287       BB->deleteTrailingDbgRecords();
288     }
289   };
290 
291   if (!SrcMarker || SrcMarker->StoredDbgRecords.empty()) {
292     ReleaseTrailingDbgRecords();
293     return;
294   }
295 
296   // If we have DbgMarkers attached to this instruction, we have to honour the
297   // ordering of DbgRecords between this and the other marker. Fall back to just
298   // absorbing from the source.
299   if (DebugMarker || It == BB->end()) {
300     // Ensure we _do_ have a marker.
301     getParent()->createMarker(this);
302     DebugMarker->absorbDebugValues(*SrcMarker, InsertAtHead);
303 
304     // Having transferred everything out of SrcMarker, we _could_ clean it up
305     // and free the marker now. However, that's a lot of heap-accounting for a
306     // small amount of memory with a good chance of re-use. Leave it for the
307     // moment. It will be released when the Instruction is freed in the worst
308     // case.
309     // However: if we transferred from a trailing marker off the end of the
310     // block, it's important to not leave the empty marker trailing. It will
311     // give a misleading impression that some debug records have been left
312     // trailing.
313     ReleaseTrailingDbgRecords();
314   } else {
315     // Optimisation: we're transferring all the DbgRecords from the source
316     // marker onto this empty location: just adopt the other instructions
317     // marker.
318     DebugMarker = SrcMarker;
319     DebugMarker->MarkedInstr = this;
320     It->DebugMarker = nullptr;
321   }
322 }
323 
dropDbgRecords()324 void Instruction::dropDbgRecords() {
325   if (DebugMarker)
326     DebugMarker->dropDbgRecords();
327 }
328 
dropOneDbgRecord(DbgRecord * DVR)329 void Instruction::dropOneDbgRecord(DbgRecord *DVR) {
330   DebugMarker->dropOneDbgRecord(DVR);
331 }
332 
comesBefore(const Instruction * Other) const333 bool Instruction::comesBefore(const Instruction *Other) const {
334   assert(getParent() && Other->getParent() &&
335          "instructions without BB parents have no order");
336   assert(getParent() == Other->getParent() &&
337          "cross-BB instruction order comparison");
338   if (!getParent()->isInstrOrderValid())
339     const_cast<BasicBlock *>(getParent())->renumberInstructions();
340   return Order < Other->Order;
341 }
342 
getInsertionPointAfterDef()343 std::optional<BasicBlock::iterator> Instruction::getInsertionPointAfterDef() {
344   assert(!getType()->isVoidTy() && "Instruction must define result");
345   BasicBlock *InsertBB;
346   BasicBlock::iterator InsertPt;
347   if (auto *PN = dyn_cast<PHINode>(this)) {
348     InsertBB = PN->getParent();
349     InsertPt = InsertBB->getFirstInsertionPt();
350   } else if (auto *II = dyn_cast<InvokeInst>(this)) {
351     InsertBB = II->getNormalDest();
352     InsertPt = InsertBB->getFirstInsertionPt();
353   } else if (isa<CallBrInst>(this)) {
354     // Def is available in multiple successors, there's no single dominating
355     // insertion point.
356     return std::nullopt;
357   } else {
358     assert(!isTerminator() && "Only invoke/callbr terminators return value");
359     InsertBB = getParent();
360     InsertPt = std::next(getIterator());
361     // Any instruction inserted immediately after "this" will come before any
362     // debug-info records take effect -- thus, set the head bit indicating that
363     // to debug-info-transfer code.
364     InsertPt.setHeadBit(true);
365   }
366 
367   // catchswitch blocks don't have any legal insertion point (because they
368   // are both an exception pad and a terminator).
369   if (InsertPt == InsertBB->end())
370     return std::nullopt;
371   return InsertPt;
372 }
373 
isOnlyUserOfAnyOperand()374 bool Instruction::isOnlyUserOfAnyOperand() {
375   return any_of(operands(), [](const Value *V) { return V->hasOneUser(); });
376 }
377 
setHasNoUnsignedWrap(bool b)378 void Instruction::setHasNoUnsignedWrap(bool b) {
379   if (auto *Inst = dyn_cast<OverflowingBinaryOperator>(this))
380     Inst->setHasNoUnsignedWrap(b);
381   else
382     cast<TruncInst>(this)->setHasNoUnsignedWrap(b);
383 }
384 
setHasNoSignedWrap(bool b)385 void Instruction::setHasNoSignedWrap(bool b) {
386   if (auto *Inst = dyn_cast<OverflowingBinaryOperator>(this))
387     Inst->setHasNoSignedWrap(b);
388   else
389     cast<TruncInst>(this)->setHasNoSignedWrap(b);
390 }
391 
setIsExact(bool b)392 void Instruction::setIsExact(bool b) {
393   cast<PossiblyExactOperator>(this)->setIsExact(b);
394 }
395 
setNonNeg(bool b)396 void Instruction::setNonNeg(bool b) {
397   assert(isa<PossiblyNonNegInst>(this) && "Must be zext/uitofp");
398   SubclassOptionalData = (SubclassOptionalData & ~PossiblyNonNegInst::NonNeg) |
399                          (b * PossiblyNonNegInst::NonNeg);
400 }
401 
hasNoUnsignedWrap() const402 bool Instruction::hasNoUnsignedWrap() const {
403   if (auto *Inst = dyn_cast<OverflowingBinaryOperator>(this))
404     return Inst->hasNoUnsignedWrap();
405 
406   return cast<TruncInst>(this)->hasNoUnsignedWrap();
407 }
408 
hasNoSignedWrap() const409 bool Instruction::hasNoSignedWrap() const {
410   if (auto *Inst = dyn_cast<OverflowingBinaryOperator>(this))
411     return Inst->hasNoSignedWrap();
412 
413   return cast<TruncInst>(this)->hasNoSignedWrap();
414 }
415 
hasNonNeg() const416 bool Instruction::hasNonNeg() const {
417   assert(isa<PossiblyNonNegInst>(this) && "Must be zext/uitofp");
418   return (SubclassOptionalData & PossiblyNonNegInst::NonNeg) != 0;
419 }
420 
hasPoisonGeneratingFlags() const421 bool Instruction::hasPoisonGeneratingFlags() const {
422   return cast<Operator>(this)->hasPoisonGeneratingFlags();
423 }
424 
dropPoisonGeneratingFlags()425 void Instruction::dropPoisonGeneratingFlags() {
426   switch (getOpcode()) {
427   case Instruction::Add:
428   case Instruction::Sub:
429   case Instruction::Mul:
430   case Instruction::Shl:
431     cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(false);
432     cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(false);
433     break;
434 
435   case Instruction::UDiv:
436   case Instruction::SDiv:
437   case Instruction::AShr:
438   case Instruction::LShr:
439     cast<PossiblyExactOperator>(this)->setIsExact(false);
440     break;
441 
442   case Instruction::Or:
443     cast<PossiblyDisjointInst>(this)->setIsDisjoint(false);
444     break;
445 
446   case Instruction::GetElementPtr:
447     cast<GetElementPtrInst>(this)->setNoWrapFlags(GEPNoWrapFlags::none());
448     break;
449 
450   case Instruction::UIToFP:
451   case Instruction::ZExt:
452     setNonNeg(false);
453     break;
454 
455   case Instruction::Trunc:
456     cast<TruncInst>(this)->setHasNoUnsignedWrap(false);
457     cast<TruncInst>(this)->setHasNoSignedWrap(false);
458     break;
459 
460   case Instruction::ICmp:
461     cast<ICmpInst>(this)->setSameSign(false);
462     break;
463   }
464 
465   if (isa<FPMathOperator>(this)) {
466     setHasNoNaNs(false);
467     setHasNoInfs(false);
468   }
469 
470   assert(!hasPoisonGeneratingFlags() && "must be kept in sync");
471 }
472 
hasPoisonGeneratingMetadata() const473 bool Instruction::hasPoisonGeneratingMetadata() const {
474   return any_of(Metadata::PoisonGeneratingIDs,
475                 [this](unsigned ID) { return hasMetadata(ID); });
476 }
477 
hasNonDebugLocLoopMetadata() const478 bool Instruction::hasNonDebugLocLoopMetadata() const {
479   // If there is no loop metadata at all, we also don't have
480   // non-debug loop metadata, obviously.
481   if (!hasMetadata(LLVMContext::MD_loop))
482     return false;
483 
484   // If we do have loop metadata, retrieve it.
485   MDNode *LoopMD = getMetadata(LLVMContext::MD_loop);
486 
487   // Check if the existing operands are debug locations. This loop
488   // should terminate after at most three iterations. Skip
489   // the first item because it is a self-reference.
490   for (const MDOperand &Op : llvm::drop_begin(LoopMD->operands())) {
491     // check for debug location type by attempting a cast.
492     if (!isa<DILocation>(Op)) {
493       return true;
494     }
495   }
496 
497   // If we get here, then all we have is debug locations in the loop metadata.
498   return false;
499 }
500 
dropPoisonGeneratingMetadata()501 void Instruction::dropPoisonGeneratingMetadata() {
502   for (unsigned ID : Metadata::PoisonGeneratingIDs)
503     eraseMetadata(ID);
504 }
505 
hasPoisonGeneratingReturnAttributes() const506 bool Instruction::hasPoisonGeneratingReturnAttributes() const {
507   if (const auto *CB = dyn_cast<CallBase>(this)) {
508     AttributeSet RetAttrs = CB->getAttributes().getRetAttrs();
509     return RetAttrs.hasAttribute(Attribute::Range) ||
510            RetAttrs.hasAttribute(Attribute::Alignment) ||
511            RetAttrs.hasAttribute(Attribute::NonNull);
512   }
513   return false;
514 }
515 
dropPoisonGeneratingReturnAttributes()516 void Instruction::dropPoisonGeneratingReturnAttributes() {
517   if (auto *CB = dyn_cast<CallBase>(this)) {
518     AttributeMask AM;
519     AM.addAttribute(Attribute::Range);
520     AM.addAttribute(Attribute::Alignment);
521     AM.addAttribute(Attribute::NonNull);
522     CB->removeRetAttrs(AM);
523   }
524   assert(!hasPoisonGeneratingReturnAttributes() && "must be kept in sync");
525 }
526 
dropUBImplyingAttrsAndUnknownMetadata(ArrayRef<unsigned> KnownIDs)527 void Instruction::dropUBImplyingAttrsAndUnknownMetadata(
528     ArrayRef<unsigned> KnownIDs) {
529   dropUnknownNonDebugMetadata(KnownIDs);
530   auto *CB = dyn_cast<CallBase>(this);
531   if (!CB)
532     return;
533   // For call instructions, we also need to drop parameter and return attributes
534   // that can cause UB if the call is moved to a location where the attribute is
535   // not valid.
536   AttributeList AL = CB->getAttributes();
537   if (AL.isEmpty())
538     return;
539   AttributeMask UBImplyingAttributes =
540       AttributeFuncs::getUBImplyingAttributes();
541   for (unsigned ArgNo = 0; ArgNo < CB->arg_size(); ArgNo++)
542     CB->removeParamAttrs(ArgNo, UBImplyingAttributes);
543   CB->removeRetAttrs(UBImplyingAttributes);
544 }
545 
dropUBImplyingAttrsAndMetadata()546 void Instruction::dropUBImplyingAttrsAndMetadata() {
547   // !annotation metadata does not impact semantics.
548   // !range, !nonnull and !align produce poison, so they are safe to speculate.
549   // !noundef and various AA metadata must be dropped, as it generally produces
550   // immediate undefined behavior.
551   unsigned KnownIDs[] = {LLVMContext::MD_annotation, LLVMContext::MD_range,
552                          LLVMContext::MD_nonnull, LLVMContext::MD_align};
553   dropUBImplyingAttrsAndUnknownMetadata(KnownIDs);
554 }
555 
hasUBImplyingAttrs() const556 bool Instruction::hasUBImplyingAttrs() const {
557   auto *CB = dyn_cast<CallBase>(this);
558   if (!CB)
559     return false;
560   // For call instructions, we also need to check parameter and return
561   // attributes that can cause UB.
562   for (unsigned ArgNo = 0; ArgNo < CB->arg_size(); ArgNo++)
563     if (CB->isPassingUndefUB(ArgNo))
564       return true;
565   return CB->hasRetAttr(Attribute::NoUndef) ||
566          CB->hasRetAttr(Attribute::Dereferenceable) ||
567          CB->hasRetAttr(Attribute::DereferenceableOrNull);
568 }
569 
isExact() const570 bool Instruction::isExact() const {
571   return cast<PossiblyExactOperator>(this)->isExact();
572 }
573 
setFast(bool B)574 void Instruction::setFast(bool B) {
575   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
576   cast<FPMathOperator>(this)->setFast(B);
577 }
578 
setHasAllowReassoc(bool B)579 void Instruction::setHasAllowReassoc(bool B) {
580   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
581   cast<FPMathOperator>(this)->setHasAllowReassoc(B);
582 }
583 
setHasNoNaNs(bool B)584 void Instruction::setHasNoNaNs(bool B) {
585   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
586   cast<FPMathOperator>(this)->setHasNoNaNs(B);
587 }
588 
setHasNoInfs(bool B)589 void Instruction::setHasNoInfs(bool B) {
590   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
591   cast<FPMathOperator>(this)->setHasNoInfs(B);
592 }
593 
setHasNoSignedZeros(bool B)594 void Instruction::setHasNoSignedZeros(bool B) {
595   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
596   cast<FPMathOperator>(this)->setHasNoSignedZeros(B);
597 }
598 
setHasAllowReciprocal(bool B)599 void Instruction::setHasAllowReciprocal(bool B) {
600   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
601   cast<FPMathOperator>(this)->setHasAllowReciprocal(B);
602 }
603 
setHasAllowContract(bool B)604 void Instruction::setHasAllowContract(bool B) {
605   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
606   cast<FPMathOperator>(this)->setHasAllowContract(B);
607 }
608 
setHasApproxFunc(bool B)609 void Instruction::setHasApproxFunc(bool B) {
610   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
611   cast<FPMathOperator>(this)->setHasApproxFunc(B);
612 }
613 
setFastMathFlags(FastMathFlags FMF)614 void Instruction::setFastMathFlags(FastMathFlags FMF) {
615   assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op");
616   cast<FPMathOperator>(this)->setFastMathFlags(FMF);
617 }
618 
copyFastMathFlags(FastMathFlags FMF)619 void Instruction::copyFastMathFlags(FastMathFlags FMF) {
620   assert(isa<FPMathOperator>(this) && "copying fast-math flag on invalid op");
621   cast<FPMathOperator>(this)->copyFastMathFlags(FMF);
622 }
623 
isFast() const624 bool Instruction::isFast() const {
625   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
626   return cast<FPMathOperator>(this)->isFast();
627 }
628 
hasAllowReassoc() const629 bool Instruction::hasAllowReassoc() const {
630   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
631   return cast<FPMathOperator>(this)->hasAllowReassoc();
632 }
633 
hasNoNaNs() const634 bool Instruction::hasNoNaNs() const {
635   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
636   return cast<FPMathOperator>(this)->hasNoNaNs();
637 }
638 
hasNoInfs() const639 bool Instruction::hasNoInfs() const {
640   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
641   return cast<FPMathOperator>(this)->hasNoInfs();
642 }
643 
hasNoSignedZeros() const644 bool Instruction::hasNoSignedZeros() const {
645   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
646   return cast<FPMathOperator>(this)->hasNoSignedZeros();
647 }
648 
hasAllowReciprocal() const649 bool Instruction::hasAllowReciprocal() const {
650   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
651   return cast<FPMathOperator>(this)->hasAllowReciprocal();
652 }
653 
hasAllowContract() const654 bool Instruction::hasAllowContract() const {
655   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
656   return cast<FPMathOperator>(this)->hasAllowContract();
657 }
658 
hasApproxFunc() const659 bool Instruction::hasApproxFunc() const {
660   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
661   return cast<FPMathOperator>(this)->hasApproxFunc();
662 }
663 
getFastMathFlags() const664 FastMathFlags Instruction::getFastMathFlags() const {
665   assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op");
666   return cast<FPMathOperator>(this)->getFastMathFlags();
667 }
668 
copyFastMathFlags(const Instruction * I)669 void Instruction::copyFastMathFlags(const Instruction *I) {
670   copyFastMathFlags(I->getFastMathFlags());
671 }
672 
copyIRFlags(const Value * V,bool IncludeWrapFlags)673 void Instruction::copyIRFlags(const Value *V, bool IncludeWrapFlags) {
674   // Copy the wrapping flags.
675   if (IncludeWrapFlags && isa<OverflowingBinaryOperator>(this)) {
676     if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
677       setHasNoSignedWrap(OB->hasNoSignedWrap());
678       setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
679     }
680   }
681 
682   if (auto *TI = dyn_cast<TruncInst>(V)) {
683     if (isa<TruncInst>(this)) {
684       setHasNoSignedWrap(TI->hasNoSignedWrap());
685       setHasNoUnsignedWrap(TI->hasNoUnsignedWrap());
686     }
687   }
688 
689   // Copy the exact flag.
690   if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
691     if (isa<PossiblyExactOperator>(this))
692       setIsExact(PE->isExact());
693 
694   if (auto *SrcPD = dyn_cast<PossiblyDisjointInst>(V))
695     if (auto *DestPD = dyn_cast<PossiblyDisjointInst>(this))
696       DestPD->setIsDisjoint(SrcPD->isDisjoint());
697 
698   // Copy the fast-math flags.
699   if (auto *FP = dyn_cast<FPMathOperator>(V))
700     if (isa<FPMathOperator>(this))
701       copyFastMathFlags(FP->getFastMathFlags());
702 
703   if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V))
704     if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this))
705       DestGEP->setNoWrapFlags(SrcGEP->getNoWrapFlags() |
706                               DestGEP->getNoWrapFlags());
707 
708   if (auto *NNI = dyn_cast<PossiblyNonNegInst>(V))
709     if (isa<PossiblyNonNegInst>(this))
710       setNonNeg(NNI->hasNonNeg());
711 
712   if (auto *SrcICmp = dyn_cast<ICmpInst>(V))
713     if (auto *DestICmp = dyn_cast<ICmpInst>(this))
714       DestICmp->setSameSign(SrcICmp->hasSameSign());
715 }
716 
andIRFlags(const Value * V)717 void Instruction::andIRFlags(const Value *V) {
718   if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
719     if (isa<OverflowingBinaryOperator>(this)) {
720       setHasNoSignedWrap(hasNoSignedWrap() && OB->hasNoSignedWrap());
721       setHasNoUnsignedWrap(hasNoUnsignedWrap() && OB->hasNoUnsignedWrap());
722     }
723   }
724 
725   if (auto *TI = dyn_cast<TruncInst>(V)) {
726     if (isa<TruncInst>(this)) {
727       setHasNoSignedWrap(hasNoSignedWrap() && TI->hasNoSignedWrap());
728       setHasNoUnsignedWrap(hasNoUnsignedWrap() && TI->hasNoUnsignedWrap());
729     }
730   }
731 
732   if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
733     if (isa<PossiblyExactOperator>(this))
734       setIsExact(isExact() && PE->isExact());
735 
736   if (auto *SrcPD = dyn_cast<PossiblyDisjointInst>(V))
737     if (auto *DestPD = dyn_cast<PossiblyDisjointInst>(this))
738       DestPD->setIsDisjoint(DestPD->isDisjoint() && SrcPD->isDisjoint());
739 
740   if (auto *FP = dyn_cast<FPMathOperator>(V)) {
741     if (isa<FPMathOperator>(this)) {
742       FastMathFlags FM = getFastMathFlags();
743       FM &= FP->getFastMathFlags();
744       copyFastMathFlags(FM);
745     }
746   }
747 
748   if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V))
749     if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this))
750       DestGEP->setNoWrapFlags(SrcGEP->getNoWrapFlags() &
751                               DestGEP->getNoWrapFlags());
752 
753   if (auto *NNI = dyn_cast<PossiblyNonNegInst>(V))
754     if (isa<PossiblyNonNegInst>(this))
755       setNonNeg(hasNonNeg() && NNI->hasNonNeg());
756 
757   if (auto *SrcICmp = dyn_cast<ICmpInst>(V))
758     if (auto *DestICmp = dyn_cast<ICmpInst>(this))
759       DestICmp->setSameSign(DestICmp->hasSameSign() && SrcICmp->hasSameSign());
760 }
761 
getOpcodeName(unsigned OpCode)762 const char *Instruction::getOpcodeName(unsigned OpCode) {
763   switch (OpCode) {
764   // Terminators
765   case Ret:    return "ret";
766   case Br:     return "br";
767   case Switch: return "switch";
768   case IndirectBr: return "indirectbr";
769   case Invoke: return "invoke";
770   case Resume: return "resume";
771   case Unreachable: return "unreachable";
772   case CleanupRet: return "cleanupret";
773   case CatchRet: return "catchret";
774   case CatchPad: return "catchpad";
775   case CatchSwitch: return "catchswitch";
776   case CallBr: return "callbr";
777 
778   // Standard unary operators...
779   case FNeg: return "fneg";
780 
781   // Standard binary operators...
782   case Add: return "add";
783   case FAdd: return "fadd";
784   case Sub: return "sub";
785   case FSub: return "fsub";
786   case Mul: return "mul";
787   case FMul: return "fmul";
788   case UDiv: return "udiv";
789   case SDiv: return "sdiv";
790   case FDiv: return "fdiv";
791   case URem: return "urem";
792   case SRem: return "srem";
793   case FRem: return "frem";
794 
795   // Logical operators...
796   case And: return "and";
797   case Or : return "or";
798   case Xor: return "xor";
799 
800   // Memory instructions...
801   case Alloca:        return "alloca";
802   case Load:          return "load";
803   case Store:         return "store";
804   case AtomicCmpXchg: return "cmpxchg";
805   case AtomicRMW:     return "atomicrmw";
806   case Fence:         return "fence";
807   case GetElementPtr: return "getelementptr";
808 
809   // Convert instructions...
810   case Trunc:         return "trunc";
811   case ZExt:          return "zext";
812   case SExt:          return "sext";
813   case FPTrunc:       return "fptrunc";
814   case FPExt:         return "fpext";
815   case FPToUI:        return "fptoui";
816   case FPToSI:        return "fptosi";
817   case UIToFP:        return "uitofp";
818   case SIToFP:        return "sitofp";
819   case IntToPtr:      return "inttoptr";
820   case PtrToInt:      return "ptrtoint";
821   case BitCast:       return "bitcast";
822   case AddrSpaceCast: return "addrspacecast";
823 
824   // Other instructions...
825   case ICmp:           return "icmp";
826   case FCmp:           return "fcmp";
827   case PHI:            return "phi";
828   case Select:         return "select";
829   case Call:           return "call";
830   case Shl:            return "shl";
831   case LShr:           return "lshr";
832   case AShr:           return "ashr";
833   case VAArg:          return "va_arg";
834   case ExtractElement: return "extractelement";
835   case InsertElement:  return "insertelement";
836   case ShuffleVector:  return "shufflevector";
837   case ExtractValue:   return "extractvalue";
838   case InsertValue:    return "insertvalue";
839   case LandingPad:     return "landingpad";
840   case CleanupPad:     return "cleanuppad";
841   case Freeze:         return "freeze";
842 
843   default: return "<Invalid operator> ";
844   }
845 }
846 
847 /// This must be kept in sync with FunctionComparator::cmpOperations in
848 /// lib/Transforms/IPO/MergeFunctions.cpp.
hasSameSpecialState(const Instruction * I2,bool IgnoreAlignment,bool IntersectAttrs) const849 bool Instruction::hasSameSpecialState(const Instruction *I2,
850                                       bool IgnoreAlignment,
851                                       bool IntersectAttrs) const {
852   auto I1 = this;
853   assert(I1->getOpcode() == I2->getOpcode() &&
854          "Can not compare special state of different instructions");
855 
856   auto CheckAttrsSame = [IntersectAttrs](const CallBase *CB0,
857                                          const CallBase *CB1) {
858     return IntersectAttrs
859                ? CB0->getAttributes()
860                      .intersectWith(CB0->getContext(), CB1->getAttributes())
861                      .has_value()
862                : CB0->getAttributes() == CB1->getAttributes();
863   };
864 
865   if (const AllocaInst *AI = dyn_cast<AllocaInst>(I1))
866     return AI->getAllocatedType() == cast<AllocaInst>(I2)->getAllocatedType() &&
867            (AI->getAlign() == cast<AllocaInst>(I2)->getAlign() ||
868             IgnoreAlignment);
869   if (const LoadInst *LI = dyn_cast<LoadInst>(I1))
870     return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() &&
871            (LI->getAlign() == cast<LoadInst>(I2)->getAlign() ||
872             IgnoreAlignment) &&
873            LI->getOrdering() == cast<LoadInst>(I2)->getOrdering() &&
874            LI->getSyncScopeID() == cast<LoadInst>(I2)->getSyncScopeID();
875   if (const StoreInst *SI = dyn_cast<StoreInst>(I1))
876     return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() &&
877            (SI->getAlign() == cast<StoreInst>(I2)->getAlign() ||
878             IgnoreAlignment) &&
879            SI->getOrdering() == cast<StoreInst>(I2)->getOrdering() &&
880            SI->getSyncScopeID() == cast<StoreInst>(I2)->getSyncScopeID();
881   if (const CmpInst *CI = dyn_cast<CmpInst>(I1))
882     return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate();
883   if (const CallInst *CI = dyn_cast<CallInst>(I1))
884     return CI->isTailCall() == cast<CallInst>(I2)->isTailCall() &&
885            CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() &&
886            CheckAttrsSame(CI, cast<CallInst>(I2)) &&
887            CI->hasIdenticalOperandBundleSchema(*cast<CallInst>(I2));
888   if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1))
889     return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() &&
890            CheckAttrsSame(CI, cast<InvokeInst>(I2)) &&
891            CI->hasIdenticalOperandBundleSchema(*cast<InvokeInst>(I2));
892   if (const CallBrInst *CI = dyn_cast<CallBrInst>(I1))
893     return CI->getCallingConv() == cast<CallBrInst>(I2)->getCallingConv() &&
894            CheckAttrsSame(CI, cast<CallBrInst>(I2)) &&
895            CI->hasIdenticalOperandBundleSchema(*cast<CallBrInst>(I2));
896   if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1))
897     return IVI->getIndices() == cast<InsertValueInst>(I2)->getIndices();
898   if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1))
899     return EVI->getIndices() == cast<ExtractValueInst>(I2)->getIndices();
900   if (const FenceInst *FI = dyn_cast<FenceInst>(I1))
901     return FI->getOrdering() == cast<FenceInst>(I2)->getOrdering() &&
902            FI->getSyncScopeID() == cast<FenceInst>(I2)->getSyncScopeID();
903   if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I1))
904     return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I2)->isVolatile() &&
905            CXI->isWeak() == cast<AtomicCmpXchgInst>(I2)->isWeak() &&
906            CXI->getSuccessOrdering() ==
907                cast<AtomicCmpXchgInst>(I2)->getSuccessOrdering() &&
908            CXI->getFailureOrdering() ==
909                cast<AtomicCmpXchgInst>(I2)->getFailureOrdering() &&
910            CXI->getSyncScopeID() ==
911                cast<AtomicCmpXchgInst>(I2)->getSyncScopeID();
912   if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I1))
913     return RMWI->getOperation() == cast<AtomicRMWInst>(I2)->getOperation() &&
914            RMWI->isVolatile() == cast<AtomicRMWInst>(I2)->isVolatile() &&
915            RMWI->getOrdering() == cast<AtomicRMWInst>(I2)->getOrdering() &&
916            RMWI->getSyncScopeID() == cast<AtomicRMWInst>(I2)->getSyncScopeID();
917   if (const ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I1))
918     return SVI->getShuffleMask() ==
919            cast<ShuffleVectorInst>(I2)->getShuffleMask();
920   if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I1))
921     return GEP->getSourceElementType() ==
922            cast<GetElementPtrInst>(I2)->getSourceElementType();
923 
924   return true;
925 }
926 
isIdenticalTo(const Instruction * I) const927 bool Instruction::isIdenticalTo(const Instruction *I) const {
928   return isIdenticalToWhenDefined(I) &&
929          SubclassOptionalData == I->SubclassOptionalData;
930 }
931 
isIdenticalToWhenDefined(const Instruction * I,bool IntersectAttrs) const932 bool Instruction::isIdenticalToWhenDefined(const Instruction *I,
933                                            bool IntersectAttrs) const {
934   if (getOpcode() != I->getOpcode() ||
935       getNumOperands() != I->getNumOperands() || getType() != I->getType())
936     return false;
937 
938   // If both instructions have no operands, they are identical.
939   if (getNumOperands() == 0 && I->getNumOperands() == 0)
940     return this->hasSameSpecialState(I, /*IgnoreAlignment=*/false,
941                                      IntersectAttrs);
942 
943   // We have two instructions of identical opcode and #operands.  Check to see
944   // if all operands are the same.
945   if (!std::equal(op_begin(), op_end(), I->op_begin()))
946     return false;
947 
948   // WARNING: this logic must be kept in sync with EliminateDuplicatePHINodes()!
949   if (const PHINode *thisPHI = dyn_cast<PHINode>(this)) {
950     const PHINode *otherPHI = cast<PHINode>(I);
951     return std::equal(thisPHI->block_begin(), thisPHI->block_end(),
952                       otherPHI->block_begin());
953   }
954 
955   return this->hasSameSpecialState(I, /*IgnoreAlignment=*/false,
956                                    IntersectAttrs);
957 }
958 
959 // Keep this in sync with FunctionComparator::cmpOperations in
960 // lib/Transforms/IPO/MergeFunctions.cpp.
isSameOperationAs(const Instruction * I,unsigned flags) const961 bool Instruction::isSameOperationAs(const Instruction *I,
962                                     unsigned flags) const {
963   bool IgnoreAlignment = flags & CompareIgnoringAlignment;
964   bool UseScalarTypes = flags & CompareUsingScalarTypes;
965   bool IntersectAttrs = flags & CompareUsingIntersectedAttrs;
966 
967   if (getOpcode() != I->getOpcode() ||
968       getNumOperands() != I->getNumOperands() ||
969       (UseScalarTypes ?
970        getType()->getScalarType() != I->getType()->getScalarType() :
971        getType() != I->getType()))
972     return false;
973 
974   // We have two instructions of identical opcode and #operands.  Check to see
975   // if all operands are the same type
976   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
977     if (UseScalarTypes ?
978         getOperand(i)->getType()->getScalarType() !=
979           I->getOperand(i)->getType()->getScalarType() :
980         getOperand(i)->getType() != I->getOperand(i)->getType())
981       return false;
982 
983   return this->hasSameSpecialState(I, IgnoreAlignment, IntersectAttrs);
984 }
985 
isUsedOutsideOfBlock(const BasicBlock * BB) const986 bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const {
987   for (const Use &U : uses()) {
988     // PHI nodes uses values in the corresponding predecessor block.  For other
989     // instructions, just check to see whether the parent of the use matches up.
990     const Instruction *I = cast<Instruction>(U.getUser());
991     const PHINode *PN = dyn_cast<PHINode>(I);
992     if (!PN) {
993       if (I->getParent() != BB)
994         return true;
995       continue;
996     }
997 
998     if (PN->getIncomingBlock(U) != BB)
999       return true;
1000   }
1001   return false;
1002 }
1003 
mayReadFromMemory() const1004 bool Instruction::mayReadFromMemory() const {
1005   switch (getOpcode()) {
1006   default: return false;
1007   case Instruction::VAArg:
1008   case Instruction::Load:
1009   case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory
1010   case Instruction::AtomicCmpXchg:
1011   case Instruction::AtomicRMW:
1012   case Instruction::CatchPad:
1013   case Instruction::CatchRet:
1014     return true;
1015   case Instruction::Call:
1016   case Instruction::Invoke:
1017   case Instruction::CallBr:
1018     return !cast<CallBase>(this)->onlyWritesMemory();
1019   case Instruction::Store:
1020     return !cast<StoreInst>(this)->isUnordered();
1021   }
1022 }
1023 
mayWriteToMemory() const1024 bool Instruction::mayWriteToMemory() const {
1025   switch (getOpcode()) {
1026   default: return false;
1027   case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory
1028   case Instruction::Store:
1029   case Instruction::VAArg:
1030   case Instruction::AtomicCmpXchg:
1031   case Instruction::AtomicRMW:
1032   case Instruction::CatchPad:
1033   case Instruction::CatchRet:
1034     return true;
1035   case Instruction::Call:
1036   case Instruction::Invoke:
1037   case Instruction::CallBr:
1038     return !cast<CallBase>(this)->onlyReadsMemory();
1039   case Instruction::Load:
1040     return !cast<LoadInst>(this)->isUnordered();
1041   }
1042 }
1043 
isAtomic() const1044 bool Instruction::isAtomic() const {
1045   switch (getOpcode()) {
1046   default:
1047     return false;
1048   case Instruction::AtomicCmpXchg:
1049   case Instruction::AtomicRMW:
1050   case Instruction::Fence:
1051     return true;
1052   case Instruction::Load:
1053     return cast<LoadInst>(this)->getOrdering() != AtomicOrdering::NotAtomic;
1054   case Instruction::Store:
1055     return cast<StoreInst>(this)->getOrdering() != AtomicOrdering::NotAtomic;
1056   }
1057 }
1058 
hasAtomicLoad() const1059 bool Instruction::hasAtomicLoad() const {
1060   assert(isAtomic());
1061   switch (getOpcode()) {
1062   default:
1063     return false;
1064   case Instruction::AtomicCmpXchg:
1065   case Instruction::AtomicRMW:
1066   case Instruction::Load:
1067     return true;
1068   }
1069 }
1070 
hasAtomicStore() const1071 bool Instruction::hasAtomicStore() const {
1072   assert(isAtomic());
1073   switch (getOpcode()) {
1074   default:
1075     return false;
1076   case Instruction::AtomicCmpXchg:
1077   case Instruction::AtomicRMW:
1078   case Instruction::Store:
1079     return true;
1080   }
1081 }
1082 
isVolatile() const1083 bool Instruction::isVolatile() const {
1084   switch (getOpcode()) {
1085   default:
1086     return false;
1087   case Instruction::AtomicRMW:
1088     return cast<AtomicRMWInst>(this)->isVolatile();
1089   case Instruction::Store:
1090     return cast<StoreInst>(this)->isVolatile();
1091   case Instruction::Load:
1092     return cast<LoadInst>(this)->isVolatile();
1093   case Instruction::AtomicCmpXchg:
1094     return cast<AtomicCmpXchgInst>(this)->isVolatile();
1095   case Instruction::Call:
1096   case Instruction::Invoke:
1097     // There are a very limited number of intrinsics with volatile flags.
1098     if (auto *II = dyn_cast<IntrinsicInst>(this)) {
1099       if (auto *MI = dyn_cast<MemIntrinsic>(II))
1100         return MI->isVolatile();
1101       switch (II->getIntrinsicID()) {
1102       default: break;
1103       case Intrinsic::matrix_column_major_load:
1104         return cast<ConstantInt>(II->getArgOperand(2))->isOne();
1105       case Intrinsic::matrix_column_major_store:
1106         return cast<ConstantInt>(II->getArgOperand(3))->isOne();
1107       }
1108     }
1109     return false;
1110   }
1111 }
1112 
getAccessType() const1113 Type *Instruction::getAccessType() const {
1114   switch (getOpcode()) {
1115   case Instruction::Store:
1116     return cast<StoreInst>(this)->getValueOperand()->getType();
1117   case Instruction::Load:
1118   case Instruction::AtomicRMW:
1119     return getType();
1120   case Instruction::AtomicCmpXchg:
1121     return cast<AtomicCmpXchgInst>(this)->getNewValOperand()->getType();
1122   case Instruction::Call:
1123   case Instruction::Invoke:
1124     if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(this)) {
1125       switch (II->getIntrinsicID()) {
1126       case Intrinsic::masked_load:
1127       case Intrinsic::masked_gather:
1128       case Intrinsic::masked_expandload:
1129       case Intrinsic::vp_load:
1130       case Intrinsic::vp_gather:
1131       case Intrinsic::experimental_vp_strided_load:
1132         return II->getType();
1133       case Intrinsic::masked_store:
1134       case Intrinsic::masked_scatter:
1135       case Intrinsic::masked_compressstore:
1136       case Intrinsic::vp_store:
1137       case Intrinsic::vp_scatter:
1138       case Intrinsic::experimental_vp_strided_store:
1139         return II->getOperand(0)->getType();
1140       default:
1141         break;
1142       }
1143     }
1144   }
1145 
1146   return nullptr;
1147 }
1148 
canUnwindPastLandingPad(const LandingPadInst * LP,bool IncludePhaseOneUnwind)1149 static bool canUnwindPastLandingPad(const LandingPadInst *LP,
1150                                     bool IncludePhaseOneUnwind) {
1151   // Because phase one unwinding skips cleanup landingpads, we effectively
1152   // unwind past this frame, and callers need to have valid unwind info.
1153   if (LP->isCleanup())
1154     return IncludePhaseOneUnwind;
1155 
1156   for (unsigned I = 0; I < LP->getNumClauses(); ++I) {
1157     Constant *Clause = LP->getClause(I);
1158     // catch ptr null catches all exceptions.
1159     if (LP->isCatch(I) && isa<ConstantPointerNull>(Clause))
1160       return false;
1161     // filter [0 x ptr] catches all exceptions.
1162     if (LP->isFilter(I) && Clause->getType()->getArrayNumElements() == 0)
1163       return false;
1164   }
1165 
1166   // May catch only some subset of exceptions, in which case other exceptions
1167   // will continue unwinding.
1168   return true;
1169 }
1170 
mayThrow(bool IncludePhaseOneUnwind) const1171 bool Instruction::mayThrow(bool IncludePhaseOneUnwind) const {
1172   switch (getOpcode()) {
1173   case Instruction::Call:
1174     return !cast<CallInst>(this)->doesNotThrow();
1175   case Instruction::CleanupRet:
1176     return cast<CleanupReturnInst>(this)->unwindsToCaller();
1177   case Instruction::CatchSwitch:
1178     return cast<CatchSwitchInst>(this)->unwindsToCaller();
1179   case Instruction::Resume:
1180     return true;
1181   case Instruction::Invoke: {
1182     // Landingpads themselves don't unwind -- however, an invoke of a skipped
1183     // landingpad may continue unwinding.
1184     BasicBlock *UnwindDest = cast<InvokeInst>(this)->getUnwindDest();
1185     BasicBlock::iterator Pad = UnwindDest->getFirstNonPHIIt();
1186     if (auto *LP = dyn_cast<LandingPadInst>(Pad))
1187       return canUnwindPastLandingPad(LP, IncludePhaseOneUnwind);
1188     return false;
1189   }
1190   case Instruction::CleanupPad:
1191     // Treat the same as cleanup landingpad.
1192     return IncludePhaseOneUnwind;
1193   default:
1194     return false;
1195   }
1196 }
1197 
mayHaveSideEffects() const1198 bool Instruction::mayHaveSideEffects() const {
1199   return mayWriteToMemory() || mayThrow() || !willReturn();
1200 }
1201 
isSafeToRemove() const1202 bool Instruction::isSafeToRemove() const {
1203   return (!isa<CallInst>(this) || !this->mayHaveSideEffects()) &&
1204          !this->isTerminator() && !this->isEHPad();
1205 }
1206 
willReturn() const1207 bool Instruction::willReturn() const {
1208   // Volatile store isn't guaranteed to return; see LangRef.
1209   if (auto *SI = dyn_cast<StoreInst>(this))
1210     return !SI->isVolatile();
1211 
1212   if (const auto *CB = dyn_cast<CallBase>(this))
1213     return CB->hasFnAttr(Attribute::WillReturn);
1214   return true;
1215 }
1216 
isLifetimeStartOrEnd() const1217 bool Instruction::isLifetimeStartOrEnd() const {
1218   auto *II = dyn_cast<IntrinsicInst>(this);
1219   if (!II)
1220     return false;
1221   Intrinsic::ID ID = II->getIntrinsicID();
1222   return ID == Intrinsic::lifetime_start || ID == Intrinsic::lifetime_end;
1223 }
1224 
isLaunderOrStripInvariantGroup() const1225 bool Instruction::isLaunderOrStripInvariantGroup() const {
1226   auto *II = dyn_cast<IntrinsicInst>(this);
1227   if (!II)
1228     return false;
1229   Intrinsic::ID ID = II->getIntrinsicID();
1230   return ID == Intrinsic::launder_invariant_group ||
1231          ID == Intrinsic::strip_invariant_group;
1232 }
1233 
isDebugOrPseudoInst() const1234 bool Instruction::isDebugOrPseudoInst() const {
1235   return isa<DbgInfoIntrinsic>(this) || isa<PseudoProbeInst>(this);
1236 }
1237 
1238 const Instruction *
getNextNonDebugInstruction(bool SkipPseudoOp) const1239 Instruction::getNextNonDebugInstruction(bool SkipPseudoOp) const {
1240   for (const Instruction *I = getNextNode(); I; I = I->getNextNode())
1241     if (!isa<DbgInfoIntrinsic>(I) && !(SkipPseudoOp && isa<PseudoProbeInst>(I)))
1242       return I;
1243   return nullptr;
1244 }
1245 
1246 const Instruction *
getPrevNonDebugInstruction(bool SkipPseudoOp) const1247 Instruction::getPrevNonDebugInstruction(bool SkipPseudoOp) const {
1248   for (const Instruction *I = getPrevNode(); I; I = I->getPrevNode())
1249     if (!isa<DbgInfoIntrinsic>(I) && !(SkipPseudoOp && isa<PseudoProbeInst>(I)))
1250       return I;
1251   return nullptr;
1252 }
1253 
getStableDebugLoc() const1254 const DebugLoc &Instruction::getStableDebugLoc() const {
1255   if (isa<DbgInfoIntrinsic>(this))
1256     if (const Instruction *Next = getNextNonDebugInstruction())
1257       return Next->getDebugLoc();
1258   return getDebugLoc();
1259 }
1260 
isAssociative() const1261 bool Instruction::isAssociative() const {
1262   if (auto *II = dyn_cast<IntrinsicInst>(this))
1263     return II->isAssociative();
1264   unsigned Opcode = getOpcode();
1265   if (isAssociative(Opcode))
1266     return true;
1267 
1268   switch (Opcode) {
1269   case FMul:
1270   case FAdd:
1271     return cast<FPMathOperator>(this)->hasAllowReassoc() &&
1272            cast<FPMathOperator>(this)->hasNoSignedZeros();
1273   default:
1274     return false;
1275   }
1276 }
1277 
isCommutative() const1278 bool Instruction::isCommutative() const {
1279   if (auto *II = dyn_cast<IntrinsicInst>(this))
1280     return II->isCommutative();
1281   // TODO: Should allow icmp/fcmp?
1282   return isCommutative(getOpcode());
1283 }
1284 
getNumSuccessors() const1285 unsigned Instruction::getNumSuccessors() const {
1286   switch (getOpcode()) {
1287 #define HANDLE_TERM_INST(N, OPC, CLASS)                                        \
1288   case Instruction::OPC:                                                       \
1289     return static_cast<const CLASS *>(this)->getNumSuccessors();
1290 #include "llvm/IR/Instruction.def"
1291   default:
1292     break;
1293   }
1294   llvm_unreachable("not a terminator");
1295 }
1296 
getSuccessor(unsigned idx) const1297 BasicBlock *Instruction::getSuccessor(unsigned idx) const {
1298   switch (getOpcode()) {
1299 #define HANDLE_TERM_INST(N, OPC, CLASS)                                        \
1300   case Instruction::OPC:                                                       \
1301     return static_cast<const CLASS *>(this)->getSuccessor(idx);
1302 #include "llvm/IR/Instruction.def"
1303   default:
1304     break;
1305   }
1306   llvm_unreachable("not a terminator");
1307 }
1308 
setSuccessor(unsigned idx,BasicBlock * B)1309 void Instruction::setSuccessor(unsigned idx, BasicBlock *B) {
1310   switch (getOpcode()) {
1311 #define HANDLE_TERM_INST(N, OPC, CLASS)                                        \
1312   case Instruction::OPC:                                                       \
1313     return static_cast<CLASS *>(this)->setSuccessor(idx, B);
1314 #include "llvm/IR/Instruction.def"
1315   default:
1316     break;
1317   }
1318   llvm_unreachable("not a terminator");
1319 }
1320 
replaceSuccessorWith(BasicBlock * OldBB,BasicBlock * NewBB)1321 void Instruction::replaceSuccessorWith(BasicBlock *OldBB, BasicBlock *NewBB) {
1322   for (unsigned Idx = 0, NumSuccessors = Instruction::getNumSuccessors();
1323        Idx != NumSuccessors; ++Idx)
1324     if (getSuccessor(Idx) == OldBB)
1325       setSuccessor(Idx, NewBB);
1326 }
1327 
cloneImpl() const1328 Instruction *Instruction::cloneImpl() const {
1329   llvm_unreachable("Subclass of Instruction failed to implement cloneImpl");
1330 }
1331 
swapProfMetadata()1332 void Instruction::swapProfMetadata() {
1333   MDNode *ProfileData = getBranchWeightMDNode(*this);
1334   if (!ProfileData)
1335     return;
1336   unsigned FirstIdx = getBranchWeightOffset(ProfileData);
1337   if (ProfileData->getNumOperands() != 2 + FirstIdx)
1338     return;
1339 
1340   unsigned SecondIdx = FirstIdx + 1;
1341   SmallVector<Metadata *, 4> Ops;
1342   // If there are more weights past the second, we can't swap them
1343   if (ProfileData->getNumOperands() > SecondIdx + 1)
1344     return;
1345   for (unsigned Idx = 0; Idx < FirstIdx; ++Idx) {
1346     Ops.push_back(ProfileData->getOperand(Idx));
1347   }
1348   // Switch the order of the weights
1349   Ops.push_back(ProfileData->getOperand(SecondIdx));
1350   Ops.push_back(ProfileData->getOperand(FirstIdx));
1351   setMetadata(LLVMContext::MD_prof,
1352               MDNode::get(ProfileData->getContext(), Ops));
1353 }
1354 
copyMetadata(const Instruction & SrcInst,ArrayRef<unsigned> WL)1355 void Instruction::copyMetadata(const Instruction &SrcInst,
1356                                ArrayRef<unsigned> WL) {
1357   if (WL.empty() || is_contained(WL, LLVMContext::MD_dbg))
1358     setDebugLoc(SrcInst.getDebugLoc().orElse(getDebugLoc()));
1359 
1360   if (!SrcInst.hasMetadata())
1361     return;
1362 
1363   SmallDenseSet<unsigned, 4> WLS(WL.begin(), WL.end());
1364 
1365   // Otherwise, enumerate and copy over metadata from the old instruction to the
1366   // new one.
1367   SmallVector<std::pair<unsigned, MDNode *>, 4> TheMDs;
1368   SrcInst.getAllMetadataOtherThanDebugLoc(TheMDs);
1369   for (const auto &MD : TheMDs) {
1370     if (WL.empty() || WLS.count(MD.first))
1371       setMetadata(MD.first, MD.second);
1372   }
1373 }
1374 
clone() const1375 Instruction *Instruction::clone() const {
1376   Instruction *New = nullptr;
1377   switch (getOpcode()) {
1378   default:
1379     llvm_unreachable("Unhandled Opcode.");
1380 #define HANDLE_INST(num, opc, clas)                                            \
1381   case Instruction::opc:                                                       \
1382     New = cast<clas>(this)->cloneImpl();                                       \
1383     break;
1384 #include "llvm/IR/Instruction.def"
1385 #undef HANDLE_INST
1386   }
1387 
1388   New->SubclassOptionalData = SubclassOptionalData;
1389   New->copyMetadata(*this);
1390   return New;
1391 }
1392