void normalizeTraversal::visit(SgNode* n) { SgBinaryOp* binaryOp = isSgBinaryOp(n); if(binaryOp != NULL) { ROSE_ASSERT(binaryOp); /* a + (b * c) ===> (b * c) + a */ if(binaryOp->variantT() == V_SgAddOp) { if(binaryOp->get_rhs_operand()->variantT() == V_SgMultiplyOp) { swapOperands(binaryOp); } else if(binaryOp->get_rhs_operand()->variantT() == V_SgCastExp) { SgCastExp* castExp = isSgCastExp(binaryOp->get_rhs_operand()); if(castExp->get_operand()->variantT() == V_SgMultiplyOp) swapOperands(binaryOp); } } /* a - (b * c) ===> -((b * c) - a) */ else if(binaryOp->variantT() == V_SgSubtractOp) { if(binaryOp->get_rhs_operand()->variantT() == V_SgMultiplyOp) { swapOperands(binaryOp); SgMinusOp* minusOp = buildMinusOp(deepCopy(binaryOp), SgUnaryOp::prefix); replaceExpression(binaryOp, minusOp, false); } else if(binaryOp->get_rhs_operand()->variantT() == V_SgCastExp) { SgCastExp* castExp = isSgCastExp(binaryOp->get_rhs_operand()); if(castExp->get_operand()->variantT() == V_SgMultiplyOp) { swapOperands(binaryOp); SgMinusOp* minusOp = buildMinusOp(deepCopy(binaryOp), SgUnaryOp::prefix); replaceExpression(binaryOp, minusOp, false); } } } } }
SgExpression* Fortran_to_C::foldBinaryOp(SgExpression* inputExpression) { SgExpression* foldedExp; if(isSgBinaryOp(inputExpression)) { SgBinaryOp* binOp = isSgBinaryOp(inputExpression); SgIntVal* rhs = isSgIntVal(binOp->get_rhs_operand()); if(!rhs) return inputExpression; SgIntVal* lhs = isSgIntVal(binOp->get_lhs_operand()); if(!lhs) return inputExpression; int foldedVal; switch(binOp->variantT()) { case V_SgAddOp: { foldedVal = lhs->get_value() + rhs->get_value(); break; } case V_SgSubtractOp: { foldedVal = lhs->get_value() - rhs->get_value(); break; } case V_SgMultiplyOp: { foldedVal = lhs->get_value() * rhs->get_value(); break; } default: { cerr<<"warning: calculuate - unhandled operator type:"<<binOp->class_name() << endl; return inputExpression; } } foldedExp = buildIntVal(foldedVal); } else return inputExpression; return foldedExp; }
static void CreateLoopProbe(SgFunctionDeclaration *funcDecl, SgBasicBlock *funcBody, SgStatement *stmt, int *loopCount) { SgExpression *lb ; SgExpression *ub ; SgExpression *step ; bool isCanonicalFor = SageInterface::isCanonicalForLoop(stmt, NULL, &lb, &ub, &step) ; /* generate loop segment, and iteration information if requested */ if (countIters && !isCanonicalFor) { Sg_File_Info *fileInfo = stmt->get_startOfConstruct() ; printf("Non-canonical loop in %s:::%s at line %d\n", fileInfo->get_filenameString().c_str(), funcDecl->get_name().str(), fileInfo->get_line()) ; } else { SgExpression *its = NULL ; if (countIters && isCanonicalFor) { bool isDecreasing = false ; bool adjustBound = false ; SgExpression *high ; SgExpression *max ; SgExpression *minuend ; SgExpression *subtrahend ; SgExpression *tmp ; SgForStatement* fs = isSgForStatement(stmt) ; ROSE_ASSERT(fs) ; SgBinaryOp* test = isSgBinaryOp(fs->get_test_expr()); ROSE_ASSERT(test) ; switch (test->variantT()) { case V_SgLessOrEqualOp: adjustBound = true ; case V_SgLessThanOp: break; case V_SgGreaterOrEqualOp: adjustBound = true ; case V_SgGreaterThanOp: // tmp = lb ; // lb = ub ; // ub = tmp ; isDecreasing = true ; break; // case V_SgNotEqualOp: // Do we really want to allow this != operator ? // break; default: /* do nothing */ ; } if (adjustBound) { high = buildAddOp(SageInterface::copyExpression(ub), buildIntVal(isDecreasing ? -1 : 1)) ; } else { high = SageInterface::copyExpression(ub) ; } /* if step != const 1, we need to adjust high */ if (!(isSgIntVal(step) && (isSgIntVal(step)->get_value() == 1))) { max = buildAddOp(high, buildSubtractOp(SageInterface::copyExpression(step), buildIntVal(1))) ; } else { max = high ; } minuend = max ; subtrahend = SageInterface::copyExpression(lb) ; #if 0 if (isDecreasing) { SgExpression *tmp ; tmp = minuend ; minuend = subtrahend ; subtrahend = tmp ; } #endif /* The control logic is purely to clean up the output, since */ /* the compiler can do a fine job of folding integer constansts */ its = buildDivideOp(buildSubtractOp(minuend, subtrahend), SageInterface::copyExpression(step)) ; #if 0 if (!(isSgIntVal(step) && (isSgIntVal(step)->get_value() == 1))) { its = buildDivideOp(buildSubtractOp(minuend, subtrahend), SageInterface::copyExpression(step)) ; } else { if (!(isSgIntVal(subtrahend) && (isSgIntVal(subtrahend)->get_value() == 0))) { its = buildSubtractOp(minuend, subtrahend) ; } else { its = minuend ; } } #endif } /* start loop code fragment */ SgVariableDeclaration *loopDecl = ET_BuildDecl(funcBody, "ET_loop", *loopCount, ETtype, true) ; RegisterItem(funcDecl, funcBody, stmt, "ET_RegisterLoop", ETtype, loopDecl, true) ; HandleItem(funcBody, stmt, "ET_PushLoopSeqId", buildVarRefExp(loopDecl), true) ; /* end loop code fragment */ HandleItem(funcBody, stmt, "ET_PopLoopSeqId", its, false) ; ++(*loopCount) ; } }
//! Handle +=, -=, */, etc vector<SgExpression*> RedefineValueRestorer::handleUseAssignDefinition(VariableRenaming::VarName destroyedVarName, const VariableVersionTable& availableVariables, SgNode* reachingDefinition) { vector<SgExpression*> results; ROSE_ASSERT(SageInterface::isAssignmentStatement(reachingDefinition) && !isSgAssignOp(reachingDefinition)); //These are all the assignments that also use the left value in addition to the right. //E.g. /=, *=, &=, etc. We restore both the left and the right operands to their pre-assignment version SgBinaryOp* assignment = isSgBinaryOp(reachingDefinition); ROSE_ASSERT(VariableRenaming::getVarName(assignment->get_lhs_operand()) == destroyedVarName); //Find the version of the variable before the assignment VariableRenaming::NumNodeRenameEntry lhsVersion = getEventHandler()->getVariableRenaming()->getReachingDefsAtNodeForName(assignment->get_lhs_operand(), destroyedVarName); //Restore the left-hand side and the right-hand side SgExpression* lhsValue = getEventHandler()->restoreVariable(destroyedVarName, availableVariables, lhsVersion); SgExpression* rhsValue = getEventHandler()->restoreExpressionValue(assignment->get_rhs_operand(), availableVariables); //Combine the lhs and rhs according to the assignment type to restore the value if (lhsValue != NULL && rhsValue != NULL) { switch (assignment->variantT()) { case V_SgPlusAssignOp: results.push_back(SageBuilder::buildAddOp(lhsValue, rhsValue)); break; case V_SgMinusAssignOp: results.push_back(SageBuilder::buildSubtractOp(lhsValue, rhsValue)); break; case V_SgMultAssignOp: results.push_back(SageBuilder::buildMultiplyOp(lhsValue, rhsValue)); break; case V_SgDivAssignOp: results.push_back(SageBuilder::buildDivideOp(lhsValue, rhsValue)); break; case V_SgModAssignOp: results.push_back(SageBuilder::buildModOp(lhsValue, rhsValue)); break; case V_SgIorAssignOp: results.push_back(SageBuilder::buildBitOrOp(lhsValue, rhsValue)); break; case V_SgAndAssignOp: results.push_back(SageBuilder::buildBitAndOp(lhsValue, rhsValue)); break; case V_SgXorAssignOp: results.push_back(SageBuilder::buildBitXorOp(lhsValue, rhsValue)); break; case V_SgLshiftAssignOp: results.push_back(SageBuilder::buildLshiftOp(lhsValue, rhsValue)); break; case V_SgRshiftAssignOp: results.push_back(SageBuilder::buildRshiftOp(lhsValue, rhsValue)); break; default: ROSE_ASSERT(false); } } else { //One of the restorations did not succeed, delete the trees if (lhsValue != NULL) { SageInterface::deepDelete(lhsValue); } if (rhsValue != NULL) { SageInterface::deepDelete(rhsValue); } } return results; }
string nodeColor( SgExpression* expression ) { /* color: colorCode:red:on color: colorCode:orange:on color: colorCode:yellow:on color: colorCode:blue:on color: colorCode:green:on color: colorCode:violet:on color: colorCode:brown:on color: colorCode:purple:on color: colorCode:lightblue:on color: colorCode:lightgreen:on color: colorCode:lightred:on color: colorCode:black:on color: colorCode:darkblue:on color: colorCode:grey:on color: colorCode:darkgrey:on color: colorCode:olivegreen:on color: colorCode:darkgreen:on */ string returnString; SgBinaryOp* binaryOperator = isSgBinaryOp(expression); if (binaryOperator != NULL) { switch (binaryOperator->variantT()) { case V_SgAddOp: case V_SgAndAssignOp: case V_SgAndOp: case V_SgArrowExp: case V_SgArrowStarOp: case V_SgAssignOp: case V_SgBitAndOp: case V_SgBitOrOp: case V_SgBitXorOp: case V_SgCommaOpExp: case V_SgDivAssignOp: case V_SgDivideOp: case V_SgDotExp: case V_SgDotStarOp: case V_SgMinusAssignOp: case V_SgModAssignOp: case V_SgModOp: case V_SgMultAssignOp: case V_SgMultiplyOp: case V_SgNotEqualOp: case V_SgOrOp: case V_SgPlusAssignOp: case V_SgPntrArrRefExp: case V_SgScopeOp: case V_SgSubtractOp: case V_SgXorAssignOp: returnString = "orange"; break; case V_SgEqualityOp: case V_SgGreaterOrEqualOp: case V_SgGreaterThanOp: case V_SgIntegerDivideOp: case V_SgIorAssignOp: case V_SgLessOrEqualOp: case V_SgLessThanOp: returnString = "yellow"; break; case V_SgLshiftAssignOp: case V_SgLshiftOp: case V_SgRshiftAssignOp: case V_SgRshiftOp: returnString = "lightred"; break; default: returnString = "ERROR DEFAULT REACHED"; printf ("Default reached in nodeColor() exiting ... (%s) \n",binaryOperator->class_name().c_str()); ROSE_ASSERT(false); break; } } SgUnaryOp* unaryOperator = isSgUnaryOp(expression); if (unaryOperator != NULL) { switch (unaryOperator->variantT()) { case V_SgAddressOfOp: case V_SgBitComplementOp: case V_SgPointerDerefExp: case V_SgThrowOp: case V_SgUnaryAddOp: returnString = "lightblue"; break; case V_SgMinusMinusOp: case V_SgMinusOp: case V_SgNotOp: case V_SgPlusPlusOp: returnString = "darkblue"; break; case V_SgCastExp: case V_SgExpressionRoot: returnString = "black"; break; default: returnString = "ERROR DEFAULT REACHED"; printf ("Default reached in nodeColor() exiting ... (%s) \n",unaryOperator->class_name().c_str()); ROSE_ASSERT(false); break; } } SgInitializer* initializer = isSgInitializer(expression); if (initializer != NULL) { switch (initializer->variantT()) { case V_SgAggregateInitializer: case V_SgAssignInitializer: case V_SgConstructorInitializer: returnString = "lightblue"; break; default: returnString = "ERROR DEFAULT REACHED"; printf ("Default reached in nodeColor() exiting ... (%s) \n",initializer->class_name().c_str()); ROSE_ASSERT(false); break; } } SgValueExp* valueExpression = isSgValueExp(expression); if (valueExpression != NULL) { switch (valueExpression->variantT()) { case V_SgComplexVal: case V_SgIntVal: case V_SgLongIntVal: case V_SgLongLongIntVal: case V_SgShortVal: case V_SgUnsignedCharVal: case V_SgUnsignedIntVal: case V_SgUnsignedLongLongIntVal: case V_SgUnsignedLongVal: case V_SgUnsignedShortVal: returnString = "lightblue"; break; case V_SgFloatVal: case V_SgDoubleVal: case V_SgLongDoubleVal: returnString = "darkblue"; break; case V_SgEnumVal: case V_SgBoolValExp: case V_SgCharVal: case V_SgWcharVal: case V_SgStringVal: returnString = "black"; break; // DQ (11/11/2012): Added support for newer IR nodes in edg4x work. case V_SgTemplateParameterVal: returnString = "red"; break; default: returnString = "ERROR DEFAULT REACHED"; printf ("Default reached in nodeColor() exiting ... (%s) \n",valueExpression->class_name().c_str()); ROSE_ASSERT(false); break; } } if (binaryOperator == NULL && unaryOperator == NULL && initializer == NULL && valueExpression == NULL) { switch (expression->variantT()) { case V_SgFunctionCallExp: case V_SgFunctionRefExp: case V_SgMemberFunctionRefExp: case V_SgPseudoDestructorRefExp: returnString = "violet"; break; case V_SgAsmOp: case V_SgClassNameRefExp: case V_SgConditionalExp: case V_SgDeleteExp: case V_SgExprListExp: case V_SgNewExp: case V_SgNullExpression: case V_SgRefExp: case V_SgSizeOfOp: case V_SgStatementExpression: case V_SgThisExp: case V_SgTypeIdOp: case V_SgVarArgCopyOp: case V_SgVarArgEndOp: case V_SgVarArgOp: case V_SgVarArgStartOneOperandOp: case V_SgVarArgStartOp: case V_SgVariantExpression: case V_SgVarRefExp: returnString = "brown"; break; // DQ (1/23/2013): Added support for newer IR nodes in edg4x work. case V_SgTemplateMemberFunctionRefExp: case V_SgTemplateFunctionRefExp: returnString = "brown"; break; default: returnString = "ERROR DEFAULT REACHED"; printf ("Default reached in nodeColor() exiting ... (%s) \n",expression->class_name().c_str()); ROSE_ASSERT(false); break; } } return returnString; }