LLVM 19.0.0git
Functions
DIExpressionOptimizer.cpp File Reference
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/IR/DebugInfoMetadata.h"

Go to the source code of this file.

Functions

static std::optional< uint64_tisConstantVal (DIExpression::ExprOperand Op)
 Returns true if the Op is a DW_OP_constu.
 
static bool isNeutralElement (uint64_t Op, uint64_t Val)
 Returns true if an operation and operand result in a No Op.
 
static std::optional< uint64_tfoldOperationIfPossible (uint64_t Const1, uint64_t Const2, dwarf::LocationAtom Operator)
 Try to fold Const1 and Const2 by applying Operator and returning the result, if there is an overflow, return a std::nullopt.
 
static bool operationsAreFoldableAndCommutative (dwarf::LocationAtom Operator1, dwarf::LocationAtom Operator2)
 Returns true if the two operations Operator1 and Operator2 are commutative and can be folded.
 
static void consumeOneOperator (DIExpressionCursor &Cursor, uint64_t &Loc, const DIExpression::ExprOperand &Op)
 Consume one operator and its operand(s).
 
void startFromBeginning (uint64_t &Loc, DIExpressionCursor &Cursor, ArrayRef< uint64_t > WorkingOps)
 Reset the Cursor to the beginning of the WorkingOps.
 
static SmallVector< uint64_tcanonicalizeDwarfOperations (ArrayRef< uint64_t > WorkingOps)
 This function will canonicalize:
 
static SmallVector< uint64_toptimizeDwarfOperations (ArrayRef< uint64_t > WorkingOps)
 This function will convert:
 
static bool tryFoldNoOpMath (uint64_t Const1, ArrayRef< DIExpression::ExprOperand > Ops, uint64_t &Loc, DIExpressionCursor &Cursor, SmallVectorImpl< uint64_t > &WorkingOps)
 {DW_OP_constu, 0, DW_OP_[plus, minus, shl, shr]} -> {} {DW_OP_constu, 1, DW_OP_[mul, div]} -> {}
 
