@Override public void replaceChild(IAstNode child, IAstNode newChild) { if (!(child instanceof Expr && newChild instanceof Expr)) { throw new IllegalArgumentException(); } for (int i = 0; i < getNumChildren(); i++) { if (getChild(i) == child) { setChild(i, (Expr) newChild); return; } } throw new ChildDoesNotExistException(child, this); }
/** * Returns a subsituted expression, and may mutate the argument expression in the process. */ private Expr applySubstitutionDestructive(Expr expr, Map<String, Expr> paramReplacement) { assert !paramReplacement.values().contains(null); if (expr instanceof VariableIdentifierExpr && paramReplacement.containsKey(((VariableIdentifierExpr) expr).getName())) { return paramReplacement.get(((VariableIdentifierExpr) expr).getName()); } for (int i = 0; i < expr.getNumChildren(); i++) { Expr newChild = applySubstitutionDestructive(expr.getChild(i), paramReplacement); assert newChild != null; expr.setChild(i, newChild); } return expr; }
private void identifyMutationPoints(Expr expr, Set<Integer> indicesToSkip) { if (insideLValueCount == 0) { for (int i = 0; i < expr.getNumChildren(); i++) { if (indicesToSkip.contains(i)) { continue; } if (typer.hasType(expr.getChild(i))) { Scope clonedScope = currentScope.shallowClone(); if (shadingLanguageVersion.restrictedForLoops()) { for (Set<String> iterators : forLoopIterators) { iterators.forEach(clonedScope::remove); } } mutationPoints.add(new MutationPoint(expr, i, typer.lookupType(expr.getChild(i)), clonedScope, isConstContext(), shadingLanguageVersion, generator, generationParams)); } } } }
private void cleanupLoop(LoopStmt loopStmt) { if (MacroNames.isLoopWrapper(loopStmt.getCondition())) { loopStmt.setCondition(loopStmt.getCondition().getChild(0)); } }
@Override public void visitScalarInitializer(ScalarInitializer scalarInitializer) { super.visitScalarInitializer(scalarInitializer); if (MacroNames.isFuzzed(scalarInitializer.getExpr())) { scalarInitializer.setExpr(scalarInitializer.getExpr().getChild(0)); } }
@Override public final void applyMutation() { Type typeToMutate = (type instanceof QualifiedType) ? ((QualifiedType) type).getTargetType() : type; if (BasicType.allScalarTypes().contains(typeToMutate) || BasicType.allVectorTypes() .contains(typeToMutate)) { // For now, restrict mutation to scalar and vector types. // TODO: add support for mutation of matrix types parent.setChild(indexOfChildToMutate, new OpaqueExpressionGenerator(generator, generationParams, shadingLanguageVersion) .applyIdentityFunction( parent.getChild(indexOfChildToMutate), (BasicType) typeToMutate, constContext, 0, new Fuzzer(new FuzzingContext(scope), shadingLanguageVersion, generator, generationParams))); } }
@Override public void visitIfStmt(IfStmt ifStmt) { super.visitIfStmt(ifStmt); if (MacroNames.isDeadByConstruction(ifStmt.getCondition()) || MacroNames.isIfWrapperFalse(ifStmt.getCondition()) || MacroNames.isIfWrapperTrue(ifStmt.getCondition())) { ifStmt.setCondition(ifStmt.getCondition().getChild(0)); } }
@Override void identifyReductionOpportunitiesForChild(IAstNode parent, Expr child) { if (!allowedToReduceExpr(parent, child)) { return; } if (inLValueContext()) { return; } final Type resultType = typer.lookupType(child); if (resultType == null) { return; } for (int i = 0; i < child.getNumChildren(); i++) { final Expr subExpr = child.getChild(i); final Type subExprType = typer.lookupType(subExpr); if (subExprType == null) { continue; } if (!subExprType.getWithoutQualifiers().equals(resultType.getWithoutQualifiers())) { continue; } addOpportunity(new SimplifyExprReductionOpportunity( parent, subExpr, child, // We mark this as deeper since we would prefer to reduce the root expression // to a constant. getVistitationDepth().deeper())); } }
private void cleanUpMacros(Expr parent) { for (int i = 0; i < parent.getNumChildren(); i++) { Expr child = parent.getChild(i); // Note: it would be redundant to have a special function reduction opportunity // be classed also as a fuzzed expression reduction opportunity if (MacroNames.isIdentity(child) || MacroNames.isZero(child) || MacroNames.isOne(child) || MacroNames.isFalse(child) || MacroNames.isTrue(child)) { replaceChildWithGrandchild(parent, i, 1); } else if (MacroNames.isFuzzed(child) || MacroNames.isDeadByConstruction(child)) { replaceChildWithGrandchild(parent, i, 0); } } }