Loading [MathJax]/extensions/tex2jax.js
CIRCT 22.0.0git
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes | Namespaces | Enumerations | Functions
FIRRTLFolds.cpp File Reference
#include "circt/Dialect/FIRRTL/FIRRTLAttributes.h"
#include "circt/Dialect/FIRRTL/FIRRTLOps.h"
#include "circt/Dialect/FIRRTL/FIRRTLTypes.h"
#include "circt/Dialect/FIRRTL/FIRRTLUtils.h"
#include "circt/Support/APInt.h"
#include "circt/Support/LLVM.h"
#include "circt/Support/Naming.h"
#include "mlir/IR/Matchers.h"
#include "mlir/IR/PatternMatch.h"
#include "llvm/ADT/APSInt.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/TypeSwitch.h"
#include "circt/Dialect/FIRRTL/FIRRTLCanonicalization.h.inc"
Include dependency graph for FIRRTLFolds.cpp:

Go to the source code of this file.

Classes

class  ReductionCat
 
class  OrRCat
 
class  AndRCat
 
class  XorRCat
 
struct  BitsOfCat
 

Namespaces

namespace  circt
 The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
 
namespace  circt::firrtl
 
namespace  circt::firrtl::patterns
 

Enumerations

enum class  BinOpKind { Normal , Compare , DivideOrShift }
 This is the policy for folding, which depends on the sort of operator we're processing. More...
 

Functions

static Value dropWrite (PatternRewriter &rewriter, OpResult old, Value passthrough)
 
static bool isOkToPropagateName (Operation *op)
 
static Value moveNameHint (OpResult old, Value passthrough)
 
static bool hasKnownWidthIntTypes (Operation *op)
 Return true if this operation's operands and results all have a known width.
 
static bool isUInt1 (Type type)
 Return true if this value is 1 bit UInt.
 
static void updateName (PatternRewriter &rewriter, Operation *op, StringAttr name)
 Set the name of an op based on the best of two names: The current name, and the name passed in.
 
static void replaceOpAndCopyName (PatternRewriter &rewriter, Operation *op, Value newValue)
 A wrapper of PatternRewriter::replaceOp to propagate "name" attribute.
 
template<typename OpTy , typename... Args>
static OpTy replaceOpWithNewOpAndCopyName (PatternRewriter &rewriter, Operation *op, Args &&...args)
 A wrapper of PatternRewriter::replaceOpWithNewOp to propagate "name" attribute.
 
static std::optional< APSInt > getExtendedConstant (Value operand, Attribute constant, int32_t destWidth)
 Implicitly replace the operand to a constant folding operation with a const 0 in case the operand is non-constant but has a bit width 0, or if the operand is an invalid value.
 
static std::optional< APSInt > getConstant (Attribute operand)
 Determine the value of a constant operand for the sake of constant folding.
 
static bool isConstantZero (Attribute operand)
 Determine whether a constant operand is a zero value for the sake of constant folding.
 
static bool isConstantOne (Attribute operand)
 Determine whether a constant operand is a one value for the sake of constant folding.
 
static Attribute constFoldFIRRTLBinaryOp (Operation *op, ArrayRef< Attribute > operands, BinOpKind opKind, const function_ref< APInt(const APSInt &, const APSInt &)> &calculate)
 Applies the constant folding function calculate to the given operands.
 
static LogicalResult canonicalizePrimOp (Operation *op, PatternRewriter &rewriter, const function_ref< OpFoldResult(ArrayRef< Attribute >)> &canonicalize)
 Applies the canonicalization function canonicalize to the given operation.
 
static APInt getMaxUnsignedValue (unsigned bitWidth)
 Get the largest unsigned value of a given bit width.
 
static APInt getMinSignedValue (unsigned bitWidth)
 Get the smallest signed value of a given bit width.
 
static APInt getMaxSignedValue (unsigned bitWidth)
 Get the largest signed value of a given bit width.
 
static void replaceWithBits (Operation *op, Value value, unsigned hiBit, unsigned loBit, PatternRewriter &rewriter)
 Replace the specified operation with a 'bits' op from the specified hi/lo bits.
 
template<typename OpTy >
static OpFoldResult foldMux (OpTy op, typename OpTy::FoldAdaptor adaptor)
 
static LogicalResult canonicalizeSingleSetConnect (MatchingConnectOp op, PatternRewriter &rewriter)
 
static AttachOp getDominatingAttachUser (Value value, AttachOp dominatedAttach)
 If the specified value has an AttachOp user strictly dominating by "dominatingAttach" then return it.
 
