diff --git a/llvm/include/llvm/CodeGen/FunctionLoweringInfo.h b/llvm/include/llvm/CodeGen/FunctionLoweringInfo.h index e880b6227ef0b..4c17e8dcc41a6 100644 --- a/llvm/include/llvm/CodeGen/FunctionLoweringInfo.h +++ b/llvm/include/llvm/CodeGen/FunctionLoweringInfo.h @@ -35,6 +35,7 @@ namespace llvm { class Argument; class BasicBlock; class BranchProbabilityInfo; +class DbgDeclareInst; class Function; class Instruction; class MachineFunction; @@ -187,6 +188,10 @@ class FunctionLoweringInfo { /// SelectionDAGISel::PrepareEHLandingPad(). unsigned ExceptionPointerVirtReg, ExceptionSelectorVirtReg; + /// Collection of dbg.declare instructions handled after argument + /// lowering and before ISel proper. + SmallPtrSet PreprocessedDbgDeclares; + /// set - Initialize this FunctionLoweringInfo with the given Function /// and its associated MachineFunction. /// diff --git a/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp b/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp index f8fde640a3f27..3e6b6798dd230 100644 --- a/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/FastISel.cpp @@ -1208,6 +1208,9 @@ bool FastISel::selectIntrinsicCall(const IntrinsicInst *II) { return true; } + if (FuncInfo.PreprocessedDbgDeclares.contains(DI)) + return true; + const Value *Address = DI->getAddress(); if (!Address || isa(Address)) { LLVM_DEBUG(dbgs() << "Dropping debug info for " << *DI @@ -1215,13 +1218,6 @@ bool FastISel::selectIntrinsicCall(const IntrinsicInst *II) { return true; } - // Byval arguments with frame indices were already handled after argument - // lowering and before isel. - const auto *Arg = - dyn_cast(Address->stripInBoundsConstantOffsets()); - if (Arg && FuncInfo.getArgumentFrameIndex(Arg) != INT_MAX) - return true; - std::optional Op; if (Register Reg = lookUpRegForValue(Address)) Op = MachineOperand::CreateReg(Reg, false); diff --git a/llvm/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp b/llvm/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp index df236c503e4b1..f5569ffdac3a8 100644 --- a/llvm/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp @@ -348,6 +348,7 @@ void FunctionLoweringInfo::clear() { StatepointStackSlots.clear(); StatepointRelocationMaps.clear(); PreferredExtendType.clear(); + PreprocessedDbgDeclares.clear(); } /// CreateReg - Allocate a single virtual register for the given type. diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp index df6b24f86941b..f4ca34ebfd582 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -6125,12 +6125,14 @@ void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, return; } case Intrinsic::dbg_declare: { + const auto &DI = cast(I); // Debug intrinsics are handled separately in assignment tracking mode. - if (AssignmentTrackingEnabled) + // Some intrinsics are handled right after Argument lowering. + if (AssignmentTrackingEnabled || + FuncInfo.PreprocessedDbgDeclares.count(&DI)) return; // Assume dbg.declare can not currently use DIArgList, i.e. // it is non-variadic. - const auto &DI = cast(I); assert(!DI.hasArgList() && "Only dbg.value should currently use DIArgList"); DILocalVariable *Variable = DI.getVariable(); DIExpression *Expression = DI.getExpression(); @@ -6149,29 +6151,6 @@ void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, bool isParameter = Variable->isParameter() || isa(Address); - // Check if this variable can be described by a frame index, typically - // either as a static alloca or a byval parameter. - int FI = std::numeric_limits::max(); - if (const auto *AI = - dyn_cast(Address->stripInBoundsConstantOffsets())) { - if (AI->isStaticAlloca()) { - auto I = FuncInfo.StaticAllocaMap.find(AI); - if (I != FuncInfo.StaticAllocaMap.end()) - FI = I->second; - } - } else if (const auto *Arg = dyn_cast( - Address->stripInBoundsConstantOffsets())) { - FI = FuncInfo.getArgumentFrameIndex(Arg); - } - - // llvm.dbg.declare is handled as a frame index in the MachineFunction - // variable table. - if (FI != std::numeric_limits::max()) { - LLVM_DEBUG(dbgs() << "Skipping " << DI - << " (variable info stashed in MF side table)\n"); - return; - } - SDValue &N = NodeMap[Address]; if (!N.getNode() && isa(Address)) // Check unused arguments map. diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp index d4e3f120107ad..36611b117296a 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp @@ -1342,13 +1342,13 @@ static bool isFoldedOrDeadInstruction(const Instruction *I, !FuncInfo.isExportedInst(I); // Exported instrs must be computed. } -static void processDbgDeclare(FunctionLoweringInfo &FuncInfo, +static bool processDbgDeclare(FunctionLoweringInfo &FuncInfo, const Value *Address, DIExpression *Expr, DILocalVariable *Var, DebugLoc DbgLoc) { if (!Address) { LLVM_DEBUG(dbgs() << "processDbgDeclares skipping " << *Var << " (bad address)\n"); - return; + return false; } MachineFunction *MF = FuncInfo.MF; const DataLayout &DL = MF->getDataLayout(); @@ -1373,7 +1373,7 @@ static void processDbgDeclare(FunctionLoweringInfo &FuncInfo, FI = FuncInfo.getArgumentFrameIndex(Arg); if (FI == std::numeric_limits::max()) - return; + return false; if (Offset.getBoolValue()) Expr = DIExpression::prepend(Expr, DIExpression::ApplyOffset, @@ -1383,18 +1383,17 @@ static void processDbgDeclare(FunctionLoweringInfo &FuncInfo, << ", Expr=" << *Expr << ", FI=" << FI << ", DbgLoc=" << DbgLoc << "\n"); MF->setVariableDbgInfo(Var, Expr, FI, DbgLoc); + return true; } /// Collect llvm.dbg.declare information. This is done after argument lowering /// in case the declarations refer to arguments. static void processDbgDeclares(FunctionLoweringInfo &FuncInfo) { - for (const BasicBlock &BB : *FuncInfo.Fn) { - for (const Instruction &I : BB) { - if (const DbgDeclareInst *DI = dyn_cast(&I)) { - processDbgDeclare(FuncInfo, DI->getAddress(), DI->getExpression(), - DI->getVariable(), DI->getDebugLoc()); - } - } + for (const auto &I : instructions(*FuncInfo.Fn)) { + const auto *DI = dyn_cast(&I); + if (DI && processDbgDeclare(FuncInfo, DI->getAddress(), DI->getExpression(), + DI->getVariable(), DI->getDebugLoc())) + FuncInfo.PreprocessedDbgDeclares.insert(DI); } }