@Override public boolean preconditionHolds() { if (!blockStmt.getStmts().contains(child)) { // Some other reduction opportunity must have removed the statement already return false; } return true; } }
@Override public void visitBlockStmt(BlockStmt stmt) { // We make a shallow copy of the children of the block statement, to allow a subclass of the // visitor to potentially add or remove children of the original block statement. List<Stmt> children = new ArrayList<>(); children.addAll(stmt.getStmts()); for (Stmt child : children) { visitChildFromParent(child, stmt); } }
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); }
private List<VariableDeclInfo> getAllVariableDeclInfosInBlock() { return block.getStmts() .stream() .filter(item -> item instanceof DeclarationStmt) .map(item -> (DeclarationStmt) item) .map(item -> item.getVariablesDeclaration()) .map(item -> item.getDeclInfos()) .reduce(Arrays.asList(), ListConcat::concatenate); }
private static Optional<String> containsOutVariableBackup(BlockStmt block) { return block.getStmts().stream() .filter(item -> item instanceof DeclarationStmt) .map(item -> (DeclarationStmt) item) .filter(LiveOutputVariableWriteReductionOpportunities::isOutVariableBackup) .findFirst() .map(vdi -> vdi.getVariablesDeclaration().getDeclInfos() .stream().map(item -> item.getName()).findFirst().get()); }
private void checkForLoopMergeReductionOpportunities(BlockStmt block) { Optional<Stmt> prev = Optional.empty(); for (Stmt current : block.getStmts()) { if (prev.isPresent() && canMergeLoops(prev.get(), current)) { opportunities.add(new LoopMergeReductionOpportunity((ForStmt) prev.get(), (ForStmt) current, block, getVistitationDepth())); } prev = Optional.of(current); } }
private boolean blockAlreadyDeclaresVector() { // When we do multiple transformation passes, the same vectorization attempt might be applied // multiple times. If we would create a vector whose name is already declared in this block, // we bail out. return block.getStmts().stream().filter(item -> item instanceof DeclarationStmt) .map(item -> ((DeclarationStmt) item).getVariablesDeclaration().getDeclInfos()) .reduce(new ArrayList<>(), ListConcat::concatenate) .stream() .anyMatch(item -> item.getName().equals(mergeSet.getMergedName())); }
@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); }
private boolean replacementVariableNameDeclaredInThisBlock() { return block.getStmts().stream() .filter(item -> item instanceof DeclarationStmt) .map(item -> (DeclarationStmt) item) .map(item -> item.getVariablesDeclaration()) .map(item -> item.getDeclInfos()) .reduce(Arrays.asList(), ListConcat::concatenate) .stream() .anyMatch(item -> item.getName().equals(findOriginalVariableInfo().getName())); }
@Override public void visitBlockStmt(BlockStmt blockStmt) { super.visitBlockStmt(blockStmt); final List<List<Stmt>> regionStack = new ArrayList<>(); regionStack.add(new ArrayList<>()); for (Stmt stmt : blockStmt.getStmts()) { addToCurrentRegion(regionStack, stmt); if (containsReturn(stmt)) { regionStack.add(new ArrayList<>()); } } blockStmt.setStmts(regionStackToStmts(regionStack)); }
/** * Determines whether the given statement came from a live code injection. * * @param stmt A statement to be analysed * @return Whether the statement is injected live code or not */ public static boolean isLiveCodeInjection(Stmt stmt) { if (isSimpleLiveCodeInjection(stmt)) { return true; } if (stmt instanceof BlockStmt) { return ((BlockStmt) stmt).getStmts().stream().anyMatch( item -> isLiveCodeVariableDeclaration(item) || isSimpleLiveCodeInjection(item)); } return false; }
@Override public boolean preconditionHolds() { if (!enclosingBlock.getStmts().contains(secondLoop)) { return false; } if (!(firstLoop.getIncrement() instanceof UnaryExpr)) { return false; } if (!(((UnaryExpr) firstLoop.getIncrement()).getExpr() instanceof VariableIdentifierExpr)) { return false; } return true; }
@Override public void visitBlockStmt(BlockStmt block) { enterBlockStmt(block); checkForLoopMergeReductionOpportunities(block); for (Stmt child : block.getStmts()) { visit(child); } leaveBlockStmt(block); }
@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); }
@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 stmt) { out.append(indent() + "{" + newLine()); increaseIndent(); for (Stmt s : stmt.getStmts()) { visit(s); } decreaseIndent(); out.append(indent() + "}" + newLine()); }
@Override protected void visitChildOfBlock(BlockStmt block, Stmt child) { if (isNonEmptyBlockStmtWithoutTopLevelDeclaration(child)) { addOpportunity( new UnwrapReductionOpportunity(child, ((BlockStmt) child).getStmts(), parentMap, getVistitationDepth())); } }
@Override public void visitBlockStmt(BlockStmt stmt) { for (Stmt child : stmt.getStmts()) { if (ReductionOpportunitiesBase.isDeadCodeInjection(child)) { injectionTracker.enterDeadCodeInjection(); } visit(child); if (ReductionOpportunitiesBase.isDeadCodeInjection(child)) { injectionTracker.exitDeadCodeInjection(); } } }
@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 visitBlockStmt(BlockStmt block) { enterBlockStmt(block); for (Stmt child : block.getStmts()) { visitChildOfBlock(block, child); if (child instanceof BreakStmt && parentMap.getParent(block) instanceof SwitchStmt) { injectionTracker.notifySwitchBreak(); } if (isDeadCodeInjection(child)) { injectionTracker.enterDeadCodeInjection(); } visit(child); if (isDeadCodeInjection(child)) { injectionTracker.exitDeadCodeInjection(); } } leaveBlockStmt(block); }