Docsity

IR Tree Canonicalization: A Solution to Problems in Current IR Tree Representation - Prof., Study notes of Computer Science

Toggle sidebar
bg1
IR Tree Canonicalization
Jingke Li
Portland State University
Jingke Li (Portland State University) CS322 IR Tree Canonicalization 1/1
Problems with the Current IR Tree Representation
There are two areas in the current IR tree language that may cause
potential problems:
•Multiple CALL nodes within the same expression may cause problems
with call-related register usage when mapped to lower-level code.
•ESEQ and CALL nodes within expressions may bring side effects to
different evaluation orders of subexpressions, causing problems for
low-level optimizations.
Jingke Li (Portland State University) CS322 IR Tree Canonicalization 2/1
pf3
pf4
pf5

Partial preview of the text

Download IR Tree Canonicalization: A Solution to Problems in Current IR Tree Representation - Prof. and more Study notes Computer Science in PDF only on Docsity!

IR Tree Canonicalization

Jingke Li

Portland State University

Jingke Li (Portland State University) CS322 IR Tree Canonicalization 1 / 1

Problems with the Current IR Tree Representation

There are two areas in the current IR tree language that may cause

potential problems:

  • (^) Multiple CALL nodes within the same expression may cause problems

with call-related register usage when mapped to lower-level code.

  • (^) ESEQ and CALL nodes within expressions may bring side effects to

different evaluation orders of subexpressions, causing problems for

low-level optimizations.

IR Tree Canonicalization

To resolve these problems, we could perform canonicalization on a tree:

  • Assign each return value immediately into a TEMP; turn a CALL node

into an ESEQ node:

(CALL (NAME f) ((CONST 1))) => (ESEQ [MOVE (TEMP 100) (CALL (NAME f) ((CONST 1)))] (TEMP 100))

  • (^) Pull the statements in all ESEQ nodes out of an expression tree,

effectively eliminating all ESEQ nodes:

[ASSIGN (TEMP 101) (BINOP + (CONST 2) (CALL (NAME f) ((CONST 1))))] => [ASSIGN (TEMP 101) (BINOP + (CONST 2) (ESEQ [MOVE (TEMP 100) (CALL (NAME f) ((CONST 1)))] (TEMP 100)))] => [MOVE (TEMP 100) (CALL (NAME f) ((CONST 1)))] [ASSIGN (TEMP 101) (BINOP + (CONST 2) (TEMP 100))]

The resulting tree is called a canonical IR tree.

Jingke Li (Portland State University) CS322 IR Tree Canonicalization 3 / 1

Implementation

Canonicalization can be implemented as a visitor program over an IR tree.

The visitor program visits every expression node, extract statements out,

and forward them to upper level.

public class Canon implements IrVI { public PROG visit(PROG t) { return new PROG(t.funcs.accept(this)); } public FUNClist visit(FUNClist t) { FUNClist funcs = new FUNClist(); for (int i=0; i<t.size(); i++) { funcs.add(((FUNC) t.elementAt(i)).accept(this)); } return funcs; } public FUNC visit(FUNC t) { return new FUNC(t.label, t.varCnt, t.argCnt, (STMTlist) t.stmts.accept(this)); } ... }

Handling Other EXP Nodes (cont.)

public EXP visit(EXPlist t) { STMT s = null; EXPlist args = new EXPlist(); for (int i=0; i<t.size(); i++) { EXP e = ((EXP) t.elementAt(i)).accept(this); STMT s1 = getStmt(e); if (s1 != null) s = mergeStmts(s, s1); args.add(getExp(e)); } if (s != null) return new ESEQ(s, args); return t; }

public EXP visit(ESEQ t) { STMT stmt = t.stmt.accept(this); EXP exp = t.exp.accept(this); STMT s = mergeStmts(stmt, getStmt(exp)); if (s != null) return new ESEQ(s, getExp(exp)); return t; }

Jingke Li (Portland State University) CS322 IR Tree Canonicalization 7 / 1

Handling STMT Nodes

public STMT visit(MOVE t) { EXP dst = t.dst.accept(this); EXP src = t.src.accept(this); STMT s = mergeStmts(getStmt(dst), getStmt(src)); if (s != null) return mergeStmts(s, new MOVE(getExp(dst), getExp(src))); return t; }

public STMT visit(CJUMP t) { EXP left = t.left.accept(this); EXP right = t.right.accept(this); STMT s = mergeStmts(getStmt(left), getStmt(right)); if (s != null) return mergeStmts(s, new CJUMP(t.op, getExp(left), getExp(right), t.target)); return t; }

Handling STMT Nodes (cont.)

public STMT visit(CALLST t) { EXP args = t.args.accept(this); STMT s = getStmt(args); if (s != null) return mergeStmts(s, new CALLST(t.func, (EXPlist) getExp(args))); return t; }

public STMT visit(RETURN t) { EXP exp = t.exp.accept(this); STMT s = getStmt(exp); if (s != null) return mergeStmts(s, new RETURN(getExp(exp))); return t; }