C-Breeze
C Compiler Infrastructure

[ Project home page]

constantFoldingChanger Class Reference

Folds constants in binary expressions. More...

#include <constprop.h>

Inheritance diagram for constantFoldingChanger:

Changer List of all members.

"at_" methods

These methods define the functions that should be performed when different classes of Nodes are encountered in the AST. The most specific "at_" method that matches a given Node's class will be called.

virtual Node * at_node (Node *the_node, Order ord)
virtual Node * at_unit (unitNode *the_unit, Order ord)
virtual Node * at_def (defNode *the_def, Order ord)
virtual Node * at_decl (declNode *the_decl, Order ord)
virtual Node * at_subdecl (subdeclNode *the_subdecl, Order ord)
virtual Node * at_proc (procNode *the_proc, Order ord)
virtual Node * at_type (typeNode *the_type, Order ord)
virtual Node * at_prim (primNode *the_prim, Order ord)
virtual Node * at_tdef (tdefNode *the_tdef, Order ord)
virtual Node * at_ptr (ptrNode *the_ptr, Order ord)
virtual Node * at_array (arrayNode *the_array, Order ord)
virtual Node * at_func (funcNode *the_func, Order ord)
virtual Node * at_sue (sueNode *the_sue, Order ord)
virtual Node * at_struct (structNode *the_struct, Order ord)
virtual Node * at_union (unionNode *the_union, Order ord)
virtual Node * at_enum (enumNode *the_enum, Order ord)
virtual Node * at_suespec (suespecNode *the_suespec, Order ord)
virtual Node * at_expr (exprNode *the_expr, Order ord)
virtual Node * at_index (indexNode *the_index, Order ord)
virtual Node * at_const (constNode *the_const, Order ord)
virtual Node * at_id (idNode *the_id, Order ord)
virtual Node * at_binary (binaryNode *the_binary, Order ord)
virtual Node * at_unary (unaryNode *the_unary, Order ord)
virtual Node * at_cast (castNode *the_cast, Order ord)
virtual Node * at_comma (commaNode *the_comma, Order ord)
virtual Node * at_ternary (ternaryNode *the_ternary, Order ord)
virtual Node * at_call (callNode *the_call, Order ord)
virtual Node * at_initializer (initializerNode *the_initializer, Order ord)
virtual Node * at_stmt (stmtNode *the_stmt, Order ord)
virtual Node * at_block (blockNode *the_block, Order ord)
virtual Node * at_exprstmt (exprstmtNode *the_exprstmt, Order ord)
virtual Node * at_target (targetNode *the_target, Order ord)
virtual Node * at_label (labelNode *the_label, Order ord)
virtual Node * at_case (caseNode *the_case, Order ord)
virtual Node * at_selection (selectionNode *the_selection, Order ord)
virtual Node * at_if (ifNode *the_if, Order ord)
virtual Node * at_switch (switchNode *the_switch, Order ord)
virtual Node * at_loop (loopNode *the_loop, Order ord)
virtual Node * at_while (whileNode *the_while, Order ord)
virtual Node * at_do (doNode *the_do, Order ord)
virtual Node * at_for (forNode *the_for, Order ord)
virtual Node * at_jump (jumpNode *the_jump, Order ord)
virtual Node * at_goto (gotoNode *the_goto, Order ord)
virtual Node * at_continue (continueNode *the_continue, Order ord)
virtual Node * at_break (breakNode *the_break, Order ord)
virtual Node * at_return (returnNode *the_return, Order ord)
virtual Node * at_attrib (attribNode *the_attrib, Order ord)
virtual Node * at_operand (operandNode *the_oper, Order ord)
virtual Node * at_text (textNode *the_text, Order ord)

Public Types

enum  Order { Preorder, Postorder, Both }
enum  Depth { Subtree, NodeOnly }

Static Public Member Functions

static void change ()
 Constant propagation on entire program.
static void change (unitNode *)
 Constant propagation on one unit.
static void change (procNode *)
 Constant propagation on one procedure.

Private Member Functions

 constantFoldingChanger ()
void reset ()
 Reset the _changed flag.
Node * at_threeAddr (threeAddrNode *, Order)
Node * at_conditiongoto (conditiongotoNode *, Order)
Node * at_basicblock (basicblockNode *, Order)
void unreachableBlocks (basicblockNode *block, set< basicblockNode * > &unreachables)
 Collect all unreachable blocks starting from block.

Private Attributes

bool _changed
 Flag to indicate if any code is changed by this optimization.
