10b57cec5SDimitry Andric //===- IndirectBrExpandPass.cpp - Expand indirectbr to switch -------------===// 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 /// \file 90b57cec5SDimitry Andric /// 100b57cec5SDimitry Andric /// Implements an expansion pass to turn `indirectbr` instructions in the IR 110b57cec5SDimitry Andric /// into `switch` instructions. This works by enumerating the basic blocks in 120b57cec5SDimitry Andric /// a dense range of integers, replacing each `blockaddr` constant with the 130b57cec5SDimitry Andric /// corresponding integer constant, and then building a switch that maps from 140b57cec5SDimitry Andric /// the integers to the actual blocks. All of the indirectbr instructions in the 150b57cec5SDimitry Andric /// function are redirected to this common switch. 160b57cec5SDimitry Andric /// 170b57cec5SDimitry Andric /// While this is generically useful if a target is unable to codegen 180b57cec5SDimitry Andric /// `indirectbr` natively, it is primarily useful when there is some desire to 190b57cec5SDimitry Andric /// get the builtin non-jump-table lowering of a switch even when the input 200b57cec5SDimitry Andric /// source contained an explicit indirect branch construct. 210b57cec5SDimitry Andric /// 220b57cec5SDimitry Andric /// Note that it doesn't make any sense to enable this pass unless a target also 230b57cec5SDimitry Andric /// disables jump-table lowering of switches. Doing that is likely to pessimize 240b57cec5SDimitry Andric /// the code. 250b57cec5SDimitry Andric /// 260b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 290b57cec5SDimitry Andric #include "llvm/ADT/Sequence.h" 300b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 31fe6060f1SDimitry Andric #include "llvm/Analysis/DomTreeUpdater.h" 325f757f3fSDimitry Andric #include "llvm/CodeGen/IndirectBrExpand.h" 330b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h" 340b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 350b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 3681ad6265SDimitry Andric #include "llvm/IR/Constants.h" 37fe6060f1SDimitry Andric #include "llvm/IR/Dominators.h" 380b57cec5SDimitry Andric #include "llvm/IR/Function.h" 390b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 40480093f4SDimitry Andric #include "llvm/InitializePasses.h" 410b57cec5SDimitry Andric #include "llvm/Pass.h" 420b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 430b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 44bdd1243dSDimitry Andric #include <optional> 450b57cec5SDimitry Andric 460b57cec5SDimitry Andric using namespace llvm; 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric #define DEBUG_TYPE "indirectbr-expand" 490b57cec5SDimitry Andric 500b57cec5SDimitry Andric namespace { 510b57cec5SDimitry Andric 525f757f3fSDimitry Andric class IndirectBrExpandLegacyPass : public FunctionPass { 530b57cec5SDimitry Andric public: 540b57cec5SDimitry Andric static char ID; // Pass identification, replacement for typeid 550b57cec5SDimitry Andric 565f757f3fSDimitry Andric IndirectBrExpandLegacyPass() : FunctionPass(ID) { 575f757f3fSDimitry Andric initializeIndirectBrExpandLegacyPassPass(*PassRegistry::getPassRegistry()); 580b57cec5SDimitry Andric } 590b57cec5SDimitry Andric 60fe6060f1SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 61fe6060f1SDimitry Andric AU.addPreserved<DominatorTreeWrapperPass>(); 62fe6060f1SDimitry Andric } 63fe6060f1SDimitry Andric 640b57cec5SDimitry Andric bool runOnFunction(Function &F) override; 650b57cec5SDimitry Andric }; 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric } // end anonymous namespace 680b57cec5SDimitry Andric 695f757f3fSDimitry Andric static bool runImpl(Function &F, const TargetLowering *TLI, 705f757f3fSDimitry Andric DomTreeUpdater *DTU); 710b57cec5SDimitry Andric 725f757f3fSDimitry Andric PreservedAnalyses IndirectBrExpandPass::run(Function &F, 735f757f3fSDimitry Andric FunctionAnalysisManager &FAM) { 745f757f3fSDimitry Andric auto *STI = TM->getSubtargetImpl(F); 755f757f3fSDimitry Andric if (!STI->enableIndirectBrExpand()) 765f757f3fSDimitry Andric return PreservedAnalyses::all(); 775f757f3fSDimitry Andric 785f757f3fSDimitry Andric auto *TLI = STI->getTargetLowering(); 795f757f3fSDimitry Andric auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F); 805f757f3fSDimitry Andric DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy); 815f757f3fSDimitry Andric 825f757f3fSDimitry Andric bool Changed = runImpl(F, TLI, DT ? &DTU : nullptr); 835f757f3fSDimitry Andric if (!Changed) 845f757f3fSDimitry Andric return PreservedAnalyses::all(); 855f757f3fSDimitry Andric PreservedAnalyses PA; 865f757f3fSDimitry Andric PA.preserve<DominatorTreeAnalysis>(); 875f757f3fSDimitry Andric return PA; 885f757f3fSDimitry Andric } 895f757f3fSDimitry Andric 905f757f3fSDimitry Andric char IndirectBrExpandLegacyPass::ID = 0; 915f757f3fSDimitry Andric 925f757f3fSDimitry Andric INITIALIZE_PASS_BEGIN(IndirectBrExpandLegacyPass, DEBUG_TYPE, 93fe6060f1SDimitry Andric "Expand indirectbr instructions", false, false) 94fe6060f1SDimitry Andric INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 955f757f3fSDimitry Andric INITIALIZE_PASS_END(IndirectBrExpandLegacyPass, DEBUG_TYPE, 960b57cec5SDimitry Andric "Expand indirectbr instructions", false, false) 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric FunctionPass *llvm::createIndirectBrExpandPass() { 995f757f3fSDimitry Andric return new IndirectBrExpandLegacyPass(); 1000b57cec5SDimitry Andric } 1010b57cec5SDimitry Andric 1025f757f3fSDimitry Andric bool runImpl(Function &F, const TargetLowering *TLI, DomTreeUpdater *DTU) { 103*0fca6ea1SDimitry Andric auto &DL = F.getDataLayout(); 104fe6060f1SDimitry Andric 1050b57cec5SDimitry Andric SmallVector<IndirectBrInst *, 1> IndirectBrs; 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric // Set of all potential successors for indirectbr instructions. 1080b57cec5SDimitry Andric SmallPtrSet<BasicBlock *, 4> IndirectBrSuccs; 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric // Build a list of indirectbrs that we want to rewrite. 1110b57cec5SDimitry Andric for (BasicBlock &BB : F) 1120b57cec5SDimitry Andric if (auto *IBr = dyn_cast<IndirectBrInst>(BB.getTerminator())) { 1130b57cec5SDimitry Andric // Handle the degenerate case of no successors by replacing the indirectbr 1140b57cec5SDimitry Andric // with unreachable as there is no successor available. 1150b57cec5SDimitry Andric if (IBr->getNumSuccessors() == 0) { 116*0fca6ea1SDimitry Andric (void)new UnreachableInst(F.getContext(), IBr->getIterator()); 1170b57cec5SDimitry Andric IBr->eraseFromParent(); 1180b57cec5SDimitry Andric continue; 1190b57cec5SDimitry Andric } 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric IndirectBrs.push_back(IBr); 1220b57cec5SDimitry Andric for (BasicBlock *SuccBB : IBr->successors()) 1230b57cec5SDimitry Andric IndirectBrSuccs.insert(SuccBB); 1240b57cec5SDimitry Andric } 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric if (IndirectBrs.empty()) 1270b57cec5SDimitry Andric return false; 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric // If we need to replace any indirectbrs we need to establish integer 1300b57cec5SDimitry Andric // constants that will correspond to each of the basic blocks in the function 1310b57cec5SDimitry Andric // whose address escapes. We do that here and rewrite all the blockaddress 1320b57cec5SDimitry Andric // constants to just be those integer constants cast to a pointer type. 1330b57cec5SDimitry Andric SmallVector<BasicBlock *, 4> BBs; 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric for (BasicBlock &BB : F) { 1360b57cec5SDimitry Andric // Skip blocks that aren't successors to an indirectbr we're going to 1370b57cec5SDimitry Andric // rewrite. 1380b57cec5SDimitry Andric if (!IndirectBrSuccs.count(&BB)) 1390b57cec5SDimitry Andric continue; 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric auto IsBlockAddressUse = [&](const Use &U) { 1420b57cec5SDimitry Andric return isa<BlockAddress>(U.getUser()); 1430b57cec5SDimitry Andric }; 1440b57cec5SDimitry Andric auto BlockAddressUseIt = llvm::find_if(BB.uses(), IsBlockAddressUse); 1450b57cec5SDimitry Andric if (BlockAddressUseIt == BB.use_end()) 1460b57cec5SDimitry Andric continue; 1470b57cec5SDimitry Andric 1480b57cec5SDimitry Andric assert(std::find_if(std::next(BlockAddressUseIt), BB.use_end(), 1490b57cec5SDimitry Andric IsBlockAddressUse) == BB.use_end() && 1500b57cec5SDimitry Andric "There should only ever be a single blockaddress use because it is " 1510b57cec5SDimitry Andric "a constant and should be uniqued."); 1520b57cec5SDimitry Andric 1530b57cec5SDimitry Andric auto *BA = cast<BlockAddress>(BlockAddressUseIt->getUser()); 1540b57cec5SDimitry Andric 1550b57cec5SDimitry Andric // Skip if the constant was formed but ended up not being used (due to DCE 1560b57cec5SDimitry Andric // or whatever). 1570b57cec5SDimitry Andric if (!BA->isConstantUsed()) 1580b57cec5SDimitry Andric continue; 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric // Compute the index we want to use for this basic block. We can't use zero 1610b57cec5SDimitry Andric // because null can be compared with block addresses. 1620b57cec5SDimitry Andric int BBIndex = BBs.size() + 1; 1630b57cec5SDimitry Andric BBs.push_back(&BB); 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric auto *ITy = cast<IntegerType>(DL.getIntPtrType(BA->getType())); 1660b57cec5SDimitry Andric ConstantInt *BBIndexC = ConstantInt::get(ITy, BBIndex); 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric // Now rewrite the blockaddress to an integer constant based on the index. 1690b57cec5SDimitry Andric // FIXME: This part doesn't properly recognize other uses of blockaddress 1700b57cec5SDimitry Andric // expressions, for instance, where they are used to pass labels to 1710b57cec5SDimitry Andric // asm-goto. This part of the pass needs a rework. 1720b57cec5SDimitry Andric BA->replaceAllUsesWith(ConstantExpr::getIntToPtr(BBIndexC, BA->getType())); 1730b57cec5SDimitry Andric } 1740b57cec5SDimitry Andric 1750b57cec5SDimitry Andric if (BBs.empty()) { 1760b57cec5SDimitry Andric // There are no blocks whose address is taken, so any indirectbr instruction 1770b57cec5SDimitry Andric // cannot get a valid input and we can replace all of them with unreachable. 178fe6060f1SDimitry Andric SmallVector<DominatorTree::UpdateType, 8> Updates; 179fe6060f1SDimitry Andric if (DTU) 180fe6060f1SDimitry Andric Updates.reserve(IndirectBrSuccs.size()); 1810b57cec5SDimitry Andric for (auto *IBr : IndirectBrs) { 182fe6060f1SDimitry Andric if (DTU) { 183fe6060f1SDimitry Andric for (BasicBlock *SuccBB : IBr->successors()) 184fe6060f1SDimitry Andric Updates.push_back({DominatorTree::Delete, IBr->getParent(), SuccBB}); 185fe6060f1SDimitry Andric } 186*0fca6ea1SDimitry Andric (void)new UnreachableInst(F.getContext(), IBr->getIterator()); 1870b57cec5SDimitry Andric IBr->eraseFromParent(); 1880b57cec5SDimitry Andric } 189fe6060f1SDimitry Andric if (DTU) { 190fe6060f1SDimitry Andric assert(Updates.size() == IndirectBrSuccs.size() && 191fe6060f1SDimitry Andric "Got unexpected update count."); 192fe6060f1SDimitry Andric DTU->applyUpdates(Updates); 193fe6060f1SDimitry Andric } 1940b57cec5SDimitry Andric return true; 1950b57cec5SDimitry Andric } 1960b57cec5SDimitry Andric 1970b57cec5SDimitry Andric BasicBlock *SwitchBB; 1980b57cec5SDimitry Andric Value *SwitchValue; 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric // Compute a common integer type across all the indirectbr instructions. 2010b57cec5SDimitry Andric IntegerType *CommonITy = nullptr; 2020b57cec5SDimitry Andric for (auto *IBr : IndirectBrs) { 2030b57cec5SDimitry Andric auto *ITy = 2040b57cec5SDimitry Andric cast<IntegerType>(DL.getIntPtrType(IBr->getAddress()->getType())); 2050b57cec5SDimitry Andric if (!CommonITy || ITy->getBitWidth() > CommonITy->getBitWidth()) 2060b57cec5SDimitry Andric CommonITy = ITy; 2070b57cec5SDimitry Andric } 2080b57cec5SDimitry Andric 209bdd1243dSDimitry Andric auto GetSwitchValue = [CommonITy](IndirectBrInst *IBr) { 210*0fca6ea1SDimitry Andric return CastInst::CreatePointerCast(IBr->getAddress(), CommonITy, 211*0fca6ea1SDimitry Andric Twine(IBr->getAddress()->getName()) + 212*0fca6ea1SDimitry Andric ".switch_cast", 213*0fca6ea1SDimitry Andric IBr->getIterator()); 2140b57cec5SDimitry Andric }; 2150b57cec5SDimitry Andric 216fe6060f1SDimitry Andric SmallVector<DominatorTree::UpdateType, 8> Updates; 217fe6060f1SDimitry Andric 2180b57cec5SDimitry Andric if (IndirectBrs.size() == 1) { 2190b57cec5SDimitry Andric // If we only have one indirectbr, we can just directly replace it within 2200b57cec5SDimitry Andric // its block. 221fe6060f1SDimitry Andric IndirectBrInst *IBr = IndirectBrs[0]; 222fe6060f1SDimitry Andric SwitchBB = IBr->getParent(); 223fe6060f1SDimitry Andric SwitchValue = GetSwitchValue(IBr); 224fe6060f1SDimitry Andric if (DTU) { 225fe6060f1SDimitry Andric Updates.reserve(IndirectBrSuccs.size()); 226fe6060f1SDimitry Andric for (BasicBlock *SuccBB : IBr->successors()) 227fe6060f1SDimitry Andric Updates.push_back({DominatorTree::Delete, IBr->getParent(), SuccBB}); 228fe6060f1SDimitry Andric assert(Updates.size() == IndirectBrSuccs.size() && 229fe6060f1SDimitry Andric "Got unexpected update count."); 230fe6060f1SDimitry Andric } 231fe6060f1SDimitry Andric IBr->eraseFromParent(); 2320b57cec5SDimitry Andric } else { 2330b57cec5SDimitry Andric // Otherwise we need to create a new block to hold the switch across BBs, 2340b57cec5SDimitry Andric // jump to that block instead of each indirectbr, and phi together the 2350b57cec5SDimitry Andric // values for the switch. 2360b57cec5SDimitry Andric SwitchBB = BasicBlock::Create(F.getContext(), "switch_bb", &F); 2370b57cec5SDimitry Andric auto *SwitchPN = PHINode::Create(CommonITy, IndirectBrs.size(), 2380b57cec5SDimitry Andric "switch_value_phi", SwitchBB); 2390b57cec5SDimitry Andric SwitchValue = SwitchPN; 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric // Now replace the indirectbr instructions with direct branches to the 2420b57cec5SDimitry Andric // switch block and fill out the PHI operands. 243fe6060f1SDimitry Andric if (DTU) 244fe6060f1SDimitry Andric Updates.reserve(IndirectBrs.size() + 2 * IndirectBrSuccs.size()); 2450b57cec5SDimitry Andric for (auto *IBr : IndirectBrs) { 2460b57cec5SDimitry Andric SwitchPN->addIncoming(GetSwitchValue(IBr), IBr->getParent()); 247*0fca6ea1SDimitry Andric BranchInst::Create(SwitchBB, IBr->getIterator()); 248fe6060f1SDimitry Andric if (DTU) { 249fe6060f1SDimitry Andric Updates.push_back({DominatorTree::Insert, IBr->getParent(), SwitchBB}); 250fe6060f1SDimitry Andric for (BasicBlock *SuccBB : IBr->successors()) 251fe6060f1SDimitry Andric Updates.push_back({DominatorTree::Delete, IBr->getParent(), SuccBB}); 252fe6060f1SDimitry Andric } 2530b57cec5SDimitry Andric IBr->eraseFromParent(); 2540b57cec5SDimitry Andric } 2550b57cec5SDimitry Andric } 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric // Now build the switch in the block. The block will have no terminator 2580b57cec5SDimitry Andric // already. 2590b57cec5SDimitry Andric auto *SI = SwitchInst::Create(SwitchValue, BBs[0], BBs.size(), SwitchBB); 2600b57cec5SDimitry Andric 2610b57cec5SDimitry Andric // Add a case for each block. 2620b57cec5SDimitry Andric for (int i : llvm::seq<int>(1, BBs.size())) 2630b57cec5SDimitry Andric SI->addCase(ConstantInt::get(CommonITy, i + 1), BBs[i]); 2640b57cec5SDimitry Andric 265fe6060f1SDimitry Andric if (DTU) { 266fe6060f1SDimitry Andric // If there were multiple indirectbr's, they may have common successors, 267fe6060f1SDimitry Andric // but in the dominator tree, we only track unique edges. 2684824e7fdSDimitry Andric SmallPtrSet<BasicBlock *, 8> UniqueSuccessors; 2694824e7fdSDimitry Andric Updates.reserve(Updates.size() + BBs.size()); 2704824e7fdSDimitry Andric for (BasicBlock *BB : BBs) { 2714824e7fdSDimitry Andric if (UniqueSuccessors.insert(BB).second) 272fe6060f1SDimitry Andric Updates.push_back({DominatorTree::Insert, SwitchBB, BB}); 2734824e7fdSDimitry Andric } 274fe6060f1SDimitry Andric DTU->applyUpdates(Updates); 275fe6060f1SDimitry Andric } 276fe6060f1SDimitry Andric 2770b57cec5SDimitry Andric return true; 2780b57cec5SDimitry Andric } 2795f757f3fSDimitry Andric 2805f757f3fSDimitry Andric bool IndirectBrExpandLegacyPass::runOnFunction(Function &F) { 2815f757f3fSDimitry Andric auto *TPC = getAnalysisIfAvailable<TargetPassConfig>(); 2825f757f3fSDimitry Andric if (!TPC) 2835f757f3fSDimitry Andric return false; 2845f757f3fSDimitry Andric 2855f757f3fSDimitry Andric auto &TM = TPC->getTM<TargetMachine>(); 2865f757f3fSDimitry Andric auto &STI = *TM.getSubtargetImpl(F); 2875f757f3fSDimitry Andric if (!STI.enableIndirectBrExpand()) 2885f757f3fSDimitry Andric return false; 2895f757f3fSDimitry Andric auto *TLI = STI.getTargetLowering(); 2905f757f3fSDimitry Andric 2915f757f3fSDimitry Andric std::optional<DomTreeUpdater> DTU; 2925f757f3fSDimitry Andric if (auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>()) 2935f757f3fSDimitry Andric DTU.emplace(DTWP->getDomTree(), DomTreeUpdater::UpdateStrategy::Lazy); 2945f757f3fSDimitry Andric 2955f757f3fSDimitry Andric return runImpl(F, TLI, DTU ? &*DTU : nullptr); 2965f757f3fSDimitry Andric } 297