P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
P4::DoConstantFolding Class Reference

statically evaluates many constant expressions. More...

Inheritance diagram for P4::DoConstantFolding:

Public Member Functions

 DoConstantFolding (const ReferenceMap *refMap, const TypeMap *typeMap, bool warnings=true, ConstantFoldingPolicy *policy=nullptr)
 
const IR::Node * postorder (IR::Add *e) override
 
const IR::Node * postorder (IR::AddSat *e) override
 
const IR::Node * postorder (IR::BAnd *e) override
 
const IR::Node * postorder (IR::BOr *e) override
 
const IR::Node * postorder (IR::BXor *e) override
 
const IR::Node * postorder (IR::Cast *e) override
 
const IR::Node * postorder (IR::Cmpl *e) override
 
const IR::Node * postorder (IR::Concat *e) override
 
const IR::Node * postorder (IR::Declaration_Constant *d) override
 
const IR::Node * postorder (IR::Div *e) override
 
const IR::Node * postorder (IR::Equ *e) override
 
const IR::Node * postorder (IR::Geq *e) override
 
const IR::Node * postorder (IR::Grt *e) override
 
const IR::Node * postorder (IR::IfStatement *statement) override
 
const IR::Node * postorder (IR::LAnd *e) override
 
const IR::Node * postorder (IR::Leq *e) override
 
const IR::Node * postorder (IR::LNot *e) override
 
const IR::Node * postorder (IR::LOr *e) override
 
const IR::Node * postorder (IR::Lss *e) override
 
const IR::Node * postorder (IR::Member *e) override
 
const IR::Node * postorder (IR::Mod *e) override
 
const IR::Node * postorder (IR::Mul *e) override
 
const IR::Node * postorder (IR::Mux *e) override
 
const IR::Node * postorder (IR::Neg *e) override
 
const IR::Node * postorder (IR::Neq *e) override
 
const IR::Node * postorder (IR::PathExpression *e) override
 
const IR::Node * postorder (IR::SelectExpression *e) override
 
const IR::Node * postorder (IR::Shl *e) override
 
const IR::Node * postorder (IR::Shr *e) override
 
const IR::Node * postorder (IR::Slice *e) override
 
const IR::Node * postorder (IR::Sub *e) override
 
const IR::Node * postorder (IR::SubSat *e) override
 
const IR::Node * postorder (IR::Type_Bits *type) override
 
const IR::Node * postorder (IR::Type_Varbits *type) override
 
const IR::Node * postorder (IR::UPlus *e) override
 
const IR::Node * preorder (IR::ArrayIndex *e) override
 
const IR::Node * preorder (IR::AssignmentStatement *statement) override
 
const IR::BlockStatement * preorder (IR::BlockStatement *bs) override
 

Protected Types

enum class  Result { Yes , No , DontKnow }
 Result type for setContains.
 

Protected Member Functions

const IR::Node * binary (const IR::Operation_Binary *op, std::function< big_int(big_int, big_int)> func, bool saturating=false)
 Statically evaluate binary operation e implemented by func.
 
const IR::Constant * cast (const IR::Constant *node, unsigned base, const IR::Type_Bits *type) const
 Statically cast constant node to type represented in the specified base.
 
const IR::Node * compare (const IR::Operation_Binary *op)
 
const IR::Expression * getConstant (const IR::Expression *expr) const
 
Result setContains (const IR::Expression *keySet, const IR::Expression *constant) const
 
const IR::Node * shift (const IR::Operation_Binary *op)
 Statically evaluate shift operation e.
 

Protected Attributes

bool assignmentTarget
 
std::map< const IR::Declaration_Constant *, const IR::Expression * > constants
 Maps declaration constants to constant expressions.
 
ConstantFoldingPolicypolicy
 
const ReferenceMaprefMap
 
const TypeMaptypeMap
 
bool typesKnown
 Set to true iff typeMap is not nullptr.
 
bool warnings
 If true then emit warnings.
 

Detailed Description

statically evaluates many constant expressions.

This pass can be invoked either with or without the refMap and typeMap. When type information is not available, constant folding is not performed for many IR nodes.

Precondition
: typeMap is up-to-date if not nullptr and similarly for refMap
Postcondition
: Ensures
  • most expressions that can be statically shown to evaluate to a constant are replaced with the constant value.
  • operations that involve constant InfInt operands are evaluated to an InfInt value
  • if typeMap and refMap are not nullptr then IR::Declaration_Constant nodes are initialized with compile-time known constants.

Member Function Documentation

◆ compare()

const IR::Node * P4::DoConstantFolding::compare ( const IR::Operation_Binary * op)
protected

Statically evaluate comparison operation e. Note that this only handles the case where e represents == or !=.

◆ getConstant()

const IR::Expression * P4::DoConstantFolding::getConstant ( const IR::Expression * expr) const
protected
Returns
a constant equivalent to expr or nullptr

◆ setContains()

DoConstantFolding::Result P4::DoConstantFolding::setContains ( const IR::Expression * keySet,
const IR::Expression * constant ) const
protected

Statically evaluate case in select expression.

Returns
  • Result::Yes
  • Result::No
  • Result::DontKnow

depending on whether constant is contained in keyset.

Member Data Documentation

◆ refMap

const ReferenceMap* P4::DoConstantFolding::refMap
protected

Used to resolve IR nodes to declarations. If nullptr, then const values cannot be resolved.

◆ typeMap

const TypeMap* P4::DoConstantFolding::typeMap
protected

Used to resolve nodes to their types. If nullptr, then type information is not available.