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