private Stmt makeBlock(Stmt stmt) { return new BlockStmt(Arrays.asList(stmt), true); }
@Override public void applyReductionImpl() { BlockStmt replacement = new BlockStmt(new ArrayList<>(), true); boolean reachedOriginalCode = false; for (Stmt stmt : switchStmt.getBody().getStmts()) { if (!reachedOriginalCode) { if (!(stmt instanceof ExprCaseLabel && isZeroLabel((ExprCaseLabel) stmt))) { continue; } reachedOriginalCode = true; } if (stmt instanceof BreakStmt) { break; } if (stmt instanceof CaseLabel) { continue; } replacement.addStmt(stmt); } parent.replaceChild(switchStmt, replacement); }
@Override public void visitBlockStmt(BlockStmt block) { final BlockStmt prevBlock = currentBlockStmt; final int prevIndex = currentIndex; currentBlockStmt = block; for (currentIndex = 0; currentIndex < block.getNumStmts(); currentIndex++) { visit(block.getStmt(currentIndex)); } currentBlockStmt = prevBlock; currentIndex = prevIndex; }
@Override public void inject(Stmt stmt) { if (nextStmt == null) { blockStmt.insertStmt(blockStmt.getNumStmts(), stmt); } else { blockStmt.insertBefore(nextStmt, stmt); } }
@Override public void applyReductionImpl() { for (int i = 0; i < blockStmt.getNumStmts(); i++) { if (child == blockStmt.getStmt(i)) { blockStmt.removeStmt(i); return; } } throw new FailedReductionException("Should be unreachable."); }
private boolean isNonEmptyBlockStmtWithoutTopLevelDeclaration(Stmt stmt) { if (!(stmt instanceof BlockStmt)) { return false; } final BlockStmt blockStmt = (BlockStmt) stmt; return blockStmt.getNumStmts() > 0 && blockStmt.getStmts().stream() .noneMatch(item -> item instanceof DeclarationStmt); }
@Override public void applyReductionImpl() { try { Stmt parent = (Stmt) parentMap.getParent(wrapper); if (parent instanceof BlockStmt) { final BlockStmt parentBlock = (BlockStmt) parent; if (!parentBlock.getStmts().contains(wrapper)) { throw new ChildDoesNotExistException(wrapper, parentBlock); } for (Stmt stmt : wrapees) { parentBlock.insertBefore(wrapper, stmt); } parentBlock.removeStmt(wrapper); } else { parent.replaceChild(wrapper, wrapees.size() == 1 ? wrapees.get(0) : new BlockStmt(wrapees, true)); } } catch (ChildDoesNotExistException exception) { // TODO: it would be cleaner to capture this in the precondition. // The wrapper has already been eliminated // by some other reduction opportunity } }
@Override public boolean preconditionHolds() { if (!blockStmt.getStmts().contains(child)) { // Some other reduction opportunity must have removed the statement already return false; } return true; } }
@Override void applyReductionImpl() { List<Stmt> newStmts = new ArrayList<>(); for (int i = 0; i < block.getNumStmts(); i++) { if (i == indexOfBackupDeclaration().get()) { continue; } if (i >= indexOfAssignmentToBackup().get() && i <= indexOfRestorationFromBackup().get()) { continue; } newStmts.add(block.getStmt(i)); } block.setStmts(newStmts); }
assert stmt instanceof BlockStmt; BlockStmt block = (BlockStmt) stmt; if (block.getNumStmts() == 0) { return; generator, generationParams)); block.setStmts(Arrays.asList(new SwitchStmt(new FunctionCallExpr(Constants.GLF_SWITCH, zero), new BlockStmt(switchBodyStmts, true))));
@Override public void replaceNext(Stmt stmt) { assert hasNextStmt(); for (int i = 0; i < blockStmt.getNumStmts(); i++) { if (blockStmt.getStmt(i) == nextStmt) { blockStmt.setStmt(i, stmt); } } }
@Override public void visitForStmt(ForStmt forStmt) { super.visitForStmt(forStmt); if (isUnwrappable(forStmt)) { Stmt wrappee = forStmt.getBody(); if (wrappee instanceof BlockStmt) { // A ForStmt's block does not introduce a new scope, but we need it to when unwrapped. assert !((BlockStmt) wrappee).introducesNewScope(); wrappee = new BlockStmt(((BlockStmt) wrappee).getStmts(), true); } addOpportunity(new UnwrapReductionOpportunity(forStmt, wrappee, parentMap, getVistitationDepth())); } }
@Override public void visitFunctionCallExpr(FunctionCallExpr functionCallExpr) { super.visitFunctionCallExpr(functionCallExpr); if (functionCallExpr != call) { return; } if (currentBlockStmt == null) { // Cannot inline e.g. at global scope return; } currentBlockStmt.insertStmt(currentIndex, new BlockStmt(inlinedStmts, true)); if (returnVariableName.isPresent()) { currentBlockStmt.insertStmt(currentIndex, new DeclarationStmt( new VariablesDeclaration( clonedFunctionDefinition.getPrototype().getReturnType() .getWithoutQualifiers(), new VariableDeclInfo(returnVariableName.get(), null, null)))); parentMap.getParent(functionCallExpr).replaceChild(functionCallExpr, new VariableIdentifierExpr(returnVariableName.get())); } else { assert clonedFunctionDefinition.getPrototype().getReturnType().getWithoutQualifiers() == VoidType.VOID; assert parentMap.getParent(functionCallExpr) instanceof ExprStmt; currentBlockStmt.removeStmt((Stmt) parentMap.getParent(functionCallExpr)); } } }.visit(tu);
@Override public void visitBlockStmt(BlockStmt stmt) { List<Stmt> newStmts = new ArrayList<>(); for (Stmt s : stmt.getStmts()) { if (shouldRemove.test(s)) { Optional<Stmt> possibleReplacement = maybeReplaceWith.apply(s); if (possibleReplacement.isPresent()) { newStmts.add(possibleReplacement.get()); } continue; } newStmts.add(s); } stmt.setStmts(newStmts); super.visitBlockStmt(stmt); }
/** * Inserts the second statement right after the first statement, which must appear in the block. * * @param originalStmt A statement that must be present in the block * @param insertedStmt A statement to be inserted right after originalStmt */ public void insertAfter(Stmt originalStmt, Stmt insertedStmt) { for (int i = 0; i < stmts.size(); i++) { if (getStmt(i) == originalStmt) { insertStmt(i + 1, insertedStmt); return; } } throw new IllegalArgumentException("Should be unreachable."); }
@Override public void applyReductionImpl() { Stmt replacement; if (compoundStmt instanceof ForStmt) { final ForStmt forStmt = (ForStmt) compoundStmt; assert childStmt == forStmt.getBody(); List<Stmt> stmts = new ArrayList<>(); stmts.add(forStmt.getInit()); if (forStmt.getBody() instanceof BlockStmt) { stmts.addAll(((BlockStmt) forStmt.getBody()).getStmts()); } else { stmts.add(forStmt.getBody()); } replacement = new BlockStmt(stmts, true); } else { replacement = childStmt; } parent.replaceChild(compoundStmt, replacement); }
new IntConstantExpr(String.valueOf(limit)), BinOp.GE), new BlockStmt(Arrays.asList(BreakStmt.INSTANCE), true), null), new ExprStmt(new UnaryExpr( ((BlockStmt) loopStmt.getBody()).insertStmt(0, limitCheckAndIncrement.get(i)); newStmts.addAll(limitCheckAndIncrement); newStmts.add(loopStmt.getBody()); loopStmt.setBody(new BlockStmt(newStmts, loopStmt instanceof DoStmt)); final BlockStmt replacementBlock = new BlockStmt( Arrays.asList(limiterDeclaration, loopStmt), true); parentMap.getParent(loopStmt).replaceChild(loopStmt, replacementBlock);
@Override public void replaceChild(IAstNode child, IAstNode newChild) { if (!hasChild(child)) { throw new ChildDoesNotExistException(child, this); } if (!(newChild instanceof Stmt)) { throw new IllegalArgumentException( "Attempt to replace child of block statement with a non-statement: " + newChild); } for (int i = 0; i < stmts.size(); i++) { if (child == getStmt(i)) { setStmt(i, (Stmt) newChild); return; } } throw new IllegalArgumentException("Should be unreachable."); }
private void addSpecialDeclarations() { if (fd.getPrototype().getReturnType().getWithoutQualifiers() != VoidType.VOID) { fd.getBody().insertStmt(0, new DeclarationStmt( new VariablesDeclaration(fd.getPrototype().getReturnType().getWithoutQualifiers(), new VariableDeclInfo(makeReturnValueName(), null, null)))); fd.getBody().addStmt(new ReturnStmt(makeReturnValue())); } fd.getBody().insertStmt(0, new DeclarationStmt( new VariablesDeclaration(BasicType.BOOL, new VariableDeclInfo(makeHasReturnedName(), null, new ScalarInitializer(BoolConstantExpr.FALSE))))); }
private void addComponentVariableIfNotPresent() { if (getAllVariableDeclInfosInBlock() .stream() .anyMatch(item -> item.getName().equals(getComponentName()))) { return; } block.insertStmt(0, new DeclarationStmt(new VariablesDeclaration( getComponentType(), new VariableDeclInfo(getComponentName(), null, null)))); }