diff options
Diffstat (limited to 'llvm/lib/CodeGen/MachineBasicBlock.cpp')
-rw-r--r-- | llvm/lib/CodeGen/MachineBasicBlock.cpp | 247 |
1 files changed, 111 insertions, 136 deletions
diff --git a/llvm/lib/CodeGen/MachineBasicBlock.cpp b/llvm/lib/CodeGen/MachineBasicBlock.cpp index f433c4b6c90b..2d4b60435d96 100644 --- a/llvm/lib/CodeGen/MachineBasicBlock.cpp +++ b/llvm/lib/CodeGen/MachineBasicBlock.cpp @@ -61,12 +61,42 @@ MCSymbol *MachineBasicBlock::getSymbol() const { const MachineFunction *MF = getParent(); MCContext &Ctx = MF->getContext(); auto Prefix = Ctx.getAsmInfo()->getPrivateLabelPrefix(); + assert(getNumber() >= 0 && "cannot get label for unreachable MBB"); - CachedMCSymbol = Ctx.getOrCreateSymbol(Twine(Prefix) + "BB" + - Twine(MF->getFunctionNumber()) + - "_" + Twine(getNumber())); - } + // We emit a non-temporary symbol for every basic block if we have BBLabels + // or -- with basic block sections -- when a basic block begins a section. + // With basic block symbols, we use a unary encoding which can + // compress the symbol names significantly. For basic block sections where + // this block is the first in a cluster, we use a non-temp descriptive name. + // Otherwise we fall back to use temp label. + if (MF->hasBBLabels()) { + auto Iter = MF->getBBSectionsSymbolPrefix().begin(); + if (getNumber() < 0 || + getNumber() >= (int)MF->getBBSectionsSymbolPrefix().size()) + report_fatal_error("Unreachable MBB: " + Twine(getNumber())); + // The basic blocks for function foo are named a.BB.foo, aa.BB.foo, and + // so on. + std::string Prefix(Iter + 1, Iter + getNumber() + 1); + std::reverse(Prefix.begin(), Prefix.end()); + CachedMCSymbol = + Ctx.getOrCreateSymbol(Twine(Prefix) + ".BB." + Twine(MF->getName())); + } else if (MF->hasBBSections() && isBeginSection()) { + SmallString<5> Suffix; + if (SectionID == MBBSectionID::ColdSectionID) { + Suffix += ".cold"; + } else if (SectionID == MBBSectionID::ExceptionSectionID) { + Suffix += ".eh"; + } else { + Suffix += "." + std::to_string(SectionID.Number); + } + CachedMCSymbol = Ctx.getOrCreateSymbol(MF->getName() + Suffix); + } else { + CachedMCSymbol = Ctx.getOrCreateSymbol(Twine(Prefix) + "BB" + + Twine(MF->getFunctionNumber()) + + "_" + Twine(getNumber())); + } + } return CachedMCSymbol; } @@ -247,8 +277,16 @@ LLVM_DUMP_METHOD void MachineBasicBlock::dump() const { } #endif +bool MachineBasicBlock::mayHaveInlineAsmBr() const { + for (const MachineBasicBlock *Succ : successors()) { + if (Succ->isInlineAsmBrIndirectTarget()) + return true; + } + return false; +} + bool MachineBasicBlock::isLegalToHoistInto() const { - if (isReturnBlock() || hasEHPadSuccessor()) + if (isReturnBlock() || hasEHPadSuccessor() || mayHaveInlineAsmBr()) return false; return true; } @@ -326,7 +364,7 @@ void MachineBasicBlock::print(raw_ostream &OS, ModuleSlotTracker &MST, OS << "landing-pad"; HasAttributes = true; } - if (getAlignment() != Align::None()) { + if (getAlignment() != Align(1)) { OS << (HasAttributes ? ", " : " ("); OS << "align " << Log2(getAlignment()); HasAttributes = true; @@ -479,7 +517,7 @@ void MachineBasicBlock::sortUniqueLiveIns() { LiveInVector::const_iterator J; LiveInVector::iterator Out = LiveIns.begin(); for (; I != LiveIns.end(); ++Out, I = J) { - unsigned PhysReg = I->PhysReg; + MCRegister PhysReg = I->PhysReg; LaneBitmask LaneMask = I->LaneMask; for (J = std::next(I); J != LiveIns.end() && J->PhysReg == PhysReg; ++J) LaneMask |= J->LaneMask; @@ -489,7 +527,7 @@ void MachineBasicBlock::sortUniqueLiveIns() { LiveIns.erase(Out, LiveIns.end()); } -unsigned +Register MachineBasicBlock::addLiveIn(MCRegister PhysReg, const TargetRegisterClass *RC) { assert(getParent() && "MBB must be inserted in function"); assert(PhysReg.isPhysical() && "Expected physreg"); @@ -529,7 +567,11 @@ void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) { getParent()->splice(++NewBefore->getIterator(), getIterator()); } -void MachineBasicBlock::updateTerminator() { +void MachineBasicBlock::updateTerminator( + MachineBasicBlock *PreviousLayoutSuccessor) { + LLVM_DEBUG(dbgs() << "Updating terminators on " << printMBBReference(*this) + << "\n"); + const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo(); // A block with no successors has no concerns with fall-through edges. if (this->succ_empty()) @@ -548,25 +590,21 @@ void MachineBasicBlock::updateTerminator() { if (isLayoutSuccessor(TBB)) TII->removeBranch(*this); } else { - // The block has an unconditional fallthrough. If its successor is not its - // layout successor, insert a branch. First we have to locate the only - // non-landing-pad successor, as that is the fallthrough block. - for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) { - if ((*SI)->isEHPad()) - continue; - assert(!TBB && "Found more than one non-landing-pad successor!"); - TBB = *SI; - } - - // If there is no non-landing-pad successor, the block has no fall-through - // edges to be concerned with. - if (!TBB) + // The block has an unconditional fallthrough, or the end of the block is + // unreachable. + + // Unfortunately, whether the end of the block is unreachable is not + // immediately obvious; we must fall back to checking the successor list, + // and assuming that if the passed in block is in the succesor list and + // not an EHPad, it must be the intended target. + if (!PreviousLayoutSuccessor || !isSuccessor(PreviousLayoutSuccessor) || + PreviousLayoutSuccessor->isEHPad()) return; - // Finally update the unconditional successor to be reached via a branch - // if it would not be reached by fallthrough. - if (!isLayoutSuccessor(TBB)) - TII->insertBranch(*this, TBB, nullptr, Cond, DL); + // If the unconditional successor block is not the current layout + // successor, insert a branch to jump to it. + if (!isLayoutSuccessor(PreviousLayoutSuccessor)) + TII->insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL); } return; } @@ -587,38 +625,20 @@ void MachineBasicBlock::updateTerminator() { return; } - // Walk through the successors and find the successor which is not a landing - // pad and is not the conditional branch destination (in TBB) as the - // fallthrough successor. - MachineBasicBlock *FallthroughBB = nullptr; - for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) { - if ((*SI)->isEHPad() || *SI == TBB) - continue; - assert(!FallthroughBB && "Found more than one fallthrough successor."); - FallthroughBB = *SI; - } - - if (!FallthroughBB) { - if (canFallThrough()) { - // We fallthrough to the same basic block as the conditional jump targets. - // Remove the conditional jump, leaving unconditional fallthrough. - // FIXME: This does not seem like a reasonable pattern to support, but it - // has been seen in the wild coming out of degenerate ARM test cases. - TII->removeBranch(*this); - - // Finally update the unconditional successor to be reached via a branch if - // it would not be reached by fallthrough. - if (!isLayoutSuccessor(TBB)) - TII->insertBranch(*this, TBB, nullptr, Cond, DL); - return; - } + // We now know we're going to fallthrough to PreviousLayoutSuccessor. + assert(PreviousLayoutSuccessor); + assert(!PreviousLayoutSuccessor->isEHPad()); + assert(isSuccessor(PreviousLayoutSuccessor)); - // We enter here iff exactly one successor is TBB which cannot fallthrough - // and the rest successors if any are EHPads. In this case, we need to - // change the conditional branch into unconditional branch. + if (PreviousLayoutSuccessor == TBB) { + // We had a fallthrough to the same basic block as the conditional jump + // targets. Remove the conditional jump, leaving an unconditional + // fallthrough or an unconditional jump. TII->removeBranch(*this); - Cond.clear(); - TII->insertBranch(*this, TBB, nullptr, Cond, DL); + if (!isLayoutSuccessor(TBB)) { + Cond.clear(); + TII->insertBranch(*this, TBB, nullptr, Cond, DL); + } return; } @@ -627,14 +647,14 @@ void MachineBasicBlock::updateTerminator() { if (TII->reverseBranchCondition(Cond)) { // We can't reverse the condition, add an unconditional branch. Cond.clear(); - TII->insertBranch(*this, FallthroughBB, nullptr, Cond, DL); + TII->insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL); return; } TII->removeBranch(*this); - TII->insertBranch(*this, FallthroughBB, nullptr, Cond, DL); - } else if (!isLayoutSuccessor(FallthroughBB)) { + TII->insertBranch(*this, PreviousLayoutSuccessor, nullptr, Cond, DL); + } else if (!isLayoutSuccessor(PreviousLayoutSuccessor)) { TII->removeBranch(*this); - TII->insertBranch(*this, TBB, FallthroughBB, Cond, DL); + TII->insertBranch(*this, TBB, PreviousLayoutSuccessor, Cond, DL); } } @@ -871,12 +891,14 @@ bool MachineBasicBlock::canFallThrough() { return getFallThrough() != nullptr; } -MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, - Pass &P) { +MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge( + MachineBasicBlock *Succ, Pass &P, + std::vector<SparseBitVector<>> *LiveInSets) { if (!canSplitCriticalEdge(Succ)) return nullptr; MachineFunction *MF = getParent(); + MachineBasicBlock *PrevFallthrough = getNextNode(); DebugLoc DL; // FIXME: this is nowhere MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock(); @@ -898,7 +920,7 @@ MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, LiveVariables *LV = P.getAnalysisIfAvailable<LiveVariables>(); // Collect a list of virtual registers killed by the terminators. - SmallVector<unsigned, 4> KilledRegs; + SmallVector<Register, 4> KilledRegs; if (LV) for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); I != E; ++I) { @@ -918,7 +940,7 @@ MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, } } - SmallVector<unsigned, 4> UsedRegs; + SmallVector<Register, 4> UsedRegs; if (LIS) { for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); I != E; ++I) { @@ -947,7 +969,11 @@ MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, Terminators.push_back(&*I); } - updateTerminator(); + // Since we replaced all uses of Succ with NMBB, that should also be treated + // as the fallthrough successor + if (Succ == PrevFallthrough) + PrevFallthrough = NMBB; + updateTerminator(PrevFallthrough); if (Indexes) { SmallVector<MachineInstr*, 4> NewTerminators; @@ -992,7 +1018,7 @@ MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, if (LV) { // Restore kills of virtual registers that were killed by the terminators. while (!KilledRegs.empty()) { - unsigned Reg = KilledRegs.pop_back_val(); + Register Reg = KilledRegs.pop_back_val(); for (instr_iterator I = instr_end(), E = instr_begin(); I != E;) { if (!(--I)->addRegisterKilled(Reg, TRI, /* AddIfNotFound= */ false)) continue; @@ -1003,7 +1029,10 @@ MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, } } // Update relevant live-through information. - LV->addNewBlock(NMBB, this, Succ); + if (LiveInSets != nullptr) + LV->addNewBlock(NMBB, this, Succ, *LiveInSets); + else + LV->addNewBlock(NMBB, this, Succ); } if (LIS) { @@ -1022,7 +1051,7 @@ MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, SlotIndex EndIndex = Indexes->getMBBEndIdx(NMBB); // Find the registers used from NMBB in PHIs in Succ. - SmallSet<unsigned, 8> PHISrcRegs; + SmallSet<Register, 8> PHISrcRegs; for (MachineBasicBlock::instr_iterator I = Succ->instr_begin(), E = Succ->instr_end(); I != E && I->isPHI(); ++I) { @@ -1045,7 +1074,7 @@ MachineBasicBlock *MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, MachineRegisterInfo *MRI = &getParent()->getRegInfo(); for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { - unsigned Reg = Register::index2VirtReg(i); + Register Reg = Register::index2VirtReg(i); if (PHISrcRegs.count(Reg) || !LIS->hasInterval(Reg)) continue; @@ -1109,15 +1138,19 @@ bool MachineBasicBlock::canSplitCriticalEdge( if (Succ->isEHPad()) return false; - const MachineFunction *MF = getParent(); + // Splitting the critical edge to a callbr's indirect block isn't advised. + // Don't do it in this generic function. + if (Succ->isInlineAsmBrIndirectTarget()) + return false; + const MachineFunction *MF = getParent(); // Performance might be harmed on HW that implements branching using exec mask // where both sides of the branches are always executed. if (MF->getTarget().requiresStructuredCFG()) return false; // We may need to update this's terminator, but we can't do that if - // AnalyzeBranch fails. If this uses a jump table, we won't touch it. + // analyzeBranch fails. If this uses a jump table, we won't touch it. const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); MachineBasicBlock *TBB = nullptr, *FBB = nullptr; SmallVector<MachineOperand, 4> Cond; @@ -1223,68 +1256,6 @@ void MachineBasicBlock::replacePhiUsesWith(MachineBasicBlock *Old, } } -/// Various pieces of code can cause excess edges in the CFG to be inserted. If -/// we have proven that MBB can only branch to DestA and DestB, remove any other -/// MBB successors from the CFG. DestA and DestB can be null. -/// -/// Besides DestA and DestB, retain other edges leading to LandingPads -/// (currently there can be only one; we don't check or require that here). -/// Note it is possible that DestA and/or DestB are LandingPads. -bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA, - MachineBasicBlock *DestB, - bool IsCond) { - // The values of DestA and DestB frequently come from a call to the - // 'TargetInstrInfo::AnalyzeBranch' method. We take our meaning of the initial - // values from there. - // - // 1. If both DestA and DestB are null, then the block ends with no branches - // (it falls through to its successor). - // 2. If DestA is set, DestB is null, and IsCond is false, then the block ends - // with only an unconditional branch. - // 3. If DestA is set, DestB is null, and IsCond is true, then the block ends - // with a conditional branch that falls through to a successor (DestB). - // 4. If DestA and DestB is set and IsCond is true, then the block ends with a - // conditional branch followed by an unconditional branch. DestA is the - // 'true' destination and DestB is the 'false' destination. - - bool Changed = false; - - MachineBasicBlock *FallThru = getNextNode(); - - if (!DestA && !DestB) { - // Block falls through to successor. - DestA = FallThru; - DestB = FallThru; - } else if (DestA && !DestB) { - if (IsCond) - // Block ends in conditional jump that falls through to successor. - DestB = FallThru; - } else { - assert(DestA && DestB && IsCond && - "CFG in a bad state. Cannot correct CFG edges"); - } - - // Remove superfluous edges. I.e., those which aren't destinations of this - // basic block, duplicate edges, or landing pads. - SmallPtrSet<const MachineBasicBlock*, 8> SeenMBBs; - MachineBasicBlock::succ_iterator SI = succ_begin(); - while (SI != succ_end()) { - const MachineBasicBlock *MBB = *SI; - if (!SeenMBBs.insert(MBB).second || - (MBB != DestA && MBB != DestB && !MBB->isEHPad())) { - // This is a superfluous edge, remove it. - SI = removeSuccessor(SI); - Changed = true; - } else { - ++SI; - } - } - - if (Changed) - normalizeSuccProbs(); - return Changed; -} - /// Find the next valid DebugLoc starting at MBBI, skipping any DBG_VALUE /// instructions. Return UnknownLoc if there is none. DebugLoc @@ -1300,8 +1271,8 @@ MachineBasicBlock::findDebugLoc(instr_iterator MBBI) { /// instructions. Return UnknownLoc if there is none. DebugLoc MachineBasicBlock::findPrevDebugLoc(instr_iterator MBBI) { if (MBBI == instr_begin()) return {}; - // Skip debug declarations, we don't want a DebugLoc from them. - MBBI = skipDebugInstructionsBackward(std::prev(MBBI), instr_begin()); + // Skip debug instructions, we don't want a DebugLoc from them. + MBBI = prev_nodbg(MBBI, instr_begin()); if (!MBBI->isDebugInstr()) return MBBI->getDebugLoc(); return {}; } @@ -1383,7 +1354,7 @@ MachineBasicBlock::getProbabilityIterator(MachineBasicBlock::succ_iterator I) { /// instructions after (searching just for defs) MI. MachineBasicBlock::LivenessQueryResult MachineBasicBlock::computeRegisterLiveness(const TargetRegisterInfo *TRI, - unsigned Reg, const_iterator Before, + MCRegister Reg, const_iterator Before, unsigned Neighborhood) const { unsigned N = Neighborhood; @@ -1503,3 +1474,7 @@ MachineBasicBlock::livein_iterator MachineBasicBlock::livein_begin() const { "Liveness information is accurate"); return LiveIns.begin(); } + +const MBBSectionID MBBSectionID::ColdSectionID(MBBSectionID::SectionType::Cold); +const MBBSectionID + MBBSectionID::ExceptionSectionID(MBBSectionID::SectionType::Exception); |