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