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