bool _cfg_changed
 Flag to indicate if cfg is changed by this optimization.
basicblockNode * _current_block
 Current basic block.

Friends

class constantPropChanger

Detailed Description

Folds constants in binary expressions.

This class performs the constant folding. The program must already be dismantled. The three public static change() functions allow the optimization to be performed on the entire program, on one unit, or on a single procedure. The ud chain of the relevant part of the program must be provided.

Constant folding is performed at threeAddrNode's and conditiongotoNode's. At a threeAddrNode, if there are two right-hand-side constants, they are folded into one. At a conditiongotoNode, if the two operands are constants, the value of the condition is evaluated. If the value is non-zero, the conditiongotoNode is replaced with a goto to the true branch; otherwise, the conditiongotoNode is eliminated.

As a result of constant folding on conditions, some branches may become unreachable code. The cfg phase can be used to eliminate the dead code.

Definition at line 112 of file constprop.h.


Member Enumeration Documentation

enum Changer::Depth [inherited]
 

Enumeration values:
Subtree 
NodeOnly 

Definition at line 148 of file changer.h.

enum Changer::Order [inherited]
 

Enumeration values:
Preorder 
Postorder 
Both 

Definition at line 147 of file changer.h.


Constructor & Destructor Documentation

constantFoldingChanger::constantFoldingChanger (   )  [inline, private]
 

Definition at line 126 of file constprop.h.


Member Function Documentation