static void replaceOpWithRegion (PatternRewriter &rewriter, Operation *op, Region &region)
 Replaces the given op with the contents of the given single-block region.
 
template<typename OpTy >
static LogicalResult demoteForceableIfUnused (OpTy op, PatternRewriter &rewriter)
 
static Attribute collectFields (MLIRContext *context, ArrayRef< Attribute > operands)
 
static bool isDefinedByOneConstantOp (Value v)
 
static LogicalResult canonicalizeRegResetWithOneReset (RegResetOp reg, PatternRewriter &rewriter)
 
static Value getPortFieldValue (Value port, StringRef name)
 
static bool isPortDisabled (Value port)
 
static bool isPortUnused (Value port, StringRef data)
 
static void replacePortField (PatternRewriter &rewriter, Value port, StringRef name, Value value)
 
static void erasePort (PatternRewriter &rewriter, Value port)
 
static LogicalResult foldHiddenReset (RegOp reg, PatternRewriter &rewriter)
 
static LogicalResult eraseIfZeroOrNotZero (Operation *op, Value predicate, Value enable, PatternRewriter &rewriter, bool eraseIfZero)
 
template<class Op , bool EraseIfZero = false>
static LogicalResult canonicalizeImmediateVerifOp (Op op, PatternRewriter &rewriter)
 
static LogicalResult canonicalizeRefResolveOfForceable (RefResolveOp op, PatternRewriter &rewriter)
 
static bool isConstantZero (Value operand)
 
template<typename Op >
static LogicalResult eraseIfPredFalse (Op op, PatternRewriter &rewriter)
 
static bool isTypeEmpty (FIRRTLType type)
 

Enumeration Type Documentation

◆ BinOpKind

enum class BinOpKind
strong

This is the policy for folding, which depends on the sort of operator we're processing.

Enumerator
Normal 
Compare 
DivideOrShift 

Definition at line 209 of file FIRRTLFolds.cpp.

Function Documentation

◆ canonicalizeImmediateVerifOp()

template<class Op , bool EraseIfZero = false>
static LogicalResult canonicalizeImmediateVerifOp ( Op  op,
PatternRewriter &  rewriter 
)
static

Definition at line 3514 of file FIRRTLFolds.cpp.

References eraseIfZeroOrNotZero().

◆ canonicalizePrimOp()

static LogicalResult canonicalizePrimOp ( Operation *  op,
PatternRewriter &  rewriter,
const function_ref< OpFoldResult(ArrayRef< Attribute >)> &  canonicalize 
)
static

Applies the canonicalization function canonicalize to the given operation.

Determines which (if any) of the operation's operands are constants, and provides them as arguments to the callback function. Any invalidvalue in the input is mapped to a constant zero. The value returned from the callback is used as the replacement for op, and an additional pad operation is inserted if necessary. Does nothing if the result of op is of unknown width, in which case the necessity of a pad cannot be determined.

Definition at line 292 of file FIRRTLFolds.cpp.

References assert(), and replaceOpAndCopyName().

◆ canonicalizeRefResolveOfForceable()

static LogicalResult canonicalizeRefResolveOfForceable ( RefResolveOp  op,
PatternRewriter &  rewriter 
)
static

Definition at line 3628 of file FIRRTLFolds.cpp.

◆ canonicalizeRegResetWithOneReset()

static LogicalResult canonicalizeRegResetWithOneReset ( RegResetOp  reg,
PatternRewriter &  rewriter 
)
static

Definition at line 2584 of file FIRRTLFolds.cpp.

References dropWrite(), and isDefinedByOneConstantOp().

◆ canonicalizeSingleSetConnect()

static LogicalResult canonicalizeSingleSetConnect ( MatchingConnectOp  op,
PatternRewriter &  rewriter 
)
static

◆ collectFields()

static Attribute collectFields ( MLIRContext *  context,
ArrayRef< Attribute >  operands 
)
static

Definition at line 2467 of file FIRRTLFolds.cpp.

◆ constFoldFIRRTLBinaryOp()

static Attribute constFoldFIRRTLBinaryOp ( Operation *  op,
ArrayRef< Attribute >  operands,
BinOpKind  opKind,
const function_ref< APInt(const APSInt &, const APSInt &)> &  calculate 
)
static

Applies the constant folding function calculate to the given operands.

Sign or zero extends the operands appropriately to the bitwidth of the result type if useDstWidth is true, else to the larger of the two operand bit widths and depending on whether the operation is to be performed on signed or unsigned operands.

Definition at line 221 of file FIRRTLFolds.cpp.

