LLVM 22.0.0git
DIExpressionOptimizer.cpp File Reference

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()

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 115 of file DIExpressionOptimizer.cpp.

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

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

◆ consumeOneOperator()

◆ foldOperationIfPossible()

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()

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

◆ isNeutralElement()

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()

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 91 of file DIExpressionOptimizer.cpp.

Referenced by tryFoldCommutativeMath(), and tryFoldCommutativeMathWithArgInBetween().

◆ optimizeDwarfOperations()

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 150 of file DIExpressionOptimizer.cpp.

References llvm::SmallVectorImpl< T >::append(), llvm::ArrayRef< T >::begin(), consumeOneOperator(), 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 105 of file DIExpressionOptimizer.cpp.

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

◆ tryFoldCommutativeMath()

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 234 of file DIExpressionOptimizer.cpp.

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

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

◆ tryFoldCommutativeMathWithArgInBetween()

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 262 of file DIExpressionOptimizer.cpp.

References AbstractManglingParser< Derived, Alloc >::Ops, 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()

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 209 of file DIExpressionOptimizer.cpp.

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

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

◆ tryFoldNoOpMath()

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 193 of file DIExpressionOptimizer.cpp.

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

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