10b57cec5SDimitry Andric //===-- Instruction.cpp - Implement the Instruction class -----------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements the Instruction class for the IR library. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "llvm/IR/Instruction.h" 140b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h" 1506c3fb27SDimitry Andric #include "llvm/IR/AttributeMask.h" 16*0fca6ea1SDimitry Andric #include "llvm/IR/Attributes.h" 170b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 18*0fca6ea1SDimitry Andric #include "llvm/IR/InstrTypes.h" 190b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 20fe6060f1SDimitry Andric #include "llvm/IR/IntrinsicInst.h" 21fe6060f1SDimitry Andric #include "llvm/IR/Intrinsics.h" 22*0fca6ea1SDimitry Andric #include "llvm/IR/MemoryModelRelaxationAnnotations.h" 23*0fca6ea1SDimitry Andric #include "llvm/IR/Module.h" 240b57cec5SDimitry Andric #include "llvm/IR/Operator.h" 25bdd1243dSDimitry Andric #include "llvm/IR/ProfDataUtils.h" 260b57cec5SDimitry Andric #include "llvm/IR/Type.h" 270b57cec5SDimitry Andric using namespace llvm; 280b57cec5SDimitry Andric 29*0fca6ea1SDimitry Andric InsertPosition::InsertPosition(Instruction *InsertBefore) 30*0fca6ea1SDimitry Andric : InsertAt(InsertBefore ? InsertBefore->getIterator() 31*0fca6ea1SDimitry Andric : InstListType::iterator()) {} 32*0fca6ea1SDimitry Andric InsertPosition::InsertPosition(BasicBlock *InsertAtEnd) 33*0fca6ea1SDimitry Andric : InsertAt(InsertAtEnd ? InsertAtEnd->end() : InstListType::iterator()) {} 340b57cec5SDimitry Andric 35*0fca6ea1SDimitry Andric Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps, 36*0fca6ea1SDimitry Andric InsertPosition InsertBefore) 37*0fca6ea1SDimitry Andric : User(ty, Value::InstructionVal + it, Ops, NumOps) { 38*0fca6ea1SDimitry Andric // When called with an iterator, there must be a block to insert into. 39*0fca6ea1SDimitry Andric if (InstListType::iterator InsertIt = InsertBefore; InsertIt.isValid()) { 40*0fca6ea1SDimitry Andric BasicBlock *BB = InsertIt.getNodeParent(); 410b57cec5SDimitry Andric assert(BB && "Instruction to insert before is not in a basic block!"); 42*0fca6ea1SDimitry Andric insertInto(BB, InsertBefore); 430b57cec5SDimitry Andric } 440b57cec5SDimitry Andric } 450b57cec5SDimitry Andric 460b57cec5SDimitry Andric Instruction::~Instruction() { 47*0fca6ea1SDimitry Andric assert(!getParent() && "Instruction still linked in the program!"); 485ffd83dbSDimitry Andric 495ffd83dbSDimitry Andric // Replace any extant metadata uses of this instruction with undef to 505ffd83dbSDimitry Andric // preserve debug info accuracy. Some alternatives include: 515ffd83dbSDimitry Andric // - Treat Instruction like any other Value, and point its extant metadata 525ffd83dbSDimitry Andric // uses to an empty ValueAsMetadata node. This makes extant dbg.value uses 535ffd83dbSDimitry Andric // trivially dead (i.e. fair game for deletion in many passes), leading to 545ffd83dbSDimitry Andric // stale dbg.values being in effect for too long. 555ffd83dbSDimitry Andric // - Call salvageDebugInfoOrMarkUndef. Not needed to make instruction removal 565ffd83dbSDimitry Andric // correct. OTOH results in wasted work in some common cases (e.g. when all 575ffd83dbSDimitry Andric // instructions in a BasicBlock are deleted). 585ffd83dbSDimitry Andric if (isUsedByMetadata()) 595ffd83dbSDimitry Andric ValueAsMetadata::handleRAUW(this, UndefValue::get(getType())); 60bdd1243dSDimitry Andric 61bdd1243dSDimitry Andric // Explicitly remove DIAssignID metadata to clear up ID -> Instruction(s) 62bdd1243dSDimitry Andric // mapping in LLVMContext. 63bdd1243dSDimitry Andric setMetadata(LLVMContext::MD_DIAssignID, nullptr); 640b57cec5SDimitry Andric } 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric const Module *Instruction::getModule() const { 670b57cec5SDimitry Andric return getParent()->getModule(); 680b57cec5SDimitry Andric } 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric const Function *Instruction::getFunction() const { 710b57cec5SDimitry Andric return getParent()->getParent(); 720b57cec5SDimitry Andric } 730b57cec5SDimitry Andric 74*0fca6ea1SDimitry Andric const DataLayout &Instruction::getDataLayout() const { 75*0fca6ea1SDimitry Andric return getModule()->getDataLayout(); 76*0fca6ea1SDimitry Andric } 77*0fca6ea1SDimitry Andric 780b57cec5SDimitry Andric void Instruction::removeFromParent() { 795f757f3fSDimitry Andric // Perform any debug-info maintenence required. 805f757f3fSDimitry Andric handleMarkerRemoval(); 815f757f3fSDimitry Andric 820b57cec5SDimitry Andric getParent()->getInstList().remove(getIterator()); 830b57cec5SDimitry Andric } 840b57cec5SDimitry Andric 855f757f3fSDimitry Andric void Instruction::handleMarkerRemoval() { 86*0fca6ea1SDimitry Andric if (!getParent()->IsNewDbgInfoFormat || !DebugMarker) 875f757f3fSDimitry Andric return; 885f757f3fSDimitry Andric 89*0fca6ea1SDimitry Andric DebugMarker->removeMarker(); 905f757f3fSDimitry Andric } 915f757f3fSDimitry Andric 925f757f3fSDimitry Andric BasicBlock::iterator Instruction::eraseFromParent() { 935f757f3fSDimitry Andric handleMarkerRemoval(); 940b57cec5SDimitry Andric return getParent()->getInstList().erase(getIterator()); 950b57cec5SDimitry Andric } 960b57cec5SDimitry Andric 975f757f3fSDimitry Andric void Instruction::insertBefore(Instruction *InsertPos) { 985f757f3fSDimitry Andric insertBefore(InsertPos->getIterator()); 995f757f3fSDimitry Andric } 1005f757f3fSDimitry Andric 1010b57cec5SDimitry Andric /// Insert an unlinked instruction into a basic block immediately before the 1020b57cec5SDimitry Andric /// specified instruction. 1035f757f3fSDimitry Andric void Instruction::insertBefore(BasicBlock::iterator InsertPos) { 1045f757f3fSDimitry Andric insertBefore(*InsertPos->getParent(), InsertPos); 1050b57cec5SDimitry Andric } 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric /// Insert an unlinked instruction into a basic block immediately after the 1080b57cec5SDimitry Andric /// specified instruction. 1090b57cec5SDimitry Andric void Instruction::insertAfter(Instruction *InsertPos) { 1105f757f3fSDimitry Andric BasicBlock *DestParent = InsertPos->getParent(); 1115f757f3fSDimitry Andric 1125f757f3fSDimitry Andric DestParent->getInstList().insertAfter(InsertPos->getIterator(), this); 113bdd1243dSDimitry Andric } 114bdd1243dSDimitry Andric 115bdd1243dSDimitry Andric BasicBlock::iterator Instruction::insertInto(BasicBlock *ParentBB, 116bdd1243dSDimitry Andric BasicBlock::iterator It) { 117bdd1243dSDimitry Andric assert(getParent() == nullptr && "Expected detached instruction"); 118bdd1243dSDimitry Andric assert((It == ParentBB->end() || It->getParent() == ParentBB) && 119bdd1243dSDimitry Andric "It not in ParentBB"); 1205f757f3fSDimitry Andric insertBefore(*ParentBB, It); 1215f757f3fSDimitry Andric return getIterator(); 1225f757f3fSDimitry Andric } 1235f757f3fSDimitry Andric 1245f757f3fSDimitry Andric extern cl::opt<bool> UseNewDbgInfoFormat; 1255f757f3fSDimitry Andric 1265f757f3fSDimitry Andric void Instruction::insertBefore(BasicBlock &BB, 1275f757f3fSDimitry Andric InstListType::iterator InsertPos) { 128*0fca6ea1SDimitry Andric assert(!DebugMarker); 1295f757f3fSDimitry Andric 1305f757f3fSDimitry Andric BB.getInstList().insert(InsertPos, this); 1315f757f3fSDimitry Andric 1325f757f3fSDimitry Andric if (!BB.IsNewDbgInfoFormat) 1335f757f3fSDimitry Andric return; 1345f757f3fSDimitry Andric 1355f757f3fSDimitry Andric // We've inserted "this": if InsertAtHead is set then it comes before any 136*0fca6ea1SDimitry Andric // DbgVariableRecords attached to InsertPos. But if it's not set, then any 137*0fca6ea1SDimitry Andric // DbgRecords should now come before "this". 1385f757f3fSDimitry Andric bool InsertAtHead = InsertPos.getHeadBit(); 1395f757f3fSDimitry Andric if (!InsertAtHead) { 140*0fca6ea1SDimitry Andric DbgMarker *SrcMarker = BB.getMarker(InsertPos); 141*0fca6ea1SDimitry Andric if (SrcMarker && !SrcMarker->empty()) { 142*0fca6ea1SDimitry Andric // If this assertion fires, the calling code is about to insert a PHI 143*0fca6ea1SDimitry Andric // after debug-records, which would form a sequence like: 144*0fca6ea1SDimitry Andric // %0 = PHI 145*0fca6ea1SDimitry Andric // #dbg_value 146*0fca6ea1SDimitry Andric // %1 = PHI 147*0fca6ea1SDimitry Andric // Which is de-normalised and undesired -- hence the assertion. To avoid 148*0fca6ea1SDimitry Andric // this, you must insert at that position using an iterator, and it must 149*0fca6ea1SDimitry Andric // be aquired by calling getFirstNonPHIIt / begin or similar methods on 150*0fca6ea1SDimitry Andric // the block. This will signal to this behind-the-scenes debug-info 151*0fca6ea1SDimitry Andric // maintenence code that you intend the PHI to be ahead of everything, 152*0fca6ea1SDimitry Andric // including any debug-info. 153*0fca6ea1SDimitry Andric assert(!isa<PHINode>(this) && "Inserting PHI after debug-records!"); 154*0fca6ea1SDimitry Andric adoptDbgRecords(&BB, InsertPos, false); 155*0fca6ea1SDimitry Andric } 1565f757f3fSDimitry Andric } 1575f757f3fSDimitry Andric 1585f757f3fSDimitry Andric // If we're inserting a terminator, check if we need to flush out 159*0fca6ea1SDimitry Andric // TrailingDbgRecords. Inserting instructions at the end of an incomplete 160*0fca6ea1SDimitry Andric // block is handled by the code block above. 1615f757f3fSDimitry Andric if (isTerminator()) 162*0fca6ea1SDimitry Andric getParent()->flushTerminatorDbgRecords(); 1630b57cec5SDimitry Andric } 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric /// Unlink this instruction from its current basic block and insert it into the 1660b57cec5SDimitry Andric /// basic block that MovePos lives in, right before MovePos. 1670b57cec5SDimitry Andric void Instruction::moveBefore(Instruction *MovePos) { 1685f757f3fSDimitry Andric moveBeforeImpl(*MovePos->getParent(), MovePos->getIterator(), false); 1695f757f3fSDimitry Andric } 1705f757f3fSDimitry Andric 1715f757f3fSDimitry Andric void Instruction::moveBeforePreserving(Instruction *MovePos) { 1725f757f3fSDimitry Andric moveBeforeImpl(*MovePos->getParent(), MovePos->getIterator(), true); 1730b57cec5SDimitry Andric } 1740b57cec5SDimitry Andric 1750b57cec5SDimitry Andric void Instruction::moveAfter(Instruction *MovePos) { 1765f757f3fSDimitry Andric auto NextIt = std::next(MovePos->getIterator()); 1775f757f3fSDimitry Andric // We want this instruction to be moved to before NextIt in the instruction 1785f757f3fSDimitry Andric // list, but before NextIt's debug value range. 1795f757f3fSDimitry Andric NextIt.setHeadBit(true); 1805f757f3fSDimitry Andric moveBeforeImpl(*MovePos->getParent(), NextIt, false); 1810b57cec5SDimitry Andric } 1820b57cec5SDimitry Andric 1835f757f3fSDimitry Andric void Instruction::moveAfterPreserving(Instruction *MovePos) { 1845f757f3fSDimitry Andric auto NextIt = std::next(MovePos->getIterator()); 1855f757f3fSDimitry Andric // We want this instruction and its debug range to be moved to before NextIt 1865f757f3fSDimitry Andric // in the instruction list, but before NextIt's debug value range. 1875f757f3fSDimitry Andric NextIt.setHeadBit(true); 1885f757f3fSDimitry Andric moveBeforeImpl(*MovePos->getParent(), NextIt, true); 1895f757f3fSDimitry Andric } 1905f757f3fSDimitry Andric 1915f757f3fSDimitry Andric void Instruction::moveBefore(BasicBlock &BB, InstListType::iterator I) { 1925f757f3fSDimitry Andric moveBeforeImpl(BB, I, false); 1935f757f3fSDimitry Andric } 1945f757f3fSDimitry Andric 1955f757f3fSDimitry Andric void Instruction::moveBeforePreserving(BasicBlock &BB, 1965f757f3fSDimitry Andric InstListType::iterator I) { 1975f757f3fSDimitry Andric moveBeforeImpl(BB, I, true); 1985f757f3fSDimitry Andric } 1995f757f3fSDimitry Andric 2005f757f3fSDimitry Andric void Instruction::moveBeforeImpl(BasicBlock &BB, InstListType::iterator I, 2015f757f3fSDimitry Andric bool Preserve) { 2020b57cec5SDimitry Andric assert(I == BB.end() || I->getParent() == &BB); 2035f757f3fSDimitry Andric bool InsertAtHead = I.getHeadBit(); 2045f757f3fSDimitry Andric 205*0fca6ea1SDimitry Andric // If we've been given the "Preserve" flag, then just move the DbgRecords with 2065f757f3fSDimitry Andric // the instruction, no more special handling needed. 207*0fca6ea1SDimitry Andric if (BB.IsNewDbgInfoFormat && DebugMarker && !Preserve) { 2085f757f3fSDimitry Andric if (I != this->getIterator() || InsertAtHead) { 2095f757f3fSDimitry Andric // "this" is definitely moving in the list, or it's moving ahead of its 210*0fca6ea1SDimitry Andric // attached DbgVariableRecords. Detach any existing DbgRecords. 2115f757f3fSDimitry Andric handleMarkerRemoval(); 2125f757f3fSDimitry Andric } 2135f757f3fSDimitry Andric } 2145f757f3fSDimitry Andric 2155f757f3fSDimitry Andric // Move this single instruction. Use the list splice method directly, not 2165f757f3fSDimitry Andric // the block splicer, which will do more debug-info things. 2175f757f3fSDimitry Andric BB.getInstList().splice(I, getParent()->getInstList(), getIterator()); 2185f757f3fSDimitry Andric 2195f757f3fSDimitry Andric if (BB.IsNewDbgInfoFormat && !Preserve) { 220*0fca6ea1SDimitry Andric DbgMarker *NextMarker = getParent()->getNextMarker(this); 2215f757f3fSDimitry Andric 222*0fca6ea1SDimitry Andric // If we're inserting at point I, and not in front of the DbgRecords 223*0fca6ea1SDimitry Andric // attached there, then we should absorb the DbgRecords attached to I. 224*0fca6ea1SDimitry Andric if (!InsertAtHead && NextMarker && !NextMarker->empty()) { 225*0fca6ea1SDimitry Andric adoptDbgRecords(&BB, I, false); 226*0fca6ea1SDimitry Andric } 2275f757f3fSDimitry Andric } 2285f757f3fSDimitry Andric 2295f757f3fSDimitry Andric if (isTerminator()) 230*0fca6ea1SDimitry Andric getParent()->flushTerminatorDbgRecords(); 2315f757f3fSDimitry Andric } 2325f757f3fSDimitry Andric 233*0fca6ea1SDimitry Andric iterator_range<DbgRecord::self_iterator> Instruction::cloneDebugInfoFrom( 234*0fca6ea1SDimitry Andric const Instruction *From, std::optional<DbgRecord::self_iterator> FromHere, 2355f757f3fSDimitry Andric bool InsertAtHead) { 236*0fca6ea1SDimitry Andric if (!From->DebugMarker) 237*0fca6ea1SDimitry Andric return DbgMarker::getEmptyDbgRecordRange(); 2385f757f3fSDimitry Andric 2395f757f3fSDimitry Andric assert(getParent()->IsNewDbgInfoFormat); 2405f757f3fSDimitry Andric assert(getParent()->IsNewDbgInfoFormat == 2415f757f3fSDimitry Andric From->getParent()->IsNewDbgInfoFormat); 2425f757f3fSDimitry Andric 243*0fca6ea1SDimitry Andric if (!DebugMarker) 2445f757f3fSDimitry Andric getParent()->createMarker(this); 2455f757f3fSDimitry Andric 246*0fca6ea1SDimitry Andric return DebugMarker->cloneDebugInfoFrom(From->DebugMarker, FromHere, 247*0fca6ea1SDimitry Andric InsertAtHead); 2485f757f3fSDimitry Andric } 2495f757f3fSDimitry Andric 250*0fca6ea1SDimitry Andric std::optional<DbgRecord::self_iterator> 251*0fca6ea1SDimitry Andric Instruction::getDbgReinsertionPosition() { 2525f757f3fSDimitry Andric // Is there a marker on the next instruction? 253*0fca6ea1SDimitry Andric DbgMarker *NextMarker = getParent()->getNextMarker(this); 2545f757f3fSDimitry Andric if (!NextMarker) 2555f757f3fSDimitry Andric return std::nullopt; 2565f757f3fSDimitry Andric 257*0fca6ea1SDimitry Andric // Are there any DbgRecords in the next marker? 258*0fca6ea1SDimitry Andric if (NextMarker->StoredDbgRecords.empty()) 2595f757f3fSDimitry Andric return std::nullopt; 2605f757f3fSDimitry Andric 261*0fca6ea1SDimitry Andric return NextMarker->StoredDbgRecords.begin(); 2625f757f3fSDimitry Andric } 2635f757f3fSDimitry Andric 264*0fca6ea1SDimitry Andric bool Instruction::hasDbgRecords() const { return !getDbgRecordRange().empty(); } 2655f757f3fSDimitry Andric 266*0fca6ea1SDimitry Andric void Instruction::adoptDbgRecords(BasicBlock *BB, BasicBlock::iterator It, 267*0fca6ea1SDimitry Andric bool InsertAtHead) { 268*0fca6ea1SDimitry Andric DbgMarker *SrcMarker = BB->getMarker(It); 269*0fca6ea1SDimitry Andric auto ReleaseTrailingDbgRecords = [BB, It, SrcMarker]() { 270*0fca6ea1SDimitry Andric if (BB->end() == It) { 271*0fca6ea1SDimitry Andric SrcMarker->eraseFromParent(); 272*0fca6ea1SDimitry Andric BB->deleteTrailingDbgRecords(); 273*0fca6ea1SDimitry Andric } 274*0fca6ea1SDimitry Andric }; 275*0fca6ea1SDimitry Andric 276*0fca6ea1SDimitry Andric if (!SrcMarker || SrcMarker->StoredDbgRecords.empty()) { 277*0fca6ea1SDimitry Andric ReleaseTrailingDbgRecords(); 278*0fca6ea1SDimitry Andric return; 2795f757f3fSDimitry Andric } 2805f757f3fSDimitry Andric 281*0fca6ea1SDimitry Andric // If we have DbgMarkers attached to this instruction, we have to honour the 282*0fca6ea1SDimitry Andric // ordering of DbgRecords between this and the other marker. Fall back to just 283*0fca6ea1SDimitry Andric // absorbing from the source. 284*0fca6ea1SDimitry Andric if (DebugMarker || It == BB->end()) { 285*0fca6ea1SDimitry Andric // Ensure we _do_ have a marker. 286*0fca6ea1SDimitry Andric getParent()->createMarker(this); 287*0fca6ea1SDimitry Andric DebugMarker->absorbDebugValues(*SrcMarker, InsertAtHead); 288*0fca6ea1SDimitry Andric 289*0fca6ea1SDimitry Andric // Having transferred everything out of SrcMarker, we _could_ clean it up 290*0fca6ea1SDimitry Andric // and free the marker now. However, that's a lot of heap-accounting for a 291*0fca6ea1SDimitry Andric // small amount of memory with a good chance of re-use. Leave it for the 292*0fca6ea1SDimitry Andric // moment. It will be released when the Instruction is freed in the worst 293*0fca6ea1SDimitry Andric // case. 294*0fca6ea1SDimitry Andric // However: if we transferred from a trailing marker off the end of the 295*0fca6ea1SDimitry Andric // block, it's important to not leave the empty marker trailing. It will 296*0fca6ea1SDimitry Andric // give a misleading impression that some debug records have been left 297*0fca6ea1SDimitry Andric // trailing. 298*0fca6ea1SDimitry Andric ReleaseTrailingDbgRecords(); 299*0fca6ea1SDimitry Andric } else { 300*0fca6ea1SDimitry Andric // Optimisation: we're transferring all the DbgRecords from the source 301*0fca6ea1SDimitry Andric // marker onto this empty location: just adopt the other instructions 302*0fca6ea1SDimitry Andric // marker. 303*0fca6ea1SDimitry Andric DebugMarker = SrcMarker; 304*0fca6ea1SDimitry Andric DebugMarker->MarkedInstr = this; 305*0fca6ea1SDimitry Andric It->DebugMarker = nullptr; 306*0fca6ea1SDimitry Andric } 307*0fca6ea1SDimitry Andric } 308*0fca6ea1SDimitry Andric 309*0fca6ea1SDimitry Andric void Instruction::dropDbgRecords() { 310*0fca6ea1SDimitry Andric if (DebugMarker) 311*0fca6ea1SDimitry Andric DebugMarker->dropDbgRecords(); 312*0fca6ea1SDimitry Andric } 313*0fca6ea1SDimitry Andric 314*0fca6ea1SDimitry Andric void Instruction::dropOneDbgRecord(DbgRecord *DVR) { 315*0fca6ea1SDimitry Andric DebugMarker->dropOneDbgRecord(DVR); 3160b57cec5SDimitry Andric } 3170b57cec5SDimitry Andric 3185ffd83dbSDimitry Andric bool Instruction::comesBefore(const Instruction *Other) const { 319*0fca6ea1SDimitry Andric assert(getParent() && Other->getParent() && 3205ffd83dbSDimitry Andric "instructions without BB parents have no order"); 321*0fca6ea1SDimitry Andric assert(getParent() == Other->getParent() && 322*0fca6ea1SDimitry Andric "cross-BB instruction order comparison"); 323*0fca6ea1SDimitry Andric if (!getParent()->isInstrOrderValid()) 324*0fca6ea1SDimitry Andric const_cast<BasicBlock *>(getParent())->renumberInstructions(); 3255ffd83dbSDimitry Andric return Order < Other->Order; 3265ffd83dbSDimitry Andric } 3275ffd83dbSDimitry Andric 3285f757f3fSDimitry Andric std::optional<BasicBlock::iterator> Instruction::getInsertionPointAfterDef() { 329bdd1243dSDimitry Andric assert(!getType()->isVoidTy() && "Instruction must define result"); 330bdd1243dSDimitry Andric BasicBlock *InsertBB; 331bdd1243dSDimitry Andric BasicBlock::iterator InsertPt; 332bdd1243dSDimitry Andric if (auto *PN = dyn_cast<PHINode>(this)) { 333bdd1243dSDimitry Andric InsertBB = PN->getParent(); 334bdd1243dSDimitry Andric InsertPt = InsertBB->getFirstInsertionPt(); 335bdd1243dSDimitry Andric } else if (auto *II = dyn_cast<InvokeInst>(this)) { 336bdd1243dSDimitry Andric InsertBB = II->getNormalDest(); 337bdd1243dSDimitry Andric InsertPt = InsertBB->getFirstInsertionPt(); 33806c3fb27SDimitry Andric } else if (isa<CallBrInst>(this)) { 33906c3fb27SDimitry Andric // Def is available in multiple successors, there's no single dominating 34006c3fb27SDimitry Andric // insertion point. 3415f757f3fSDimitry Andric return std::nullopt; 342bdd1243dSDimitry Andric } else { 343bdd1243dSDimitry Andric assert(!isTerminator() && "Only invoke/callbr terminators return value"); 344bdd1243dSDimitry Andric InsertBB = getParent(); 345bdd1243dSDimitry Andric InsertPt = std::next(getIterator()); 3465f757f3fSDimitry Andric // Any instruction inserted immediately after "this" will come before any 3475f757f3fSDimitry Andric // debug-info records take effect -- thus, set the head bit indicating that 3485f757f3fSDimitry Andric // to debug-info-transfer code. 3495f757f3fSDimitry Andric InsertPt.setHeadBit(true); 350bdd1243dSDimitry Andric } 351bdd1243dSDimitry Andric 352bdd1243dSDimitry Andric // catchswitch blocks don't have any legal insertion point (because they 353bdd1243dSDimitry Andric // are both an exception pad and a terminator). 354bdd1243dSDimitry Andric if (InsertPt == InsertBB->end()) 3555f757f3fSDimitry Andric return std::nullopt; 3565f757f3fSDimitry Andric return InsertPt; 357bdd1243dSDimitry Andric } 358bdd1243dSDimitry Andric 359fe6060f1SDimitry Andric bool Instruction::isOnlyUserOfAnyOperand() { 360fe6060f1SDimitry Andric return any_of(operands(), [](Value *V) { return V->hasOneUser(); }); 361fe6060f1SDimitry Andric } 362fe6060f1SDimitry Andric 3630b57cec5SDimitry Andric void Instruction::setHasNoUnsignedWrap(bool b) { 364*0fca6ea1SDimitry Andric if (auto *Inst = dyn_cast<OverflowingBinaryOperator>(this)) 365*0fca6ea1SDimitry Andric Inst->setHasNoUnsignedWrap(b); 366*0fca6ea1SDimitry Andric else 367*0fca6ea1SDimitry Andric cast<TruncInst>(this)->setHasNoUnsignedWrap(b); 3680b57cec5SDimitry Andric } 3690b57cec5SDimitry Andric 3700b57cec5SDimitry Andric void Instruction::setHasNoSignedWrap(bool b) { 371*0fca6ea1SDimitry Andric if (auto *Inst = dyn_cast<OverflowingBinaryOperator>(this)) 372*0fca6ea1SDimitry Andric Inst->setHasNoSignedWrap(b); 373*0fca6ea1SDimitry Andric else 374*0fca6ea1SDimitry Andric cast<TruncInst>(this)->setHasNoSignedWrap(b); 3750b57cec5SDimitry Andric } 3760b57cec5SDimitry Andric 3770b57cec5SDimitry Andric void Instruction::setIsExact(bool b) { 3780b57cec5SDimitry Andric cast<PossiblyExactOperator>(this)->setIsExact(b); 3790b57cec5SDimitry Andric } 3800b57cec5SDimitry Andric 3815f757f3fSDimitry Andric void Instruction::setNonNeg(bool b) { 382*0fca6ea1SDimitry Andric assert(isa<PossiblyNonNegInst>(this) && "Must be zext/uitofp"); 3835f757f3fSDimitry Andric SubclassOptionalData = (SubclassOptionalData & ~PossiblyNonNegInst::NonNeg) | 3845f757f3fSDimitry Andric (b * PossiblyNonNegInst::NonNeg); 3855f757f3fSDimitry Andric } 3865f757f3fSDimitry Andric 3870b57cec5SDimitry Andric bool Instruction::hasNoUnsignedWrap() const { 388*0fca6ea1SDimitry Andric if (auto *Inst = dyn_cast<OverflowingBinaryOperator>(this)) 389*0fca6ea1SDimitry Andric return Inst->hasNoUnsignedWrap(); 390*0fca6ea1SDimitry Andric 391*0fca6ea1SDimitry Andric return cast<TruncInst>(this)->hasNoUnsignedWrap(); 3920b57cec5SDimitry Andric } 3930b57cec5SDimitry Andric 3940b57cec5SDimitry Andric bool Instruction::hasNoSignedWrap() const { 395*0fca6ea1SDimitry Andric if (auto *Inst = dyn_cast<OverflowingBinaryOperator>(this)) 396*0fca6ea1SDimitry Andric return Inst->hasNoSignedWrap(); 397*0fca6ea1SDimitry Andric 398*0fca6ea1SDimitry Andric return cast<TruncInst>(this)->hasNoSignedWrap(); 3990b57cec5SDimitry Andric } 4000b57cec5SDimitry Andric 4015f757f3fSDimitry Andric bool Instruction::hasNonNeg() const { 402*0fca6ea1SDimitry Andric assert(isa<PossiblyNonNegInst>(this) && "Must be zext/uitofp"); 4035f757f3fSDimitry Andric return (SubclassOptionalData & PossiblyNonNegInst::NonNeg) != 0; 4045f757f3fSDimitry Andric } 4055f757f3fSDimitry Andric 406349cc55cSDimitry Andric bool Instruction::hasPoisonGeneratingFlags() const { 407349cc55cSDimitry Andric return cast<Operator>(this)->hasPoisonGeneratingFlags(); 408349cc55cSDimitry Andric } 409349cc55cSDimitry Andric 4100b57cec5SDimitry Andric void Instruction::dropPoisonGeneratingFlags() { 4110b57cec5SDimitry Andric switch (getOpcode()) { 4120b57cec5SDimitry Andric case Instruction::Add: 4130b57cec5SDimitry Andric case Instruction::Sub: 4140b57cec5SDimitry Andric case Instruction::Mul: 4150b57cec5SDimitry Andric case Instruction::Shl: 4160b57cec5SDimitry Andric cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(false); 4170b57cec5SDimitry Andric cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(false); 4180b57cec5SDimitry Andric break; 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andric case Instruction::UDiv: 4210b57cec5SDimitry Andric case Instruction::SDiv: 4220b57cec5SDimitry Andric case Instruction::AShr: 4230b57cec5SDimitry Andric case Instruction::LShr: 4240b57cec5SDimitry Andric cast<PossiblyExactOperator>(this)->setIsExact(false); 4250b57cec5SDimitry Andric break; 4260b57cec5SDimitry Andric 4275f757f3fSDimitry Andric case Instruction::Or: 4285f757f3fSDimitry Andric cast<PossiblyDisjointInst>(this)->setIsDisjoint(false); 4295f757f3fSDimitry Andric break; 4305f757f3fSDimitry Andric 4310b57cec5SDimitry Andric case Instruction::GetElementPtr: 432*0fca6ea1SDimitry Andric cast<GetElementPtrInst>(this)->setNoWrapFlags(GEPNoWrapFlags::none()); 4330b57cec5SDimitry Andric break; 4345f757f3fSDimitry Andric 435*0fca6ea1SDimitry Andric case Instruction::UIToFP: 4365f757f3fSDimitry Andric case Instruction::ZExt: 4375f757f3fSDimitry Andric setNonNeg(false); 4385f757f3fSDimitry Andric break; 439*0fca6ea1SDimitry Andric 440*0fca6ea1SDimitry Andric case Instruction::Trunc: 441*0fca6ea1SDimitry Andric cast<TruncInst>(this)->setHasNoUnsignedWrap(false); 442*0fca6ea1SDimitry Andric cast<TruncInst>(this)->setHasNoSignedWrap(false); 443*0fca6ea1SDimitry Andric break; 4440b57cec5SDimitry Andric } 4455f757f3fSDimitry Andric 4460eae32dcSDimitry Andric if (isa<FPMathOperator>(this)) { 4470eae32dcSDimitry Andric setHasNoNaNs(false); 4480eae32dcSDimitry Andric setHasNoInfs(false); 4490eae32dcSDimitry Andric } 450349cc55cSDimitry Andric 451349cc55cSDimitry Andric assert(!hasPoisonGeneratingFlags() && "must be kept in sync"); 4520b57cec5SDimitry Andric } 4530b57cec5SDimitry Andric 454bdd1243dSDimitry Andric bool Instruction::hasPoisonGeneratingMetadata() const { 455bdd1243dSDimitry Andric return hasMetadata(LLVMContext::MD_range) || 456bdd1243dSDimitry Andric hasMetadata(LLVMContext::MD_nonnull) || 457bdd1243dSDimitry Andric hasMetadata(LLVMContext::MD_align); 458bdd1243dSDimitry Andric } 459bdd1243dSDimitry Andric 460bdd1243dSDimitry Andric void Instruction::dropPoisonGeneratingMetadata() { 461bdd1243dSDimitry Andric eraseMetadata(LLVMContext::MD_range); 462bdd1243dSDimitry Andric eraseMetadata(LLVMContext::MD_nonnull); 463bdd1243dSDimitry Andric eraseMetadata(LLVMContext::MD_align); 464bdd1243dSDimitry Andric } 465bdd1243dSDimitry Andric 466*0fca6ea1SDimitry Andric bool Instruction::hasPoisonGeneratingReturnAttributes() const { 467*0fca6ea1SDimitry Andric if (const auto *CB = dyn_cast<CallBase>(this)) { 468*0fca6ea1SDimitry Andric AttributeSet RetAttrs = CB->getAttributes().getRetAttrs(); 469*0fca6ea1SDimitry Andric return RetAttrs.hasAttribute(Attribute::Range) || 470*0fca6ea1SDimitry Andric RetAttrs.hasAttribute(Attribute::Alignment) || 471*0fca6ea1SDimitry Andric RetAttrs.hasAttribute(Attribute::NonNull); 472*0fca6ea1SDimitry Andric } 473*0fca6ea1SDimitry Andric return false; 474*0fca6ea1SDimitry Andric } 475*0fca6ea1SDimitry Andric 476*0fca6ea1SDimitry Andric void Instruction::dropPoisonGeneratingReturnAttributes() { 477*0fca6ea1SDimitry Andric if (auto *CB = dyn_cast<CallBase>(this)) { 478*0fca6ea1SDimitry Andric AttributeMask AM; 479*0fca6ea1SDimitry Andric AM.addAttribute(Attribute::Range); 480*0fca6ea1SDimitry Andric AM.addAttribute(Attribute::Alignment); 481*0fca6ea1SDimitry Andric AM.addAttribute(Attribute::NonNull); 482*0fca6ea1SDimitry Andric CB->removeRetAttrs(AM); 483*0fca6ea1SDimitry Andric } 484*0fca6ea1SDimitry Andric assert(!hasPoisonGeneratingReturnAttributes() && "must be kept in sync"); 485*0fca6ea1SDimitry Andric } 486*0fca6ea1SDimitry Andric 48706c3fb27SDimitry Andric void Instruction::dropUBImplyingAttrsAndUnknownMetadata( 488fe6060f1SDimitry Andric ArrayRef<unsigned> KnownIDs) { 489fe6060f1SDimitry Andric dropUnknownNonDebugMetadata(KnownIDs); 490fe6060f1SDimitry Andric auto *CB = dyn_cast<CallBase>(this); 491fe6060f1SDimitry Andric if (!CB) 492fe6060f1SDimitry Andric return; 493fe6060f1SDimitry Andric // For call instructions, we also need to drop parameter and return attributes 494fe6060f1SDimitry Andric // that are can cause UB if the call is moved to a location where the 495fe6060f1SDimitry Andric // attribute is not valid. 496fe6060f1SDimitry Andric AttributeList AL = CB->getAttributes(); 497fe6060f1SDimitry Andric if (AL.isEmpty()) 498fe6060f1SDimitry Andric return; 49904eeddc0SDimitry Andric AttributeMask UBImplyingAttributes = 50004eeddc0SDimitry Andric AttributeFuncs::getUBImplyingAttributes(); 501349cc55cSDimitry Andric for (unsigned ArgNo = 0; ArgNo < CB->arg_size(); ArgNo++) 502fe6060f1SDimitry Andric CB->removeParamAttrs(ArgNo, UBImplyingAttributes); 503349cc55cSDimitry Andric CB->removeRetAttrs(UBImplyingAttributes); 504fe6060f1SDimitry Andric } 5050b57cec5SDimitry Andric 50606c3fb27SDimitry Andric void Instruction::dropUBImplyingAttrsAndMetadata() { 50706c3fb27SDimitry Andric // !annotation metadata does not impact semantics. 50806c3fb27SDimitry Andric // !range, !nonnull and !align produce poison, so they are safe to speculate. 50906c3fb27SDimitry Andric // !noundef and various AA metadata must be dropped, as it generally produces 51006c3fb27SDimitry Andric // immediate undefined behavior. 51106c3fb27SDimitry Andric unsigned KnownIDs[] = {LLVMContext::MD_annotation, LLVMContext::MD_range, 51206c3fb27SDimitry Andric LLVMContext::MD_nonnull, LLVMContext::MD_align}; 51306c3fb27SDimitry Andric dropUBImplyingAttrsAndUnknownMetadata(KnownIDs); 51406c3fb27SDimitry Andric } 51506c3fb27SDimitry Andric 5160b57cec5SDimitry Andric bool Instruction::isExact() const { 5170b57cec5SDimitry Andric return cast<PossiblyExactOperator>(this)->isExact(); 5180b57cec5SDimitry Andric } 5190b57cec5SDimitry Andric 5200b57cec5SDimitry Andric void Instruction::setFast(bool B) { 5210b57cec5SDimitry Andric assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); 5220b57cec5SDimitry Andric cast<FPMathOperator>(this)->setFast(B); 5230b57cec5SDimitry Andric } 5240b57cec5SDimitry Andric 5250b57cec5SDimitry Andric void Instruction::setHasAllowReassoc(bool B) { 5260b57cec5SDimitry Andric assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); 5270b57cec5SDimitry Andric cast<FPMathOperator>(this)->setHasAllowReassoc(B); 5280b57cec5SDimitry Andric } 5290b57cec5SDimitry Andric 5300b57cec5SDimitry Andric void Instruction::setHasNoNaNs(bool B) { 5310b57cec5SDimitry Andric assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); 5320b57cec5SDimitry Andric cast<FPMathOperator>(this)->setHasNoNaNs(B); 5330b57cec5SDimitry Andric } 5340b57cec5SDimitry Andric 5350b57cec5SDimitry Andric void Instruction::setHasNoInfs(bool B) { 5360b57cec5SDimitry Andric assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); 5370b57cec5SDimitry Andric cast<FPMathOperator>(this)->setHasNoInfs(B); 5380b57cec5SDimitry Andric } 5390b57cec5SDimitry Andric 5400b57cec5SDimitry Andric void Instruction::setHasNoSignedZeros(bool B) { 5410b57cec5SDimitry Andric assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); 5420b57cec5SDimitry Andric cast<FPMathOperator>(this)->setHasNoSignedZeros(B); 5430b57cec5SDimitry Andric } 5440b57cec5SDimitry Andric 5450b57cec5SDimitry Andric void Instruction::setHasAllowReciprocal(bool B) { 5460b57cec5SDimitry Andric assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); 5470b57cec5SDimitry Andric cast<FPMathOperator>(this)->setHasAllowReciprocal(B); 5480b57cec5SDimitry Andric } 5490b57cec5SDimitry Andric 5505ffd83dbSDimitry Andric void Instruction::setHasAllowContract(bool B) { 5515ffd83dbSDimitry Andric assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); 5525ffd83dbSDimitry Andric cast<FPMathOperator>(this)->setHasAllowContract(B); 5535ffd83dbSDimitry Andric } 5545ffd83dbSDimitry Andric 5550b57cec5SDimitry Andric void Instruction::setHasApproxFunc(bool B) { 5560b57cec5SDimitry Andric assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); 5570b57cec5SDimitry Andric cast<FPMathOperator>(this)->setHasApproxFunc(B); 5580b57cec5SDimitry Andric } 5590b57cec5SDimitry Andric 5600b57cec5SDimitry Andric void Instruction::setFastMathFlags(FastMathFlags FMF) { 5610b57cec5SDimitry Andric assert(isa<FPMathOperator>(this) && "setting fast-math flag on invalid op"); 5620b57cec5SDimitry Andric cast<FPMathOperator>(this)->setFastMathFlags(FMF); 5630b57cec5SDimitry Andric } 5640b57cec5SDimitry Andric 5650b57cec5SDimitry Andric void Instruction::copyFastMathFlags(FastMathFlags FMF) { 5660b57cec5SDimitry Andric assert(isa<FPMathOperator>(this) && "copying fast-math flag on invalid op"); 5670b57cec5SDimitry Andric cast<FPMathOperator>(this)->copyFastMathFlags(FMF); 5680b57cec5SDimitry Andric } 5690b57cec5SDimitry Andric 5700b57cec5SDimitry Andric bool Instruction::isFast() const { 5710b57cec5SDimitry Andric assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 5720b57cec5SDimitry Andric return cast<FPMathOperator>(this)->isFast(); 5730b57cec5SDimitry Andric } 5740b57cec5SDimitry Andric 5750b57cec5SDimitry Andric bool Instruction::hasAllowReassoc() const { 5760b57cec5SDimitry Andric assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 5770b57cec5SDimitry Andric return cast<FPMathOperator>(this)->hasAllowReassoc(); 5780b57cec5SDimitry Andric } 5790b57cec5SDimitry Andric 5800b57cec5SDimitry Andric bool Instruction::hasNoNaNs() const { 5810b57cec5SDimitry Andric assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 5820b57cec5SDimitry Andric return cast<FPMathOperator>(this)->hasNoNaNs(); 5830b57cec5SDimitry Andric } 5840b57cec5SDimitry Andric 5850b57cec5SDimitry Andric bool Instruction::hasNoInfs() const { 5860b57cec5SDimitry Andric assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 5870b57cec5SDimitry Andric return cast<FPMathOperator>(this)->hasNoInfs(); 5880b57cec5SDimitry Andric } 5890b57cec5SDimitry Andric 5900b57cec5SDimitry Andric bool Instruction::hasNoSignedZeros() const { 5910b57cec5SDimitry Andric assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 5920b57cec5SDimitry Andric return cast<FPMathOperator>(this)->hasNoSignedZeros(); 5930b57cec5SDimitry Andric } 5940b57cec5SDimitry Andric 5950b57cec5SDimitry Andric bool Instruction::hasAllowReciprocal() const { 5960b57cec5SDimitry Andric assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 5970b57cec5SDimitry Andric return cast<FPMathOperator>(this)->hasAllowReciprocal(); 5980b57cec5SDimitry Andric } 5990b57cec5SDimitry Andric 6000b57cec5SDimitry Andric bool Instruction::hasAllowContract() const { 6010b57cec5SDimitry Andric assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 6020b57cec5SDimitry Andric return cast<FPMathOperator>(this)->hasAllowContract(); 6030b57cec5SDimitry Andric } 6040b57cec5SDimitry Andric 6050b57cec5SDimitry Andric bool Instruction::hasApproxFunc() const { 6060b57cec5SDimitry Andric assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 6070b57cec5SDimitry Andric return cast<FPMathOperator>(this)->hasApproxFunc(); 6080b57cec5SDimitry Andric } 6090b57cec5SDimitry Andric 6100b57cec5SDimitry Andric FastMathFlags Instruction::getFastMathFlags() const { 6110b57cec5SDimitry Andric assert(isa<FPMathOperator>(this) && "getting fast-math flag on invalid op"); 6120b57cec5SDimitry Andric return cast<FPMathOperator>(this)->getFastMathFlags(); 6130b57cec5SDimitry Andric } 6140b57cec5SDimitry Andric 6150b57cec5SDimitry Andric void Instruction::copyFastMathFlags(const Instruction *I) { 6160b57cec5SDimitry Andric copyFastMathFlags(I->getFastMathFlags()); 6170b57cec5SDimitry Andric } 6180b57cec5SDimitry Andric 6190b57cec5SDimitry Andric void Instruction::copyIRFlags(const Value *V, bool IncludeWrapFlags) { 6200b57cec5SDimitry Andric // Copy the wrapping flags. 6210b57cec5SDimitry Andric if (IncludeWrapFlags && isa<OverflowingBinaryOperator>(this)) { 6220b57cec5SDimitry Andric if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) { 6230b57cec5SDimitry Andric setHasNoSignedWrap(OB->hasNoSignedWrap()); 6240b57cec5SDimitry Andric setHasNoUnsignedWrap(OB->hasNoUnsignedWrap()); 6250b57cec5SDimitry Andric } 6260b57cec5SDimitry Andric } 6270b57cec5SDimitry Andric 628*0fca6ea1SDimitry Andric if (auto *TI = dyn_cast<TruncInst>(V)) { 629*0fca6ea1SDimitry Andric if (isa<TruncInst>(this)) { 630*0fca6ea1SDimitry Andric setHasNoSignedWrap(TI->hasNoSignedWrap()); 631*0fca6ea1SDimitry Andric setHasNoUnsignedWrap(TI->hasNoUnsignedWrap()); 632*0fca6ea1SDimitry Andric } 633*0fca6ea1SDimitry Andric } 634*0fca6ea1SDimitry Andric 6350b57cec5SDimitry Andric // Copy the exact flag. 6360b57cec5SDimitry Andric if (auto *PE = dyn_cast<PossiblyExactOperator>(V)) 6370b57cec5SDimitry Andric if (isa<PossiblyExactOperator>(this)) 6380b57cec5SDimitry Andric setIsExact(PE->isExact()); 6390b57cec5SDimitry Andric 6405f757f3fSDimitry Andric if (auto *SrcPD = dyn_cast<PossiblyDisjointInst>(V)) 6415f757f3fSDimitry Andric if (auto *DestPD = dyn_cast<PossiblyDisjointInst>(this)) 6425f757f3fSDimitry Andric DestPD->setIsDisjoint(SrcPD->isDisjoint()); 6435f757f3fSDimitry Andric 6440b57cec5SDimitry Andric // Copy the fast-math flags. 6450b57cec5SDimitry Andric if (auto *FP = dyn_cast<FPMathOperator>(V)) 6460b57cec5SDimitry Andric if (isa<FPMathOperator>(this)) 6470b57cec5SDimitry Andric copyFastMathFlags(FP->getFastMathFlags()); 6480b57cec5SDimitry Andric 6490b57cec5SDimitry Andric if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V)) 6500b57cec5SDimitry Andric if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this)) 651*0fca6ea1SDimitry Andric DestGEP->setNoWrapFlags(SrcGEP->getNoWrapFlags() | 652*0fca6ea1SDimitry Andric DestGEP->getNoWrapFlags()); 6535f757f3fSDimitry Andric 6545f757f3fSDimitry Andric if (auto *NNI = dyn_cast<PossiblyNonNegInst>(V)) 6555f757f3fSDimitry Andric if (isa<PossiblyNonNegInst>(this)) 6565f757f3fSDimitry Andric setNonNeg(NNI->hasNonNeg()); 6570b57cec5SDimitry Andric } 6580b57cec5SDimitry Andric 6590b57cec5SDimitry Andric void Instruction::andIRFlags(const Value *V) { 6600b57cec5SDimitry Andric if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) { 6610b57cec5SDimitry Andric if (isa<OverflowingBinaryOperator>(this)) { 662349cc55cSDimitry Andric setHasNoSignedWrap(hasNoSignedWrap() && OB->hasNoSignedWrap()); 663349cc55cSDimitry Andric setHasNoUnsignedWrap(hasNoUnsignedWrap() && OB->hasNoUnsignedWrap()); 6640b57cec5SDimitry Andric } 6650b57cec5SDimitry Andric } 6660b57cec5SDimitry Andric 667*0fca6ea1SDimitry Andric if (auto *TI = dyn_cast<TruncInst>(V)) { 668*0fca6ea1SDimitry Andric if (isa<TruncInst>(this)) { 669*0fca6ea1SDimitry Andric setHasNoSignedWrap(hasNoSignedWrap() && TI->hasNoSignedWrap()); 670*0fca6ea1SDimitry Andric setHasNoUnsignedWrap(hasNoUnsignedWrap() && TI->hasNoUnsignedWrap()); 671*0fca6ea1SDimitry Andric } 672*0fca6ea1SDimitry Andric } 673*0fca6ea1SDimitry Andric 6740b57cec5SDimitry Andric if (auto *PE = dyn_cast<PossiblyExactOperator>(V)) 6750b57cec5SDimitry Andric if (isa<PossiblyExactOperator>(this)) 676349cc55cSDimitry Andric setIsExact(isExact() && PE->isExact()); 6770b57cec5SDimitry Andric 6785f757f3fSDimitry Andric if (auto *SrcPD = dyn_cast<PossiblyDisjointInst>(V)) 6795f757f3fSDimitry Andric if (auto *DestPD = dyn_cast<PossiblyDisjointInst>(this)) 6805f757f3fSDimitry Andric DestPD->setIsDisjoint(DestPD->isDisjoint() && SrcPD->isDisjoint()); 6815f757f3fSDimitry Andric 6820b57cec5SDimitry Andric if (auto *FP = dyn_cast<FPMathOperator>(V)) { 6830b57cec5SDimitry Andric if (isa<FPMathOperator>(this)) { 6840b57cec5SDimitry Andric FastMathFlags FM = getFastMathFlags(); 6850b57cec5SDimitry Andric FM &= FP->getFastMathFlags(); 6860b57cec5SDimitry Andric copyFastMathFlags(FM); 6870b57cec5SDimitry Andric } 6880b57cec5SDimitry Andric } 6890b57cec5SDimitry Andric 6900b57cec5SDimitry Andric if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V)) 6910b57cec5SDimitry Andric if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this)) 692*0fca6ea1SDimitry Andric DestGEP->setNoWrapFlags(SrcGEP->getNoWrapFlags() & 693*0fca6ea1SDimitry Andric DestGEP->getNoWrapFlags()); 6945f757f3fSDimitry Andric 6955f757f3fSDimitry Andric if (auto *NNI = dyn_cast<PossiblyNonNegInst>(V)) 6965f757f3fSDimitry Andric if (isa<PossiblyNonNegInst>(this)) 6975f757f3fSDimitry Andric setNonNeg(hasNonNeg() && NNI->hasNonNeg()); 6980b57cec5SDimitry Andric } 6990b57cec5SDimitry Andric 7000b57cec5SDimitry Andric const char *Instruction::getOpcodeName(unsigned OpCode) { 7010b57cec5SDimitry Andric switch (OpCode) { 7020b57cec5SDimitry Andric // Terminators 7030b57cec5SDimitry Andric case Ret: return "ret"; 7040b57cec5SDimitry Andric case Br: return "br"; 7050b57cec5SDimitry Andric case Switch: return "switch"; 7060b57cec5SDimitry Andric case IndirectBr: return "indirectbr"; 7070b57cec5SDimitry Andric case Invoke: return "invoke"; 7080b57cec5SDimitry Andric case Resume: return "resume"; 7090b57cec5SDimitry Andric case Unreachable: return "unreachable"; 7100b57cec5SDimitry Andric case CleanupRet: return "cleanupret"; 7110b57cec5SDimitry Andric case CatchRet: return "catchret"; 7120b57cec5SDimitry Andric case CatchPad: return "catchpad"; 7130b57cec5SDimitry Andric case CatchSwitch: return "catchswitch"; 7140b57cec5SDimitry Andric case CallBr: return "callbr"; 7150b57cec5SDimitry Andric 7160b57cec5SDimitry Andric // Standard unary operators... 7170b57cec5SDimitry Andric case FNeg: return "fneg"; 7180b57cec5SDimitry Andric 7190b57cec5SDimitry Andric // Standard binary operators... 7200b57cec5SDimitry Andric case Add: return "add"; 7210b57cec5SDimitry Andric case FAdd: return "fadd"; 7220b57cec5SDimitry Andric case Sub: return "sub"; 7230b57cec5SDimitry Andric case FSub: return "fsub"; 7240b57cec5SDimitry Andric case Mul: return "mul"; 7250b57cec5SDimitry Andric case FMul: return "fmul"; 7260b57cec5SDimitry Andric case UDiv: return "udiv"; 7270b57cec5SDimitry Andric case SDiv: return "sdiv"; 7280b57cec5SDimitry Andric case FDiv: return "fdiv"; 7290b57cec5SDimitry Andric case URem: return "urem"; 7300b57cec5SDimitry Andric case SRem: return "srem"; 7310b57cec5SDimitry Andric case FRem: return "frem"; 7320b57cec5SDimitry Andric 7330b57cec5SDimitry Andric // Logical operators... 7340b57cec5SDimitry Andric case And: return "and"; 7350b57cec5SDimitry Andric case Or : return "or"; 7360b57cec5SDimitry Andric case Xor: return "xor"; 7370b57cec5SDimitry Andric 7380b57cec5SDimitry Andric // Memory instructions... 7390b57cec5SDimitry Andric case Alloca: return "alloca"; 7400b57cec5SDimitry Andric case Load: return "load"; 7410b57cec5SDimitry Andric case Store: return "store"; 7420b57cec5SDimitry Andric case AtomicCmpXchg: return "cmpxchg"; 7430b57cec5SDimitry Andric case AtomicRMW: return "atomicrmw"; 7440b57cec5SDimitry Andric case Fence: return "fence"; 7450b57cec5SDimitry Andric case GetElementPtr: return "getelementptr"; 7460b57cec5SDimitry Andric 7470b57cec5SDimitry Andric // Convert instructions... 7480b57cec5SDimitry Andric case Trunc: return "trunc"; 7490b57cec5SDimitry Andric case ZExt: return "zext"; 7500b57cec5SDimitry Andric case SExt: return "sext"; 7510b57cec5SDimitry Andric case FPTrunc: return "fptrunc"; 7520b57cec5SDimitry Andric case FPExt: return "fpext"; 7530b57cec5SDimitry Andric case FPToUI: return "fptoui"; 7540b57cec5SDimitry Andric case FPToSI: return "fptosi"; 7550b57cec5SDimitry Andric case UIToFP: return "uitofp"; 7560b57cec5SDimitry Andric case SIToFP: return "sitofp"; 7570b57cec5SDimitry Andric case IntToPtr: return "inttoptr"; 7580b57cec5SDimitry Andric case PtrToInt: return "ptrtoint"; 7590b57cec5SDimitry Andric case BitCast: return "bitcast"; 7600b57cec5SDimitry Andric case AddrSpaceCast: return "addrspacecast"; 7610b57cec5SDimitry Andric 7620b57cec5SDimitry Andric // Other instructions... 7630b57cec5SDimitry Andric case ICmp: return "icmp"; 7640b57cec5SDimitry Andric case FCmp: return "fcmp"; 7650b57cec5SDimitry Andric case PHI: return "phi"; 7660b57cec5SDimitry Andric case Select: return "select"; 7670b57cec5SDimitry Andric case Call: return "call"; 7680b57cec5SDimitry Andric case Shl: return "shl"; 7690b57cec5SDimitry Andric case LShr: return "lshr"; 7700b57cec5SDimitry Andric case AShr: return "ashr"; 7710b57cec5SDimitry Andric case VAArg: return "va_arg"; 7720b57cec5SDimitry Andric case ExtractElement: return "extractelement"; 7730b57cec5SDimitry Andric case InsertElement: return "insertelement"; 7740b57cec5SDimitry Andric case ShuffleVector: return "shufflevector"; 7750b57cec5SDimitry Andric case ExtractValue: return "extractvalue"; 7760b57cec5SDimitry Andric case InsertValue: return "insertvalue"; 7770b57cec5SDimitry Andric case LandingPad: return "landingpad"; 7780b57cec5SDimitry Andric case CleanupPad: return "cleanuppad"; 779480093f4SDimitry Andric case Freeze: return "freeze"; 7800b57cec5SDimitry Andric 7810b57cec5SDimitry Andric default: return "<Invalid operator> "; 7820b57cec5SDimitry Andric } 7830b57cec5SDimitry Andric } 7840b57cec5SDimitry Andric 78506c3fb27SDimitry Andric /// This must be kept in sync with FunctionComparator::cmpOperations in 7860b57cec5SDimitry Andric /// lib/Transforms/IPO/MergeFunctions.cpp. 78706c3fb27SDimitry Andric bool Instruction::hasSameSpecialState(const Instruction *I2, 78806c3fb27SDimitry Andric bool IgnoreAlignment) const { 78906c3fb27SDimitry Andric auto I1 = this; 7900b57cec5SDimitry Andric assert(I1->getOpcode() == I2->getOpcode() && 7910b57cec5SDimitry Andric "Can not compare special state of different instructions"); 7920b57cec5SDimitry Andric 7930b57cec5SDimitry Andric if (const AllocaInst *AI = dyn_cast<AllocaInst>(I1)) 7940b57cec5SDimitry Andric return AI->getAllocatedType() == cast<AllocaInst>(I2)->getAllocatedType() && 7950eae32dcSDimitry Andric (AI->getAlign() == cast<AllocaInst>(I2)->getAlign() || 7960b57cec5SDimitry Andric IgnoreAlignment); 7970b57cec5SDimitry Andric if (const LoadInst *LI = dyn_cast<LoadInst>(I1)) 7980b57cec5SDimitry Andric return LI->isVolatile() == cast<LoadInst>(I2)->isVolatile() && 7990eae32dcSDimitry Andric (LI->getAlign() == cast<LoadInst>(I2)->getAlign() || 8000b57cec5SDimitry Andric IgnoreAlignment) && 8010b57cec5SDimitry Andric LI->getOrdering() == cast<LoadInst>(I2)->getOrdering() && 8020b57cec5SDimitry Andric LI->getSyncScopeID() == cast<LoadInst>(I2)->getSyncScopeID(); 8030b57cec5SDimitry Andric if (const StoreInst *SI = dyn_cast<StoreInst>(I1)) 8040b57cec5SDimitry Andric return SI->isVolatile() == cast<StoreInst>(I2)->isVolatile() && 8050eae32dcSDimitry Andric (SI->getAlign() == cast<StoreInst>(I2)->getAlign() || 8060b57cec5SDimitry Andric IgnoreAlignment) && 8070b57cec5SDimitry Andric SI->getOrdering() == cast<StoreInst>(I2)->getOrdering() && 8080b57cec5SDimitry Andric SI->getSyncScopeID() == cast<StoreInst>(I2)->getSyncScopeID(); 8090b57cec5SDimitry Andric if (const CmpInst *CI = dyn_cast<CmpInst>(I1)) 8100b57cec5SDimitry Andric return CI->getPredicate() == cast<CmpInst>(I2)->getPredicate(); 8110b57cec5SDimitry Andric if (const CallInst *CI = dyn_cast<CallInst>(I1)) 8120b57cec5SDimitry Andric return CI->isTailCall() == cast<CallInst>(I2)->isTailCall() && 8130b57cec5SDimitry Andric CI->getCallingConv() == cast<CallInst>(I2)->getCallingConv() && 8140b57cec5SDimitry Andric CI->getAttributes() == cast<CallInst>(I2)->getAttributes() && 8150b57cec5SDimitry Andric CI->hasIdenticalOperandBundleSchema(*cast<CallInst>(I2)); 8160b57cec5SDimitry Andric if (const InvokeInst *CI = dyn_cast<InvokeInst>(I1)) 8170b57cec5SDimitry Andric return CI->getCallingConv() == cast<InvokeInst>(I2)->getCallingConv() && 8180b57cec5SDimitry Andric CI->getAttributes() == cast<InvokeInst>(I2)->getAttributes() && 8190b57cec5SDimitry Andric CI->hasIdenticalOperandBundleSchema(*cast<InvokeInst>(I2)); 8200b57cec5SDimitry Andric if (const CallBrInst *CI = dyn_cast<CallBrInst>(I1)) 8210b57cec5SDimitry Andric return CI->getCallingConv() == cast<CallBrInst>(I2)->getCallingConv() && 8220b57cec5SDimitry Andric CI->getAttributes() == cast<CallBrInst>(I2)->getAttributes() && 8230b57cec5SDimitry Andric CI->hasIdenticalOperandBundleSchema(*cast<CallBrInst>(I2)); 8240b57cec5SDimitry Andric if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(I1)) 8250b57cec5SDimitry Andric return IVI->getIndices() == cast<InsertValueInst>(I2)->getIndices(); 8260b57cec5SDimitry Andric if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I1)) 8270b57cec5SDimitry Andric return EVI->getIndices() == cast<ExtractValueInst>(I2)->getIndices(); 8280b57cec5SDimitry Andric if (const FenceInst *FI = dyn_cast<FenceInst>(I1)) 8290b57cec5SDimitry Andric return FI->getOrdering() == cast<FenceInst>(I2)->getOrdering() && 8300b57cec5SDimitry Andric FI->getSyncScopeID() == cast<FenceInst>(I2)->getSyncScopeID(); 8310b57cec5SDimitry Andric if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I1)) 8320b57cec5SDimitry Andric return CXI->isVolatile() == cast<AtomicCmpXchgInst>(I2)->isVolatile() && 8330b57cec5SDimitry Andric CXI->isWeak() == cast<AtomicCmpXchgInst>(I2)->isWeak() && 8340b57cec5SDimitry Andric CXI->getSuccessOrdering() == 8350b57cec5SDimitry Andric cast<AtomicCmpXchgInst>(I2)->getSuccessOrdering() && 8360b57cec5SDimitry Andric CXI->getFailureOrdering() == 8370b57cec5SDimitry Andric cast<AtomicCmpXchgInst>(I2)->getFailureOrdering() && 8380b57cec5SDimitry Andric CXI->getSyncScopeID() == 8390b57cec5SDimitry Andric cast<AtomicCmpXchgInst>(I2)->getSyncScopeID(); 8400b57cec5SDimitry Andric if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I1)) 8410b57cec5SDimitry Andric return RMWI->getOperation() == cast<AtomicRMWInst>(I2)->getOperation() && 8420b57cec5SDimitry Andric RMWI->isVolatile() == cast<AtomicRMWInst>(I2)->isVolatile() && 8430b57cec5SDimitry Andric RMWI->getOrdering() == cast<AtomicRMWInst>(I2)->getOrdering() && 8440b57cec5SDimitry Andric RMWI->getSyncScopeID() == cast<AtomicRMWInst>(I2)->getSyncScopeID(); 8455ffd83dbSDimitry Andric if (const ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I1)) 8465ffd83dbSDimitry Andric return SVI->getShuffleMask() == 8475ffd83dbSDimitry Andric cast<ShuffleVectorInst>(I2)->getShuffleMask(); 84881ad6265SDimitry Andric if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I1)) 84981ad6265SDimitry Andric return GEP->getSourceElementType() == 85081ad6265SDimitry Andric cast<GetElementPtrInst>(I2)->getSourceElementType(); 8510b57cec5SDimitry Andric 8520b57cec5SDimitry Andric return true; 8530b57cec5SDimitry Andric } 8540b57cec5SDimitry Andric 8550b57cec5SDimitry Andric bool Instruction::isIdenticalTo(const Instruction *I) const { 8560b57cec5SDimitry Andric return isIdenticalToWhenDefined(I) && 8570b57cec5SDimitry Andric SubclassOptionalData == I->SubclassOptionalData; 8580b57cec5SDimitry Andric } 8590b57cec5SDimitry Andric 8600b57cec5SDimitry Andric bool Instruction::isIdenticalToWhenDefined(const Instruction *I) const { 8610b57cec5SDimitry Andric if (getOpcode() != I->getOpcode() || 8620b57cec5SDimitry Andric getNumOperands() != I->getNumOperands() || 8630b57cec5SDimitry Andric getType() != I->getType()) 8640b57cec5SDimitry Andric return false; 8650b57cec5SDimitry Andric 8660b57cec5SDimitry Andric // If both instructions have no operands, they are identical. 8670b57cec5SDimitry Andric if (getNumOperands() == 0 && I->getNumOperands() == 0) 86806c3fb27SDimitry Andric return this->hasSameSpecialState(I); 8690b57cec5SDimitry Andric 8700b57cec5SDimitry Andric // We have two instructions of identical opcode and #operands. Check to see 8710b57cec5SDimitry Andric // if all operands are the same. 8720b57cec5SDimitry Andric if (!std::equal(op_begin(), op_end(), I->op_begin())) 8730b57cec5SDimitry Andric return false; 8740b57cec5SDimitry Andric 875e8d8bef9SDimitry Andric // WARNING: this logic must be kept in sync with EliminateDuplicatePHINodes()! 8760b57cec5SDimitry Andric if (const PHINode *thisPHI = dyn_cast<PHINode>(this)) { 8770b57cec5SDimitry Andric const PHINode *otherPHI = cast<PHINode>(I); 8780b57cec5SDimitry Andric return std::equal(thisPHI->block_begin(), thisPHI->block_end(), 8790b57cec5SDimitry Andric otherPHI->block_begin()); 8800b57cec5SDimitry Andric } 8810b57cec5SDimitry Andric 88206c3fb27SDimitry Andric return this->hasSameSpecialState(I); 8830b57cec5SDimitry Andric } 8840b57cec5SDimitry Andric 8850b57cec5SDimitry Andric // Keep this in sync with FunctionComparator::cmpOperations in 8860b57cec5SDimitry Andric // lib/Transforms/IPO/MergeFunctions.cpp. 8870b57cec5SDimitry Andric bool Instruction::isSameOperationAs(const Instruction *I, 8880b57cec5SDimitry Andric unsigned flags) const { 8890b57cec5SDimitry Andric bool IgnoreAlignment = flags & CompareIgnoringAlignment; 8900b57cec5SDimitry Andric bool UseScalarTypes = flags & CompareUsingScalarTypes; 8910b57cec5SDimitry Andric 8920b57cec5SDimitry Andric if (getOpcode() != I->getOpcode() || 8930b57cec5SDimitry Andric getNumOperands() != I->getNumOperands() || 8940b57cec5SDimitry Andric (UseScalarTypes ? 8950b57cec5SDimitry Andric getType()->getScalarType() != I->getType()->getScalarType() : 8960b57cec5SDimitry Andric getType() != I->getType())) 8970b57cec5SDimitry Andric return false; 8980b57cec5SDimitry Andric 8990b57cec5SDimitry Andric // We have two instructions of identical opcode and #operands. Check to see 9000b57cec5SDimitry Andric // if all operands are the same type 9010b57cec5SDimitry Andric for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 9020b57cec5SDimitry Andric if (UseScalarTypes ? 9030b57cec5SDimitry Andric getOperand(i)->getType()->getScalarType() != 9040b57cec5SDimitry Andric I->getOperand(i)->getType()->getScalarType() : 9050b57cec5SDimitry Andric getOperand(i)->getType() != I->getOperand(i)->getType()) 9060b57cec5SDimitry Andric return false; 9070b57cec5SDimitry Andric 90806c3fb27SDimitry Andric return this->hasSameSpecialState(I, IgnoreAlignment); 9090b57cec5SDimitry Andric } 9100b57cec5SDimitry Andric 9110b57cec5SDimitry Andric bool Instruction::isUsedOutsideOfBlock(const BasicBlock *BB) const { 9120b57cec5SDimitry Andric for (const Use &U : uses()) { 9130b57cec5SDimitry Andric // PHI nodes uses values in the corresponding predecessor block. For other 9140b57cec5SDimitry Andric // instructions, just check to see whether the parent of the use matches up. 9150b57cec5SDimitry Andric const Instruction *I = cast<Instruction>(U.getUser()); 9160b57cec5SDimitry Andric const PHINode *PN = dyn_cast<PHINode>(I); 9170b57cec5SDimitry Andric if (!PN) { 9180b57cec5SDimitry Andric if (I->getParent() != BB) 9190b57cec5SDimitry Andric return true; 9200b57cec5SDimitry Andric continue; 9210b57cec5SDimitry Andric } 9220b57cec5SDimitry Andric 9230b57cec5SDimitry Andric if (PN->getIncomingBlock(U) != BB) 9240b57cec5SDimitry Andric return true; 9250b57cec5SDimitry Andric } 9260b57cec5SDimitry Andric return false; 9270b57cec5SDimitry Andric } 9280b57cec5SDimitry Andric 9290b57cec5SDimitry Andric bool Instruction::mayReadFromMemory() const { 9300b57cec5SDimitry Andric switch (getOpcode()) { 9310b57cec5SDimitry Andric default: return false; 9320b57cec5SDimitry Andric case Instruction::VAArg: 9330b57cec5SDimitry Andric case Instruction::Load: 9340b57cec5SDimitry Andric case Instruction::Fence: // FIXME: refine definition of mayReadFromMemory 9350b57cec5SDimitry Andric case Instruction::AtomicCmpXchg: 9360b57cec5SDimitry Andric case Instruction::AtomicRMW: 9370b57cec5SDimitry Andric case Instruction::CatchPad: 9380b57cec5SDimitry Andric case Instruction::CatchRet: 9390b57cec5SDimitry Andric return true; 9400b57cec5SDimitry Andric case Instruction::Call: 9410b57cec5SDimitry Andric case Instruction::Invoke: 9420b57cec5SDimitry Andric case Instruction::CallBr: 94304eeddc0SDimitry Andric return !cast<CallBase>(this)->onlyWritesMemory(); 9440b57cec5SDimitry Andric case Instruction::Store: 9450b57cec5SDimitry Andric return !cast<StoreInst>(this)->isUnordered(); 9460b57cec5SDimitry Andric } 9470b57cec5SDimitry Andric } 9480b57cec5SDimitry Andric 9490b57cec5SDimitry Andric bool Instruction::mayWriteToMemory() const { 9500b57cec5SDimitry Andric switch (getOpcode()) { 9510b57cec5SDimitry Andric default: return false; 9520b57cec5SDimitry Andric case Instruction::Fence: // FIXME: refine definition of mayWriteToMemory 9530b57cec5SDimitry Andric case Instruction::Store: 9540b57cec5SDimitry Andric case Instruction::VAArg: 9550b57cec5SDimitry Andric case Instruction::AtomicCmpXchg: 9560b57cec5SDimitry Andric case Instruction::AtomicRMW: 9570b57cec5SDimitry Andric case Instruction::CatchPad: 9580b57cec5SDimitry Andric case Instruction::CatchRet: 9590b57cec5SDimitry Andric return true; 9600b57cec5SDimitry Andric case Instruction::Call: 9610b57cec5SDimitry Andric case Instruction::Invoke: 9620b57cec5SDimitry Andric case Instruction::CallBr: 9630b57cec5SDimitry Andric return !cast<CallBase>(this)->onlyReadsMemory(); 9640b57cec5SDimitry Andric case Instruction::Load: 9650b57cec5SDimitry Andric return !cast<LoadInst>(this)->isUnordered(); 9660b57cec5SDimitry Andric } 9670b57cec5SDimitry Andric } 9680b57cec5SDimitry Andric 9690b57cec5SDimitry Andric bool Instruction::isAtomic() const { 9700b57cec5SDimitry Andric switch (getOpcode()) { 9710b57cec5SDimitry Andric default: 9720b57cec5SDimitry Andric return false; 9730b57cec5SDimitry Andric case Instruction::AtomicCmpXchg: 9740b57cec5SDimitry Andric case Instruction::AtomicRMW: 9750b57cec5SDimitry Andric case Instruction::Fence: 9760b57cec5SDimitry Andric return true; 9770b57cec5SDimitry Andric case Instruction::Load: 9780b57cec5SDimitry Andric return cast<LoadInst>(this)->getOrdering() != AtomicOrdering::NotAtomic; 9790b57cec5SDimitry Andric case Instruction::Store: 9800b57cec5SDimitry Andric return cast<StoreInst>(this)->getOrdering() != AtomicOrdering::NotAtomic; 9810b57cec5SDimitry Andric } 9820b57cec5SDimitry Andric } 9830b57cec5SDimitry Andric 9840b57cec5SDimitry Andric bool Instruction::hasAtomicLoad() const { 9850b57cec5SDimitry Andric assert(isAtomic()); 9860b57cec5SDimitry Andric switch (getOpcode()) { 9870b57cec5SDimitry Andric default: 9880b57cec5SDimitry Andric return false; 9890b57cec5SDimitry Andric case Instruction::AtomicCmpXchg: 9900b57cec5SDimitry Andric case Instruction::AtomicRMW: 9910b57cec5SDimitry Andric case Instruction::Load: 9920b57cec5SDimitry Andric return true; 9930b57cec5SDimitry Andric } 9940b57cec5SDimitry Andric } 9950b57cec5SDimitry Andric 9960b57cec5SDimitry Andric bool Instruction::hasAtomicStore() const { 9970b57cec5SDimitry Andric assert(isAtomic()); 9980b57cec5SDimitry Andric switch (getOpcode()) { 9990b57cec5SDimitry Andric default: 10000b57cec5SDimitry Andric return false; 10010b57cec5SDimitry Andric case Instruction::AtomicCmpXchg: 10020b57cec5SDimitry Andric case Instruction::AtomicRMW: 10030b57cec5SDimitry Andric case Instruction::Store: 10040b57cec5SDimitry Andric return true; 10050b57cec5SDimitry Andric } 10060b57cec5SDimitry Andric } 10070b57cec5SDimitry Andric 1008fe6060f1SDimitry Andric bool Instruction::isVolatile() const { 1009fe6060f1SDimitry Andric switch (getOpcode()) { 1010fe6060f1SDimitry Andric default: 1011fe6060f1SDimitry Andric return false; 1012fe6060f1SDimitry Andric case Instruction::AtomicRMW: 1013fe6060f1SDimitry Andric return cast<AtomicRMWInst>(this)->isVolatile(); 1014fe6060f1SDimitry Andric case Instruction::Store: 1015fe6060f1SDimitry Andric return cast<StoreInst>(this)->isVolatile(); 1016fe6060f1SDimitry Andric case Instruction::Load: 1017fe6060f1SDimitry Andric return cast<LoadInst>(this)->isVolatile(); 1018fe6060f1SDimitry Andric case Instruction::AtomicCmpXchg: 1019fe6060f1SDimitry Andric return cast<AtomicCmpXchgInst>(this)->isVolatile(); 1020fe6060f1SDimitry Andric case Instruction::Call: 1021fe6060f1SDimitry Andric case Instruction::Invoke: 1022fe6060f1SDimitry Andric // There are a very limited number of intrinsics with volatile flags. 1023fe6060f1SDimitry Andric if (auto *II = dyn_cast<IntrinsicInst>(this)) { 1024fe6060f1SDimitry Andric if (auto *MI = dyn_cast<MemIntrinsic>(II)) 1025fe6060f1SDimitry Andric return MI->isVolatile(); 1026fe6060f1SDimitry Andric switch (II->getIntrinsicID()) { 1027fe6060f1SDimitry Andric default: break; 1028fe6060f1SDimitry Andric case Intrinsic::matrix_column_major_load: 1029fe6060f1SDimitry Andric return cast<ConstantInt>(II->getArgOperand(2))->isOne(); 1030fe6060f1SDimitry Andric case Intrinsic::matrix_column_major_store: 1031fe6060f1SDimitry Andric return cast<ConstantInt>(II->getArgOperand(3))->isOne(); 1032fe6060f1SDimitry Andric } 1033fe6060f1SDimitry Andric } 1034fe6060f1SDimitry Andric return false; 1035fe6060f1SDimitry Andric } 1036fe6060f1SDimitry Andric } 1037fe6060f1SDimitry Andric 103806c3fb27SDimitry Andric Type *Instruction::getAccessType() const { 103906c3fb27SDimitry Andric switch (getOpcode()) { 104006c3fb27SDimitry Andric case Instruction::Store: 104106c3fb27SDimitry Andric return cast<StoreInst>(this)->getValueOperand()->getType(); 104206c3fb27SDimitry Andric case Instruction::Load: 104306c3fb27SDimitry Andric case Instruction::AtomicRMW: 104406c3fb27SDimitry Andric return getType(); 104506c3fb27SDimitry Andric case Instruction::AtomicCmpXchg: 104606c3fb27SDimitry Andric return cast<AtomicCmpXchgInst>(this)->getNewValOperand()->getType(); 104706c3fb27SDimitry Andric case Instruction::Call: 104806c3fb27SDimitry Andric case Instruction::Invoke: 104906c3fb27SDimitry Andric if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(this)) { 105006c3fb27SDimitry Andric switch (II->getIntrinsicID()) { 105106c3fb27SDimitry Andric case Intrinsic::masked_load: 105206c3fb27SDimitry Andric case Intrinsic::masked_gather: 105306c3fb27SDimitry Andric case Intrinsic::masked_expandload: 105406c3fb27SDimitry Andric case Intrinsic::vp_load: 105506c3fb27SDimitry Andric case Intrinsic::vp_gather: 105606c3fb27SDimitry Andric case Intrinsic::experimental_vp_strided_load: 105706c3fb27SDimitry Andric return II->getType(); 105806c3fb27SDimitry Andric case Intrinsic::masked_store: 105906c3fb27SDimitry Andric case Intrinsic::masked_scatter: 106006c3fb27SDimitry Andric case Intrinsic::masked_compressstore: 106106c3fb27SDimitry Andric case Intrinsic::vp_store: 106206c3fb27SDimitry Andric case Intrinsic::vp_scatter: 106306c3fb27SDimitry Andric case Intrinsic::experimental_vp_strided_store: 106406c3fb27SDimitry Andric return II->getOperand(0)->getType(); 106506c3fb27SDimitry Andric default: 106606c3fb27SDimitry Andric break; 106706c3fb27SDimitry Andric } 106806c3fb27SDimitry Andric } 106906c3fb27SDimitry Andric } 107006c3fb27SDimitry Andric 107106c3fb27SDimitry Andric return nullptr; 107206c3fb27SDimitry Andric } 107306c3fb27SDimitry Andric 107406c3fb27SDimitry Andric static bool canUnwindPastLandingPad(const LandingPadInst *LP, 107506c3fb27SDimitry Andric bool IncludePhaseOneUnwind) { 107606c3fb27SDimitry Andric // Because phase one unwinding skips cleanup landingpads, we effectively 107706c3fb27SDimitry Andric // unwind past this frame, and callers need to have valid unwind info. 107806c3fb27SDimitry Andric if (LP->isCleanup()) 107906c3fb27SDimitry Andric return IncludePhaseOneUnwind; 108006c3fb27SDimitry Andric 108106c3fb27SDimitry Andric for (unsigned I = 0; I < LP->getNumClauses(); ++I) { 108206c3fb27SDimitry Andric Constant *Clause = LP->getClause(I); 108306c3fb27SDimitry Andric // catch ptr null catches all exceptions. 108406c3fb27SDimitry Andric if (LP->isCatch(I) && isa<ConstantPointerNull>(Clause)) 108506c3fb27SDimitry Andric return false; 108606c3fb27SDimitry Andric // filter [0 x ptr] catches all exceptions. 108706c3fb27SDimitry Andric if (LP->isFilter(I) && Clause->getType()->getArrayNumElements() == 0) 108806c3fb27SDimitry Andric return false; 108906c3fb27SDimitry Andric } 109006c3fb27SDimitry Andric 109106c3fb27SDimitry Andric // May catch only some subset of exceptions, in which case other exceptions 109206c3fb27SDimitry Andric // will continue unwinding. 109306c3fb27SDimitry Andric return true; 109406c3fb27SDimitry Andric } 109506c3fb27SDimitry Andric 109606c3fb27SDimitry Andric bool Instruction::mayThrow(bool IncludePhaseOneUnwind) const { 109706c3fb27SDimitry Andric switch (getOpcode()) { 109806c3fb27SDimitry Andric case Instruction::Call: 109906c3fb27SDimitry Andric return !cast<CallInst>(this)->doesNotThrow(); 110006c3fb27SDimitry Andric case Instruction::CleanupRet: 110106c3fb27SDimitry Andric return cast<CleanupReturnInst>(this)->unwindsToCaller(); 110206c3fb27SDimitry Andric case Instruction::CatchSwitch: 110306c3fb27SDimitry Andric return cast<CatchSwitchInst>(this)->unwindsToCaller(); 110406c3fb27SDimitry Andric case Instruction::Resume: 110506c3fb27SDimitry Andric return true; 110606c3fb27SDimitry Andric case Instruction::Invoke: { 110706c3fb27SDimitry Andric // Landingpads themselves don't unwind -- however, an invoke of a skipped 110806c3fb27SDimitry Andric // landingpad may continue unwinding. 110906c3fb27SDimitry Andric BasicBlock *UnwindDest = cast<InvokeInst>(this)->getUnwindDest(); 111006c3fb27SDimitry Andric Instruction *Pad = UnwindDest->getFirstNonPHI(); 111106c3fb27SDimitry Andric if (auto *LP = dyn_cast<LandingPadInst>(Pad)) 111206c3fb27SDimitry Andric return canUnwindPastLandingPad(LP, IncludePhaseOneUnwind); 111306c3fb27SDimitry Andric return false; 111406c3fb27SDimitry Andric } 111506c3fb27SDimitry Andric case Instruction::CleanupPad: 111606c3fb27SDimitry Andric // Treat the same as cleanup landingpad. 111706c3fb27SDimitry Andric return IncludePhaseOneUnwind; 111806c3fb27SDimitry Andric default: 111906c3fb27SDimitry Andric return false; 112006c3fb27SDimitry Andric } 11210b57cec5SDimitry Andric } 11220b57cec5SDimitry Andric 1123fe6060f1SDimitry Andric bool Instruction::mayHaveSideEffects() const { 1124fe6060f1SDimitry Andric return mayWriteToMemory() || mayThrow() || !willReturn(); 1125fe6060f1SDimitry Andric } 1126fe6060f1SDimitry Andric 11270b57cec5SDimitry Andric bool Instruction::isSafeToRemove() const { 11280b57cec5SDimitry Andric return (!isa<CallInst>(this) || !this->mayHaveSideEffects()) && 112981ad6265SDimitry Andric !this->isTerminator() && !this->isEHPad(); 11300b57cec5SDimitry Andric } 11310b57cec5SDimitry Andric 1132d409305fSDimitry Andric bool Instruction::willReturn() const { 1133fe6060f1SDimitry Andric // Volatile store isn't guaranteed to return; see LangRef. 1134fe6060f1SDimitry Andric if (auto *SI = dyn_cast<StoreInst>(this)) 1135fe6060f1SDimitry Andric return !SI->isVolatile(); 1136fe6060f1SDimitry Andric 1137d409305fSDimitry Andric if (const auto *CB = dyn_cast<CallBase>(this)) 1138bdd1243dSDimitry Andric return CB->hasFnAttr(Attribute::WillReturn); 1139d409305fSDimitry Andric return true; 1140d409305fSDimitry Andric } 1141d409305fSDimitry Andric 11420b57cec5SDimitry Andric bool Instruction::isLifetimeStartOrEnd() const { 1143fe6060f1SDimitry Andric auto *II = dyn_cast<IntrinsicInst>(this); 11440b57cec5SDimitry Andric if (!II) 11450b57cec5SDimitry Andric return false; 11460b57cec5SDimitry Andric Intrinsic::ID ID = II->getIntrinsicID(); 11470b57cec5SDimitry Andric return ID == Intrinsic::lifetime_start || ID == Intrinsic::lifetime_end; 11480b57cec5SDimitry Andric } 11490b57cec5SDimitry Andric 1150fe6060f1SDimitry Andric bool Instruction::isLaunderOrStripInvariantGroup() const { 1151fe6060f1SDimitry Andric auto *II = dyn_cast<IntrinsicInst>(this); 1152fe6060f1SDimitry Andric if (!II) 1153fe6060f1SDimitry Andric return false; 1154fe6060f1SDimitry Andric Intrinsic::ID ID = II->getIntrinsicID(); 1155fe6060f1SDimitry Andric return ID == Intrinsic::launder_invariant_group || 1156fe6060f1SDimitry Andric ID == Intrinsic::strip_invariant_group; 1157fe6060f1SDimitry Andric } 1158fe6060f1SDimitry Andric 1159d409305fSDimitry Andric bool Instruction::isDebugOrPseudoInst() const { 1160d409305fSDimitry Andric return isa<DbgInfoIntrinsic>(this) || isa<PseudoProbeInst>(this); 1161d409305fSDimitry Andric } 1162d409305fSDimitry Andric 1163e8d8bef9SDimitry Andric const Instruction * 1164e8d8bef9SDimitry Andric Instruction::getNextNonDebugInstruction(bool SkipPseudoOp) const { 11650b57cec5SDimitry Andric for (const Instruction *I = getNextNode(); I; I = I->getNextNode()) 1166e8d8bef9SDimitry Andric if (!isa<DbgInfoIntrinsic>(I) && !(SkipPseudoOp && isa<PseudoProbeInst>(I))) 11670b57cec5SDimitry Andric return I; 11680b57cec5SDimitry Andric return nullptr; 11690b57cec5SDimitry Andric } 11700b57cec5SDimitry Andric 1171e8d8bef9SDimitry Andric const Instruction * 1172e8d8bef9SDimitry Andric Instruction::getPrevNonDebugInstruction(bool SkipPseudoOp) const { 11730b57cec5SDimitry Andric for (const Instruction *I = getPrevNode(); I; I = I->getPrevNode()) 1174e8d8bef9SDimitry Andric if (!isa<DbgInfoIntrinsic>(I) && !(SkipPseudoOp && isa<PseudoProbeInst>(I))) 11750b57cec5SDimitry Andric return I; 11760b57cec5SDimitry Andric return nullptr; 11770b57cec5SDimitry Andric } 11780b57cec5SDimitry Andric 11795f757f3fSDimitry Andric const DebugLoc &Instruction::getStableDebugLoc() const { 11805f757f3fSDimitry Andric if (isa<DbgInfoIntrinsic>(this)) 11815f757f3fSDimitry Andric if (const Instruction *Next = getNextNonDebugInstruction()) 11825f757f3fSDimitry Andric return Next->getDebugLoc(); 11835f757f3fSDimitry Andric return getDebugLoc(); 11845f757f3fSDimitry Andric } 11855f757f3fSDimitry Andric 11860b57cec5SDimitry Andric bool Instruction::isAssociative() const { 11875f757f3fSDimitry Andric if (auto *II = dyn_cast<IntrinsicInst>(this)) 11885f757f3fSDimitry Andric return II->isAssociative(); 11890b57cec5SDimitry Andric unsigned Opcode = getOpcode(); 11900b57cec5SDimitry Andric if (isAssociative(Opcode)) 11910b57cec5SDimitry Andric return true; 11920b57cec5SDimitry Andric 11930b57cec5SDimitry Andric switch (Opcode) { 11940b57cec5SDimitry Andric case FMul: 11950b57cec5SDimitry Andric case FAdd: 11960b57cec5SDimitry Andric return cast<FPMathOperator>(this)->hasAllowReassoc() && 11970b57cec5SDimitry Andric cast<FPMathOperator>(this)->hasNoSignedZeros(); 11980b57cec5SDimitry Andric default: 11990b57cec5SDimitry Andric return false; 12000b57cec5SDimitry Andric } 12010b57cec5SDimitry Andric } 12020b57cec5SDimitry Andric 1203e8d8bef9SDimitry Andric bool Instruction::isCommutative() const { 1204e8d8bef9SDimitry Andric if (auto *II = dyn_cast<IntrinsicInst>(this)) 1205e8d8bef9SDimitry Andric return II->isCommutative(); 1206e8d8bef9SDimitry Andric // TODO: Should allow icmp/fcmp? 1207e8d8bef9SDimitry Andric return isCommutative(getOpcode()); 1208e8d8bef9SDimitry Andric } 1209e8d8bef9SDimitry Andric 12100b57cec5SDimitry Andric unsigned Instruction::getNumSuccessors() const { 12110b57cec5SDimitry Andric switch (getOpcode()) { 12120b57cec5SDimitry Andric #define HANDLE_TERM_INST(N, OPC, CLASS) \ 12130b57cec5SDimitry Andric case Instruction::OPC: \ 12140b57cec5SDimitry Andric return static_cast<const CLASS *>(this)->getNumSuccessors(); 12150b57cec5SDimitry Andric #include "llvm/IR/Instruction.def" 12160b57cec5SDimitry Andric default: 12170b57cec5SDimitry Andric break; 12180b57cec5SDimitry Andric } 12190b57cec5SDimitry Andric llvm_unreachable("not a terminator"); 12200b57cec5SDimitry Andric } 12210b57cec5SDimitry Andric 12220b57cec5SDimitry Andric BasicBlock *Instruction::getSuccessor(unsigned idx) const { 12230b57cec5SDimitry Andric switch (getOpcode()) { 12240b57cec5SDimitry Andric #define HANDLE_TERM_INST(N, OPC, CLASS) \ 12250b57cec5SDimitry Andric case Instruction::OPC: \ 12260b57cec5SDimitry Andric return static_cast<const CLASS *>(this)->getSuccessor(idx); 12270b57cec5SDimitry Andric #include "llvm/IR/Instruction.def" 12280b57cec5SDimitry Andric default: 12290b57cec5SDimitry Andric break; 12300b57cec5SDimitry Andric } 12310b57cec5SDimitry Andric llvm_unreachable("not a terminator"); 12320b57cec5SDimitry Andric } 12330b57cec5SDimitry Andric 12340b57cec5SDimitry Andric void Instruction::setSuccessor(unsigned idx, BasicBlock *B) { 12350b57cec5SDimitry Andric switch (getOpcode()) { 12360b57cec5SDimitry Andric #define HANDLE_TERM_INST(N, OPC, CLASS) \ 12370b57cec5SDimitry Andric case Instruction::OPC: \ 12380b57cec5SDimitry Andric return static_cast<CLASS *>(this)->setSuccessor(idx, B); 12390b57cec5SDimitry Andric #include "llvm/IR/Instruction.def" 12400b57cec5SDimitry Andric default: 12410b57cec5SDimitry Andric break; 12420b57cec5SDimitry Andric } 12430b57cec5SDimitry Andric llvm_unreachable("not a terminator"); 12440b57cec5SDimitry Andric } 12450b57cec5SDimitry Andric 12460b57cec5SDimitry Andric void Instruction::replaceSuccessorWith(BasicBlock *OldBB, BasicBlock *NewBB) { 12470b57cec5SDimitry Andric for (unsigned Idx = 0, NumSuccessors = Instruction::getNumSuccessors(); 12480b57cec5SDimitry Andric Idx != NumSuccessors; ++Idx) 12490b57cec5SDimitry Andric if (getSuccessor(Idx) == OldBB) 12500b57cec5SDimitry Andric setSuccessor(Idx, NewBB); 12510b57cec5SDimitry Andric } 12520b57cec5SDimitry Andric 12530b57cec5SDimitry Andric Instruction *Instruction::cloneImpl() const { 12540b57cec5SDimitry Andric llvm_unreachable("Subclass of Instruction failed to implement cloneImpl"); 12550b57cec5SDimitry Andric } 12560b57cec5SDimitry Andric 12570b57cec5SDimitry Andric void Instruction::swapProfMetadata() { 1258bdd1243dSDimitry Andric MDNode *ProfileData = getBranchWeightMDNode(*this); 1259*0fca6ea1SDimitry Andric if (!ProfileData) 1260*0fca6ea1SDimitry Andric return; 1261*0fca6ea1SDimitry Andric unsigned FirstIdx = getBranchWeightOffset(ProfileData); 1262*0fca6ea1SDimitry Andric if (ProfileData->getNumOperands() != 2 + FirstIdx) 12630b57cec5SDimitry Andric return; 12640b57cec5SDimitry Andric 1265*0fca6ea1SDimitry Andric unsigned SecondIdx = FirstIdx + 1; 1266*0fca6ea1SDimitry Andric SmallVector<Metadata *, 4> Ops; 1267*0fca6ea1SDimitry Andric // If there are more weights past the second, we can't swap them 1268*0fca6ea1SDimitry Andric if (ProfileData->getNumOperands() > SecondIdx + 1) 1269*0fca6ea1SDimitry Andric return; 1270*0fca6ea1SDimitry Andric for (unsigned Idx = 0; Idx < FirstIdx; ++Idx) { 1271*0fca6ea1SDimitry Andric Ops.push_back(ProfileData->getOperand(Idx)); 1272*0fca6ea1SDimitry Andric } 1273*0fca6ea1SDimitry Andric // Switch the order of the weights 1274*0fca6ea1SDimitry Andric Ops.push_back(ProfileData->getOperand(SecondIdx)); 1275*0fca6ea1SDimitry Andric Ops.push_back(ProfileData->getOperand(FirstIdx)); 12760b57cec5SDimitry Andric setMetadata(LLVMContext::MD_prof, 12770b57cec5SDimitry Andric MDNode::get(ProfileData->getContext(), Ops)); 12780b57cec5SDimitry Andric } 12790b57cec5SDimitry Andric 12800b57cec5SDimitry Andric void Instruction::copyMetadata(const Instruction &SrcInst, 12810b57cec5SDimitry Andric ArrayRef<unsigned> WL) { 12820b57cec5SDimitry Andric if (!SrcInst.hasMetadata()) 12830b57cec5SDimitry Andric return; 12840b57cec5SDimitry Andric 1285*0fca6ea1SDimitry Andric SmallDenseSet<unsigned, 4> WLS(WL.begin(), WL.end()); 12860b57cec5SDimitry Andric 12870b57cec5SDimitry Andric // Otherwise, enumerate and copy over metadata from the old instruction to the 12880b57cec5SDimitry Andric // new one. 12890b57cec5SDimitry Andric SmallVector<std::pair<unsigned, MDNode *>, 4> TheMDs; 12900b57cec5SDimitry Andric SrcInst.getAllMetadataOtherThanDebugLoc(TheMDs); 12910b57cec5SDimitry Andric for (const auto &MD : TheMDs) { 12920b57cec5SDimitry Andric if (WL.empty() || WLS.count(MD.first)) 12930b57cec5SDimitry Andric setMetadata(MD.first, MD.second); 12940b57cec5SDimitry Andric } 12950b57cec5SDimitry Andric if (WL.empty() || WLS.count(LLVMContext::MD_dbg)) 12960b57cec5SDimitry Andric setDebugLoc(SrcInst.getDebugLoc()); 12970b57cec5SDimitry Andric } 12980b57cec5SDimitry Andric 12990b57cec5SDimitry Andric Instruction *Instruction::clone() const { 13000b57cec5SDimitry Andric Instruction *New = nullptr; 13010b57cec5SDimitry Andric switch (getOpcode()) { 13020b57cec5SDimitry Andric default: 13030b57cec5SDimitry Andric llvm_unreachable("Unhandled Opcode."); 13040b57cec5SDimitry Andric #define HANDLE_INST(num, opc, clas) \ 13050b57cec5SDimitry Andric case Instruction::opc: \ 13060b57cec5SDimitry Andric New = cast<clas>(this)->cloneImpl(); \ 13070b57cec5SDimitry Andric break; 13080b57cec5SDimitry Andric #include "llvm/IR/Instruction.def" 13090b57cec5SDimitry Andric #undef HANDLE_INST 13100b57cec5SDimitry Andric } 13110b57cec5SDimitry Andric 13120b57cec5SDimitry Andric New->SubclassOptionalData = SubclassOptionalData; 13130b57cec5SDimitry Andric New->copyMetadata(*this); 13140b57cec5SDimitry Andric return New; 13150b57cec5SDimitry Andric } 1316