Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ConstantPropagatorAndFolder more extensible #2134

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ public static ConstantPropagatorAndFolder v() {
protected void internalTransform(Body b, String phaseName, Map<String, String> options) {
int numFolded = 0;
int numPropagated = 0;
final boolean verbose = Options.v().verbose();

if (Options.v().verbose()) {
if (verbose) {
logger.debug("[" + b.getMethod().getName() + "] Propagating and folding constants...");
}

Expand All @@ -79,47 +80,59 @@ protected void internalTransform(Body b, String phaseName, Map<String, String> o
// go through each use box in each statement
for (Unit u : (new PseudoTopologicalOrderer<Unit>()).newList(g, false)) {
// propagation pass
for (ValueBox useBox : u.getUseBoxes()) {
Value value = useBox.getValue();
if (value instanceof Local) {
Local local = (Local) value;
List<Unit> defsOfUse = localDefs.getDefsOfAt(local, u);
if (defsOfUse.size() == 1) {
DefinitionStmt defStmt = (DefinitionStmt) defsOfUse.get(0);
Value rhs = defStmt.getRightOp();
if (rhs instanceof NumericConstant || rhs instanceof StringConstant || rhs instanceof NullConstant) {
if (useBox.canContainValue(rhs)) {
useBox.setValue(rhs);
numPropagated++;
}
} else if (rhs instanceof CastExpr) {
CastExpr ce = (CastExpr) rhs;
if (ce.getCastType() instanceof RefType && ce.getOp() instanceof NullConstant) {
defStmt.getRightOpBox().setValue(NullConstant.v());
numPropagated++;
}
}
numPropagated += propagate(localDefs, u);

// folding pass
numFolded += fold(u);
}

if (verbose) {
logger.debug("[" + b.getMethod().getName() + "] Propagated: " + numPropagated + ", Folded: " + numFolded);
}
}

protected int fold(Unit u) {
int numFolded = 0;
for (ValueBox useBox : u.getUseBoxes()) {
Value value = useBox.getValue();
if (!(value instanceof Constant)) {
if (Evaluator.isValueConstantValued(value)) {
Value constValue = Evaluator.getConstantValueOf(value);
if (useBox.canContainValue(constValue)) {
useBox.setValue(constValue);
numFolded++;
}
}
}
}
return numFolded;
}

// folding pass
for (ValueBox useBox : u.getUseBoxes()) {
Value value = useBox.getValue();
if (!(value instanceof Constant)) {
if (Evaluator.isValueConstantValued(value)) {
Value constValue = Evaluator.getConstantValueOf(value);
if (useBox.canContainValue(constValue)) {
useBox.setValue(constValue);
numFolded++;
protected int propagate(LocalDefs localDefs, Unit u) {
int numPropagated = 0;
for (ValueBox useBox : u.getUseBoxes()) {
Value value = useBox.getValue();
if (value instanceof Local) {
Local local = (Local) value;
List<Unit> defsOfUse = localDefs.getDefsOfAt(local, u);
if (defsOfUse.size() == 1) {
DefinitionStmt defStmt = (DefinitionStmt) defsOfUse.get(0);
Value rhs = defStmt.getRightOp();
if (rhs instanceof NumericConstant || rhs instanceof StringConstant || rhs instanceof NullConstant) {
if (useBox.canContainValue(rhs)) {
useBox.setValue(rhs);
numPropagated++;
}
} else if (rhs instanceof CastExpr) {
CastExpr ce = (CastExpr) rhs;
if (ce.getCastType() instanceof RefType && ce.getOp() instanceof NullConstant) {
defStmt.getRightOpBox().setValue(NullConstant.v());
numPropagated++;
}
}
}
}
}

if (Options.v().verbose()) {
logger.debug("[" + b.getMethod().getName() + "] Propagated: " + numPropagated + ", Folded: " + numFolded);
}
return numPropagated;
}
}
Loading