private int findRangeAndAdjust(NormalSsaInsn insn) {
    RegisterSpecList sources = insn.getSources();
    int szSources = sources.size();
    // the category for each source index
    int[] categoriesForIndex = new int[szSources];
    int rangeLength = 0;

    // Compute rangeLength and categoriesForIndex
    for (int i = 0; i < szSources; i++) {
      int category = sources.get(i).getCategory();
      categoriesForIndex[i] = category;
      rangeLength += categoriesForIndex[i];
    }

    // the highest score of fits tried so far
    int maxScore = Integer.MIN_VALUE;
    // the high scoring range's start
    int resultRangeStart = -1;
    // by source index: set of sources needing moves in high scoring plan
    BitSet resultMovesRequired = null;

    /*
     * First, go through each source that's already been mapped. Try
     * to center the range around the rop register this source is mapped
     * to.
     */
    int rangeStartOffset = 0;
    for (int i = 0; i < szSources; i++) {
      int ssaCenterReg = sources.get(i).getReg();

      if (i != 0) {
        rangeStartOffset -= categoriesForIndex[i - 1];
      }
      if (!ssaRegsMapped.get(ssaCenterReg)) {
        continue;
      }

      int rangeStart = mapper.oldToNew(ssaCenterReg) + rangeStartOffset;

      if (rangeStart < 0 || spansParamRange(rangeStart, rangeLength)) {
        continue;
      }

      BitSet curMovesRequired = new BitSet(szSources);

      int fitWidth = fitPlanForRange(rangeStart, insn, categoriesForIndex, curMovesRequired);

      if (fitWidth < 0) {
        continue;
      }

      int score = fitWidth - curMovesRequired.cardinality();

      if (score > maxScore) {
        maxScore = score;
        resultRangeStart = rangeStart;
        resultMovesRequired = curMovesRequired;
      }

      if (fitWidth == rangeLength) {
        // We can't do any better than this, so stop here
        break;
      }
    }

    /*
     * If we were unable to find a plan for a fit centered around
     * an already-mapped source, just try to find a range of
     * registers we can move the range into.
     */

    if (resultRangeStart == -1) {
      resultMovesRequired = new BitSet(szSources);

      resultRangeStart =
          findAnyFittingRange(insn, rangeLength, categoriesForIndex, resultMovesRequired);
    }

    /*
     * Now, insert any moves required.
     */

    for (int i = resultMovesRequired.nextSetBit(0);
        i >= 0;
        i = resultMovesRequired.nextSetBit(i + 1)) {
      insn.changeOneSource(i, insertMoveBefore(insn, sources.get(i)));
    }

    return resultRangeStart;
  }
Find a contiguous rop register range that fits the specified instruction's sources. First, try
to center the range around sources that have already been mapped to rop registers. If that
fails, just find a new contiguous range that doesn't interfere.

@param insn {@code non-null;} the insn whose sources need to fit. Must be last insn in basic
    block.
@return {@code >= 0;} rop register of start of range
android/android_framework/dex-maker/src/main/java/com/android/dx/ssa/back/FirstFitLocalCombiningAllocator.java