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

statically evaluates many constant expressions. More...

#include <constantFolding.h>

Inheritance diagram for P4::DoConstantFolding:
[legend]

Public Member Functions

 DoConstantFolding (const DeclarationLookup *refMap, const TypeMap *typeMap, bool warnings=true, ConstantFoldingPolicy *policy=nullptr)
 
 DoConstantFolding (const TypeMap *typeMap, bool warnings=true, ConstantFoldingPolicy *policy=nullptr)
 
const IR::Nodepostorder (IR::Add *e) override
 
const IR::Nodepostorder (IR::AddSat *e) override
 
const IR::Nodepostorder (IR::BAnd *e) override
 
const IR::Nodepostorder (IR::BOr *e) override
 
const IR::Nodepostorder (IR::BXor *e) override
 
const IR::Nodepostorder (IR::Cast *e) override
 
const IR::Nodepostorder (IR::Cmpl *e) override
 
const IR::Nodepostorder (IR::Concat *e) override
 
const IR::Nodepostorder (IR::Declaration_Constant *d) override
 
const IR::Nodepostorder (IR::Div *e) override
 
const IR::Nodepostorder (IR::Equ *e) override
 
const IR::Nodepostorder (IR::Geq *e) override
 
const IR::Nodepostorder (IR::Grt *e) override
 
const IR::Nodepostorder (IR::IfStatement *statement) override
 
const IR::Nodepostorder (IR::LAnd *e) override
 
const IR::Nodepostorder (IR::Leq *e) override
 
const IR::Nodepostorder (IR::LNot *e) override
 
const IR::Nodepostorder (IR::LOr *e) override
 
const IR::Nodepostorder (IR::Lss *e) override
 
const IR::Nodepostorder (IR::Member *e) override
 
const IR::Nodepostorder (IR::Mod *e) override
 
const IR::Nodepostorder (IR::Mul *e) override
 
const IR::Nodepostorder (IR::Mux *e) override
 
const IR::Nodepostorder (IR::Neg *e) override
 
const IR::Nodepostorder (IR::Neq *e) override
 
const IR::Nodepostorder (IR::PathExpression *e) override
 
const IR::Nodepostorder (IR::PlusSlice *e) override
 
const IR::Nodepostorder (IR::SelectExpression *e) override
 
const IR::Nodepostorder (IR::Shl *e) override
 
const IR::Nodepostorder (IR::Shr *e) override
 
const IR::Nodepostorder (IR::Slice *e) override
 
const IR::Nodepostorder (IR::Sub *e) override
 
const IR::Nodepostorder (IR::SubSat *e) override
 
const IR::Nodepostorder (IR::Type_Bits *type) override
 
const IR::Nodepostorder (IR::Type_Varbits *type) override
 
const IR::Nodepostorder (IR::UPlus *e) override
 
const IR::Nodepreorder (IR::ArrayIndex *e) override
 
const IR::Nodepreorder (IR::AssignmentStatement *statement) override
 
const IR::BlockStatement * preorder (IR::BlockStatement *bs) override
 
const IR::Nodepreorder (IR::SwitchCase *c) override
 
- Public Member Functions inherited from P4::Transform
const IR::Nodeapply_visitor (const IR::Node *, const char *name=0) override
 
profile_t init_apply (const IR::Node *root) override
 
virtual void loop_revisit (const IR::Node *)
 
virtual const IR::Nodepostorder (IR::Node *n)
 
virtual const IR::Nodepreorder (IR::Node *n)
 
void prune ()
 
virtual void revisit (const IR::Node *, const IR::Node *)
 
void revisit_visited ()
 
bool visit_in_progress (const IR::Node *) const
 
void visitAgain () const override
 
void visitOnce () const override
 
- Public Member Functions inherited from P4::Visitor
virtual bool check_global (cstring)
 
virtual void clear_globals ()
 
virtual Visitorclone () const
 
virtual ControlFlowVisitorcontrolFlowVisitor ()
 
virtual void end_apply ()
 
virtual void end_apply (const IR::Node *root)
 
virtual void erase_global (cstring)
 
template<class T >
const T * findContext () const
 
template<class T >
const T * findContext (const Context *&c) const
 
template<class T >
const T * findOrigCtxt () const
 
template<class T >
const T * findOrigCtxt (const Context *&c) const
 
virtual Visitorflow_clone ()
 
virtual void flow_merge (Visitor &)
 
virtual bool flow_merge_closure (Visitor &)
 
virtual void flow_merge_global_from (cstring)
 
virtual void flow_merge_global_to (cstring)
 
const ContextgetChildContext () const
 
int getChildrenVisited () const
 
const ContextgetContext () const
 
int getContextDepth () const
 
const IR::NodegetCurrentNode () const
 
template<class T >
const T * getCurrentNode () const
 
const IR::NodegetOriginal () const
 
template<class T >
const T * getOriginal () const
 
template<class T >
const T * getParent () const
 
virtual bool has_flow_joins () const
 
profile_t init_apply (const IR::Node *root, const Context *parent_context)
 
