コード例 #1
0
ファイル: LoopPermute.java プロジェクト: troore/scale
  public Cost computeRefCost(LoopHeaderChord loop, SubscriptExpr se) {
    Cost tripL = loop.getTripCount();
    InductionVar ivar = loop.getPrimaryInductionVar();
    if (ivar == null) return tripL;

    // Compute
    // tripL = (ubL - lbL + stepL) / stepL

    int nsubs = se.numSubscripts(); // number of subscripts in the subscript
    LoopHeaderChord tloop = loop.getTopLoop();
    VariableDecl iv = ivar.getVar();

    for (int i = 0; i < nsubs - 1; i++) { // for c code
      // If the loop index variable of this loop appears in any of the
      // subscripts other then the last, then return tripL.
      Expr sI = se.getSubscript(i);
      AffineExpr ae = tloop.isAffine(sI);

      if (ae == null) return null;

      if (ae.hasTerm(iv)) return tripL;
    }

    int fs = nsubs - 1;
    Expr s0 = se.getSubscript(fs);
    AffineExpr ae = tloop.isAffine(s0);
    if (ae == null) return null;

    int at = ae.getTermIndexOrig(iv);
    long coeff = 0;
    if (at >= 0) coeff = ae.getCoefficient(at);

    if (coeff == 0) // Invariant Reuse.
    return new Cost(1.0, 0);

    long stepL = loop.getStepValue();
    long stride = stepL * coeff;
    if (stride < 0) stride = -stride;

    // Unit Reuse.

    Type et = se.getCoreType().getPointedTo();
    int bs = et.memorySizeAsInt(Machine.currentMachine);
    int cs = Machine.currentMachine.getCacheSize(bs);
    if (stride <= cs) { // cache line or block size
      tripL.multiply(stride);
      tripL.divide(cs);
      return tripL;
    }

    // No Reuse.

    return tripL;
  }