References assert(), Compare, DivideOrShift, getExtendedConstant(), circt::firrtl::getIntAttr(), and Normal.

◆ demoteForceableIfUnused()

template<typename OpTy >
static LogicalResult demoteForceableIfUnused ( OpTy  op,
PatternRewriter &  rewriter 
)
static

◆ dropWrite()

static Value dropWrite ( PatternRewriter &  rewriter,
OpResult  old,
Value  passthrough 
)
static

Definition at line 32 of file FIRRTLFolds.cpp.

Referenced by canonicalizeRegResetWithOneReset().

◆ eraseIfPredFalse()

template<typename Op >
static LogicalResult eraseIfPredFalse ( Op  op,
PatternRewriter &  rewriter 
)
static

Definition at line 3657 of file FIRRTLFolds.cpp.

References circt::isConstantZero().

◆ eraseIfZeroOrNotZero()

static LogicalResult eraseIfZeroOrNotZero ( Operation *  op,
Value  predicate,
Value  enable,
PatternRewriter &  rewriter,
bool  eraseIfZero 
)
static

◆ erasePort()

static void erasePort ( PatternRewriter &  rewriter,
Value  port 
)
static

Definition at line 2671 of file FIRRTLFolds.cpp.

References assert().

◆ foldHiddenReset()

static LogicalResult foldHiddenReset ( RegOp  reg,
PatternRewriter &  rewriter 
)
static

Definition at line 3407 of file FIRRTLFolds.cpp.

References circt::firrtl::getSingleConnectUserOf().

◆ foldMux()

template<typename OpTy >
static OpFoldResult foldMux ( OpTy  op,
typename OpTy::FoldAdaptor  adaptor 
)
static

Definition at line 1601 of file FIRRTLFolds.cpp.

References getConstant(), and circt::firrtl::getIntAttr().

◆ getConstant()

static std::optional< APSInt > getConstant ( Attribute  operand)
static

Determine the value of a constant operand for the sake of constant folding.

Definition at line 181 of file FIRRTLFolds.cpp.

Referenced by emulateBinaryOpForUnknownBits(), foldMux(), isConstantOne(), isConstantZero(), circt::firrtl::InstanceInfo::LatticeValue::mergeIn(), and circt::firrtl::InstanceInfo::LatticeValue::operator!().

◆ getDominatingAttachUser()

static AttachOp getDominatingAttachUser ( Value  value,
AttachOp  dominatedAttach 
)
static

If the specified value has an AttachOp user strictly dominating by "dominatingAttach" then return it.

Definition at line 2151 of file FIRRTLFolds.cpp.

◆ getExtendedConstant()

static std::optional< APSInt > getExtendedConstant ( Value  operand,
Attribute  constant,
int32_t  destWidth 
)
static

Implicitly replace the operand to a constant folding operation with a const 0 in case the operand is non-constant but has a bit width 0, or if the operand is an invalid value.

This makes constant folding significantly easier, as we can simply pass the operands to an operation through this function to appropriately replace any zero-width dynamic values or invalid values with a constant of value 0.

Definition at line 160 of file FIRRTLFolds.cpp.

References assert(), and circt::extOrTruncZeroWidth().

Referenced by constFoldFIRRTLBinaryOp().

◆ getMaxSignedValue()

static APInt getMaxSignedValue ( unsigned  bitWidth)
static

Get the largest signed value of a given bit width.

Returns a 1-bit zero value if bitWidth is 0.

Definition at line 362 of file FIRRTLFolds.cpp.

◆ getMaxUnsignedValue()

static APInt getMaxUnsignedValue ( unsigned  bitWidth)
static

Get the largest unsigned value of a given bit width.

Returns a 1-bit zero value if bitWidth is 0.

Definition at line 350 of file FIRRTLFolds.cpp.

◆ getMinSignedValue()

static APInt getMinSignedValue ( unsigned  bitWidth)
static

Get the smallest signed value of a given bit width.

Returns a 1-bit zero value if bitWidth is 0.

Definition at line 356 of file FIRRTLFolds.cpp.

◆ getPortFieldValue()

static Value getPortFieldValue ( Value  port,
StringRef  name 
)
static

Definition at line 2608 of file FIRRTLFolds.cpp.

References assert(), and circt::firrtl::getSingleConnectUserOf().

Referenced by isPortDisabled().

◆ hasKnownWidthIntTypes()

static bool hasKnownWidthIntTypes ( Operation *  op)
static

Return true if this operation's operands and results all have a known width.

This only works for integer types.

Definition at line 82 of file FIRRTLFolds.cpp.