virtual Node* Changer::at_array (  arrayNode *  the_array,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 253 of file changer.h.

References Changer::at_type().

virtual Node* Changer::at_attrib (  attribNode *  the_attrib,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 364 of file changer.h.

References Changer::at_stmt().

Node* constantFoldingChanger::at_basicblock (  basicblockNode *  ,
Order 
)  [private, virtual]
 

Reimplemented from Changer.

virtual Node* Changer::at_binary (  binaryNode *  the_binary,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in SelectionDismantle, ArrowDismantle, and ExpressionDismantle.

Definition at line 286 of file changer.h.

References Changer::at_expr().

virtual Node* Changer::at_block (  blockNode *  the_block,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in StaticToGlobalDismantle, and InitializerDismantle.

Definition at line 310 of file changer.h.

References Changer::at_stmt().

Referenced by Changer::at_basicblock().

virtual Node* Changer::at_break (  breakNode *  the_break,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in BreakContinueChanger.

Definition at line 358 of file changer.h.

References Changer::at_jump().

virtual Node* Changer::at_call (  callNode *  the_call,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in ExpressionDismantle.

Definition at line 301 of file changer.h.

References Changer::at_expr().

virtual Node* Changer::at_case (  caseNode *  the_case,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 325 of file changer.h.

References Changer::at_target().

virtual Node* Changer::at_cast (  castNode *  the_cast,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in ExpressionDismantle.

Definition at line 292 of file changer.h.

References Changer::at_expr().

virtual Node* Changer::at_comma (  commaNode *  the_comma,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in ExpressionDismantle.

Definition at line 295 of file changer.h.

References Changer::at_expr().

Node* constantFoldingChanger::at_conditiongoto (  conditiongotoNode *  ,
Order 
)  [private, virtual]
 

Reimplemented from Changer.

virtual Node* Changer::at_const (  constNode *  the_const,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 280 of file changer.h.

References Changer::at_index().

virtual Node* Changer::at_continue (  continueNode *  the_continue,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in BreakContinueChanger.

Definition at line 355 of file changer.h.

References Changer::at_jump().

virtual Node* Changer::at_decl (  declNode *  the_decl,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in StaticToGlobalDismantle, InitializerDismantle, FlattenDismantle, and ref_clone_changer.

Definition at line 232 of file changer.h.

References Changer::at_def().

Referenced by Changer::at_subdecl().

virtual Node* Changer::at_def (  defNode *  the_def,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 229 of file changer.h.

References Changer::at_node().

Referenced by Changer::at_decl(), and Changer::at_proc().

virtual Node* Changer::at_do (  doNode *  the_do,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in LoopDismantle.

Definition at line 343 of file changer.h.

References Changer::at_loop().

virtual Node* Changer::at_enum (  enumNode *  the_enum,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 268 of file changer.h.

References Changer::at_sue().

virtual Node* Changer::at_expr (  exprNode *  the_expr,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 274 of file changer.h.

References Changer::at_node().

Referenced by Changer::at_binary(), Changer::at_call(), Changer::at_cast(), Changer::at_comma(), Changer::at_index(), Changer::at_initializer(), Changer::at_operand(), Changer::at_ternary(), and Changer::at_unary().

virtual Node* Changer::at_exprstmt (  exprstmtNode *  the_exprstmt,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in SelectionDismantle, ExpressionDismantle, and FlattenDismantle.

Definition at line 316 of file changer.h.

References Changer::at_stmt().

virtual Node* Changer::at_for (  forNode *  the_for,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in LoopDismantle.

Definition at line 346 of file changer.h.

References Changer::at_loop().

virtual Node* Changer::at_func (  funcNode *  the_func,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 256 of file changer.h.

References Changer::at_type().

virtual Node* Changer::at_goto (  gotoNode *  the_goto,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in FlattenDismantle.

Definition at line 352 of file changer.h.

References Changer::at_jump().

Referenced by Changer::at_conditiongoto().

virtual Node* Changer::at_id (  idNode *  the_id,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in UnusedDeclarationCleanupChanger, and constantsChanger.

Definition at line 283 of file changer.h.

References Changer::at_index().

virtual Node* Changer::at_if (  ifNode *  the_if,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in SelectionDismantle.

Definition at line 331 of file changer.h.

References Changer::at_selection().

virtual Node* Changer::at_index (  indexNode *  the_index,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 277 of file changer.h.

References Changer::at_expr().

Referenced by Changer::at_const(), and Changer::at_id().

virtual Node* Changer::at_initializer (  initializerNode *  the_initializer,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 304 of file changer.h.

References Changer::at_expr().

virtual Node* Changer::at_jump (  jumpNode *  the_jump,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 349 of file changer.h.

References Changer::at_stmt().

Referenced by Changer::at_break(), Changer::at_continue(), Changer::at_goto(), and Changer::at_return().

virtual Node* Changer::at_label (  labelNode *  the_label,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in LabelDismantle, FlattenDismantle, and ref_clone_changer.

Definition at line 322 of file changer.h.

References Changer::at_target().

virtual Node* Changer::at_loop (  loopNode *  the_loop,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 337 of file changer.h.

References Changer::at_stmt().

Referenced by Changer::at_do(), Changer::at_for(), and Changer::at_while().

virtual Node* Changer::at_node (  Node *  the_node,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in ref_clone_changer.

Definition at line 223 of file changer.h.

Referenced by Changer::at_def(), Changer::at_expr(), Changer::at_stmt(), Changer::at_text(), Changer::at_type(), and Changer::at_unit().

virtual Node* Changer::at_operand (  operandNode *  the_oper,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 367 of file changer.h.

References Changer::at_expr().

virtual Node* Changer::at_prim (  primNode *  the_prim,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 244 of file changer.h.

References Changer::at_type().

virtual Node* Changer::at_proc (  procNode *  the_proc,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in Dismantle, StaticToGlobalDismantle, ControlDismantle, ReturnDismantle, FlattenDismantle, function_inline, cfg_changer, copyPropChanger, deadCodeEliminationChanger, and UnusedDeclarationCleanupChanger.

Definition at line 238 of file changer.h.

References Changer::at_def().

virtual Node* Changer::at_ptr (  ptrNode *  the_ptr,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 250 of file changer.h.

References Changer::at_type().

virtual Node* Changer::at_return (  returnNode *  the_return,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in SelectionDismantle, ReturnDismantle, FlattenDismantle, and constantPropChanger.

Definition at line 361 of file changer.h.

References Changer::at_jump().

virtual Node* Changer::at_selection (  selectionNode *  the_selection,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 328 of file changer.h.

References Changer::at_stmt().

Referenced by Changer::at_if(), and Changer::at_switch().

virtual Node* Changer::at_stmt (  stmtNode *  the_stmt,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in TernaryDismantle, and SelectionDismantle.

Definition at line 307 of file changer.h.

References Changer::at_node().

Referenced by Changer::at_attrib(), Changer::at_block(), Changer::at_exprstmt(), Changer::at_jump(), Changer::at_loop(), Changer::at_selection(), Changer::at_target(), and Changer::at_threeAddr().

virtual Node* Changer::at_struct (  structNode *  the_struct,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 262 of file changer.h.

References Changer::at_sue().

virtual Node* Changer::at_subdecl (  subdeclNode *  the_subdecl,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 235 of file changer.h.

References Changer::at_decl().

virtual Node* Changer::at_sue (  sueNode *  the_sue,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 259 of file changer.h.

References Changer::at_type().

Referenced by Changer::at_enum(), Changer::at_struct(), and Changer::at_union().

virtual Node* Changer::at_suespec (  suespecNode *  the_suespec,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 271 of file changer.h.

References Changer::at_type().

virtual Node* Changer::at_switch (  switchNode *  the_switch,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in SelectionDismantle, and ref_clone_changer.

Definition at line 334 of file changer.h.

References Changer::at_selection().

virtual Node* Changer::at_target (  targetNode *  the_target,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 319 of file changer.h.

References Changer::at_stmt().

Referenced by Changer::at_case(), and Changer::at_label().

virtual Node* Changer::at_tdef (  tdefNode *  the_tdef,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 247 of file changer.h.

References Changer::at_type().

virtual Node* Changer::at_ternary (  ternaryNode *  the_ternary,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in TernaryDismantle.

Definition at line 298 of file changer.h.

References Changer::at_expr().

virtual Node* Changer::at_text (  textNode *  the_text,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 376 of file changer.h.

References Changer::at_node().

Node* constantFoldingChanger::at_threeAddr (  threeAddrNode *  ,
Order 
)  [private, virtual]
 

Reimplemented from Changer.

virtual Node* Changer::at_type (  typeNode *  the_type,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in TernaryDismantle, and ExpressionDismantle.

Definition at line 241 of file changer.h.

References Changer::at_node().

Referenced by Changer::at_array(), Changer::at_func(), Changer::at_prim(), Changer::at_ptr(), Changer::at_sue(), Changer::at_suespec(), and Changer::at_tdef().

virtual Node* Changer::at_unary (  unaryNode *  the_unary,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in ExpressionDismantle.

Definition at line 289 of file changer.h.

References Changer::at_expr().

virtual Node* Changer::at_union (  unionNode *  the_union,
Order  ord
)  [inline, virtual, inherited]
 

Definition at line 265 of file changer.h.

References Changer::at_sue().

virtual Node* Changer::at_unit (  unitNode *  the_unit,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in Dismantle, and StaticToGlobalDismantle.

Definition at line 226 of file changer.h.

References Changer::at_node().

virtual Node* Changer::at_while (  whileNode *  the_while,
Order  ord
)  [inline, virtual, inherited]
 

Reimplemented in LoopDismantle.

Definition at line 340 of file changer.h.

References Changer::at_loop().

static void constantFoldingChanger::change (  procNode *   )  [static]
 

Constant propagation on one procedure.

static void constantFoldingChanger::change (  unitNode *   )  [static]
 

Constant propagation on one unit.

static void constantFoldingChanger::change (   )  [static]
 

Constant propagation on entire program.

bool Changer::delete_old (   )  const [inline, inherited]
 

Return whether this Changer deletes Nodes which are removed from the AST by "at_" methods.

Definition at line 209 of file changer.h.

References Changer::_delete_old.

Referenced by change_list().

Depth Changer::depth (   )  const [inline, inherited]
 

Return which Nodes of an AST should be visited.

A value of SubTree specifies that the entire AST should be traversed. A value of NodeOnly specifies that only the root Node in the AST should be visited.

Definition at line 204 of file changer.h.

References Changer::_depth.

Order Changer::order (   )  const [inline, inherited]
 

Return the order in which Nodes of an AST should be visited.

Nodes can be visited before their children (Preorder), after their children (Postorder), or both (Both).

Definition at line 196 of file changer.h.

References Changer::_order.

void constantFoldingChanger::reset (   )  [inline, private]
 

Reset the _changed flag.

Definition at line 131 of file constprop.h.

References _cfg_changed, and _changed.

void constantFoldingChanger::unreachableBlocks (  basicblockNode *  block,
set< basicblockNode * > &  unreachables
)  [private]
 

Collect all unreachable blocks starting from block.


Friends And Related Function Documentation

friend class constantPropChanger [friend]
 

Definition at line 113 of file constprop.h.


Member Data Documentation

bool constantFoldingChanger::_cfg_changed [private]
 

Flag to indicate if cfg is changed by this optimization.

Definition at line 137 of file constprop.h.

Referenced by reset().

bool constantFoldingChanger::_changed [private]
 

Flag to indicate if any code is changed by this optimization.

Definition at line 134 of file constprop.h.

Referenced by reset().

basicblockNode* constantFoldingChanger::_current_block [private]
 

Current basic block.

Definition at line 140 of file constprop.h.


The documentation for this class was generated from the following file:

Generated on February 1, 2006
Back to the C-Breeze home page