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