From c8f630ac9c21810e09d5624ba89d78fc4bc2277d Mon Sep 17 00:00:00 2001 From: "igor%mir2.org" Date: Sat, 8 May 2004 22:24:02 +0000 Subject: [PATCH] OptLocalVariable cleanup: under optimization GETVAR/SETVAR nodes always has a reference to them --- .../mozilla/javascript/optimizer/Block.java | 69 ++++++---------- .../mozilla/javascript/optimizer/Codegen.java | 21 ++--- .../optimizer/OptLocalVariable.java | 13 ++- .../javascript/optimizer/Optimizer.java | 82 +++++++------------ 4 files changed, 74 insertions(+), 111 deletions(-) diff --git a/js/rhino/src/org/mozilla/javascript/optimizer/Block.java b/js/rhino/src/org/mozilla/javascript/optimizer/Block.java index 3d980ec66ad..e3e305f28f1 100644 --- a/js/rhino/src/org/mozilla/javascript/optimizer/Block.java +++ b/js/rhino/src/org/mozilla/javascript/optimizer/Block.java @@ -267,13 +267,10 @@ class Block { Node child = n.getFirstChild(); if (child.getType() == Token.GETVAR) { - Object theVarProp = child.getProp(Node.VARIABLE_PROP); - if (theVarProp != null) { - int theVarIndex = ((OptLocalVariable)theVarProp).getIndex(); - if (!itsNotDefSet.test(theVarIndex)) - itsUseBeforeDefSet.set(theVarIndex); - itsNotDefSet.set(theVarIndex); - } + int theVarIndex = OptLocalVariable.get(child).getIndex(); + if (!itsNotDefSet.test(theVarIndex)) + itsUseBeforeDefSet.set(theVarIndex); + itsNotDefSet.set(theVarIndex); } } break; @@ -282,25 +279,19 @@ class Block Node lhs = n.getFirstChild(); Node rhs = lhs.getNext(); lookForVariableAccess(rhs, lastUse); - Object theVarProp = n.getProp(Node.VARIABLE_PROP); - if (theVarProp != null) { - int theVarIndex = ((OptLocalVariable)theVarProp).getIndex(); - itsNotDefSet.set(theVarIndex); - if (lastUse[theVarIndex] != null) - lastUse[theVarIndex].putProp(Node.LASTUSE_PROP, - theVarProp); - } + OptLocalVariable theVar = OptLocalVariable.get(n); + int theVarIndex = theVar.getIndex(); + itsNotDefSet.set(theVarIndex); + if (lastUse[theVarIndex] != null) + lastUse[theVarIndex].putProp(Node.LASTUSE_PROP, theVar); } break; case Token.GETVAR : { - Object theVarProp = n.getProp(Node.VARIABLE_PROP); - if (theVarProp != null) { - int theVarIndex = ((OptLocalVariable)theVarProp).getIndex(); - if (!itsNotDefSet.test(theVarIndex)) - itsUseBeforeDefSet.set(theVarIndex); - lastUse[theVarIndex] = n; - } + int theVarIndex = OptLocalVariable.get(n).getIndex(); + if (!itsNotDefSet.test(theVarIndex)) + itsUseBeforeDefSet.set(theVarIndex); + lastUse[theVarIndex] = n; } break; default : @@ -372,12 +363,8 @@ class Block case Token.GETELEM : return Optimizer.AnyType; - case Token.GETVAR : { - OptLocalVariable theVar = (OptLocalVariable) - (n.getProp(Node.VARIABLE_PROP)); - if (theVar != null) - return theVar.getTypeUnion(); - } + case Token.GETVAR : + return OptLocalVariable.get(n).getTypeUnion(); case Token.INC : case Token.DEC : @@ -416,7 +403,7 @@ class Block } } - boolean findDefPoints(Node n) + private static boolean findDefPoints(Node n) { boolean result = false; switch (n.getType()) { @@ -431,10 +418,9 @@ class Block case Token.DEC : case Token.INC : { Node firstChild = n.getFirstChild(); - OptLocalVariable theVar = (OptLocalVariable) - (firstChild.getProp(Node.VARIABLE_PROP)); - if (theVar != null) { + if (firstChild.getType() == Token.GETVAR) { // theVar is a Number now + OptLocalVariable theVar = OptLocalVariable.get(firstChild); result |= theVar.assignType(Optimizer.NumberType); } } @@ -447,10 +433,8 @@ class Block Node rhs = nameChild.getNext(); if (baseChild != null) { if (baseChild.getType() == Token.GETVAR) { - OptLocalVariable theVar = (OptLocalVariable) - (baseChild.getProp(Node.VARIABLE_PROP)); - if (theVar != null) - theVar.assignType(Optimizer.AnyType); + OptLocalVariable theVar = OptLocalVariable.get(baseChild); + theVar.assignType(Optimizer.AnyType); } result |= findDefPoints(baseChild); } @@ -461,13 +445,10 @@ class Block case Token.SETVAR : { Node firstChild = n.getFirstChild(); - OptLocalVariable theVar = (OptLocalVariable) - (n.getProp(Node.VARIABLE_PROP)); - if (theVar != null) { - Node rValue = firstChild.getNext(); - int theType = findExpressionType(rValue); - result |= theVar.assignType(theType); - } + OptLocalVariable theVar = OptLocalVariable.get(n); + Node rValue = firstChild.getNext(); + int theType = findExpressionType(rValue); + result |= theVar.assignType(theType); } break; } @@ -476,7 +457,7 @@ class Block // a total misnomer for now. To start with we're only trying to find // duplicate getProp calls on 'this' that can be merged - void localCSE(Node parent, Node n, Hashtable theCSETable, OptFunctionNode theFunction) + private void localCSE(Node parent, Node n, Hashtable theCSETable, OptFunctionNode theFunction) { switch (n.getType()) { default : { diff --git a/js/rhino/src/org/mozilla/javascript/optimizer/Codegen.java b/js/rhino/src/org/mozilla/javascript/optimizer/Codegen.java index 7037e8dd14a..99345f13cc5 100644 --- a/js/rhino/src/org/mozilla/javascript/optimizer/Codegen.java +++ b/js/rhino/src/org/mozilla/javascript/optimizer/Codegen.java @@ -1820,8 +1820,7 @@ class BodyCodegen break; case Token.GETVAR: { - OptLocalVariable lVar - = (OptLocalVariable)(node.getProp(Node.VARIABLE_PROP)); + OptLocalVariable lVar = OptLocalVariable.get(node); visitGetVar(lVar, node.getIntProp(Node.ISNUMBER_PROP, -1) != -1, node.getString()); @@ -2217,8 +2216,7 @@ class BodyCodegen boolean handled = false; if ((child.getType() == Token.GETVAR) && inDirectCallFunction) { - OptLocalVariable lVar - = (OptLocalVariable)(child.getProp(Node.VARIABLE_PROP)); + OptLocalVariable lVar = OptLocalVariable.get(child); if (lVar != null && lVar.isParameter()) { handled = true; cfw.addALoad(lVar.getJRegister()); @@ -2417,8 +2415,7 @@ class BodyCodegen boolean handled = false; if ((child.getType() == Token.GETVAR) && inDirectCallFunction) { - OptLocalVariable lVar - = (OptLocalVariable)(child.getProp(Node.VARIABLE_PROP)); + OptLocalVariable lVar = OptLocalVariable.get(child); if (lVar != null && lVar.isParameter()) { child.removeProp(Node.ISNUMBER_PROP); generateCodeFromNode(child, node); @@ -2762,8 +2759,7 @@ class BodyCodegen { Node child = node.getFirstChild(); if (node.getIntProp(Node.ISNUMBER_PROP, -1) != -1) { - OptLocalVariable lVar - = (OptLocalVariable)(child.getProp(Node.VARIABLE_PROP)); + OptLocalVariable lVar = OptLocalVariable.get(child); if (lVar.getJRegister() == -1) lVar.assignJRegister(getNewWordPairLocal()); cfw.addDLoad(lVar.getJRegister()); @@ -2772,10 +2768,9 @@ class BodyCodegen cfw.add((isInc) ? ByteCode.DADD : ByteCode.DSUB); cfw.addDStore(lVar.getJRegister()); } else { - OptLocalVariable lVar - = (OptLocalVariable)(child.getProp(Node.VARIABLE_PROP)); int childType = child.getType(); if (hasVarsInRegs && childType == Token.GETVAR) { + OptLocalVariable lVar = OptLocalVariable.get(child); if (lVar == null) lVar = fnCurrent.getVar(child.getString()); int reg = lVar.getJRegister(); @@ -2916,8 +2911,7 @@ class BodyCodegen private int nodeIsDirectCallParameter(Node node) { if (node.getType() == Token.GETVAR) { - OptLocalVariable lVar - = (OptLocalVariable)(node.getProp(Node.VARIABLE_PROP)); + OptLocalVariable lVar = OptLocalVariable.get(node); if (lVar != null && lVar.isParameter() && inDirectCallFunction && !itsForcedObjectParameters) { @@ -3267,8 +3261,7 @@ class BodyCodegen private void visitSetVar(Node node, Node child, boolean needValue) { - OptLocalVariable lVar; - lVar = (OptLocalVariable)(node.getProp(Node.VARIABLE_PROP)); + OptLocalVariable lVar = OptLocalVariable.get(node); // XXX is this right? If so, clean up. if (hasVarsInRegs && lVar == null) lVar = fnCurrent.getVar(child.getString()); diff --git a/js/rhino/src/org/mozilla/javascript/optimizer/OptLocalVariable.java b/js/rhino/src/org/mozilla/javascript/optimizer/OptLocalVariable.java index ec3eea3dd01..fec4e6bb830 100644 --- a/js/rhino/src/org/mozilla/javascript/optimizer/OptLocalVariable.java +++ b/js/rhino/src/org/mozilla/javascript/optimizer/OptLocalVariable.java @@ -39,9 +39,11 @@ package org.mozilla.javascript.optimizer; import org.mozilla.javascript.*; import org.mozilla.classfile.JavaVariable; -final class OptLocalVariable implements JavaVariable { +final class OptLocalVariable implements JavaVariable +{ - public OptLocalVariable(String name, boolean isParameter) { + OptLocalVariable(String name, boolean isParameter) + { itsName = name; itsIsParameter = isParameter; // If the variable is a parameter, it could have any type. @@ -50,6 +52,13 @@ final class OptLocalVariable implements JavaVariable { itsTypeUnion = isParameter ? Optimizer.AnyType : Optimizer.NoType; } + static OptLocalVariable get(Node n) + { + int type = n.getType(); + if (type != Token.GETVAR && type != Token.SETVAR) Kit.codeBug(); + return (OptLocalVariable)n.getProp(Node.VARIABLE_PROP); + } + public String toString() { return "LocalVariable : '" + getName() + "', index = " + getIndex() diff --git a/js/rhino/src/org/mozilla/javascript/optimizer/Optimizer.java b/js/rhino/src/org/mozilla/javascript/optimizer/Optimizer.java index aa82cbacc4d..6549b179bc9 100644 --- a/js/rhino/src/org/mozilla/javascript/optimizer/Optimizer.java +++ b/js/rhino/src/org/mozilla/javascript/optimizer/Optimizer.java @@ -328,11 +328,8 @@ class Optimizer */ private void markDCPNumberContext(Node n) { - if (inDirectCallFunction && (n.getType() == Token.GETVAR)) - { - OptLocalVariable theVar - = (OptLocalVariable)(n.getProp(Node.VARIABLE_PROP)); - if ((theVar != null) && theVar.isParameter()) { + if (inDirectCallFunction && n.getType() == Token.GETVAR) { + if (OptLocalVariable.get(n).isParameter()) { parameterUsedInNumberContext = true; } } @@ -340,11 +337,8 @@ class Optimizer private boolean convertParameter(Node n) { - if (inDirectCallFunction && (n.getType() == Token.GETVAR)) - { - OptLocalVariable theVar - = (OptLocalVariable)(n.getProp(Node.VARIABLE_PROP)); - if ((theVar != null) && theVar.isParameter()) { + if (inDirectCallFunction && n.getType() == Token.GETVAR) { + if (OptLocalVariable.get(n).isParameter()) { n.removeProp(Node.ISNUMBER_PROP); return true; } @@ -367,18 +361,14 @@ class Optimizer return NumberType; case Token.GETVAR : { - OptLocalVariable theVar - = (OptLocalVariable)(n.getProp(Node.VARIABLE_PROP)); - if (theVar != null) { - if (inDirectCallFunction && theVar.isParameter()) { - n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH); - return NumberType; - } - else - if (theVar.isNumber()) { - n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH); - return NumberType; - } + OptLocalVariable theVar = OptLocalVariable.get(n); + if (inDirectCallFunction && theVar.isParameter()) { + n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH); + return NumberType; + } + else if (theVar.isNumber()) { + n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH); + return NumberType; } return NoType; } @@ -387,9 +377,8 @@ class Optimizer case Token.DEC : { Node child = n.getFirstChild(); // will be a GETVAR or GETPROP if (child.getType() == Token.GETVAR) { - OptLocalVariable theVar - = (OptLocalVariable)(child.getProp(Node.VARIABLE_PROP)); - if ((theVar != null) && theVar.isNumber()) { + OptLocalVariable theVar = OptLocalVariable.get(child); + if (theVar.isNumber()) { n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH); markDCPNumberContext(child); return NumberType; @@ -404,8 +393,7 @@ class Optimizer Node lChild = n.getFirstChild(); Node rChild = lChild.getNext(); int rType = rewriteForNumberVariables(rChild); - OptLocalVariable theVar - = (OptLocalVariable)(n.getProp(Node.VARIABLE_PROP)); + OptLocalVariable theVar = OptLocalVariable.get(n); if (inDirectCallFunction && theVar.isParameter()) { if (rType == NumberType) { if (!convertParameter(rChild)) { @@ -418,26 +406,24 @@ class Optimizer else return rType; } + else if (theVar.isNumber()) { + if (rType != NumberType) { + n.removeChild(rChild); + n.addChildToBack(new Node(TO_DOUBLE, rChild)); + } + n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH); + markDCPNumberContext(rChild); + return NumberType; + } else { - if ((theVar != null) && theVar.isNumber()) { - if (rType != NumberType) { + if (rType == NumberType) { + if (!convertParameter(rChild)) { n.removeChild(rChild); - n.addChildToBack(new Node(TO_DOUBLE, rChild)); + n.addChildToBack(new Node(TO_OBJECT, + rChild)); } - n.putIntProp(Node.ISNUMBER_PROP, Node.BOTH); - markDCPNumberContext(rChild); - return NumberType; - } - else { - if (rType == NumberType) { - if (!convertParameter(rChild)) { - n.removeChild(rChild); - n.addChildToBack(new Node(TO_OBJECT, - rChild)); - } - } - return NoType; } + return NoType; } } case Token.LE : @@ -685,16 +671,10 @@ class Optimizer int type = n.getType(); if (type == Token.SETVAR) { String name = n.getFirstChild().getString(); - OptLocalVariable theVar = fn.getVar(name); - if (theVar != null) { - n.putProp(Node.VARIABLE_PROP, theVar); - } + n.putProp(Node.VARIABLE_PROP, fn.getVar(name)); } else if (type == Token.GETVAR) { String name = n.getString(); - OptLocalVariable theVar = fn.getVar(name); - if (theVar != null) { - n.putProp(Node.VARIABLE_PROP, theVar); - } + n.putProp(Node.VARIABLE_PROP, fn.getVar(name)); } } private static void buildStatementList_r(Node node, ObjArray statements)