bool isInContext (const IR::Node *n) const
 
virtual const char * name () const
 
template<class T >
void parallel_visit (const IR::Vector< T > &v, const char *name, int cidx)
 
template<class T >
void parallel_visit (const IR::Vector< T > &v, const char *name=0)
 
template<class T >
void parallel_visit (IR::Vector< T > &v, const char *name, int cidx)
 
template<class T >
void parallel_visit (IR::Vector< T > &v, const char *name=0)
 
void print_context () const
 
const VisitorsetCalledBy (const Visitor *visitor)
 
void setName (const char *name)
 
void visit (const IR::Node &n, const char *name, int cidx)
 
void visit (const IR::Node &n, const char *name=0)
 
void visit (const IR::Node *&n, const char *name, int cidx)
 
void visit (const IR::Node *&n, const char *name=0)
 
void visit (const IR::Node *const &n, const char *name, int cidx)
 
void visit (const IR::Node *const &n, const char *name=0)
 
void visit (IR::Node &n, const char *name, int cidx)
 
void visit (IR::Node &n, const char *name=0)
 
void visit (IR::Node *&, const char *=0, int=0)
 
template<class T , typename = std::enable_if_t<Util::has_SourceInfo_v<T> && !std::is_pointer_v<T>>, class... Args>
void warn (const int kind, const char *format, const T &node, Args &&...args)
 The const ref variant of the above.
 
template<class T , typename = std::enable_if_t<Util::has_SourceInfo_v<T>>, class... Args>
void warn (const int kind, const char *format, const T *node, Args &&...args)
 
bool warning_enabled (int warning_kind) const
 
- Public Member Functions inherited from P4::ResolutionContext
const IR::IDeclarationgetDeclaration (const IR::Path *path, bool notNull=false) const
 
const IR::IDeclarationgetDeclaration (const IR::This *, bool notNull=false) const
 
auto getDeclarations (const IR::INamespace *ns) const
 Returns the set of decls that exist in the given namespace.
 
auto getDeclsByName (const IR::INamespace *ns, cstring name) const
 Returns the set of decls with the given name that exist in the given namespace.
 
std::vector< const IR::IDeclaration * > resolve (const IR::ID &name, ResolutionType type) const
 Resolve references for name, restricted to type declarations.
 
virtual const IR::IDeclarationresolvePath (const IR::Path *path, bool isType) const
 
const IR::Type * resolveType (const IR::Type *type) const
 Resolve a refrence to a type type.
 
const IR::IDeclarationresolveUnique (const IR::ID &name, ResolutionType type, const IR::INamespace *=nullptr) const
 Resolve reference for name, restricted to type declarations, and expect one result.
 
- Public Member Functions inherited from P4::DeclarationLookup

Protected Types

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

Protected Member Functions

const IR::Nodebinary (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::Nodecompare (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::Nodeshift (const IR::Operation_Binary *op)
 Statically evaluate shift operation e.
 
- Protected Member Functions inherited from P4::Transform
const IR::Nodetransform_child (const IR::Node *child)
 
- Protected Member Functions inherited from P4::Visitor
virtual void init_join_flows (const IR::Node *)
 
virtual bool join_flows (const IR::Node *)
 
virtual void post_join_flows (const IR::Node *, const IR::Node *)
 
void visit_children (const IR::Node *, std::function< void()> fn)
 
- Protected Member Functions inherited from P4::ResolutionContext
 ResolutionContext (bool ao)
 
std::vector< const IR::IDeclaration * > lookup (const IR::INamespace *ns, const IR::ID &name, ResolutionType type) const
 
std::vector< const IR::IDeclaration * > lookupMatchKind (const IR::ID &name) const
 
const IR::Vector< IR::Argument > * methodArguments (cstring name) const
 We are resolving a method call. Find the arguments from the context.
 

Protected Attributes

bool assignmentTarget
 
std::map< const IR::Declaration_Constant *, const IR::Expression * > constants
 Maps declaration constants to constant expressions.
 
ConstantFoldingPolicypolicy
 
const DeclarationLookuprefMap
 
const TypeMaptypeMap
 
bool typesKnown
 Set to true iff typeMap is not nullptr.
 
bool warnings
 If true then emit warnings.
 
- Protected Attributes inherited from P4::Transform
bool forceClone = false
 
- Protected Attributes inherited from P4::Visitor
bool dontForwardChildrenBeforePreorder = false
 
bool joinFlows = false
 
bool visitDagOnce = true
 
- Protected Attributes inherited from P4::ResolutionContext
bool anyOrder = false
 

Additional Inherited Members

- Public Types inherited from P4::Visitor
typedef Visitor_Context Context
 
- Static Public Member Functions inherited from P4::Visitor
static cstring demangle (const char *)
 
static bool warning_enabled (const Visitor *visitor, int warning_kind)
 
- Public Attributes inherited from P4::Visitor
const Visitorcalled_by = nullptr
 
cstring internalName
 
SplitFlowVisit_base *& split_link
 
SplitFlowVisit_basesplit_link_mem = nullptr
 

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 DeclarationLookup* 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.