Skip to content

Commit

Permalink
[SelectionDAG][NFCI] Use common logic for identifying MMI vars
Browse files Browse the repository at this point in the history
After function argument lowering, but prior to instruction selection,
dbg declares pointing to function arguments are lowered using special
logic.

Later, during instruction selection (both "fast" and regular ISel), this
logic is "repeated" in order to identify which intrinsics have already
been lowered. This is bad for two reasons:

1. The logic is not _really_ repeated, the code is different, which
could lead to duplicate lowering of the intrinsic.
2. Even if the logic were repeated properly, this is still code
duplication.

This patch addresses these issues by storing all preprocessed
dbg.declare intrinsics in a set inside FuncInfo; the set is queried upon
instruction selection.

Differential Revision: https://reviews.llvm.org/D149682
  • Loading branch information
felipepiovezan committed May 3, 2023
1 parent 8e46ac3 commit a524f84
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 42 deletions.
5 changes: 5 additions & 0 deletions llvm/include/llvm/CodeGen/FunctionLoweringInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace llvm {
class Argument;
class BasicBlock;
class BranchProbabilityInfo;
class DbgDeclareInst;
class Function;
class Instruction;
class MachineFunction;
Expand Down Expand Up @@ -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<const DbgDeclareInst *, 8> PreprocessedDbgDeclares;

/// set - Initialize this FunctionLoweringInfo with the given Function
/// and its associated MachineFunction.
///
Expand Down
10 changes: 3 additions & 7 deletions llvm/lib/CodeGen/SelectionDAG/FastISel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1208,20 +1208,16 @@ bool FastISel::selectIntrinsicCall(const IntrinsicInst *II) {
return true;
}

if (FuncInfo.PreprocessedDbgDeclares.contains(DI))
return true;

const Value *Address = DI->getAddress();
if (!Address || isa<UndefValue>(Address)) {
LLVM_DEBUG(dbgs() << "Dropping debug info for " << *DI
<< " (bad/undef address)\n");
return true;
}

// Byval arguments with frame indices were already handled after argument
// lowering and before isel.
const auto *Arg =
dyn_cast<Argument>(Address->stripInBoundsConstantOffsets());
if (Arg && FuncInfo.getArgumentFrameIndex(Arg) != INT_MAX)
return true;

std::optional<MachineOperand> Op;
if (Register Reg = lookUpRegForValue(Address))
Op = MachineOperand::CreateReg(Reg, false);
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 4 additions & 25 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6125,12 +6125,14 @@ void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
return;
}
case Intrinsic::dbg_declare: {
const auto &DI = cast<DbgDeclareInst>(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<DbgVariableIntrinsic>(I);
assert(!DI.hasArgList() && "Only dbg.value should currently use DIArgList");
DILocalVariable *Variable = DI.getVariable();
DIExpression *Expression = DI.getExpression();
Expand All @@ -6149,29 +6151,6 @@ void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,

bool isParameter = Variable->isParameter() || isa<Argument>(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<int>::max();
if (const auto *AI =
dyn_cast<AllocaInst>(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<Argument>(
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<int>::max()) {
LLVM_DEBUG(dbgs() << "Skipping " << DI
<< " (variable info stashed in MF side table)\n");
return;
}

SDValue &N = NodeMap[Address];
if (!N.getNode() && isa<Argument>(Address))
// Check unused arguments map.
Expand Down
19 changes: 9 additions & 10 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -1373,7 +1373,7 @@ static void processDbgDeclare(FunctionLoweringInfo &FuncInfo,
FI = FuncInfo.getArgumentFrameIndex(Arg);

if (FI == std::numeric_limits<int>::max())
return;
return false;

if (Offset.getBoolValue())
Expr = DIExpression::prepend(Expr, DIExpression::ApplyOffset,
Expand All @@ -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<DbgDeclareInst>(&I)) {
processDbgDeclare(FuncInfo, DI->getAddress(), DI->getExpression(),
DI->getVariable(), DI->getDebugLoc());
}
}
for (const auto &I : instructions(*FuncInfo.Fn)) {
const auto *DI = dyn_cast<DbgDeclareInst>(&I);
if (DI && processDbgDeclare(FuncInfo, DI->getAddress(), DI->getExpression(),
DI->getVariable(), DI->getDebugLoc()))
FuncInfo.PreprocessedDbgDeclares.insert(DI);
}
}

Expand Down

0 comments on commit a524f84

Please sign in to comment.