static bool tryFoldConstants (uint64_t Const1, ArrayRef< DIExpression::ExprOperand > Ops, uint64_t &Loc, DIExpressionCursor &Cursor, SmallVectorImpl< uint64_t > &WorkingOps)
 {DW_OP_constu, Const1, DW_OP_constu, Const2, DW_OP_[plus, minus, mul, div, shl, shr] -> {DW_OP_constu, Const1 [+, -, *, /, <<, >>] Const2}
 
static bool tryFoldCommutativeMath (uint64_t Const1, ArrayRef< DIExpression::ExprOperand > Ops, uint64_t &Loc, DIExpressionCursor &Cursor, SmallVectorImpl< uint64_t > &WorkingOps)
 {DW_OP_constu, Const1, DW_OP_[plus, mul], DW_OP_constu, Const2, DW_OP_[plus, mul]} -> {DW_OP_constu, Const1 [+, *] Const2, DW_OP_[plus, mul]}
 
static bool tryFoldCommutativeMathWithArgInBetween (uint64_t Const1, ArrayRef< DIExpression::ExprOperand > Ops, uint64_t &Loc, DIExpressionCursor &Cursor, SmallVectorImpl< uint64_t > &WorkingOps)
 {DW_OP_constu, Const1, DW_OP_[plus, mul], DW_OP_LLVM_arg, Arg1, DW_OP_[plus, mul], DW_OP_constu, Const2, DW_OP_[plus, mul]} -> {DW_OP_constu, Const1 [+, *] Const2, DW_OP_[plus, mul], DW_OP_LLVM_arg, Arg1, DW_OP_[plus, mul]}
 

Function Documentation

◆ canonicalizeDwarfOperations()

static SmallVector< uint64_t > canonicalizeDwarfOperations ( ArrayRef< uint64_t WorkingOps)
static

This function will canonicalize:

  1. DW_OP_plus_uconst to DW_OP_constu <const-val> DW_OP_plus
  2. DW_OP_lit<n> to DW_OP_constu <n>

Expression has no operations, break.

Definition at line 113 of file DIExpressionOptimizer.cpp.

References llvm::SmallVectorImpl< T >::append(), llvm::ArrayRef< T >::begin(), consumeOneOperator(), llvm::DIExpressionCursor::peek(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), and llvm::ArrayRef< T >::size().

Referenced by llvm::DIExpression::foldConstantMath().

◆ consumeOneOperator()

static void consumeOneOperator ( DIExpressionCursor Cursor,
uint64_t Loc,
const DIExpression::ExprOperand Op 
)
static

◆ foldOperationIfPossible()

static std::optional< uint64_t > foldOperationIfPossible ( uint64_t  Const1,
uint64_t  Const2,
dwarf::LocationAtom  Operator 
)
static

Try to fold Const1 and Const2 by applying Operator and returning the result, if there is an overflow, return a std::nullopt.

Definition at line 45 of file DIExpressionOptimizer.cpp.

References llvm::countl_zero(), llvm::countr_zero(), llvm::SaturatingAdd(), and llvm::SaturatingMultiply().

Referenced by tryFoldCommutativeMath(), tryFoldCommutativeMathWithArgInBetween(), and tryFoldConstants().

◆ isConstantVal()

static std::optional< uint64_t > isConstantVal ( DIExpression::ExprOperand  Op)
static

Returns true if the Op is a DW_OP_constu.

Definition at line 20 of file DIExpressionOptimizer.cpp.

Referenced by llvm::DIExpression::foldConstantMath(), tryFoldCommutativeMath(), tryFoldCommutativeMathWithArgInBetween(), and tryFoldConstants().

◆ isNeutralElement()

static bool isNeutralElement ( uint64_t  Op,
uint64_t  Val 
)
static

Returns true if an operation and operand result in a No Op.

Definition at line 27 of file DIExpressionOptimizer.cpp.

Referenced by tryFoldNoOpMath().

◆ operationsAreFoldableAndCommutative()

static bool operationsAreFoldableAndCommutative ( dwarf::LocationAtom  Operator1,
dwarf::LocationAtom  Operator2 
)
static

Returns true if the two operations Operator1 and Operator2 are commutative and can be folded.

Definition at line 89 of file DIExpressionOptimizer.cpp.

Referenced by tryFoldCommutativeMath(), and tryFoldCommutativeMathWithArgInBetween().

◆ optimizeDwarfOperations()

static SmallVector< uint64_t > optimizeDwarfOperations ( ArrayRef< uint64_t WorkingOps)
static

This function will convert:

  1. DW_OP_constu <const-val> DW_OP_plus to DW_OP_plus_uconst
  2. DW_OP_constu, 0 to DW_OP_lit0

Expression has no operations, exit.

Expression has no more operations, copy into ResultOps and exit.

Definition at line 148 of file DIExpressionOptimizer.cpp.

References llvm::SmallVectorImpl< T >::append(), llvm::ArrayRef< T >::begin(), consumeOneOperator(), llvm::DIExpressionCursor::peek(), llvm::DIExpressionCursor::peekNext(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), and llvm::ArrayRef< T >::size().

Referenced by llvm::DIExpression::foldConstantMath().

◆ startFromBeginning()

void startFromBeginning ( uint64_t Loc,
DIExpressionCursor Cursor,
ArrayRef< uint64_t WorkingOps 
)

Reset the Cursor to the beginning of the WorkingOps.

Definition at line 103 of file DIExpressionOptimizer.cpp.

References llvm::DIExpressionCursor::assignNewExpr().

Referenced by tryFoldCommutativeMath(), tryFoldCommutativeMathWithArgInBetween(), tryFoldConstants(), and tryFoldNoOpMath().

◆ tryFoldCommutativeMath()

static bool tryFoldCommutativeMath ( uint64_t  Const1,
ArrayRef< DIExpression::ExprOperand Ops,
uint64_t Loc,
DIExpressionCursor Cursor,
SmallVectorImpl< uint64_t > &  WorkingOps 
)
static

{DW_OP_constu, Const1, DW_OP_[plus, mul], DW_OP_constu, Const2, DW_OP_[plus, mul]} -> {DW_OP_constu, Const1 [+, *] Const2, DW_OP_[plus, mul]}

Definition at line 232 of file DIExpressionOptimizer.cpp.

References llvm::SmallVectorTemplateCommon< T, typename >::begin(), consumeOneOperator(), llvm::SmallVectorImpl< T >::erase(), foldOperationIfPossible(), isConstantVal(), operationsAreFoldableAndCommutative(), and startFromBeginning().

Referenced by llvm::DIExpression::foldConstantMath().

◆ tryFoldCommutativeMathWithArgInBetween()

static bool tryFoldCommutativeMathWithArgInBetween ( uint64_t  Const1,
ArrayRef< DIExpression::ExprOperand Ops,
uint64_t Loc,
DIExpressionCursor Cursor,
SmallVectorImpl< uint64_t > &  WorkingOps 
)
static

{DW_OP_constu, Const1, DW_OP_[plus, mul], DW_OP_LLVM_arg, Arg1, DW_OP_[plus, mul], DW_OP_constu, Const2, DW_OP_[plus, mul]} -> {DW_OP_constu, Const1 [+, *] Const2, DW_OP_[plus, mul], DW_OP_LLVM_arg, Arg1, DW_OP_[plus, mul]}

Definition at line 260 of file DIExpressionOptimizer.cpp.

References llvm::SmallVectorTemplateCommon< T, typename >::begin(), consumeOneOperator(), llvm::dwarf::DW_OP_LLVM_arg, llvm::SmallVectorImpl< T >::erase(), foldOperationIfPossible(), isConstantVal(), operationsAreFoldableAndCommutative(), and startFromBeginning().

Referenced by llvm::DIExpression::foldConstantMath().

◆ tryFoldConstants()

static bool tryFoldConstants ( uint64_t  Const1,
ArrayRef< DIExpression::ExprOperand Ops,
uint64_t Loc,
DIExpressionCursor Cursor,
SmallVectorImpl< uint64_t > &  WorkingOps 
)
static

{DW_OP_constu, Const1, DW_OP_constu, Const2, DW_OP_[plus, minus, mul, div, shl, shr] -> {DW_OP_constu, Const1 [+, -, *, /, <<, >>] Const2}

Definition at line 207 of file DIExpressionOptimizer.cpp.

References llvm::SmallVectorTemplateCommon< T, typename >::begin(), consumeOneOperator(), llvm::SmallVectorImpl< T >::erase(), foldOperationIfPossible(), isConstantVal(), and startFromBeginning().

Referenced by llvm::DIExpression::foldConstantMath().

◆ tryFoldNoOpMath()

static bool tryFoldNoOpMath ( uint64_t  Const1,
ArrayRef< DIExpression::ExprOperand Ops,
uint64_t Loc,
DIExpressionCursor Cursor,
SmallVectorImpl< uint64_t > &  WorkingOps 
)
static

{DW_OP_constu, 0, DW_OP_[plus, minus, shl, shr]} -> {} {DW_OP_constu, 1, DW_OP_[mul, div]} -> {}

Definition at line 191 of file DIExpressionOptimizer.cpp.

References llvm::SmallVectorTemplateCommon< T, typename >::begin(), llvm::SmallVectorImpl< T >::erase(), isNeutralElement(), and startFromBeginning().

Referenced by llvm::DIExpression::foldConstantMath().