Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/Scalar/ADCE.cpp
35269 views
//===- ADCE.cpp - Code to perform dead code elimination -------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements the Aggressive Dead Code Elimination pass. This pass9// optimistically assumes that all instructions are dead until proven otherwise,10// allowing it to eliminate dead computations that other DCE passes do not11// catch, particularly involving loop computations.12//13//===----------------------------------------------------------------------===//1415#include "llvm/Transforms/Scalar/ADCE.h"16#include "llvm/ADT/DenseMap.h"17#include "llvm/ADT/DepthFirstIterator.h"18#include "llvm/ADT/GraphTraits.h"19#include "llvm/ADT/MapVector.h"20#include "llvm/ADT/PostOrderIterator.h"21#include "llvm/ADT/SetVector.h"22#include "llvm/ADT/SmallPtrSet.h"23#include "llvm/ADT/SmallVector.h"24#include "llvm/ADT/Statistic.h"25#include "llvm/Analysis/DomTreeUpdater.h"26#include "llvm/Analysis/GlobalsModRef.h"27#include "llvm/Analysis/IteratedDominanceFrontier.h"28#include "llvm/Analysis/MemorySSA.h"29#include "llvm/Analysis/PostDominators.h"30#include "llvm/IR/BasicBlock.h"31#include "llvm/IR/CFG.h"32#include "llvm/IR/DebugInfo.h"33#include "llvm/IR/DebugInfoMetadata.h"34#include "llvm/IR/DebugLoc.h"35#include "llvm/IR/Dominators.h"36#include "llvm/IR/Function.h"37#include "llvm/IR/IRBuilder.h"38#include "llvm/IR/InstIterator.h"39#include "llvm/IR/Instruction.h"40#include "llvm/IR/Instructions.h"41#include "llvm/IR/IntrinsicInst.h"42#include "llvm/IR/PassManager.h"43#include "llvm/IR/Use.h"44#include "llvm/IR/Value.h"45#include "llvm/ProfileData/InstrProf.h"46#include "llvm/Support/Casting.h"47#include "llvm/Support/CommandLine.h"48#include "llvm/Support/Debug.h"49#include "llvm/Support/raw_ostream.h"50#include "llvm/Transforms/Utils/Local.h"51#include <cassert>52#include <cstddef>53#include <utility>5455using namespace llvm;5657#define DEBUG_TYPE "adce"5859STATISTIC(NumRemoved, "Number of instructions removed");60STATISTIC(NumBranchesRemoved, "Number of branch instructions removed");6162// This is a temporary option until we change the interface to this pass based63// on optimization level.64static cl::opt<bool> RemoveControlFlowFlag("adce-remove-control-flow",65cl::init(true), cl::Hidden);6667// This option enables removing of may-be-infinite loops which have no other68// effect.69static cl::opt<bool> RemoveLoops("adce-remove-loops", cl::init(false),70cl::Hidden);7172namespace {7374/// Information about Instructions75struct InstInfoType {76/// True if the associated instruction is live.77bool Live = false;7879/// Quick access to information for block containing associated Instruction.80struct BlockInfoType *Block = nullptr;81};8283/// Information about basic blocks relevant to dead code elimination.84struct BlockInfoType {85/// True when this block contains a live instructions.86bool Live = false;8788/// True when this block ends in an unconditional branch.89bool UnconditionalBranch = false;9091/// True when this block is known to have live PHI nodes.92bool HasLivePhiNodes = false;9394/// Control dependence sources need to be live for this block.95bool CFLive = false;9697/// Quick access to the LiveInfo for the terminator,98/// holds the value &InstInfo[Terminator]99InstInfoType *TerminatorLiveInfo = nullptr;100101/// Corresponding BasicBlock.102BasicBlock *BB = nullptr;103104/// Cache of BB->getTerminator().105Instruction *Terminator = nullptr;106107/// Post-order numbering of reverse control flow graph.108unsigned PostOrder;109110bool terminatorIsLive() const { return TerminatorLiveInfo->Live; }111};112113struct ADCEChanged {114bool ChangedAnything = false;115bool ChangedNonDebugInstr = false;116bool ChangedControlFlow = false;117};118119class AggressiveDeadCodeElimination {120Function &F;121122// ADCE does not use DominatorTree per se, but it updates it to preserve the123// analysis.124DominatorTree *DT;125PostDominatorTree &PDT;126127/// Mapping of blocks to associated information, an element in BlockInfoVec.128/// Use MapVector to get deterministic iteration order.129MapVector<BasicBlock *, BlockInfoType> BlockInfo;130bool isLive(BasicBlock *BB) { return BlockInfo[BB].Live; }131132/// Mapping of instructions to associated information.133DenseMap<Instruction *, InstInfoType> InstInfo;134bool isLive(Instruction *I) { return InstInfo[I].Live; }135136/// Instructions known to be live where we need to mark137/// reaching definitions as live.138SmallVector<Instruction *, 128> Worklist;139140/// Debug info scopes around a live instruction.141SmallPtrSet<const Metadata *, 32> AliveScopes;142143/// Set of blocks with not known to have live terminators.144SmallSetVector<BasicBlock *, 16> BlocksWithDeadTerminators;145146/// The set of blocks which we have determined whose control147/// dependence sources must be live and which have not had148/// those dependences analyzed.149SmallPtrSet<BasicBlock *, 16> NewLiveBlocks;150151/// Set up auxiliary data structures for Instructions and BasicBlocks and152/// initialize the Worklist to the set of must-be-live Instruscions.153void initialize();154155/// Return true for operations which are always treated as live.156bool isAlwaysLive(Instruction &I);157158/// Return true for instrumentation instructions for value profiling.159bool isInstrumentsConstant(Instruction &I);160161/// Propagate liveness to reaching definitions.162void markLiveInstructions();163164/// Mark an instruction as live.165void markLive(Instruction *I);166167/// Mark a block as live.168void markLive(BlockInfoType &BB);169void markLive(BasicBlock *BB) { markLive(BlockInfo[BB]); }170171/// Mark terminators of control predecessors of a PHI node live.172void markPhiLive(PHINode *PN);173174/// Record the Debug Scopes which surround live debug information.175void collectLiveScopes(const DILocalScope &LS);176void collectLiveScopes(const DILocation &DL);177178/// Analyze dead branches to find those whose branches are the sources179/// of control dependences impacting a live block. Those branches are180/// marked live.181void markLiveBranchesFromControlDependences();182183/// Remove instructions not marked live, return if any instruction was184/// removed.185ADCEChanged removeDeadInstructions();186187/// Identify connected sections of the control flow graph which have188/// dead terminators and rewrite the control flow graph to remove them.189bool updateDeadRegions();190191/// Set the BlockInfo::PostOrder field based on a post-order192/// numbering of the reverse control flow graph.193void computeReversePostOrder();194195/// Make the terminator of this block an unconditional branch to \p Target.196void makeUnconditional(BasicBlock *BB, BasicBlock *Target);197198public:199AggressiveDeadCodeElimination(Function &F, DominatorTree *DT,200PostDominatorTree &PDT)201: F(F), DT(DT), PDT(PDT) {}202203ADCEChanged performDeadCodeElimination();204};205206} // end anonymous namespace207208ADCEChanged AggressiveDeadCodeElimination::performDeadCodeElimination() {209initialize();210markLiveInstructions();211return removeDeadInstructions();212}213214static bool isUnconditionalBranch(Instruction *Term) {215auto *BR = dyn_cast<BranchInst>(Term);216return BR && BR->isUnconditional();217}218219void AggressiveDeadCodeElimination::initialize() {220auto NumBlocks = F.size();221222// We will have an entry in the map for each block so we grow the223// structure to twice that size to keep the load factor low in the hash table.224BlockInfo.reserve(NumBlocks);225size_t NumInsts = 0;226227// Iterate over blocks and initialize BlockInfoVec entries, count228// instructions to size the InstInfo hash table.229for (auto &BB : F) {230NumInsts += BB.size();231auto &Info = BlockInfo[&BB];232Info.BB = &BB;233Info.Terminator = BB.getTerminator();234Info.UnconditionalBranch = isUnconditionalBranch(Info.Terminator);235}236237// Initialize instruction map and set pointers to block info.238InstInfo.reserve(NumInsts);239for (auto &BBInfo : BlockInfo)240for (Instruction &I : *BBInfo.second.BB)241InstInfo[&I].Block = &BBInfo.second;242243// Since BlockInfoVec holds pointers into InstInfo and vice-versa, we may not244// add any more elements to either after this point.245for (auto &BBInfo : BlockInfo)246BBInfo.second.TerminatorLiveInfo = &InstInfo[BBInfo.second.Terminator];247248// Collect the set of "root" instructions that are known live.249for (Instruction &I : instructions(F))250if (isAlwaysLive(I))251markLive(&I);252253if (!RemoveControlFlowFlag)254return;255256if (!RemoveLoops) {257// This stores state for the depth-first iterator. In addition258// to recording which nodes have been visited we also record whether259// a node is currently on the "stack" of active ancestors of the current260// node.261using StatusMap = DenseMap<BasicBlock *, bool>;262263class DFState : public StatusMap {264public:265std::pair<StatusMap::iterator, bool> insert(BasicBlock *BB) {266return StatusMap::insert(std::make_pair(BB, true));267}268269// Invoked after we have visited all children of a node.270void completed(BasicBlock *BB) { (*this)[BB] = false; }271272// Return true if \p BB is currently on the active stack273// of ancestors.274bool onStack(BasicBlock *BB) {275auto Iter = find(BB);276return Iter != end() && Iter->second;277}278} State;279280State.reserve(F.size());281// Iterate over blocks in depth-first pre-order and282// treat all edges to a block already seen as loop back edges283// and mark the branch live it if there is a back edge.284for (auto *BB: depth_first_ext(&F.getEntryBlock(), State)) {285Instruction *Term = BB->getTerminator();286if (isLive(Term))287continue;288289for (auto *Succ : successors(BB))290if (State.onStack(Succ)) {291// back edge....292markLive(Term);293break;294}295}296}297298// Mark blocks live if there is no path from the block to a299// return of the function.300// We do this by seeing which of the postdomtree root children exit the301// program, and for all others, mark the subtree live.302for (const auto &PDTChild : children<DomTreeNode *>(PDT.getRootNode())) {303auto *BB = PDTChild->getBlock();304auto &Info = BlockInfo[BB];305// Real function return306if (isa<ReturnInst>(Info.Terminator)) {307LLVM_DEBUG(dbgs() << "post-dom root child is a return: " << BB->getName()308<< '\n';);309continue;310}311312// This child is something else, like an infinite loop.313for (auto *DFNode : depth_first(PDTChild))314markLive(BlockInfo[DFNode->getBlock()].Terminator);315}316317// Treat the entry block as always live318auto *BB = &F.getEntryBlock();319auto &EntryInfo = BlockInfo[BB];320EntryInfo.Live = true;321if (EntryInfo.UnconditionalBranch)322markLive(EntryInfo.Terminator);323324// Build initial collection of blocks with dead terminators325for (auto &BBInfo : BlockInfo)326if (!BBInfo.second.terminatorIsLive())327BlocksWithDeadTerminators.insert(BBInfo.second.BB);328}329330bool AggressiveDeadCodeElimination::isAlwaysLive(Instruction &I) {331// TODO -- use llvm::isInstructionTriviallyDead332if (I.isEHPad() || I.mayHaveSideEffects()) {333// Skip any value profile instrumentation calls if they are334// instrumenting constants.335if (isInstrumentsConstant(I))336return false;337return true;338}339if (!I.isTerminator())340return false;341if (RemoveControlFlowFlag && (isa<BranchInst>(I) || isa<SwitchInst>(I)))342return false;343return true;344}345346// Check if this instruction is a runtime call for value profiling and347// if it's instrumenting a constant.348bool AggressiveDeadCodeElimination::isInstrumentsConstant(Instruction &I) {349// TODO -- move this test into llvm::isInstructionTriviallyDead350if (CallInst *CI = dyn_cast<CallInst>(&I))351if (Function *Callee = CI->getCalledFunction())352if (Callee->getName() == getInstrProfValueProfFuncName())353if (isa<Constant>(CI->getArgOperand(0)))354return true;355return false;356}357358void AggressiveDeadCodeElimination::markLiveInstructions() {359// Propagate liveness backwards to operands.360do {361// Worklist holds newly discovered live instructions362// where we need to mark the inputs as live.363while (!Worklist.empty()) {364Instruction *LiveInst = Worklist.pop_back_val();365LLVM_DEBUG(dbgs() << "work live: "; LiveInst->dump(););366367for (Use &OI : LiveInst->operands())368if (Instruction *Inst = dyn_cast<Instruction>(OI))369markLive(Inst);370371if (auto *PN = dyn_cast<PHINode>(LiveInst))372markPhiLive(PN);373}374375// After data flow liveness has been identified, examine which branch376// decisions are required to determine live instructions are executed.377markLiveBranchesFromControlDependences();378379} while (!Worklist.empty());380}381382void AggressiveDeadCodeElimination::markLive(Instruction *I) {383auto &Info = InstInfo[I];384if (Info.Live)385return;386387LLVM_DEBUG(dbgs() << "mark live: "; I->dump());388Info.Live = true;389Worklist.push_back(I);390391// Collect the live debug info scopes attached to this instruction.392if (const DILocation *DL = I->getDebugLoc())393collectLiveScopes(*DL);394395// Mark the containing block live396auto &BBInfo = *Info.Block;397if (BBInfo.Terminator == I) {398BlocksWithDeadTerminators.remove(BBInfo.BB);399// For live terminators, mark destination blocks400// live to preserve this control flow edges.401if (!BBInfo.UnconditionalBranch)402for (auto *BB : successors(I->getParent()))403markLive(BB);404}405markLive(BBInfo);406}407408void AggressiveDeadCodeElimination::markLive(BlockInfoType &BBInfo) {409if (BBInfo.Live)410return;411LLVM_DEBUG(dbgs() << "mark block live: " << BBInfo.BB->getName() << '\n');412BBInfo.Live = true;413if (!BBInfo.CFLive) {414BBInfo.CFLive = true;415NewLiveBlocks.insert(BBInfo.BB);416}417418// Mark unconditional branches at the end of live419// blocks as live since there is no work to do for them later420if (BBInfo.UnconditionalBranch)421markLive(BBInfo.Terminator);422}423424void AggressiveDeadCodeElimination::collectLiveScopes(const DILocalScope &LS) {425if (!AliveScopes.insert(&LS).second)426return;427428if (isa<DISubprogram>(LS))429return;430431// Tail-recurse through the scope chain.432collectLiveScopes(cast<DILocalScope>(*LS.getScope()));433}434435void AggressiveDeadCodeElimination::collectLiveScopes(const DILocation &DL) {436// Even though DILocations are not scopes, shove them into AliveScopes so we437// don't revisit them.438if (!AliveScopes.insert(&DL).second)439return;440441// Collect live scopes from the scope chain.442collectLiveScopes(*DL.getScope());443444// Tail-recurse through the inlined-at chain.445if (const DILocation *IA = DL.getInlinedAt())446collectLiveScopes(*IA);447}448449void AggressiveDeadCodeElimination::markPhiLive(PHINode *PN) {450auto &Info = BlockInfo[PN->getParent()];451// Only need to check this once per block.452if (Info.HasLivePhiNodes)453return;454Info.HasLivePhiNodes = true;455456// If a predecessor block is not live, mark it as control-flow live457// which will trigger marking live branches upon which458// that block is control dependent.459for (auto *PredBB : predecessors(Info.BB)) {460auto &Info = BlockInfo[PredBB];461if (!Info.CFLive) {462Info.CFLive = true;463NewLiveBlocks.insert(PredBB);464}465}466}467468void AggressiveDeadCodeElimination::markLiveBranchesFromControlDependences() {469if (BlocksWithDeadTerminators.empty())470return;471472LLVM_DEBUG({473dbgs() << "new live blocks:\n";474for (auto *BB : NewLiveBlocks)475dbgs() << "\t" << BB->getName() << '\n';476dbgs() << "dead terminator blocks:\n";477for (auto *BB : BlocksWithDeadTerminators)478dbgs() << "\t" << BB->getName() << '\n';479});480481// The dominance frontier of a live block X in the reverse482// control graph is the set of blocks upon which X is control483// dependent. The following sequence computes the set of blocks484// which currently have dead terminators that are control485// dependence sources of a block which is in NewLiveBlocks.486487const SmallPtrSet<BasicBlock *, 16> BWDT{488BlocksWithDeadTerminators.begin(),489BlocksWithDeadTerminators.end()490};491SmallVector<BasicBlock *, 32> IDFBlocks;492ReverseIDFCalculator IDFs(PDT);493IDFs.setDefiningBlocks(NewLiveBlocks);494IDFs.setLiveInBlocks(BWDT);495IDFs.calculate(IDFBlocks);496NewLiveBlocks.clear();497498// Dead terminators which control live blocks are now marked live.499for (auto *BB : IDFBlocks) {500LLVM_DEBUG(dbgs() << "live control in: " << BB->getName() << '\n');501markLive(BB->getTerminator());502}503}504505//===----------------------------------------------------------------------===//506//507// Routines to update the CFG and SSA information before removing dead code.508//509//===----------------------------------------------------------------------===//510ADCEChanged AggressiveDeadCodeElimination::removeDeadInstructions() {511ADCEChanged Changed;512// Updates control and dataflow around dead blocks513Changed.ChangedControlFlow = updateDeadRegions();514515LLVM_DEBUG({516for (Instruction &I : instructions(F)) {517// Check if the instruction is alive.518if (isLive(&I))519continue;520521if (auto *DII = dyn_cast<DbgVariableIntrinsic>(&I)) {522// Check if the scope of this variable location is alive.523if (AliveScopes.count(DII->getDebugLoc()->getScope()))524continue;525526// If intrinsic is pointing at a live SSA value, there may be an527// earlier optimization bug: if we know the location of the variable,528// why isn't the scope of the location alive?529for (Value *V : DII->location_ops()) {530if (Instruction *II = dyn_cast<Instruction>(V)) {531if (isLive(II)) {532dbgs() << "Dropping debug info for " << *DII << "\n";533break;534}535}536}537}538}539});540541// The inverse of the live set is the dead set. These are those instructions542// that have no side effects and do not influence the control flow or return543// value of the function, and may therefore be deleted safely.544// NOTE: We reuse the Worklist vector here for memory efficiency.545for (Instruction &I : llvm::reverse(instructions(F))) {546// With "RemoveDIs" debug-info stored in DbgVariableRecord objects,547// debug-info attached to this instruction, and drop any for scopes that548// aren't alive, like the rest of this loop does. Extending support to549// assignment tracking is future work.550for (DbgRecord &DR : make_early_inc_range(I.getDbgRecordRange())) {551// Avoid removing a DVR that is linked to instructions because it holds552// information about an existing store.553if (DbgVariableRecord *DVR = dyn_cast<DbgVariableRecord>(&DR);554DVR && DVR->isDbgAssign())555if (!at::getAssignmentInsts(DVR).empty())556continue;557if (AliveScopes.count(DR.getDebugLoc()->getScope()))558continue;559I.dropOneDbgRecord(&DR);560}561562// Check if the instruction is alive.563if (isLive(&I))564continue;565566if (auto *DII = dyn_cast<DbgInfoIntrinsic>(&I)) {567// Avoid removing a dbg.assign that is linked to instructions because it568// holds information about an existing store.569if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(DII))570if (!at::getAssignmentInsts(DAI).empty())571continue;572// Check if the scope of this variable location is alive.573if (AliveScopes.count(DII->getDebugLoc()->getScope()))574continue;575576// Fallthrough and drop the intrinsic.577} else {578Changed.ChangedNonDebugInstr = true;579}580581// Prepare to delete.582Worklist.push_back(&I);583salvageDebugInfo(I);584}585586for (Instruction *&I : Worklist)587I->dropAllReferences();588589for (Instruction *&I : Worklist) {590++NumRemoved;591I->eraseFromParent();592}593594Changed.ChangedAnything = Changed.ChangedControlFlow || !Worklist.empty();595596return Changed;597}598599// A dead region is the set of dead blocks with a common live post-dominator.600bool AggressiveDeadCodeElimination::updateDeadRegions() {601LLVM_DEBUG({602dbgs() << "final dead terminator blocks: " << '\n';603for (auto *BB : BlocksWithDeadTerminators)604dbgs() << '\t' << BB->getName()605<< (BlockInfo[BB].Live ? " LIVE\n" : "\n");606});607608// Don't compute the post ordering unless we needed it.609bool HavePostOrder = false;610bool Changed = false;611SmallVector<DominatorTree::UpdateType, 10> DeletedEdges;612613for (auto *BB : BlocksWithDeadTerminators) {614auto &Info = BlockInfo[BB];615if (Info.UnconditionalBranch) {616InstInfo[Info.Terminator].Live = true;617continue;618}619620if (!HavePostOrder) {621computeReversePostOrder();622HavePostOrder = true;623}624625// Add an unconditional branch to the successor closest to the626// end of the function which insures a path to the exit for each627// live edge.628BlockInfoType *PreferredSucc = nullptr;629for (auto *Succ : successors(BB)) {630auto *Info = &BlockInfo[Succ];631if (!PreferredSucc || PreferredSucc->PostOrder < Info->PostOrder)632PreferredSucc = Info;633}634assert((PreferredSucc && PreferredSucc->PostOrder > 0) &&635"Failed to find safe successor for dead branch");636637// Collect removed successors to update the (Post)DominatorTrees.638SmallPtrSet<BasicBlock *, 4> RemovedSuccessors;639bool First = true;640for (auto *Succ : successors(BB)) {641if (!First || Succ != PreferredSucc->BB) {642Succ->removePredecessor(BB);643RemovedSuccessors.insert(Succ);644} else645First = false;646}647makeUnconditional(BB, PreferredSucc->BB);648649// Inform the dominators about the deleted CFG edges.650for (auto *Succ : RemovedSuccessors) {651// It might have happened that the same successor appeared multiple times652// and the CFG edge wasn't really removed.653if (Succ != PreferredSucc->BB) {654LLVM_DEBUG(dbgs() << "ADCE: (Post)DomTree edge enqueued for deletion"655<< BB->getName() << " -> " << Succ->getName()656<< "\n");657DeletedEdges.push_back({DominatorTree::Delete, BB, Succ});658}659}660661NumBranchesRemoved += 1;662Changed = true;663}664665if (!DeletedEdges.empty())666DomTreeUpdater(DT, &PDT, DomTreeUpdater::UpdateStrategy::Eager)667.applyUpdates(DeletedEdges);668669return Changed;670}671672// reverse top-sort order673void AggressiveDeadCodeElimination::computeReversePostOrder() {674// This provides a post-order numbering of the reverse control flow graph675// Note that it is incomplete in the presence of infinite loops but we don't676// need numbers blocks which don't reach the end of the functions since677// all branches in those blocks are forced live.678679// For each block without successors, extend the DFS from the block680// backward through the graph681SmallPtrSet<BasicBlock*, 16> Visited;682unsigned PostOrder = 0;683for (auto &BB : F) {684if (!succ_empty(&BB))685continue;686for (BasicBlock *Block : inverse_post_order_ext(&BB,Visited))687BlockInfo[Block].PostOrder = PostOrder++;688}689}690691void AggressiveDeadCodeElimination::makeUnconditional(BasicBlock *BB,692BasicBlock *Target) {693Instruction *PredTerm = BB->getTerminator();694// Collect the live debug info scopes attached to this instruction.695if (const DILocation *DL = PredTerm->getDebugLoc())696collectLiveScopes(*DL);697698// Just mark live an existing unconditional branch699if (isUnconditionalBranch(PredTerm)) {700PredTerm->setSuccessor(0, Target);701InstInfo[PredTerm].Live = true;702return;703}704LLVM_DEBUG(dbgs() << "making unconditional " << BB->getName() << '\n');705NumBranchesRemoved += 1;706IRBuilder<> Builder(PredTerm);707auto *NewTerm = Builder.CreateBr(Target);708InstInfo[NewTerm].Live = true;709if (const DILocation *DL = PredTerm->getDebugLoc())710NewTerm->setDebugLoc(DL);711712InstInfo.erase(PredTerm);713PredTerm->eraseFromParent();714}715716//===----------------------------------------------------------------------===//717//718// Pass Manager integration code719//720//===----------------------------------------------------------------------===//721PreservedAnalyses ADCEPass::run(Function &F, FunctionAnalysisManager &FAM) {722// ADCE does not need DominatorTree, but require DominatorTree here723// to update analysis if it is already available.724auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F);725auto &PDT = FAM.getResult<PostDominatorTreeAnalysis>(F);726ADCEChanged Changed =727AggressiveDeadCodeElimination(F, DT, PDT).performDeadCodeElimination();728if (!Changed.ChangedAnything)729return PreservedAnalyses::all();730731PreservedAnalyses PA;732if (!Changed.ChangedControlFlow) {733PA.preserveSet<CFGAnalyses>();734if (!Changed.ChangedNonDebugInstr) {735// Only removing debug instructions does not affect MemorySSA.736//737// Therefore we preserve MemorySSA when only removing debug instructions738// since otherwise later passes may behave differently which then makes739// the presence of debug info affect code generation.740PA.preserve<MemorySSAAnalysis>();741}742}743PA.preserve<DominatorTreeAnalysis>();744PA.preserve<PostDominatorTreeAnalysis>();745746return PA;747}748749750