示例#1
0
AstNodePtr SymbolicPlus::
CodeGenOP( AstInterface &fa, const AstNodePtr& a1, const AstNodePtr& a2) const
 { 
    AstNodePtr opd;
    AstInterface::OperatorEnum opr;
    if (fa.IsUnaryOp(a2, &opr, &opd) && opr == AstInterface::UOP_MINUS) {
      return fa.CreateBinaryOP(AstInterface::BOP_MINUS,a1, fa.CopyAstTree(opd));
    }
    else if (fa.IsUnaryOp(a1, &opr, &opd) && opr == AstInterface::UOP_MINUS) {
      return fa.CreateBinaryOP(AstInterface::BOP_MINUS,a2, fa.CopyAstTree(opd));
    }
    return fa.CreateBinaryOP(AstInterface::BOP_PLUS, a1, a2); 
 }
示例#2
0
AstNodePtr SymbolicAstWrap::CodeGen( AstInterface &fa) const
      { 
         if (codegen == 0)
             return fa.CopyAstTree(ast);
         else
             return (*codegen)(&fa, ast);
      }
示例#3
0
bool LoopUnrolling::operator() ( AstInterface& fa, const AstNodePtr& s, AstNodePtr& r)
{
   bool isLoop = false;
   if (enclosingloop == s || (enclosingloop == AST_NULL && (isLoop = fa.IsLoop(s)))) {
       for (enclosingloop = fa.GetParent(s); 
            enclosingloop != AST_NULL && !fa.IsLoop(enclosingloop); 
            enclosingloop = fa.GetParent(enclosingloop));
       if (!isLoop)
          return false;
   }

   AstNodePtr body;
   SymbolicVal stepval, ubval, lbval;
   SymbolicVar ivar;
   if (!SymbolicValGenerator::IsFortranLoop(fa, s, &ivar, &lbval, &ubval, &stepval, &body)) 
      return false; 
    
   if (opt & POET_TUNING) {
     AutoTuningInterface* tune = LoopTransformInterface::getAutoTuningInterface();
     if (tune == 0) {
        std::cerr << "ERROR: AutoTuning Interface not defined!\n";
        assert(0);
     }
     tune->UnrollLoop(fa,s, unrollsize);
   }
   else {
          AstNodePtr r = s;
          SymbolicVal nstepval = stepval * unrollsize;
          SymbolicVal nubval = ubval;

          bool hasleft = true, negativeStep = (stepval < 0);
          std::vector<AstNodePtr> bodylist;
          AstNodePtr leftbody, lefthead;

          int stepnum=0, loopnum = 0;
          SymbolicVal loopval = ubval - lbval + 1;
          if (stepval.isConstInt(stepnum) && loopval.isConstInt(loopnum) 
               && !(loopnum % stepnum)) {
             hasleft = false; 
          }
          else {
             nubval = ubval - SymbolicVal(unrollsize - 1);
             if (opt & COND_LEFTOVER) {
                 leftbody = fa.CreateBlock();
                 lefthead = leftbody;
             }
             else {
                 leftbody = fa.CopyAstTree(body);
                 lefthead = fa.CreateLoop( ivar.CodeGen(fa), 
                                           AstNodePtr(), 
                                           ubval.CodeGen(fa), 
                                           stepval.CodeGen(fa), leftbody,
                                           negativeStep);
             }
          }
          fa.RemoveStmt(body);
          AstNodePtr s1 = fa.CreateLoop(ivar.CodeGen(fa), lbval.CodeGen(fa),
                                          nubval.CodeGen(fa), 
                                          nstepval.CodeGen(fa), body,
                                           negativeStep);
          fa.ReplaceAst( s,s1);
          r = s1; 

          AstNodePtr origbody = fa.CopyAstTree(body);
          std::string nvarname = "";
          SymbolicVal nvar;
          if (opt & USE_NEWVAR) {
               nvarname = fa.NewVar(fa.GetType("int"),"",true,body, ivar.CodeGen(fa)); 
               nvar = SymbolicVar(nvarname,body);
          }
          bodylist.push_back(body);
          for (int i = 1; i < unrollsize; ++i) {
              AstNodePtr bodycopy = fa.CopyAstTree(origbody);
              if (opt & USE_NEWVAR) {
                 AstNodePtr nvarassign = 
                     fa.CreateAssignment(nvar.CodeGen(fa), (nvar+1).CodeGen(fa));
                 fa.BlockAppendStmt( body, nvarassign);
                 AstTreeReplaceVar repl(ivar, nvar);
                 repl( fa, bodycopy);
              }
              else {
                 AstTreeReplaceVar repl(ivar, ivar+i);
                 repl( fa, bodycopy);
              }
              fa.BlockAppendStmt( body, bodycopy);
              bodylist.push_back(bodycopy);
              if (hasleft && (opt & COND_LEFTOVER)) {
                 AstNodePtr cond = 
                      fa.CreateBinaryOP( AstInterface::BOP_LE, ivar.CodeGen(fa), (ubval-(i-1)).CodeGen(fa));
                 AstNodePtr body1 = fa.CopyAstTree(bodylist[i-1]);
                 AstNodePtr ifstmt =  fa.CreateIf( cond, body1);
                 fa.BlockAppendStmt( leftbody, ifstmt);
                 leftbody = body1;
              }
          }
          if (hasleft) {
              fa.InsertStmt( r, lefthead, false, true);
          }
          r  = s;
          return true;
   }
   return false;
}