◆ isConstantOne()

static bool isConstantOne ( Attribute  operand)
static

Determine whether a constant operand is a one value for the sake of constant folding.

Definition at line 201 of file FIRRTLFolds.cpp.

References getConstant().

◆ isConstantZero() [1/2]

static bool isConstantZero ( Attribute  operand)
static

Determine whether a constant operand is a zero value for the sake of constant folding.

This considers invalidvalue to be zero.

Definition at line 193 of file FIRRTLFolds.cpp.

References getConstant().

◆ isConstantZero() [2/2]

static bool isConstantZero ( Value  operand)
static

Definition at line 3651 of file FIRRTLFolds.cpp.

◆ isDefinedByOneConstantOp()

static bool isDefinedByOneConstantOp ( Value  v)
static

Definition at line 2575 of file FIRRTLFolds.cpp.

Referenced by canonicalizeRegResetWithOneReset().

◆ isOkToPropagateName()

static bool isOkToPropagateName ( Operation *  op)
static

Definition at line 47 of file FIRRTLFolds.cpp.

Referenced by moveNameHint(), and updateName().

◆ isPortDisabled()

static bool isPortDisabled ( Value  port)
static

Definition at line 2627 of file FIRRTLFolds.cpp.

References getPortFieldValue().

◆ isPortUnused()

static bool isPortUnused ( Value  port,
StringRef  data 
)
static

Definition at line 2638 of file FIRRTLFolds.cpp.

References assert(), and circt::firrtl::data.

◆ isTypeEmpty()

static bool isTypeEmpty ( FIRRTLType  type)
static

Definition at line 3707 of file FIRRTLFolds.cpp.

References circt::firrtl::FIRRTLTypeSwitch< T, ResultT >::Case(), and isTypeEmpty().

Referenced by isTypeEmpty().

◆ isUInt1()

static bool isUInt1 ( Type  type)
static

Return true if this value is 1 bit UInt.

Definition at line 93 of file FIRRTLFolds.cpp.

◆ moveNameHint()

static Value moveNameHint ( OpResult  old,
Value  passthrough 
)
static

Definition at line 59 of file FIRRTLFolds.cpp.

References assert(), and isOkToPropagateName().

◆ replaceOpAndCopyName()

static void replaceOpAndCopyName ( PatternRewriter &  rewriter,
Operation *  op,
Value  newValue 
)
static

A wrapper of PatternRewriter::replaceOp to propagate "name" attribute.

If a replaced op has a "name" attribute, this function propagates the name to the new value.

Definition at line 122 of file FIRRTLFolds.cpp.

References updateName().

Referenced by canonicalizePrimOp(), canonicalizeSingleSetConnect(), and BitsOfCat::matchAndRewrite().

◆ replaceOpWithNewOpAndCopyName()

template<typename OpTy , typename... Args>
static OpTy replaceOpWithNewOpAndCopyName ( PatternRewriter &  rewriter,
Operation *  op,
Args &&...  args 
)
static

A wrapper of PatternRewriter::replaceOpWithNewOp to propagate "name" attribute.

If a replaced op has a "name" attribute, this function propagates the name to the new value.

Definition at line 135 of file FIRRTLFolds.cpp.

References updateName().

◆ replaceOpWithRegion()

static void replaceOpWithRegion ( PatternRewriter &  rewriter,
Operation *  op,
Region &  region 
)
static

Replaces the given op with the contents of the given single-block region.

Definition at line 2209 of file FIRRTLFolds.cpp.

References assert().

◆ replacePortField()

static void replacePortField ( PatternRewriter &  rewriter,
Value  port,
StringRef  name,
Value  value 
)
static

Definition at line 2655 of file FIRRTLFolds.cpp.

References assert().

◆ replaceWithBits()

static void replaceWithBits ( Operation *  op,
Value  value,
unsigned  hiBit,
unsigned  loBit,
PatternRewriter &  rewriter 
)
static

Replace the specified operation with a 'bits' op from the specified hi/lo bits.

Insert a cast to handle the case where the original operation returned a signed integer.

Definition at line 1585 of file FIRRTLFolds.cpp.

◆ updateName()

static void updateName ( PatternRewriter &  rewriter,
Operation *  op,
StringAttr  name 
)
static

Set the name of an op based on the best of two names: The current name, and the name passed in.

Definition at line 102 of file FIRRTLFolds.cpp.

References assert(), circt::chooseName(), and isOkToPropagateName().

Referenced by replaceOpAndCopyName(), and replaceOpWithNewOpAndCopyName().