public ScopeEntry lookupScopeEntry(String name) { if (variableMapping.containsKey(name)) { return variableMapping.get(name); } if (parent != null) { return parent.lookupScopeEntry(name); } return null; }
public Type lookupType(String name) { ScopeEntry entry = lookupScopeEntry(name); if (entry == null) { return null; } return entry.getType(); }
@Override public void visitBlockStmt(BlockStmt stmt) { if (stmt == blockOfInterest) { inBlock = true; possiblyShadowedScopeEntry = currentScope.lookupScopeEntry(nameOfInterest); } super.visitBlockStmt(stmt); if (stmt == blockOfInterest) { inBlock = false; possiblyShadowedScopeEntry = null; } }
@Override public void visitVariableIdentifierExpr(VariableIdentifierExpr variableIdentifierExpr) { super.visitVariableIdentifierExpr(variableIdentifierExpr); final String name = variableIdentifierExpr.getName(); if (scopeOfStmt.lookupScopeEntry(name) != null && !referencedVars.contains(name)) { referencedVars.add(name); } }
@Override public void visitVariableIdentifierExpr(VariableIdentifierExpr variableIdentifierExpr) { super.visitVariableIdentifierExpr(variableIdentifierExpr); if (inBlock && possiblyShadowedScopeEntry != null && currentScope.lookupScopeEntry(variableIdentifierExpr.getName()) == possiblyShadowedScopeEntry) { ok = false; } }
@Override public void visitVariableIdentifierExpr(VariableIdentifierExpr variableIdentifierExpr) { super.visitVariableIdentifierExpr(variableIdentifierExpr); if (arrayIndexDepth > 0 && currentScope.lookupScopeEntry(variableIdentifierExpr.getName()) == null) { // A free variable that appears under an array index found = true; } }
private String applyVariableNameMapping(String name) { ScopeEntry scopeEntry = currentScope.lookupScopeEntry(name); if (scopeEntry == null) { return name; } if (scopeEntry.hasVariableDeclInfo()) { return varDeclMapping.getOrDefault(scopeEntry.getVariableDeclInfo(), name); } assert scopeEntry.hasParameterDecl(); return paramDeclMapping.getOrDefault(scopeEntry.getParameterDecl(), name); }
@Override public void visitVariableIdentifierExpr(VariableIdentifierExpr variableIdentifierExpr) { super.visitVariableIdentifierExpr(variableIdentifierExpr); ScopeEntry scopeEntry = currentScope.lookupScopeEntry(variableIdentifierExpr.getName()); if (scopeEntry != null) { addReferencedScopeEntry(scopeEntry); } }
@Override public void visitVariableIdentifierExpr(VariableIdentifierExpr variableIdentifierExpr) { super.visitVariableIdentifierExpr(variableIdentifierExpr); final ScopeEntry scopeEntry = currentScope.lookupScopeEntry(variableIdentifierExpr.getName()); if (scopeEntry != null && scopeEntry.hasParameterDecl()) { unusedParametersForCurrentFunction.remove(scopeEntry.getParameterDecl()); } }
@Override public void visitVariableIdentifierExpr(VariableIdentifierExpr variableIdentifierExpr) { super.visitVariableIdentifierExpr(variableIdentifierExpr); final ScopeEntry scopeEntry = currentScope.lookupScopeEntry(variableIdentifierExpr.getName()); if (scopeEntry != null && scopeEntry.hasVariableDeclInfo()) { // If this is a global, mark it as used. unusedGlobals.remove(scopeEntry.getVariableDeclInfo()); } }
private boolean isCurrentComponentVariable(String name) { ScopeEntry entry = currentScope.lookupScopeEntry(name); return entry != null && entry.hasVariableDeclInfo() && currentComponent.getVariableDeclInfo() == entry.getVariableDeclInfo(); }
private void getReductionOpportunitiesForUnusedGlobals() { for (String name : currentScope.keys()) { ScopeEntry entry = currentScope.lookupScopeEntry(name); assert entry.hasVariableDeclInfo(); assert referencedScopeEntries.peek() != null; if (!referencedScopeEntries.peek().contains(entry)) { addOpportunity(new VariableDeclReductionOpportunity(entry.getVariableDeclInfo(), entry.getVariablesDeclaration(), getVistitationDepth())); } } }
@Override public void visitVariableIdentifierExpr(VariableIdentifierExpr variableIdentifierExpr) { super.visitVariableIdentifierExpr(variableIdentifierExpr); final ScopeEntry se = currentScope.lookupScopeEntry(variableIdentifierExpr.getName()); if (se != null && se.hasVariableDeclInfo() && se.getVariableDeclInfo() == backupVdi) { found = true; } }
private boolean nonConst(VariableIdentifierExpr variableIdentifierExpr) { final ScopeEntry se = currentScope.lookupScopeEntry(variableIdentifierExpr.getName()); return se != null && se.hasVariablesDeclaration() && !se.getVariablesDeclaration().getBaseType() .hasQualifier(TypeQualifier.CONST); }
@Override public void visitVariableIdentifierExpr(VariableIdentifierExpr variableIdentifierExpr) { super.visitVariableIdentifierExpr(variableIdentifierExpr); if (parentMap.getParent(variableIdentifierExpr) instanceof MemberLookupExpr) { return; } if (!variableIdentifierExpr.getName().equals(vectorVariableDeclInfo.getName())) { return; } final ScopeEntry se = currentScope.lookupScopeEntry(variableIdentifierExpr.getName()); if (se != null && se.hasVariableDeclInfo() && se.getVariableDeclInfo() == vectorVariableDeclInfo) { vectorUsedDirectly = true; } }
@Override public void visitVariableIdentifierExpr(VariableIdentifierExpr variableIdentifierExpr) { super.visitVariableIdentifierExpr(variableIdentifierExpr); final ScopeEntry se = currentScope.lookupScopeEntry(variableIdentifierExpr.getName()); if (se == null || !se.hasVariableDeclInfo()) { return; } final VariableDeclInfo variableDeclInfo = se.getVariableDeclInfo(); if (!allowedToReduce(variableDeclInfo)) { return; } if (inLValueContext()) { if (!context.reduceEverywhere() && !currentProgramPointHasNoEffect()) { // The declaration is used as an l-value. To preserve semantics we cannot inline its // initializer. For example, in: // int x = 2; // x += 2; // x += x; // we end up with x == 8, but if we would inline the initializer to the r-value usage of // x we would get x == 6. blackList.add(variableDeclInfo); } return; } inlineableUsages.add(new ImmutablePair<>(variableDeclInfo, variableIdentifierExpr)); }
@Override public void visitVariableIdentifierExpr(VariableIdentifierExpr variableIdentifierExpr) { super.visitVariableIdentifierExpr(variableIdentifierExpr); ScopeEntry se = currentScope.lookupScopeEntry(variableIdentifierExpr.getName()); if (se == null) { // We are traversing a block in isolation, so we won't have a scope entry for any variable // declared outside the block. return; } if (se.getVariablesDeclaration() == declToTransform.getVariablesDeclaration()) { parentMap.getParent(variableIdentifierExpr).replaceChild( variableIdentifierExpr, structifiedExpr.clone()); } } }.visit(block);
@Override public void visitMemberLookupExpr(MemberLookupExpr memberLookupExpr) { super.visitMemberLookupExpr(memberLookupExpr); if (!(memberLookupExpr.getStructure() instanceof VariableIdentifierExpr)) { return; } VariableIdentifierExpr structVariable = ((VariableIdentifierExpr) memberLookupExpr .getStructure()); ScopeEntry se = currentScope.lookupScopeEntry(structVariable.getName()); if (se == null) { return; } if (se.getType().getWithoutQualifiers().equals(declaration.getVariablesDeclaration() .getBaseType().getWithoutQualifiers())) { // We've found the variable reference, but now we might have a chain, like: // s._f0._f2._f1._f0.v // We need to find the member expression that has .v and replace that with v MemberLookupExpr current = memberLookupExpr; while (current.getMember().startsWith(Constants.STRUCTIFICATION_FIELD_PREFIX)) { current = (MemberLookupExpr) parentMap.getParent(current); } parentMap.getParent(current).replaceChild( current, new VariableIdentifierExpr(originalVariableInfo.getName())); } } }.visit(tu);
private boolean isComponentAccess(MemberLookupExpr memberLookupExpr, Scope currentScope) { if (!(memberLookupExpr.getStructure() instanceof VariableIdentifierExpr)) { return false; } final String structureName = ((VariableIdentifierExpr) memberLookupExpr.getStructure()) .getName(); final ScopeEntry scopeEntry = currentScope.lookupScopeEntry(structureName); if (!isScopeEntryForVectorVariable(scopeEntry)) { return false; } // We've established that this is a lookup on the vector. // Now we need to look at the swizzle to see whether it hits the right components. int swizzleOffset = getOffsetFromSwizzle(memberLookupExpr.getMember()); int swizzleWidth = getWidthFromSwizzle(memberLookupExpr.getMember()); // This is the component we care about. There are now two scenarios: // (1) The swizzle was generated during vectorization, as a proxy for an original variable // (2) The swizzle was generated by our expression fuzzer (e.g. as part of identity // function // application // In case (1), we want to replace the swizzle with the original variable // In case (2), we should do nothing. // It's OK if we mistake case (2) for case (1), as arbitrary type-safe changes to fuzzed // expressions are OK. return swizzleOffset == componentData.getOffset() && swizzleWidth == componentData.getWidth(); }
@Override public void visitVariableIdentifierExpr(VariableIdentifierExpr variableIdentifierExpr) { super.visitVariableIdentifierExpr(variableIdentifierExpr); final String name = variableIdentifierExpr.getName(); final ScopeEntry se = currentScope.lookupScopeEntry(name); if (se == null) { return; } if (se.getType() == null) { return; } if (!se.getType().hasQualifier(TypeQualifier.UNIFORM)) { return; } if (se.getType().getWithoutQualifiers() instanceof BasicType && uniformsInfo.containsKey(name)) { final BasicType basicType = (BasicType) se.getType().getWithoutQualifiers(); addOpportunity(new SimplifyExprReductionOpportunity( parentMap.getParent(variableIdentifierExpr), PruneUniforms.getBasicTypeLiteralExpr(basicType, uniformsInfo.getArgs(name)), variableIdentifierExpr, getVistitationDepth())); } }