1//===- LLVMDebugIntrinsicOps.cpp - LLVM dialect debug intrinsics ----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the ops of op partition `DebugIntrinsic` of the LLVM
10// dialect.
11//
12//===----------------------------------------------------------------------===//
13
14#include "mlir/Dialect/LLVMIR/LLVMDebugIntrinsicOps.h"
15
16#include "IR/LLVMOps.h"
17#include "mlir/Dialect/LLVMIR/LLVMConstantOps.h"
18#include "mlir/IR/Builders.h"
19#include "mlir/IR/DialectImplementation.h"
20#include "mlir/Interfaces/DataLayoutInterfaces.h"
21#include "mlir/Interfaces/MemorySlotInterfaces.h"
22
23using namespace mlir;
24using namespace mlir::LLVM;
25
26bool LLVM::DbgDeclareOp::canUsesBeRemoved(
27 const SmallPtrSetImpl<OpOperand *> &blockingUses,
28 SmallVectorImpl<OpOperand *> &newBlockingUses,
29 const DataLayout &dataLayout) {
30 return true;
31}
32
33DeletionKind LLVM::DbgDeclareOp::removeBlockingUses(
34 const SmallPtrSetImpl<OpOperand *> &blockingUses, OpBuilder &builder) {
35 return DeletionKind::Delete;
36}
37
38bool LLVM::DbgValueOp::canUsesBeRemoved(
39 const SmallPtrSetImpl<OpOperand *> &blockingUses,
40 SmallVectorImpl<OpOperand *> &newBlockingUses,
41 const DataLayout &dataLayout) {
42 // There is only one operand that we can remove the use of.
43 if (blockingUses.size() != 1)
44 return false;
45
46 return (*blockingUses.begin())->get() == getValue();
47}
48
49DeletionKind LLVM::DbgValueOp::removeBlockingUses(
50 const SmallPtrSetImpl<OpOperand *> &blockingUses, OpBuilder &builder) {
51 // builder by default is after '*this', but we need it before '*this'.
52 builder.setInsertionPoint(*this);
53
54 // Rather than dropping the debug value, replace it with undef to preserve the
55 // debug local variable info. This allows the debugger to inform the user that
56 // the variable has been optimized out.
57 auto undef =
58 UndefOp::create(builder, location: getValue().getLoc(), res: getValue().getType());
59 getValueMutable().assign(value: undef);
60 return DeletionKind::Keep;
61}
62
63bool LLVM::DbgDeclareOp::requiresReplacedValues() { return true; }
64
65void LLVM::DbgDeclareOp::visitReplacedValues(
66 ArrayRef<std::pair<Operation *, Value>> definitions, OpBuilder &builder) {
67 for (auto [op, value] : definitions) {
68 builder.setInsertionPointAfter(op);
69 LLVM::DbgValueOp::create(builder, location: getLoc(), value, varInfo: getVarInfo(),
70 locationExpr: getLocationExpr());
71 }
72}
73
74#define GET_OP_CLASSES
75#include "mlir/Dialect/LLVMIR/LLVMDebugIntrinsicOps.cpp.inc"
76