10b57cec5SDimitry Andric //===-- BasicBlock.cpp - Implement BasicBlock related methods -------------===//
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 BasicBlock class for the IR library.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
140b57cec5SDimitry Andric #include "SymbolTableListTraitsImpl.h"
150b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
161fd87a68SDimitry Andric #include "llvm/ADT/Statistic.h"
170b57cec5SDimitry Andric #include "llvm/IR/CFG.h"
180b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
195f757f3fSDimitry Andric #include "llvm/IR/DebugProgramInstruction.h"
200b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
210b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
220b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h"
230b57cec5SDimitry Andric #include "llvm/IR/Type.h"
245f757f3fSDimitry Andric #include "llvm/Support/CommandLine.h"
255f757f3fSDimitry Andric
265f757f3fSDimitry Andric #include "LLVMContextImpl.h"
270b57cec5SDimitry Andric
280b57cec5SDimitry Andric using namespace llvm;
290b57cec5SDimitry Andric
30349cc55cSDimitry Andric #define DEBUG_TYPE "ir"
31349cc55cSDimitry Andric STATISTIC(NumInstrRenumberings, "Number of renumberings across all blocks");
32349cc55cSDimitry Andric
330fca6ea1SDimitry Andric cl::opt<bool> UseNewDbgInfoFormat(
340fca6ea1SDimitry Andric "experimental-debuginfo-iterators",
350fca6ea1SDimitry Andric cl::desc("Enable communicating debuginfo positions through iterators, "
360fca6ea1SDimitry Andric "eliminating intrinsics. Has no effect if "
370fca6ea1SDimitry Andric "--preserve-input-debuginfo-format=true."),
380fca6ea1SDimitry Andric cl::init(true));
390fca6ea1SDimitry Andric cl::opt<cl::boolOrDefault> PreserveInputDbgFormat(
400fca6ea1SDimitry Andric "preserve-input-debuginfo-format", cl::Hidden,
410fca6ea1SDimitry Andric cl::desc("When set to true, IR files will be processed and printed in "
420fca6ea1SDimitry Andric "their current debug info format, regardless of default behaviour "
430fca6ea1SDimitry Andric "or other flags passed. Has no effect if input IR does not "
440fca6ea1SDimitry Andric "contain debug records or intrinsics. Ignored in llvm-link, "
450fca6ea1SDimitry Andric "llvm-lto, and llvm-lto2."));
465f757f3fSDimitry Andric
470fca6ea1SDimitry Andric bool WriteNewDbgInfoFormatToBitcode /*set default value in cl::init() below*/;
480fca6ea1SDimitry Andric cl::opt<bool, true> WriteNewDbgInfoFormatToBitcode2(
490fca6ea1SDimitry Andric "write-experimental-debuginfo-iterators-to-bitcode", cl::Hidden,
500fca6ea1SDimitry Andric cl::location(WriteNewDbgInfoFormatToBitcode), cl::init(true));
510fca6ea1SDimitry Andric
createMarker(Instruction * I)520fca6ea1SDimitry Andric DbgMarker *BasicBlock::createMarker(Instruction *I) {
535f757f3fSDimitry Andric assert(IsNewDbgInfoFormat &&
545f757f3fSDimitry Andric "Tried to create a marker in a non new debug-info block!");
550fca6ea1SDimitry Andric if (I->DebugMarker)
560fca6ea1SDimitry Andric return I->DebugMarker;
570fca6ea1SDimitry Andric DbgMarker *Marker = new DbgMarker();
585f757f3fSDimitry Andric Marker->MarkedInstr = I;
590fca6ea1SDimitry Andric I->DebugMarker = Marker;
605f757f3fSDimitry Andric return Marker;
615f757f3fSDimitry Andric }
625f757f3fSDimitry Andric
createMarker(InstListType::iterator It)630fca6ea1SDimitry Andric DbgMarker *BasicBlock::createMarker(InstListType::iterator It) {
645f757f3fSDimitry Andric assert(IsNewDbgInfoFormat &&
655f757f3fSDimitry Andric "Tried to create a marker in a non new debug-info block!");
665f757f3fSDimitry Andric if (It != end())
675f757f3fSDimitry Andric return createMarker(&*It);
680fca6ea1SDimitry Andric DbgMarker *DM = getTrailingDbgRecords();
690fca6ea1SDimitry Andric if (DM)
700fca6ea1SDimitry Andric return DM;
710fca6ea1SDimitry Andric DM = new DbgMarker();
720fca6ea1SDimitry Andric setTrailingDbgRecords(DM);
730fca6ea1SDimitry Andric return DM;
745f757f3fSDimitry Andric }
755f757f3fSDimitry Andric
convertToNewDbgValues()765f757f3fSDimitry Andric void BasicBlock::convertToNewDbgValues() {
775f757f3fSDimitry Andric IsNewDbgInfoFormat = true;
785f757f3fSDimitry Andric
790fca6ea1SDimitry Andric // Iterate over all instructions in the instruction list, collecting debug
800fca6ea1SDimitry Andric // info intrinsics and converting them to DbgRecords. Once we find a "real"
810fca6ea1SDimitry Andric // instruction, attach all those DbgRecords to a DbgMarker in that
820fca6ea1SDimitry Andric // instruction.
830fca6ea1SDimitry Andric SmallVector<DbgRecord *, 4> DbgVarRecs;
845f757f3fSDimitry Andric for (Instruction &I : make_early_inc_range(InstList)) {
850fca6ea1SDimitry Andric assert(!I.DebugMarker && "DebugMarker already set on old-format instrs?");
865f757f3fSDimitry Andric if (DbgVariableIntrinsic *DVI = dyn_cast<DbgVariableIntrinsic>(&I)) {
870fca6ea1SDimitry Andric // Convert this dbg.value to a DbgVariableRecord.
880fca6ea1SDimitry Andric DbgVariableRecord *Value = new DbgVariableRecord(DVI);
890fca6ea1SDimitry Andric DbgVarRecs.push_back(Value);
905f757f3fSDimitry Andric DVI->eraseFromParent();
915f757f3fSDimitry Andric continue;
925f757f3fSDimitry Andric }
935f757f3fSDimitry Andric
940fca6ea1SDimitry Andric if (DbgLabelInst *DLI = dyn_cast<DbgLabelInst>(&I)) {
950fca6ea1SDimitry Andric DbgVarRecs.push_back(
960fca6ea1SDimitry Andric new DbgLabelRecord(DLI->getLabel(), DLI->getDebugLoc()));
970fca6ea1SDimitry Andric DLI->eraseFromParent();
980fca6ea1SDimitry Andric continue;
990fca6ea1SDimitry Andric }
1000fca6ea1SDimitry Andric
1010fca6ea1SDimitry Andric if (DbgVarRecs.empty())
1020fca6ea1SDimitry Andric continue;
1030fca6ea1SDimitry Andric
1040fca6ea1SDimitry Andric // Create a marker to store DbgRecords in.
1055f757f3fSDimitry Andric createMarker(&I);
1060fca6ea1SDimitry Andric DbgMarker *Marker = I.DebugMarker;
1075f757f3fSDimitry Andric
1080fca6ea1SDimitry Andric for (DbgRecord *DVR : DbgVarRecs)
1090fca6ea1SDimitry Andric Marker->insertDbgRecord(DVR, false);
1105f757f3fSDimitry Andric
1110fca6ea1SDimitry Andric DbgVarRecs.clear();
1125f757f3fSDimitry Andric }
1135f757f3fSDimitry Andric }
1145f757f3fSDimitry Andric
convertFromNewDbgValues()1155f757f3fSDimitry Andric void BasicBlock::convertFromNewDbgValues() {
1165f757f3fSDimitry Andric invalidateOrders();
1175f757f3fSDimitry Andric IsNewDbgInfoFormat = false;
1185f757f3fSDimitry Andric
1190fca6ea1SDimitry Andric // Iterate over the block, finding instructions annotated with DbgMarkers.
1200fca6ea1SDimitry Andric // Convert any attached DbgRecords to debug intrinsics and insert ahead of the
1215f757f3fSDimitry Andric // instruction.
1225f757f3fSDimitry Andric for (auto &Inst : *this) {
1230fca6ea1SDimitry Andric if (!Inst.DebugMarker)
1245f757f3fSDimitry Andric continue;
1255f757f3fSDimitry Andric
1260fca6ea1SDimitry Andric DbgMarker &Marker = *Inst.DebugMarker;
1270fca6ea1SDimitry Andric for (DbgRecord &DR : Marker.getDbgRecordRange())
1285f757f3fSDimitry Andric InstList.insert(Inst.getIterator(),
1290fca6ea1SDimitry Andric DR.createDebugIntrinsic(getModule(), nullptr));
1305f757f3fSDimitry Andric
1315f757f3fSDimitry Andric Marker.eraseFromParent();
1320fca6ea1SDimitry Andric }
1335f757f3fSDimitry Andric
1340fca6ea1SDimitry Andric // Assume no trailing DbgRecords: we could technically create them at the end
1355f757f3fSDimitry Andric // of the block, after a terminator, but this would be non-cannonical and
1365f757f3fSDimitry Andric // indicates that something else is broken somewhere.
1370fca6ea1SDimitry Andric assert(!getTrailingDbgRecords());
1385f757f3fSDimitry Andric }
1395f757f3fSDimitry Andric
1405f757f3fSDimitry Andric #ifndef NDEBUG
dumpDbgValues() const1415f757f3fSDimitry Andric void BasicBlock::dumpDbgValues() const {
1425f757f3fSDimitry Andric for (auto &Inst : *this) {
1430fca6ea1SDimitry Andric if (!Inst.DebugMarker)
1445f757f3fSDimitry Andric continue;
1455f757f3fSDimitry Andric
1460fca6ea1SDimitry Andric dbgs() << "@ " << Inst.DebugMarker << " ";
1470fca6ea1SDimitry Andric Inst.DebugMarker->dump();
1485f757f3fSDimitry Andric };
1495f757f3fSDimitry Andric }
1505f757f3fSDimitry Andric #endif
1515f757f3fSDimitry Andric
setIsNewDbgInfoFormat(bool NewFlag)1525f757f3fSDimitry Andric void BasicBlock::setIsNewDbgInfoFormat(bool NewFlag) {
1535f757f3fSDimitry Andric if (NewFlag && !IsNewDbgInfoFormat)
1545f757f3fSDimitry Andric convertToNewDbgValues();
1555f757f3fSDimitry Andric else if (!NewFlag && IsNewDbgInfoFormat)
1565f757f3fSDimitry Andric convertFromNewDbgValues();
1575f757f3fSDimitry Andric }
setNewDbgInfoFormatFlag(bool NewFlag)1580fca6ea1SDimitry Andric void BasicBlock::setNewDbgInfoFormatFlag(bool NewFlag) {
1590fca6ea1SDimitry Andric IsNewDbgInfoFormat = NewFlag;
1600fca6ea1SDimitry Andric }
1615f757f3fSDimitry Andric
getValueSymbolTable()1620b57cec5SDimitry Andric ValueSymbolTable *BasicBlock::getValueSymbolTable() {
1630b57cec5SDimitry Andric if (Function *F = getParent())
1640b57cec5SDimitry Andric return F->getValueSymbolTable();
1650b57cec5SDimitry Andric return nullptr;
1660b57cec5SDimitry Andric }
1670b57cec5SDimitry Andric
getContext() const1680b57cec5SDimitry Andric LLVMContext &BasicBlock::getContext() const {
1690b57cec5SDimitry Andric return getType()->getContext();
1700b57cec5SDimitry Andric }
1710b57cec5SDimitry Andric
invalidateParentIListOrdering(BasicBlock * BB)1725ffd83dbSDimitry Andric template <> void llvm::invalidateParentIListOrdering(BasicBlock *BB) {
1735ffd83dbSDimitry Andric BB->invalidateOrders();
1745ffd83dbSDimitry Andric }
1755ffd83dbSDimitry Andric
1760b57cec5SDimitry Andric // Explicit instantiation of SymbolTableListTraits since some of the methods
1770b57cec5SDimitry Andric // are not in the public header file...
1780fca6ea1SDimitry Andric template class llvm::SymbolTableListTraits<
1790fca6ea1SDimitry Andric Instruction, ilist_iterator_bits<true>, ilist_parent<BasicBlock>>;
1800b57cec5SDimitry Andric
BasicBlock(LLVMContext & C,const Twine & Name,Function * NewParent,BasicBlock * InsertBefore)1810b57cec5SDimitry Andric BasicBlock::BasicBlock(LLVMContext &C, const Twine &Name, Function *NewParent,
1820b57cec5SDimitry Andric BasicBlock *InsertBefore)
1835f757f3fSDimitry Andric : Value(Type::getLabelTy(C), Value::BasicBlockVal),
1840fca6ea1SDimitry Andric IsNewDbgInfoFormat(UseNewDbgInfoFormat), Parent(nullptr) {
1850b57cec5SDimitry Andric
1860b57cec5SDimitry Andric if (NewParent)
1870b57cec5SDimitry Andric insertInto(NewParent, InsertBefore);
1880b57cec5SDimitry Andric else
1890b57cec5SDimitry Andric assert(!InsertBefore &&
1900b57cec5SDimitry Andric "Cannot insert block before another block with no function!");
1910b57cec5SDimitry Andric
1920fca6ea1SDimitry Andric end().getNodePtr()->setParent(this);
1930b57cec5SDimitry Andric setName(Name);
1945f757f3fSDimitry Andric if (NewParent)
1955f757f3fSDimitry Andric setIsNewDbgInfoFormat(NewParent->IsNewDbgInfoFormat);
1960b57cec5SDimitry Andric }
1970b57cec5SDimitry Andric
insertInto(Function * NewParent,BasicBlock * InsertBefore)1980b57cec5SDimitry Andric void BasicBlock::insertInto(Function *NewParent, BasicBlock *InsertBefore) {
1990b57cec5SDimitry Andric assert(NewParent && "Expected a parent");
2000b57cec5SDimitry Andric assert(!Parent && "Already has a parent");
2010b57cec5SDimitry Andric
2020b57cec5SDimitry Andric if (InsertBefore)
203bdd1243dSDimitry Andric NewParent->insert(InsertBefore->getIterator(), this);
2040b57cec5SDimitry Andric else
205bdd1243dSDimitry Andric NewParent->insert(NewParent->end(), this);
2067a6dacacSDimitry Andric
2077a6dacacSDimitry Andric setIsNewDbgInfoFormat(NewParent->IsNewDbgInfoFormat);
2080b57cec5SDimitry Andric }
2090b57cec5SDimitry Andric
~BasicBlock()2100b57cec5SDimitry Andric BasicBlock::~BasicBlock() {
2115ffd83dbSDimitry Andric validateInstrOrdering();
2125ffd83dbSDimitry Andric
2130b57cec5SDimitry Andric // If the address of the block is taken and it is being deleted (e.g. because
2140b57cec5SDimitry Andric // it is dead), this means that there is either a dangling constant expr
2150b57cec5SDimitry Andric // hanging off the block, or an undefined use of the block (source code
2160b57cec5SDimitry Andric // expecting the address of a label to keep the block alive even though there
2170b57cec5SDimitry Andric // is no indirect branch). Handle these cases by zapping the BlockAddress
2180b57cec5SDimitry Andric // nodes. There are no other possible uses at this point.
2190b57cec5SDimitry Andric if (hasAddressTaken()) {
2200b57cec5SDimitry Andric assert(!use_empty() && "There should be at least one blockaddress!");
2210b57cec5SDimitry Andric Constant *Replacement =
2220b57cec5SDimitry Andric ConstantInt::get(llvm::Type::getInt32Ty(getContext()), 1);
2230b57cec5SDimitry Andric while (!use_empty()) {
2240b57cec5SDimitry Andric BlockAddress *BA = cast<BlockAddress>(user_back());
2250b57cec5SDimitry Andric BA->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement,
2260b57cec5SDimitry Andric BA->getType()));
2270b57cec5SDimitry Andric BA->destroyConstant();
2280b57cec5SDimitry Andric }
2290b57cec5SDimitry Andric }
2300b57cec5SDimitry Andric
2310b57cec5SDimitry Andric assert(getParent() == nullptr && "BasicBlock still linked into the program!");
2320b57cec5SDimitry Andric dropAllReferences();
2335f757f3fSDimitry Andric for (auto &Inst : *this) {
2340fca6ea1SDimitry Andric if (!Inst.DebugMarker)
2355f757f3fSDimitry Andric continue;
2360fca6ea1SDimitry Andric Inst.DebugMarker->eraseFromParent();
2375f757f3fSDimitry Andric }
2380b57cec5SDimitry Andric InstList.clear();
2390b57cec5SDimitry Andric }
2400b57cec5SDimitry Andric
setParent(Function * parent)2410b57cec5SDimitry Andric void BasicBlock::setParent(Function *parent) {
2420b57cec5SDimitry Andric // Set Parent=parent, updating instruction symtab entries as appropriate.
2430b57cec5SDimitry Andric InstList.setSymTabObject(&Parent, parent);
2440b57cec5SDimitry Andric }
2450b57cec5SDimitry Andric
2460b57cec5SDimitry Andric iterator_range<filter_iterator<BasicBlock::const_iterator,
2470b57cec5SDimitry Andric std::function<bool(const Instruction &)>>>
instructionsWithoutDebug(bool SkipPseudoOp) const248e8d8bef9SDimitry Andric BasicBlock::instructionsWithoutDebug(bool SkipPseudoOp) const {
249e8d8bef9SDimitry Andric std::function<bool(const Instruction &)> Fn = [=](const Instruction &I) {
250e8d8bef9SDimitry Andric return !isa<DbgInfoIntrinsic>(I) &&
251e8d8bef9SDimitry Andric !(SkipPseudoOp && isa<PseudoProbeInst>(I));
2520b57cec5SDimitry Andric };
2530b57cec5SDimitry Andric return make_filter_range(*this, Fn);
2540b57cec5SDimitry Andric }
2550b57cec5SDimitry Andric
256e8d8bef9SDimitry Andric iterator_range<
257e8d8bef9SDimitry Andric filter_iterator<BasicBlock::iterator, std::function<bool(Instruction &)>>>
instructionsWithoutDebug(bool SkipPseudoOp)258e8d8bef9SDimitry Andric BasicBlock::instructionsWithoutDebug(bool SkipPseudoOp) {
259e8d8bef9SDimitry Andric std::function<bool(Instruction &)> Fn = [=](Instruction &I) {
260e8d8bef9SDimitry Andric return !isa<DbgInfoIntrinsic>(I) &&
261e8d8bef9SDimitry Andric !(SkipPseudoOp && isa<PseudoProbeInst>(I));
2620b57cec5SDimitry Andric };
2630b57cec5SDimitry Andric return make_filter_range(*this, Fn);
2640b57cec5SDimitry Andric }
2650b57cec5SDimitry Andric
2668bcb0991SDimitry Andric filter_iterator<BasicBlock::const_iterator,
2678bcb0991SDimitry Andric std::function<bool(const Instruction &)>>::difference_type
sizeWithoutDebug() const2688bcb0991SDimitry Andric BasicBlock::sizeWithoutDebug() const {
2698bcb0991SDimitry Andric return std::distance(instructionsWithoutDebug().begin(),
2708bcb0991SDimitry Andric instructionsWithoutDebug().end());
2718bcb0991SDimitry Andric }
2728bcb0991SDimitry Andric
removeFromParent()2730b57cec5SDimitry Andric void BasicBlock::removeFromParent() {
2740b57cec5SDimitry Andric getParent()->getBasicBlockList().remove(getIterator());
2750b57cec5SDimitry Andric }
2760b57cec5SDimitry Andric
eraseFromParent()2770b57cec5SDimitry Andric iplist<BasicBlock>::iterator BasicBlock::eraseFromParent() {
2780b57cec5SDimitry Andric return getParent()->getBasicBlockList().erase(getIterator());
2790b57cec5SDimitry Andric }
2800b57cec5SDimitry Andric
moveBefore(SymbolTableList<BasicBlock>::iterator MovePos)28106c3fb27SDimitry Andric void BasicBlock::moveBefore(SymbolTableList<BasicBlock>::iterator MovePos) {
28206c3fb27SDimitry Andric getParent()->splice(MovePos, getParent(), getIterator());
2830b57cec5SDimitry Andric }
2840b57cec5SDimitry Andric
moveAfter(BasicBlock * MovePos)2850b57cec5SDimitry Andric void BasicBlock::moveAfter(BasicBlock *MovePos) {
286bdd1243dSDimitry Andric MovePos->getParent()->splice(++MovePos->getIterator(), getParent(),
2870b57cec5SDimitry Andric getIterator());
2880b57cec5SDimitry Andric }
2890b57cec5SDimitry Andric
getModule() const2900b57cec5SDimitry Andric const Module *BasicBlock::getModule() const {
2910b57cec5SDimitry Andric return getParent()->getParent();
2920b57cec5SDimitry Andric }
2930b57cec5SDimitry Andric
getDataLayout() const2940fca6ea1SDimitry Andric const DataLayout &BasicBlock::getDataLayout() const {
2950fca6ea1SDimitry Andric return getModule()->getDataLayout();
2960fca6ea1SDimitry Andric }
2970fca6ea1SDimitry Andric
getTerminatingMustTailCall() const2980b57cec5SDimitry Andric const CallInst *BasicBlock::getTerminatingMustTailCall() const {
2990b57cec5SDimitry Andric if (InstList.empty())
3000b57cec5SDimitry Andric return nullptr;
3010b57cec5SDimitry Andric const ReturnInst *RI = dyn_cast<ReturnInst>(&InstList.back());
3020b57cec5SDimitry Andric if (!RI || RI == &InstList.front())
3030b57cec5SDimitry Andric return nullptr;
3040b57cec5SDimitry Andric
3050b57cec5SDimitry Andric const Instruction *Prev = RI->getPrevNode();
3060b57cec5SDimitry Andric if (!Prev)
3070b57cec5SDimitry Andric return nullptr;
3080b57cec5SDimitry Andric
3090b57cec5SDimitry Andric if (Value *RV = RI->getReturnValue()) {
3100b57cec5SDimitry Andric if (RV != Prev)
3110b57cec5SDimitry Andric return nullptr;
3120b57cec5SDimitry Andric
3130b57cec5SDimitry Andric // Look through the optional bitcast.
3140b57cec5SDimitry Andric if (auto *BI = dyn_cast<BitCastInst>(Prev)) {
3150b57cec5SDimitry Andric RV = BI->getOperand(0);
3160b57cec5SDimitry Andric Prev = BI->getPrevNode();
3170b57cec5SDimitry Andric if (!Prev || RV != Prev)
3180b57cec5SDimitry Andric return nullptr;
3190b57cec5SDimitry Andric }
3200b57cec5SDimitry Andric }
3210b57cec5SDimitry Andric
3220b57cec5SDimitry Andric if (auto *CI = dyn_cast<CallInst>(Prev)) {
3230b57cec5SDimitry Andric if (CI->isMustTailCall())
3240b57cec5SDimitry Andric return CI;
3250b57cec5SDimitry Andric }
3260b57cec5SDimitry Andric return nullptr;
3270b57cec5SDimitry Andric }
3280b57cec5SDimitry Andric
getTerminatingDeoptimizeCall() const3290b57cec5SDimitry Andric const CallInst *BasicBlock::getTerminatingDeoptimizeCall() const {
3300b57cec5SDimitry Andric if (InstList.empty())
3310b57cec5SDimitry Andric return nullptr;
3320b57cec5SDimitry Andric auto *RI = dyn_cast<ReturnInst>(&InstList.back());
3330b57cec5SDimitry Andric if (!RI || RI == &InstList.front())
3340b57cec5SDimitry Andric return nullptr;
3350b57cec5SDimitry Andric
3360b57cec5SDimitry Andric if (auto *CI = dyn_cast_or_null<CallInst>(RI->getPrevNode()))
3370b57cec5SDimitry Andric if (Function *F = CI->getCalledFunction())
3380b57cec5SDimitry Andric if (F->getIntrinsicID() == Intrinsic::experimental_deoptimize)
3390b57cec5SDimitry Andric return CI;
3400b57cec5SDimitry Andric
3410b57cec5SDimitry Andric return nullptr;
3420b57cec5SDimitry Andric }
3430b57cec5SDimitry Andric
getPostdominatingDeoptimizeCall() const3445ffd83dbSDimitry Andric const CallInst *BasicBlock::getPostdominatingDeoptimizeCall() const {
3455ffd83dbSDimitry Andric const BasicBlock* BB = this;
3465ffd83dbSDimitry Andric SmallPtrSet<const BasicBlock *, 8> Visited;
3475ffd83dbSDimitry Andric Visited.insert(BB);
3485ffd83dbSDimitry Andric while (auto *Succ = BB->getUniqueSuccessor()) {
3495ffd83dbSDimitry Andric if (!Visited.insert(Succ).second)
3505ffd83dbSDimitry Andric return nullptr;
3515ffd83dbSDimitry Andric BB = Succ;
3525ffd83dbSDimitry Andric }
3535ffd83dbSDimitry Andric return BB->getTerminatingDeoptimizeCall();
3545ffd83dbSDimitry Andric }
3555ffd83dbSDimitry Andric
getFirstMayFaultInst() const35606c3fb27SDimitry Andric const Instruction *BasicBlock::getFirstMayFaultInst() const {
35706c3fb27SDimitry Andric if (InstList.empty())
35806c3fb27SDimitry Andric return nullptr;
35906c3fb27SDimitry Andric for (const Instruction &I : *this)
36006c3fb27SDimitry Andric if (isa<LoadInst>(I) || isa<StoreInst>(I) || isa<CallBase>(I))
36106c3fb27SDimitry Andric return &I;
36206c3fb27SDimitry Andric return nullptr;
36306c3fb27SDimitry Andric }
36406c3fb27SDimitry Andric
getFirstNonPHI() const3650b57cec5SDimitry Andric const Instruction* BasicBlock::getFirstNonPHI() const {
3660b57cec5SDimitry Andric for (const Instruction &I : *this)
3670b57cec5SDimitry Andric if (!isa<PHINode>(I))
3680b57cec5SDimitry Andric return &I;
3690b57cec5SDimitry Andric return nullptr;
3700b57cec5SDimitry Andric }
3710b57cec5SDimitry Andric
getFirstNonPHIIt() const3725f757f3fSDimitry Andric BasicBlock::const_iterator BasicBlock::getFirstNonPHIIt() const {
3735f757f3fSDimitry Andric const Instruction *I = getFirstNonPHI();
3740fca6ea1SDimitry Andric if (!I)
3750fca6ea1SDimitry Andric return end();
3765f757f3fSDimitry Andric BasicBlock::const_iterator It = I->getIterator();
3775f757f3fSDimitry Andric // Set the head-inclusive bit to indicate that this iterator includes
3785f757f3fSDimitry Andric // any debug-info at the start of the block. This is a no-op unless the
3795f757f3fSDimitry Andric // appropriate CMake flag is set.
3805f757f3fSDimitry Andric It.setHeadBit(true);
3815f757f3fSDimitry Andric return It;
3825f757f3fSDimitry Andric }
3835f757f3fSDimitry Andric
getFirstNonPHIOrDbg(bool SkipPseudoOp) const384e8d8bef9SDimitry Andric const Instruction *BasicBlock::getFirstNonPHIOrDbg(bool SkipPseudoOp) const {
385e8d8bef9SDimitry Andric for (const Instruction &I : *this) {
386e8d8bef9SDimitry Andric if (isa<PHINode>(I) || isa<DbgInfoIntrinsic>(I))
387e8d8bef9SDimitry Andric continue;
388e8d8bef9SDimitry Andric
389e8d8bef9SDimitry Andric if (SkipPseudoOp && isa<PseudoProbeInst>(I))
390e8d8bef9SDimitry Andric continue;
391e8d8bef9SDimitry Andric
3920b57cec5SDimitry Andric return &I;
393e8d8bef9SDimitry Andric }
3940b57cec5SDimitry Andric return nullptr;
3950b57cec5SDimitry Andric }
3960b57cec5SDimitry Andric
397e8d8bef9SDimitry Andric const Instruction *
getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp) const398e8d8bef9SDimitry Andric BasicBlock::getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp) const {
3990b57cec5SDimitry Andric for (const Instruction &I : *this) {
4000b57cec5SDimitry Andric if (isa<PHINode>(I) || isa<DbgInfoIntrinsic>(I))
4010b57cec5SDimitry Andric continue;
4020b57cec5SDimitry Andric
4030b57cec5SDimitry Andric if (I.isLifetimeStartOrEnd())
4040b57cec5SDimitry Andric continue;
4050b57cec5SDimitry Andric
406e8d8bef9SDimitry Andric if (SkipPseudoOp && isa<PseudoProbeInst>(I))
407e8d8bef9SDimitry Andric continue;
408e8d8bef9SDimitry Andric
4090b57cec5SDimitry Andric return &I;
4100b57cec5SDimitry Andric }
4110b57cec5SDimitry Andric return nullptr;
4120b57cec5SDimitry Andric }
4130b57cec5SDimitry Andric
getFirstInsertionPt() const4140b57cec5SDimitry Andric BasicBlock::const_iterator BasicBlock::getFirstInsertionPt() const {
4150b57cec5SDimitry Andric const Instruction *FirstNonPHI = getFirstNonPHI();
4160b57cec5SDimitry Andric if (!FirstNonPHI)
4170b57cec5SDimitry Andric return end();
4180b57cec5SDimitry Andric
4190b57cec5SDimitry Andric const_iterator InsertPt = FirstNonPHI->getIterator();
4200b57cec5SDimitry Andric if (InsertPt->isEHPad()) ++InsertPt;
4215f757f3fSDimitry Andric // Set the head-inclusive bit to indicate that this iterator includes
4225f757f3fSDimitry Andric // any debug-info at the start of the block. This is a no-op unless the
4235f757f3fSDimitry Andric // appropriate CMake flag is set.
4245f757f3fSDimitry Andric InsertPt.setHeadBit(true);
4250b57cec5SDimitry Andric return InsertPt;
4260b57cec5SDimitry Andric }
4270b57cec5SDimitry Andric
getFirstNonPHIOrDbgOrAlloca() const428bdd1243dSDimitry Andric BasicBlock::const_iterator BasicBlock::getFirstNonPHIOrDbgOrAlloca() const {
429bdd1243dSDimitry Andric const Instruction *FirstNonPHI = getFirstNonPHI();
430bdd1243dSDimitry Andric if (!FirstNonPHI)
431bdd1243dSDimitry Andric return end();
432bdd1243dSDimitry Andric
433bdd1243dSDimitry Andric const_iterator InsertPt = FirstNonPHI->getIterator();
434bdd1243dSDimitry Andric if (InsertPt->isEHPad())
435bdd1243dSDimitry Andric ++InsertPt;
436bdd1243dSDimitry Andric
437bdd1243dSDimitry Andric if (isEntryBlock()) {
438bdd1243dSDimitry Andric const_iterator End = end();
439bdd1243dSDimitry Andric while (InsertPt != End &&
440bdd1243dSDimitry Andric (isa<AllocaInst>(*InsertPt) || isa<DbgInfoIntrinsic>(*InsertPt) ||
441bdd1243dSDimitry Andric isa<PseudoProbeInst>(*InsertPt))) {
442bdd1243dSDimitry Andric if (const AllocaInst *AI = dyn_cast<AllocaInst>(&*InsertPt)) {
443bdd1243dSDimitry Andric if (!AI->isStaticAlloca())
444bdd1243dSDimitry Andric break;
445bdd1243dSDimitry Andric }
446bdd1243dSDimitry Andric ++InsertPt;
447bdd1243dSDimitry Andric }
448bdd1243dSDimitry Andric }
449bdd1243dSDimitry Andric return InsertPt;
450bdd1243dSDimitry Andric }
451bdd1243dSDimitry Andric
dropAllReferences()4520b57cec5SDimitry Andric void BasicBlock::dropAllReferences() {
4530b57cec5SDimitry Andric for (Instruction &I : *this)
4540b57cec5SDimitry Andric I.dropAllReferences();
4550b57cec5SDimitry Andric }
4560b57cec5SDimitry Andric
getSinglePredecessor() const4570b57cec5SDimitry Andric const BasicBlock *BasicBlock::getSinglePredecessor() const {
4580b57cec5SDimitry Andric const_pred_iterator PI = pred_begin(this), E = pred_end(this);
4590b57cec5SDimitry Andric if (PI == E) return nullptr; // No preds.
4600b57cec5SDimitry Andric const BasicBlock *ThePred = *PI;
4610b57cec5SDimitry Andric ++PI;
4620b57cec5SDimitry Andric return (PI == E) ? ThePred : nullptr /*multiple preds*/;
4630b57cec5SDimitry Andric }
4640b57cec5SDimitry Andric
getUniquePredecessor() const4650b57cec5SDimitry Andric const BasicBlock *BasicBlock::getUniquePredecessor() const {
4660b57cec5SDimitry Andric const_pred_iterator PI = pred_begin(this), E = pred_end(this);
4670b57cec5SDimitry Andric if (PI == E) return nullptr; // No preds.
4680b57cec5SDimitry Andric const BasicBlock *PredBB = *PI;
4690b57cec5SDimitry Andric ++PI;
4700b57cec5SDimitry Andric for (;PI != E; ++PI) {
4710b57cec5SDimitry Andric if (*PI != PredBB)
4720b57cec5SDimitry Andric return nullptr;
4730b57cec5SDimitry Andric // The same predecessor appears multiple times in the predecessor list.
4740b57cec5SDimitry Andric // This is OK.
4750b57cec5SDimitry Andric }
4760b57cec5SDimitry Andric return PredBB;
4770b57cec5SDimitry Andric }
4780b57cec5SDimitry Andric
hasNPredecessors(unsigned N) const4790b57cec5SDimitry Andric bool BasicBlock::hasNPredecessors(unsigned N) const {
4800b57cec5SDimitry Andric return hasNItems(pred_begin(this), pred_end(this), N);
4810b57cec5SDimitry Andric }
4820b57cec5SDimitry Andric
hasNPredecessorsOrMore(unsigned N) const4830b57cec5SDimitry Andric bool BasicBlock::hasNPredecessorsOrMore(unsigned N) const {
4840b57cec5SDimitry Andric return hasNItemsOrMore(pred_begin(this), pred_end(this), N);
4850b57cec5SDimitry Andric }
4860b57cec5SDimitry Andric
getSingleSuccessor() const4870b57cec5SDimitry Andric const BasicBlock *BasicBlock::getSingleSuccessor() const {
4885ffd83dbSDimitry Andric const_succ_iterator SI = succ_begin(this), E = succ_end(this);
4890b57cec5SDimitry Andric if (SI == E) return nullptr; // no successors
4900b57cec5SDimitry Andric const BasicBlock *TheSucc = *SI;
4910b57cec5SDimitry Andric ++SI;
4920b57cec5SDimitry Andric return (SI == E) ? TheSucc : nullptr /* multiple successors */;
4930b57cec5SDimitry Andric }
4940b57cec5SDimitry Andric
getUniqueSuccessor() const4950b57cec5SDimitry Andric const BasicBlock *BasicBlock::getUniqueSuccessor() const {
4965ffd83dbSDimitry Andric const_succ_iterator SI = succ_begin(this), E = succ_end(this);
4970b57cec5SDimitry Andric if (SI == E) return nullptr; // No successors
4980b57cec5SDimitry Andric const BasicBlock *SuccBB = *SI;
4990b57cec5SDimitry Andric ++SI;
5000b57cec5SDimitry Andric for (;SI != E; ++SI) {
5010b57cec5SDimitry Andric if (*SI != SuccBB)
5020b57cec5SDimitry Andric return nullptr;
5030b57cec5SDimitry Andric // The same successor appears multiple times in the successor list.
5040b57cec5SDimitry Andric // This is OK.
5050b57cec5SDimitry Andric }
5060b57cec5SDimitry Andric return SuccBB;
5070b57cec5SDimitry Andric }
5080b57cec5SDimitry Andric
phis()5090b57cec5SDimitry Andric iterator_range<BasicBlock::phi_iterator> BasicBlock::phis() {
5100b57cec5SDimitry Andric PHINode *P = empty() ? nullptr : dyn_cast<PHINode>(&*begin());
5110b57cec5SDimitry Andric return make_range<phi_iterator>(P, nullptr);
5120b57cec5SDimitry Andric }
5130b57cec5SDimitry Andric
removePredecessor(BasicBlock * Pred,bool KeepOneInputPHIs)5140b57cec5SDimitry Andric void BasicBlock::removePredecessor(BasicBlock *Pred,
5150b57cec5SDimitry Andric bool KeepOneInputPHIs) {
5165ffd83dbSDimitry Andric // Use hasNUsesOrMore to bound the cost of this assertion for complex CFGs.
517e8d8bef9SDimitry Andric assert((hasNUsesOrMore(16) || llvm::is_contained(predecessors(this), Pred)) &&
5185ffd83dbSDimitry Andric "Pred is not a predecessor!");
5190b57cec5SDimitry Andric
5205ffd83dbSDimitry Andric // Return early if there are no PHI nodes to update.
521e8d8bef9SDimitry Andric if (empty() || !isa<PHINode>(begin()))
5225ffd83dbSDimitry Andric return;
5230b57cec5SDimitry Andric
524e8d8bef9SDimitry Andric unsigned NumPreds = cast<PHINode>(front()).getNumIncomingValues();
525e8d8bef9SDimitry Andric for (PHINode &Phi : make_early_inc_range(phis())) {
526e8d8bef9SDimitry Andric Phi.removeIncomingValue(Pred, !KeepOneInputPHIs);
527e8d8bef9SDimitry Andric if (KeepOneInputPHIs)
528e8d8bef9SDimitry Andric continue;
529e8d8bef9SDimitry Andric
530e8d8bef9SDimitry Andric // If we have a single predecessor, removeIncomingValue may have erased the
531e8d8bef9SDimitry Andric // PHI node itself.
532e8d8bef9SDimitry Andric if (NumPreds == 1)
533e8d8bef9SDimitry Andric continue;
534e8d8bef9SDimitry Andric
535e8d8bef9SDimitry Andric // Try to replace the PHI node with a constant value.
536e8d8bef9SDimitry Andric if (Value *PhiConstant = Phi.hasConstantValue()) {
537e8d8bef9SDimitry Andric Phi.replaceAllUsesWith(PhiConstant);
538e8d8bef9SDimitry Andric Phi.eraseFromParent();
5390b57cec5SDimitry Andric }
5400b57cec5SDimitry Andric }
5415ffd83dbSDimitry Andric }
5420b57cec5SDimitry Andric
canSplitPredecessors() const5430b57cec5SDimitry Andric bool BasicBlock::canSplitPredecessors() const {
5440b57cec5SDimitry Andric const Instruction *FirstNonPHI = getFirstNonPHI();
5450b57cec5SDimitry Andric if (isa<LandingPadInst>(FirstNonPHI))
5460b57cec5SDimitry Andric return true;
5470b57cec5SDimitry Andric // This is perhaps a little conservative because constructs like
5480b57cec5SDimitry Andric // CleanupBlockInst are pretty easy to split. However, SplitBlockPredecessors
5490b57cec5SDimitry Andric // cannot handle such things just yet.
5500b57cec5SDimitry Andric if (FirstNonPHI->isEHPad())
5510b57cec5SDimitry Andric return false;
5520b57cec5SDimitry Andric return true;
5530b57cec5SDimitry Andric }
5540b57cec5SDimitry Andric
isLegalToHoistInto() const5550b57cec5SDimitry Andric bool BasicBlock::isLegalToHoistInto() const {
5560b57cec5SDimitry Andric auto *Term = getTerminator();
5570b57cec5SDimitry Andric // No terminator means the block is under construction.
5580b57cec5SDimitry Andric if (!Term)
5590b57cec5SDimitry Andric return true;
5600b57cec5SDimitry Andric
5610b57cec5SDimitry Andric // If the block has no successors, there can be no instructions to hoist.
5620b57cec5SDimitry Andric assert(Term->getNumSuccessors() > 0);
5630b57cec5SDimitry Andric
5645f757f3fSDimitry Andric // Instructions should not be hoisted across special terminators, which may
5655f757f3fSDimitry Andric // have side effects or return values.
5665f757f3fSDimitry Andric return !Term->isSpecialTerminator();
5670b57cec5SDimitry Andric }
5680b57cec5SDimitry Andric
isEntryBlock() const569fe6060f1SDimitry Andric bool BasicBlock::isEntryBlock() const {
570fe6060f1SDimitry Andric const Function *F = getParent();
571fe6060f1SDimitry Andric assert(F && "Block must have a parent function to use this API");
572fe6060f1SDimitry Andric return this == &F->getEntryBlock();
573fe6060f1SDimitry Andric }
574fe6060f1SDimitry Andric
splitBasicBlock(iterator I,const Twine & BBName,bool Before)575e8d8bef9SDimitry Andric BasicBlock *BasicBlock::splitBasicBlock(iterator I, const Twine &BBName,
576e8d8bef9SDimitry Andric bool Before) {
577e8d8bef9SDimitry Andric if (Before)
578e8d8bef9SDimitry Andric return splitBasicBlockBefore(I, BBName);
579e8d8bef9SDimitry Andric
5800b57cec5SDimitry Andric assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
5810b57cec5SDimitry Andric assert(I != InstList.end() &&
5820b57cec5SDimitry Andric "Trying to get me to create degenerate basic block!");
5830b57cec5SDimitry Andric
5840b57cec5SDimitry Andric BasicBlock *New = BasicBlock::Create(getContext(), BBName, getParent(),
5850b57cec5SDimitry Andric this->getNextNode());
5860b57cec5SDimitry Andric
5870b57cec5SDimitry Andric // Save DebugLoc of split point before invalidating iterator.
5885f757f3fSDimitry Andric DebugLoc Loc = I->getStableDebugLoc();
5890b57cec5SDimitry Andric // Move all of the specified instructions from the original basic block into
5900b57cec5SDimitry Andric // the new basic block.
591bdd1243dSDimitry Andric New->splice(New->end(), this, I, end());
5920b57cec5SDimitry Andric
5930b57cec5SDimitry Andric // Add a branch instruction to the newly formed basic block.
5940b57cec5SDimitry Andric BranchInst *BI = BranchInst::Create(New, this);
5950b57cec5SDimitry Andric BI->setDebugLoc(Loc);
5960b57cec5SDimitry Andric
5970b57cec5SDimitry Andric // Now we must loop through all of the successors of the New block (which
5980b57cec5SDimitry Andric // _were_ the successors of the 'this' block), and update any PHI nodes in
5990b57cec5SDimitry Andric // successors. If there were PHI nodes in the successors, then they need to
6000b57cec5SDimitry Andric // know that incoming branches will be from New, not from Old (this).
6010b57cec5SDimitry Andric //
6020b57cec5SDimitry Andric New->replaceSuccessorsPhiUsesWith(this, New);
6030b57cec5SDimitry Andric return New;
6040b57cec5SDimitry Andric }
6050b57cec5SDimitry Andric
splitBasicBlockBefore(iterator I,const Twine & BBName)606e8d8bef9SDimitry Andric BasicBlock *BasicBlock::splitBasicBlockBefore(iterator I, const Twine &BBName) {
607e8d8bef9SDimitry Andric assert(getTerminator() &&
608e8d8bef9SDimitry Andric "Can't use splitBasicBlockBefore on degenerate BB!");
609e8d8bef9SDimitry Andric assert(I != InstList.end() &&
610e8d8bef9SDimitry Andric "Trying to get me to create degenerate basic block!");
611e8d8bef9SDimitry Andric
612e8d8bef9SDimitry Andric assert((!isa<PHINode>(*I) || getSinglePredecessor()) &&
613e8d8bef9SDimitry Andric "cannot split on multi incoming phis");
614e8d8bef9SDimitry Andric
615e8d8bef9SDimitry Andric BasicBlock *New = BasicBlock::Create(getContext(), BBName, getParent(), this);
616e8d8bef9SDimitry Andric // Save DebugLoc of split point before invalidating iterator.
617e8d8bef9SDimitry Andric DebugLoc Loc = I->getDebugLoc();
618e8d8bef9SDimitry Andric // Move all of the specified instructions from the original basic block into
619e8d8bef9SDimitry Andric // the new basic block.
620bdd1243dSDimitry Andric New->splice(New->end(), this, begin(), I);
621e8d8bef9SDimitry Andric
622e8d8bef9SDimitry Andric // Loop through all of the predecessors of the 'this' block (which will be the
623e8d8bef9SDimitry Andric // predecessors of the New block), replace the specified successor 'this'
624e8d8bef9SDimitry Andric // block to point at the New block and update any PHI nodes in 'this' block.
625e8d8bef9SDimitry Andric // If there were PHI nodes in 'this' block, the PHI nodes are updated
626e8d8bef9SDimitry Andric // to reflect that the incoming branches will be from the New block and not
627e8d8bef9SDimitry Andric // from predecessors of the 'this' block.
628bdd1243dSDimitry Andric // Save predecessors to separate vector before modifying them.
629bdd1243dSDimitry Andric SmallVector<BasicBlock *, 4> Predecessors;
630bdd1243dSDimitry Andric for (BasicBlock *Pred : predecessors(this))
631bdd1243dSDimitry Andric Predecessors.push_back(Pred);
632bdd1243dSDimitry Andric for (BasicBlock *Pred : Predecessors) {
633e8d8bef9SDimitry Andric Instruction *TI = Pred->getTerminator();
634e8d8bef9SDimitry Andric TI->replaceSuccessorWith(this, New);
635e8d8bef9SDimitry Andric this->replacePhiUsesWith(Pred, New);
636e8d8bef9SDimitry Andric }
637e8d8bef9SDimitry Andric // Add a branch instruction from "New" to "this" Block.
638e8d8bef9SDimitry Andric BranchInst *BI = BranchInst::Create(this, New);
639e8d8bef9SDimitry Andric BI->setDebugLoc(Loc);
640e8d8bef9SDimitry Andric
641e8d8bef9SDimitry Andric return New;
642e8d8bef9SDimitry Andric }
643e8d8bef9SDimitry Andric
erase(BasicBlock::iterator FromIt,BasicBlock::iterator ToIt)644bdd1243dSDimitry Andric BasicBlock::iterator BasicBlock::erase(BasicBlock::iterator FromIt,
645bdd1243dSDimitry Andric BasicBlock::iterator ToIt) {
6460fca6ea1SDimitry Andric for (Instruction &I : make_early_inc_range(make_range(FromIt, ToIt)))
6470fca6ea1SDimitry Andric I.eraseFromParent();
6480fca6ea1SDimitry Andric return ToIt;
649bdd1243dSDimitry Andric }
650bdd1243dSDimitry Andric
replacePhiUsesWith(BasicBlock * Old,BasicBlock * New)6510b57cec5SDimitry Andric void BasicBlock::replacePhiUsesWith(BasicBlock *Old, BasicBlock *New) {
6520b57cec5SDimitry Andric // N.B. This might not be a complete BasicBlock, so don't assume
6530b57cec5SDimitry Andric // that it ends with a non-phi instruction.
6540eae32dcSDimitry Andric for (Instruction &I : *this) {
6550eae32dcSDimitry Andric PHINode *PN = dyn_cast<PHINode>(&I);
6560b57cec5SDimitry Andric if (!PN)
6570b57cec5SDimitry Andric break;
6580b57cec5SDimitry Andric PN->replaceIncomingBlockWith(Old, New);
6590b57cec5SDimitry Andric }
6600b57cec5SDimitry Andric }
6610b57cec5SDimitry Andric
replaceSuccessorsPhiUsesWith(BasicBlock * Old,BasicBlock * New)6620b57cec5SDimitry Andric void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *Old,
6630b57cec5SDimitry Andric BasicBlock *New) {
6640b57cec5SDimitry Andric Instruction *TI = getTerminator();
6650b57cec5SDimitry Andric if (!TI)
6660b57cec5SDimitry Andric // Cope with being called on a BasicBlock that doesn't have a terminator
6670b57cec5SDimitry Andric // yet. Clang's CodeGenFunction::EmitReturnBlock() likes to do this.
6680b57cec5SDimitry Andric return;
669fe6060f1SDimitry Andric for (BasicBlock *Succ : successors(TI))
6700b57cec5SDimitry Andric Succ->replacePhiUsesWith(Old, New);
6710b57cec5SDimitry Andric }
6720b57cec5SDimitry Andric
replaceSuccessorsPhiUsesWith(BasicBlock * New)6730b57cec5SDimitry Andric void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *New) {
6740b57cec5SDimitry Andric this->replaceSuccessorsPhiUsesWith(this, New);
6750b57cec5SDimitry Andric }
6760b57cec5SDimitry Andric
isLandingPad() const6770b57cec5SDimitry Andric bool BasicBlock::isLandingPad() const {
6780b57cec5SDimitry Andric return isa<LandingPadInst>(getFirstNonPHI());
6790b57cec5SDimitry Andric }
6800b57cec5SDimitry Andric
getLandingPadInst() const6810b57cec5SDimitry Andric const LandingPadInst *BasicBlock::getLandingPadInst() const {
6820b57cec5SDimitry Andric return dyn_cast<LandingPadInst>(getFirstNonPHI());
6830b57cec5SDimitry Andric }
6840b57cec5SDimitry Andric
getIrrLoopHeaderWeight() const685bdd1243dSDimitry Andric std::optional<uint64_t> BasicBlock::getIrrLoopHeaderWeight() const {
6860b57cec5SDimitry Andric const Instruction *TI = getTerminator();
6870b57cec5SDimitry Andric if (MDNode *MDIrrLoopHeader =
6880b57cec5SDimitry Andric TI->getMetadata(LLVMContext::MD_irr_loop)) {
6890b57cec5SDimitry Andric MDString *MDName = cast<MDString>(MDIrrLoopHeader->getOperand(0));
6900fca6ea1SDimitry Andric if (MDName->getString() == "loop_header_weight") {
6910b57cec5SDimitry Andric auto *CI = mdconst::extract<ConstantInt>(MDIrrLoopHeader->getOperand(1));
692bdd1243dSDimitry Andric return std::optional<uint64_t>(CI->getValue().getZExtValue());
6930b57cec5SDimitry Andric }
6940b57cec5SDimitry Andric }
695bdd1243dSDimitry Andric return std::nullopt;
6960b57cec5SDimitry Andric }
6970b57cec5SDimitry Andric
skipDebugIntrinsics(BasicBlock::iterator It)6980b57cec5SDimitry Andric BasicBlock::iterator llvm::skipDebugIntrinsics(BasicBlock::iterator It) {
6990b57cec5SDimitry Andric while (isa<DbgInfoIntrinsic>(It))
7000b57cec5SDimitry Andric ++It;
7010b57cec5SDimitry Andric return It;
7020b57cec5SDimitry Andric }
7035ffd83dbSDimitry Andric
renumberInstructions()7045ffd83dbSDimitry Andric void BasicBlock::renumberInstructions() {
7055ffd83dbSDimitry Andric unsigned Order = 0;
7065ffd83dbSDimitry Andric for (Instruction &I : *this)
7075ffd83dbSDimitry Andric I.Order = Order++;
7085ffd83dbSDimitry Andric
7095ffd83dbSDimitry Andric // Set the bit to indicate that the instruction order valid and cached.
7105ffd83dbSDimitry Andric BasicBlockBits Bits = getBasicBlockBits();
7115ffd83dbSDimitry Andric Bits.InstrOrderValid = true;
7125ffd83dbSDimitry Andric setBasicBlockBits(Bits);
713349cc55cSDimitry Andric
714349cc55cSDimitry Andric NumInstrRenumberings++;
7155ffd83dbSDimitry Andric }
7165ffd83dbSDimitry Andric
flushTerminatorDbgRecords()7170fca6ea1SDimitry Andric void BasicBlock::flushTerminatorDbgRecords() {
7180fca6ea1SDimitry Andric // If we erase the terminator in a block, any DbgRecords will sink and "fall
7195f757f3fSDimitry Andric // off the end", existing after any terminator that gets inserted. With
7205f757f3fSDimitry Andric // dbg.value intrinsics we would just insert the terminator at end() and
7210fca6ea1SDimitry Andric // the dbg.values would come before the terminator. With DbgRecords, we must
7225f757f3fSDimitry Andric // do this manually.
7235f757f3fSDimitry Andric // To get out of this unfortunate form, whenever we insert a terminator,
7240fca6ea1SDimitry Andric // check whether there's anything trailing at the end and move those
7250fca6ea1SDimitry Andric // DbgRecords in front of the terminator.
7265f757f3fSDimitry Andric
7275f757f3fSDimitry Andric // Do nothing if we're not in new debug-info format.
7285f757f3fSDimitry Andric if (!IsNewDbgInfoFormat)
7295f757f3fSDimitry Andric return;
7305f757f3fSDimitry Andric
7315f757f3fSDimitry Andric // If there's no terminator, there's nothing to do.
7325f757f3fSDimitry Andric Instruction *Term = getTerminator();
7335f757f3fSDimitry Andric if (!Term)
7345f757f3fSDimitry Andric return;
7355f757f3fSDimitry Andric
7360fca6ea1SDimitry Andric // Are there any dangling DbgRecords?
7370fca6ea1SDimitry Andric DbgMarker *TrailingDbgRecords = getTrailingDbgRecords();
7380fca6ea1SDimitry Andric if (!TrailingDbgRecords)
7395f757f3fSDimitry Andric return;
7405f757f3fSDimitry Andric
7410fca6ea1SDimitry Andric // Transfer DbgRecords from the trailing position onto the terminator.
7420fca6ea1SDimitry Andric createMarker(Term);
7430fca6ea1SDimitry Andric Term->DebugMarker->absorbDebugValues(*TrailingDbgRecords, false);
7440fca6ea1SDimitry Andric TrailingDbgRecords->eraseFromParent();
7450fca6ea1SDimitry Andric deleteTrailingDbgRecords();
7465f757f3fSDimitry Andric }
7475f757f3fSDimitry Andric
spliceDebugInfoEmptyBlock(BasicBlock::iterator Dest,BasicBlock * Src,BasicBlock::iterator First,BasicBlock::iterator Last)7485f757f3fSDimitry Andric void BasicBlock::spliceDebugInfoEmptyBlock(BasicBlock::iterator Dest,
7495f757f3fSDimitry Andric BasicBlock *Src,
7505f757f3fSDimitry Andric BasicBlock::iterator First,
7515f757f3fSDimitry Andric BasicBlock::iterator Last) {
7525f757f3fSDimitry Andric // Imagine the folowing:
7535f757f3fSDimitry Andric //
7545f757f3fSDimitry Andric // bb1:
7555f757f3fSDimitry Andric // dbg.value(...
7565f757f3fSDimitry Andric // ret i32 0
7575f757f3fSDimitry Andric //
7585f757f3fSDimitry Andric // If an optimisation pass attempts to splice the contents of the block from
7595f757f3fSDimitry Andric // BB1->begin() to BB1->getTerminator(), then the dbg.value will be
7605f757f3fSDimitry Andric // transferred to the destination.
7610fca6ea1SDimitry Andric // However, in the "new" DbgRecord format for debug-info, that range is empty:
7625f757f3fSDimitry Andric // begin() returns an iterator to the terminator, as there will only be a
7635f757f3fSDimitry Andric // single instruction in the block. We must piece together from the bits set
7645f757f3fSDimitry Andric // in the iterators whether there was the intention to transfer any debug
7655f757f3fSDimitry Andric // info.
7665f757f3fSDimitry Andric
7675f757f3fSDimitry Andric // If we're not in "new" debug-info format, do nothing.
7685f757f3fSDimitry Andric if (!IsNewDbgInfoFormat)
7695f757f3fSDimitry Andric return;
7705f757f3fSDimitry Andric
7715f757f3fSDimitry Andric assert(First == Last);
7725f757f3fSDimitry Andric bool InsertAtHead = Dest.getHeadBit();
7735f757f3fSDimitry Andric bool ReadFromHead = First.getHeadBit();
7745f757f3fSDimitry Andric
7755f757f3fSDimitry Andric // If the source block is completely empty, including no terminator, then
7760fca6ea1SDimitry Andric // transfer any trailing DbgRecords that are still hanging around. This can
7775f757f3fSDimitry Andric // occur when a block is optimised away and the terminator has been moved
7785f757f3fSDimitry Andric // somewhere else.
7795f757f3fSDimitry Andric if (Src->empty()) {
7800fca6ea1SDimitry Andric DbgMarker *SrcTrailingDbgRecords = Src->getTrailingDbgRecords();
7810fca6ea1SDimitry Andric if (!SrcTrailingDbgRecords)
7825f757f3fSDimitry Andric return;
7835f757f3fSDimitry Andric
7840fca6ea1SDimitry Andric Dest->adoptDbgRecords(Src, Src->end(), InsertAtHead);
7850fca6ea1SDimitry Andric // adoptDbgRecords should have released the trailing DbgRecords.
7860fca6ea1SDimitry Andric assert(!Src->getTrailingDbgRecords());
7875f757f3fSDimitry Andric return;
7885f757f3fSDimitry Andric }
7895f757f3fSDimitry Andric
7905f757f3fSDimitry Andric // There are instructions in this block; if the First iterator was
7915f757f3fSDimitry Andric // with begin() / getFirstInsertionPt() then the caller intended debug-info
7927a6dacacSDimitry Andric // at the start of the block to be transferred. Return otherwise.
7937a6dacacSDimitry Andric if (Src->empty() || First != Src->begin() || !ReadFromHead)
7947a6dacacSDimitry Andric return;
7957a6dacacSDimitry Andric
7967a6dacacSDimitry Andric // Is there actually anything to transfer?
7970fca6ea1SDimitry Andric if (!First->hasDbgRecords())
7987a6dacacSDimitry Andric return;
7997a6dacacSDimitry Andric
8000fca6ea1SDimitry Andric createMarker(Dest)->absorbDebugValues(*First->DebugMarker, InsertAtHead);
8015f757f3fSDimitry Andric
8025f757f3fSDimitry Andric return;
8035f757f3fSDimitry Andric }
8045f757f3fSDimitry Andric
spliceDebugInfo(BasicBlock::iterator Dest,BasicBlock * Src,BasicBlock::iterator First,BasicBlock::iterator Last)8055f757f3fSDimitry Andric void BasicBlock::spliceDebugInfo(BasicBlock::iterator Dest, BasicBlock *Src,
8065f757f3fSDimitry Andric BasicBlock::iterator First,
8075f757f3fSDimitry Andric BasicBlock::iterator Last) {
8085f757f3fSDimitry Andric /* Do a quick normalisation before calling the real splice implementation. We
8095f757f3fSDimitry Andric might be operating on a degenerate basic block that has no instructions
8105f757f3fSDimitry Andric in it, a legitimate transient state. In that case, Dest will be end() and
8110fca6ea1SDimitry Andric any DbgRecords temporarily stored in the TrailingDbgRecords map in
8120fca6ea1SDimitry Andric LLVMContext. We might illustrate it thus:
8135f757f3fSDimitry Andric
8145f757f3fSDimitry Andric Dest
8155f757f3fSDimitry Andric |
8165f757f3fSDimitry Andric this-block: ~~~~~~~~
8175f757f3fSDimitry Andric Src-block: ++++B---B---B---B:::C
8185f757f3fSDimitry Andric | |
8195f757f3fSDimitry Andric First Last
8205f757f3fSDimitry Andric
8210fca6ea1SDimitry Andric However: does the caller expect the "~" DbgRecords to end up before or
8220fca6ea1SDimitry Andric after the spliced segment? This is communciated in the "Head" bit of Dest,
8230fca6ea1SDimitry Andric which signals whether the caller called begin() or end() on this block.
8245f757f3fSDimitry Andric
8250fca6ea1SDimitry Andric If the head bit is set, then all is well, we leave DbgRecords trailing just
8265f757f3fSDimitry Andric like how dbg.value instructions would trail after instructions spliced to
8275f757f3fSDimitry Andric the beginning of this block.
8285f757f3fSDimitry Andric
8290fca6ea1SDimitry Andric If the head bit isn't set, then try to jam the "~" DbgRecords onto the
8300fca6ea1SDimitry Andric front of the First instruction, then splice like normal, which joins the
8310fca6ea1SDimitry Andric "~" DbgRecords with the "+" DbgRecords. However if the "+" DbgRecords are
8320fca6ea1SDimitry Andric supposed to be left behind in Src, then:
8330fca6ea1SDimitry Andric * detach the "+" DbgRecords,
8340fca6ea1SDimitry Andric * move the "~" DbgRecords onto First,
8355f757f3fSDimitry Andric * splice like normal,
8360fca6ea1SDimitry Andric * replace the "+" DbgRecords onto the Last position.
8375f757f3fSDimitry Andric Complicated, but gets the job done. */
8385f757f3fSDimitry Andric
8390fca6ea1SDimitry Andric // If we're inserting at end(), and not in front of dangling DbgRecords, then
8400fca6ea1SDimitry Andric // move the DbgRecords onto "First". They'll then be moved naturally in the
8415f757f3fSDimitry Andric // splice process.
8420fca6ea1SDimitry Andric DbgMarker *MoreDanglingDbgRecords = nullptr;
8430fca6ea1SDimitry Andric DbgMarker *OurTrailingDbgRecords = getTrailingDbgRecords();
8440fca6ea1SDimitry Andric if (Dest == end() && !Dest.getHeadBit() && OurTrailingDbgRecords) {
8450fca6ea1SDimitry Andric // Are the "+" DbgRecords not supposed to move? If so, detach them
8465f757f3fSDimitry Andric // temporarily.
8470fca6ea1SDimitry Andric if (!First.getHeadBit() && First->hasDbgRecords()) {
8480fca6ea1SDimitry Andric MoreDanglingDbgRecords = Src->getMarker(First);
8490fca6ea1SDimitry Andric MoreDanglingDbgRecords->removeFromParent();
8505f757f3fSDimitry Andric }
8515f757f3fSDimitry Andric
8520fca6ea1SDimitry Andric if (First->hasDbgRecords()) {
8535f757f3fSDimitry Andric // Place them at the front, it would look like this:
8545f757f3fSDimitry Andric // Dest
8555f757f3fSDimitry Andric // |
8565f757f3fSDimitry Andric // this-block:
8575f757f3fSDimitry Andric // Src-block: ~~~~~~~~++++B---B---B---B:::C
8585f757f3fSDimitry Andric // | |
8595f757f3fSDimitry Andric // First Last
8600fca6ea1SDimitry Andric First->adoptDbgRecords(this, end(), true);
8615f757f3fSDimitry Andric } else {
8625f757f3fSDimitry Andric // No current marker, create one and absorb in. (FIXME: we can avoid an
8635f757f3fSDimitry Andric // allocation in the future).
8640fca6ea1SDimitry Andric DbgMarker *CurMarker = Src->createMarker(&*First);
8650fca6ea1SDimitry Andric CurMarker->absorbDebugValues(*OurTrailingDbgRecords, false);
8660fca6ea1SDimitry Andric OurTrailingDbgRecords->eraseFromParent();
8675f757f3fSDimitry Andric }
8680fca6ea1SDimitry Andric deleteTrailingDbgRecords();
8695f757f3fSDimitry Andric First.setHeadBit(true);
8705f757f3fSDimitry Andric }
8715f757f3fSDimitry Andric
8725f757f3fSDimitry Andric // Call the main debug-info-splicing implementation.
8735f757f3fSDimitry Andric spliceDebugInfoImpl(Dest, Src, First, Last);
8745f757f3fSDimitry Andric
8750fca6ea1SDimitry Andric // Do we have some "+" DbgRecords hanging around that weren't supposed to
8760fca6ea1SDimitry Andric // move, and we detached to make things easier?
8770fca6ea1SDimitry Andric if (!MoreDanglingDbgRecords)
8785f757f3fSDimitry Andric return;
8795f757f3fSDimitry Andric
8800fca6ea1SDimitry Andric // FIXME: we could avoid an allocation here sometimes. (adoptDbgRecords
8810fca6ea1SDimitry Andric // requires an iterator).
8820fca6ea1SDimitry Andric DbgMarker *LastMarker = Src->createMarker(Last);
8830fca6ea1SDimitry Andric LastMarker->absorbDebugValues(*MoreDanglingDbgRecords, true);
8840fca6ea1SDimitry Andric MoreDanglingDbgRecords->eraseFromParent();
8855f757f3fSDimitry Andric }
8865f757f3fSDimitry Andric
spliceDebugInfoImpl(BasicBlock::iterator Dest,BasicBlock * Src,BasicBlock::iterator First,BasicBlock::iterator Last)8875f757f3fSDimitry Andric void BasicBlock::spliceDebugInfoImpl(BasicBlock::iterator Dest, BasicBlock *Src,
8885f757f3fSDimitry Andric BasicBlock::iterator First,
8895f757f3fSDimitry Andric BasicBlock::iterator Last) {
8905f757f3fSDimitry Andric // Find out where to _place_ these dbg.values; if InsertAtHead is specified,
8915f757f3fSDimitry Andric // this will be at the start of Dest's debug value range, otherwise this is
8925f757f3fSDimitry Andric // just Dest's marker.
8935f757f3fSDimitry Andric bool InsertAtHead = Dest.getHeadBit();
8945f757f3fSDimitry Andric bool ReadFromHead = First.getHeadBit();
8955f757f3fSDimitry Andric // Use this flag to signal the abnormal case, where we don't want to copy the
8960fca6ea1SDimitry Andric // DbgRecords ahead of the "Last" position.
8975f757f3fSDimitry Andric bool ReadFromTail = !Last.getTailBit();
8985f757f3fSDimitry Andric bool LastIsEnd = (Last == Src->end());
8995f757f3fSDimitry Andric
9005f757f3fSDimitry Andric /*
9015f757f3fSDimitry Andric Here's an illustration of what we're about to do. We have two blocks, this
9025f757f3fSDimitry Andric and Src, and two segments of list. Each instruction is marked by a capital
9030fca6ea1SDimitry Andric while potential DbgRecord debug-info is marked out by "-" characters and a
9040fca6ea1SDimitry Andric few other special characters (+:=) where I want to highlight what's going
9050fca6ea1SDimitry Andric on.
9065f757f3fSDimitry Andric
9075f757f3fSDimitry Andric Dest
9085f757f3fSDimitry Andric |
9095f757f3fSDimitry Andric this-block: A----A----A ====A----A----A----A---A---A
9105f757f3fSDimitry Andric Src-block ++++B---B---B---B:::C
9115f757f3fSDimitry Andric | |
9125f757f3fSDimitry Andric First Last
9135f757f3fSDimitry Andric
9145f757f3fSDimitry Andric The splice method is going to take all the instructions from First up to
9155f757f3fSDimitry Andric (but not including) Last and insert them in _front_ of Dest, forming one
9160fca6ea1SDimitry Andric long list. All the DbgRecords attached to instructions _between_ First and
9175f757f3fSDimitry Andric Last need no maintenence. However, we have to do special things with the
9180fca6ea1SDimitry Andric DbgRecords marked with the +:= characters. We only have three positions:
9190fca6ea1SDimitry Andric should the "+" DbgRecords be transferred, and if so to where? Do we move the
9200fca6ea1SDimitry Andric ":" DbgRecords? Would they go in front of the "=" DbgRecords, or should the
9210fca6ea1SDimitry Andric "=" DbgRecords go before "+" DbgRecords?
9225f757f3fSDimitry Andric
9235f757f3fSDimitry Andric We're told which way it should be by the bits carried in the iterators. The
9245f757f3fSDimitry Andric "Head" bit indicates whether the specified position is supposed to be at the
9250fca6ea1SDimitry Andric front of the attached DbgRecords (true) or not (false). The Tail bit is true
9260fca6ea1SDimitry Andric on the other end of a range: is the range intended to include DbgRecords up
9270fca6ea1SDimitry Andric to the end (false) or not (true).
9285f757f3fSDimitry Andric
9295f757f3fSDimitry Andric FIXME: the tail bit doesn't need to be distinct from the head bit, we could
9305f757f3fSDimitry Andric combine them.
9315f757f3fSDimitry Andric
9325f757f3fSDimitry Andric Here are some examples of different configurations:
9335f757f3fSDimitry Andric
9345f757f3fSDimitry Andric Dest.Head = true, First.Head = true, Last.Tail = false
9355f757f3fSDimitry Andric
9365f757f3fSDimitry Andric this-block: A----A----A++++B---B---B---B:::====A----A----A----A---A---A
9375f757f3fSDimitry Andric | |
9385f757f3fSDimitry Andric First Dest
9395f757f3fSDimitry Andric
9405f757f3fSDimitry Andric Wheras if we didn't want to read from the Src list,
9415f757f3fSDimitry Andric
9425f757f3fSDimitry Andric Dest.Head = true, First.Head = false, Last.Tail = false
9435f757f3fSDimitry Andric
9445f757f3fSDimitry Andric this-block: A----A----AB---B---B---B:::====A----A----A----A---A---A
9455f757f3fSDimitry Andric | |
9465f757f3fSDimitry Andric First Dest
9475f757f3fSDimitry Andric
9485f757f3fSDimitry Andric Or if we didn't want to insert at the head of Dest:
9495f757f3fSDimitry Andric
9505f757f3fSDimitry Andric Dest.Head = false, First.Head = false, Last.Tail = false
9515f757f3fSDimitry Andric
9525f757f3fSDimitry Andric this-block: A----A----A====B---B---B---B:::A----A----A----A---A---A
9535f757f3fSDimitry Andric | |
9545f757f3fSDimitry Andric First Dest
9555f757f3fSDimitry Andric
9565f757f3fSDimitry Andric Tests for these various configurations can be found in the unit test file
9575f757f3fSDimitry Andric BasicBlockDbgInfoTest.cpp.
9585f757f3fSDimitry Andric
9595f757f3fSDimitry Andric */
9605f757f3fSDimitry Andric
9610fca6ea1SDimitry Andric // Detach the marker at Dest -- this lets us move the "====" DbgRecords
9620fca6ea1SDimitry Andric // around.
9630fca6ea1SDimitry Andric DbgMarker *DestMarker = nullptr;
9646c4b055cSDimitry Andric if ((DestMarker = getMarker(Dest))) {
9656c4b055cSDimitry Andric if (Dest == end()) {
9666c4b055cSDimitry Andric assert(DestMarker == getTrailingDbgRecords());
9676c4b055cSDimitry Andric deleteTrailingDbgRecords();
9686c4b055cSDimitry Andric } else {
9695f757f3fSDimitry Andric DestMarker->removeFromParent();
9705f757f3fSDimitry Andric }
9716c4b055cSDimitry Andric }
9725f757f3fSDimitry Andric
9730fca6ea1SDimitry Andric // If we're moving the tail range of DbgRecords (":::"), absorb them into the
9740fca6ea1SDimitry Andric // front of the DbgRecords at Dest.
9755f757f3fSDimitry Andric if (ReadFromTail && Src->getMarker(Last)) {
9760fca6ea1SDimitry Andric DbgMarker *FromLast = Src->getMarker(Last);
9775f757f3fSDimitry Andric if (LastIsEnd) {
978*c80e69b0SDimitry Andric if (Dest == end()) {
979*c80e69b0SDimitry Andric // Abosrb the trailing markers from Src.
980*c80e69b0SDimitry Andric assert(FromLast == Src->getTrailingDbgRecords());
981*c80e69b0SDimitry Andric createMarker(Dest)->absorbDebugValues(*FromLast, true);
982*c80e69b0SDimitry Andric FromLast->eraseFromParent();
983*c80e69b0SDimitry Andric Src->deleteTrailingDbgRecords();
984*c80e69b0SDimitry Andric } else {
9850fca6ea1SDimitry Andric // adoptDbgRecords will release any trailers.
986*c80e69b0SDimitry Andric Dest->adoptDbgRecords(Src, Last, true);
987*c80e69b0SDimitry Andric }
9880fca6ea1SDimitry Andric assert(!Src->getTrailingDbgRecords());
9890fca6ea1SDimitry Andric } else {
9900fca6ea1SDimitry Andric // FIXME: can we use adoptDbgRecords here to reduce allocations?
9910fca6ea1SDimitry Andric DbgMarker *OntoDest = createMarker(Dest);
9920fca6ea1SDimitry Andric OntoDest->absorbDebugValues(*FromLast, true);
9935f757f3fSDimitry Andric }
9945f757f3fSDimitry Andric }
9955f757f3fSDimitry Andric
9960fca6ea1SDimitry Andric // If we're _not_ reading from the head of First, i.e. the "++++" DbgRecords,
9975f757f3fSDimitry Andric // move their markers onto Last. They remain in the Src block. No action
9985f757f3fSDimitry Andric // needed.
9990fca6ea1SDimitry Andric if (!ReadFromHead && First->hasDbgRecords()) {
10000fca6ea1SDimitry Andric if (Last != Src->end()) {
10010fca6ea1SDimitry Andric Last->adoptDbgRecords(Src, First, true);
10020fca6ea1SDimitry Andric } else {
10030fca6ea1SDimitry Andric DbgMarker *OntoLast = Src->createMarker(Last);
10040fca6ea1SDimitry Andric DbgMarker *FromFirst = Src->createMarker(First);
10050fca6ea1SDimitry Andric // Always insert at front of Last.
10060fca6ea1SDimitry Andric OntoLast->absorbDebugValues(*FromFirst, true);
10070fca6ea1SDimitry Andric }
10085f757f3fSDimitry Andric }
10095f757f3fSDimitry Andric
10100fca6ea1SDimitry Andric // Finally, do something with the "====" DbgRecords we detached.
10115f757f3fSDimitry Andric if (DestMarker) {
10125f757f3fSDimitry Andric if (InsertAtHead) {
10130fca6ea1SDimitry Andric // Insert them at the end of the DbgRecords at Dest. The "::::" DbgRecords
10145f757f3fSDimitry Andric // might be in front of them.
10150fca6ea1SDimitry Andric DbgMarker *NewDestMarker = createMarker(Dest);
10165f757f3fSDimitry Andric NewDestMarker->absorbDebugValues(*DestMarker, false);
10175f757f3fSDimitry Andric } else {
10185f757f3fSDimitry Andric // Insert them right at the start of the range we moved, ahead of First
10190fca6ea1SDimitry Andric // and the "++++" DbgRecords.
10206c4b055cSDimitry Andric // This also covers the rare circumstance where we insert at end(), and we
10216c4b055cSDimitry Andric // did not generate the iterator with begin() / getFirstInsertionPt(),
10226c4b055cSDimitry Andric // meaning any trailing debug-info at the end of the block would
10236c4b055cSDimitry Andric // "normally" have been pushed in front of "First". We move it there now.
10240fca6ea1SDimitry Andric DbgMarker *FirstMarker = createMarker(First);
10255f757f3fSDimitry Andric FirstMarker->absorbDebugValues(*DestMarker, true);
10265f757f3fSDimitry Andric }
10275f757f3fSDimitry Andric DestMarker->eraseFromParent();
10285f757f3fSDimitry Andric }
10295f757f3fSDimitry Andric }
10305f757f3fSDimitry Andric
splice(iterator Dest,BasicBlock * Src,iterator First,iterator Last)10315f757f3fSDimitry Andric void BasicBlock::splice(iterator Dest, BasicBlock *Src, iterator First,
10325f757f3fSDimitry Andric iterator Last) {
10335f757f3fSDimitry Andric assert(Src->IsNewDbgInfoFormat == IsNewDbgInfoFormat);
10345f757f3fSDimitry Andric
10355f757f3fSDimitry Andric #ifdef EXPENSIVE_CHECKS
10365f757f3fSDimitry Andric // Check that First is before Last.
10375f757f3fSDimitry Andric auto FromBBEnd = Src->end();
10385f757f3fSDimitry Andric for (auto It = First; It != Last; ++It)
10395f757f3fSDimitry Andric assert(It != FromBBEnd && "FromBeginIt not before FromEndIt!");
10405f757f3fSDimitry Andric #endif // EXPENSIVE_CHECKS
10415f757f3fSDimitry Andric
10425f757f3fSDimitry Andric // Lots of horrible special casing for empty transfers: the dbg.values between
10435f757f3fSDimitry Andric // two positions could be spliced in dbg.value mode.
10445f757f3fSDimitry Andric if (First == Last) {
10455f757f3fSDimitry Andric spliceDebugInfoEmptyBlock(Dest, Src, First, Last);
10465f757f3fSDimitry Andric return;
10475f757f3fSDimitry Andric }
10485f757f3fSDimitry Andric
10495f757f3fSDimitry Andric // Handle non-instr debug-info specific juggling.
10505f757f3fSDimitry Andric if (IsNewDbgInfoFormat)
10515f757f3fSDimitry Andric spliceDebugInfo(Dest, Src, First, Last);
10525f757f3fSDimitry Andric
10535f757f3fSDimitry Andric // And move the instructions.
10545f757f3fSDimitry Andric getInstList().splice(Dest, Src->getInstList(), First, Last);
10555f757f3fSDimitry Andric
10560fca6ea1SDimitry Andric flushTerminatorDbgRecords();
10575f757f3fSDimitry Andric }
10585f757f3fSDimitry Andric
insertDbgRecordAfter(DbgRecord * DR,Instruction * I)10590fca6ea1SDimitry Andric void BasicBlock::insertDbgRecordAfter(DbgRecord *DR, Instruction *I) {
10605f757f3fSDimitry Andric assert(IsNewDbgInfoFormat);
10615f757f3fSDimitry Andric assert(I->getParent() == this);
10625f757f3fSDimitry Andric
10635f757f3fSDimitry Andric iterator NextIt = std::next(I->getIterator());
10640fca6ea1SDimitry Andric DbgMarker *NextMarker = createMarker(NextIt);
10650fca6ea1SDimitry Andric NextMarker->insertDbgRecord(DR, true);
10665f757f3fSDimitry Andric }
10675f757f3fSDimitry Andric
insertDbgRecordBefore(DbgRecord * DR,InstListType::iterator Where)10680fca6ea1SDimitry Andric void BasicBlock::insertDbgRecordBefore(DbgRecord *DR,
10695f757f3fSDimitry Andric InstListType::iterator Where) {
10700fca6ea1SDimitry Andric assert(Where == end() || Where->getParent() == this);
10715f757f3fSDimitry Andric bool InsertAtHead = Where.getHeadBit();
10720fca6ea1SDimitry Andric DbgMarker *M = createMarker(Where);
10730fca6ea1SDimitry Andric M->insertDbgRecord(DR, InsertAtHead);
10745f757f3fSDimitry Andric }
10755f757f3fSDimitry Andric
getNextMarker(Instruction * I)10760fca6ea1SDimitry Andric DbgMarker *BasicBlock::getNextMarker(Instruction *I) {
10775f757f3fSDimitry Andric return getMarker(std::next(I->getIterator()));
10785f757f3fSDimitry Andric }
10795f757f3fSDimitry Andric
getMarker(InstListType::iterator It)10800fca6ea1SDimitry Andric DbgMarker *BasicBlock::getMarker(InstListType::iterator It) {
10815f757f3fSDimitry Andric if (It == end()) {
10820fca6ea1SDimitry Andric DbgMarker *DM = getTrailingDbgRecords();
10830fca6ea1SDimitry Andric return DM;
10845f757f3fSDimitry Andric }
10850fca6ea1SDimitry Andric return It->DebugMarker;
10865f757f3fSDimitry Andric }
10875f757f3fSDimitry Andric
reinsertInstInDbgRecords(Instruction * I,std::optional<DbgRecord::self_iterator> Pos)10880fca6ea1SDimitry Andric void BasicBlock::reinsertInstInDbgRecords(
10890fca6ea1SDimitry Andric Instruction *I, std::optional<DbgRecord::self_iterator> Pos) {
10905f757f3fSDimitry Andric // "I" was originally removed from a position where it was
10910fca6ea1SDimitry Andric // immediately in front of Pos. Any DbgRecords on that position then "fell
10920fca6ea1SDimitry Andric // down" onto Pos. "I" has been re-inserted at the front of that wedge of
10930fca6ea1SDimitry Andric // DbgRecords, shuffle them around to represent the original positioning. To
10940fca6ea1SDimitry Andric // illustrate:
10955f757f3fSDimitry Andric //
10965f757f3fSDimitry Andric // Instructions: I1---I---I0
10970fca6ea1SDimitry Andric // DbgRecords: DDD DDD
10985f757f3fSDimitry Andric //
10995f757f3fSDimitry Andric // Instruction "I" removed,
11005f757f3fSDimitry Andric //
11015f757f3fSDimitry Andric // Instructions: I1------I0
11020fca6ea1SDimitry Andric // DbgRecords: DDDDDD
11035f757f3fSDimitry Andric // ^Pos
11045f757f3fSDimitry Andric //
11055f757f3fSDimitry Andric // Instruction "I" re-inserted (now):
11065f757f3fSDimitry Andric //
11075f757f3fSDimitry Andric // Instructions: I1---I------I0
11080fca6ea1SDimitry Andric // DbgRecords: DDDDDD
11095f757f3fSDimitry Andric // ^Pos
11105f757f3fSDimitry Andric //
11115f757f3fSDimitry Andric // After this method completes:
11125f757f3fSDimitry Andric //
11135f757f3fSDimitry Andric // Instructions: I1---I---I0
11140fca6ea1SDimitry Andric // DbgRecords: DDD DDD
11155f757f3fSDimitry Andric
11160fca6ea1SDimitry Andric // This happens if there were no DbgRecords on I0. Are there now DbgRecords
11170fca6ea1SDimitry Andric // there?
11185f757f3fSDimitry Andric if (!Pos) {
11190fca6ea1SDimitry Andric DbgMarker *NextMarker = getNextMarker(I);
11205f757f3fSDimitry Andric if (!NextMarker)
11215f757f3fSDimitry Andric return;
11220fca6ea1SDimitry Andric if (NextMarker->StoredDbgRecords.empty())
11235f757f3fSDimitry Andric return;
11240fca6ea1SDimitry Andric // There are DbgMarkers there now -- they fell down from "I".
11250fca6ea1SDimitry Andric DbgMarker *ThisMarker = createMarker(I);
11265f757f3fSDimitry Andric ThisMarker->absorbDebugValues(*NextMarker, false);
11275f757f3fSDimitry Andric return;
11285f757f3fSDimitry Andric }
11295f757f3fSDimitry Andric
11300fca6ea1SDimitry Andric // Is there even a range of DbgRecords to move?
11310fca6ea1SDimitry Andric DbgMarker *DM = (*Pos)->getMarker();
11320fca6ea1SDimitry Andric auto Range = make_range(DM->StoredDbgRecords.begin(), (*Pos));
11335f757f3fSDimitry Andric if (Range.begin() == Range.end())
11345f757f3fSDimitry Andric return;
11355f757f3fSDimitry Andric
11365f757f3fSDimitry Andric // Otherwise: splice.
11370fca6ea1SDimitry Andric DbgMarker *ThisMarker = createMarker(I);
11380fca6ea1SDimitry Andric assert(ThisMarker->StoredDbgRecords.empty());
11390fca6ea1SDimitry Andric ThisMarker->absorbDebugValues(Range, *DM, true);
11405f757f3fSDimitry Andric }
11415f757f3fSDimitry Andric
11425ffd83dbSDimitry Andric #ifndef NDEBUG
11435ffd83dbSDimitry Andric /// In asserts builds, this checks the numbering. In non-asserts builds, it
11445ffd83dbSDimitry Andric /// is defined as a no-op inline function in BasicBlock.h.
validateInstrOrdering() const11455ffd83dbSDimitry Andric void BasicBlock::validateInstrOrdering() const {
11465ffd83dbSDimitry Andric if (!isInstrOrderValid())
11475ffd83dbSDimitry Andric return;
11485ffd83dbSDimitry Andric const Instruction *Prev = nullptr;
11495ffd83dbSDimitry Andric for (const Instruction &I : *this) {
11505ffd83dbSDimitry Andric assert((!Prev || Prev->comesBefore(&I)) &&
11515ffd83dbSDimitry Andric "cached instruction ordering is incorrect");
11525ffd83dbSDimitry Andric Prev = &I;
11535ffd83dbSDimitry Andric }
11545ffd83dbSDimitry Andric }
11555ffd83dbSDimitry Andric #endif
11565f757f3fSDimitry Andric
setTrailingDbgRecords(DbgMarker * foo)11570fca6ea1SDimitry Andric void BasicBlock::setTrailingDbgRecords(DbgMarker *foo) {
11580fca6ea1SDimitry Andric getContext().pImpl->setTrailingDbgRecords(this, foo);
11595f757f3fSDimitry Andric }
11605f757f3fSDimitry Andric
getTrailingDbgRecords()11610fca6ea1SDimitry Andric DbgMarker *BasicBlock::getTrailingDbgRecords() {
11620fca6ea1SDimitry Andric return getContext().pImpl->getTrailingDbgRecords(this);
11635f757f3fSDimitry Andric }
11645f757f3fSDimitry Andric
deleteTrailingDbgRecords()11650fca6ea1SDimitry Andric void BasicBlock::deleteTrailingDbgRecords() {
11660fca6ea1SDimitry Andric getContext().pImpl->deleteTrailingDbgRecords(this);
11675f757f3fSDimitry Andric }
1168