ForLoopsLowering: Reduce unnecessary temporary variables for the

"checked step" (check for a positive step arg) and "negated step"
(negate the step arg when the nested step is negative).
This commit is contained in:
Mark Punzalan
2020-07-16 00:42:36 -07:00
committed by Alexander Udalov
parent 291d62f653
commit b1ce21bc55
29 changed files with 153 additions and 173 deletions
@@ -15,12 +15,11 @@ fun box(): String {
// // Standard form of loop over progression
// var inductionVar = 2
// val last = getProgressionLastElement(2, Int.MIN_VALUE - 1, 2) // `(Int.MIN_VALUE - 1)` underflows to Int.MAX_VALUE
// val step = 2
// if (false && inductionVar <= last) { // `false` comes from constant folding of `Int.MIN_VALUE != Int.MIN_VALUE`
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// inductionVar += 2
// // Loop body
// } while (i != last)
// }
@@ -11,18 +11,17 @@ fun box(): String {
//
// Expected lowered form of loop (before bytecode optimization):
//
// // Additional variables:
// val newStep = throw IllegalArgumentException("Step must be positive, was: 0.")
// // Additional statements:
// throw IllegalArgumentException("Step must be positive, was: 0.")
//
// // Standard form of loop over progression
// var inductionVar = 1
// val last = getProgressionLastElement(1, 6, newStep)
// val step = newStep
// val last = getProgressionLastElement(1, 6, 0)
// if (inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// inductionVar += 0
// // Loop body
// } while (i != last)
// }
@@ -15,12 +15,11 @@ fun box(): String {
// // Standard form of loop over progression
// val last = getProgressionLastElement(8, 1, -2)
// var inductionVar = 8
// val step = -2
// if (last <= inductionVar) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// inductionVar += -2
// // Loop body
// } while (i != last)
// }
@@ -15,12 +15,11 @@ fun box(): String {
// // Standard form of loop over progression
// var inductionVar = 1
// val last = getProgressionLastElement(1, 7, 2)
// val step = 2
// if (inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// inductionVar += 2
// // Loop body
// } while (i != last)
// }
@@ -14,22 +14,21 @@ fun box(): String {
//
// Expected lowered form of loop:
//
// // Additional variables:
// // Additional statements:
// val progression = intProgression
// val nestedFirst = progression.first
// val nestedLast = progression.last
// val nestedStep = progression.step
// val newStep = if (nestedStep > 0) 2 else -2
// val maybeNegatedStep = if (nestedStep <= 0) -2 else 2
//
// // Standard form of loop over progression
// var inductionVar = nestedFirst
// val last = getProgressionLastElement(nestedFirst, nestedLast, newStep)
// val step = newStep
// if ((step > 0 && inductionVar <= last) || (step < 0 && last <= inductionVar)) {
// val last = getProgressionLastElement(nestedFirst, nestedLast, maybeNegatedStep)
// if ((maybeNegatedStep > 0 && inductionVar <= last) || (maybeNegatedStep < 0 && last <= inductionVar)) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// inductionVar += maybeNegatedStep
// // Loop body
// } while (i != last)
// }
@@ -46,7 +45,8 @@ fun box(): String {
// 1 IF_ICMPGT
// 1 IF_ICMPLE
// 1 IF_ICMPNE
// 2 IFLE
// 1 IFLE
// 1 IFGT
// 1 IFGE
// 6 IF
// 0 INEG
@@ -13,20 +13,18 @@ fun box(): String {
//
// Expected lowered form of loop:
//
// // Additional variables:
// val stepArg = one()
// val newStep = if (stepArg > 0) stepArg
// else throw IllegalArgumentException("Step must be positive, was: $stepArg.")
// // Additional statements:
// var stepArg = one()
// if (stepArg <= 0) throw IllegalArgumentException("Step must be positive, was: $stepArg.")
//
// // Standard form of loop over progression
// var inductionVar = 1
// val last = getProgressionLastElement(1, 7, newStep)
// val step = newStep
// val last = getProgressionLastElement(1, 7, stepArg)
// if (inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// inductionVar += stepArg
// // Loop body
// } while (i != last)
// }
@@ -42,5 +40,5 @@ fun box(): String {
// 1 ATHROW
// 1 IF_ICMPGT
// 1 IF_ICMPNE
// 1 IFLE
// 1 IFGT
// 3 IF
@@ -15,25 +15,23 @@ fun box(): String {
//
// Expected lowered form of loop:
//
// // Additional variables:
// // Additional statements:
// val progression = intProgression
// val nestedFirst = progression.first
// val nestedLast = progression.last
// val nestedStep = progression.step
// val stepArg = one()
// val checkedStep = if (stepArg > 0) stepArg
// else throw IllegalArgumentException("Step must be positive, was: $stepArg.")
// val newStep = if (nestedStep > 0) checkedStep else -checkedStep
// var stepArg = one()
// if (stepArg <= 0) throw IllegalArgumentException("Step must be positive, was: $stepArg.")
// if (nestedStep <= 0) stepArg = -stepArg
//
// // Standard form of loop over progression
// var inductionVar = nestedFirst
// val last = getProgressionLastElement(nestedFirst, nestedLast, newStep)
// val step = newStep
// if ((step > 0 && inductionVar <= last) || (step < 0 && last <= inductionVar)) {
// val last = getProgressionLastElement(nestedFirst, nestedLast, stepArg)
// if ((stepArg > 0 && inductionVar <= last) || (stepArg < 0 && last <= inductionVar)) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// inductionVar += stepArg
// // Loop body
// } while (i != last)
// }
@@ -50,7 +48,8 @@ fun box(): String {
// 1 IF_ICMPGT
// 1 IF_ICMPLE
// 1 IF_ICMPNE
// 3 IFLE
// 1 IFLE
// 2 IFGT
// 1 IFGE
// 7 IF
// 1 INEG
@@ -14,12 +14,11 @@ fun box(): String {
// // Standard form of loop over progression
// var inductionVar = 1
// val last = 4
// val step = 1
// if (inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// inductionVar += 1
// // Loop body
// } while (inductionVar <= last)
// }
@@ -14,12 +14,11 @@ fun box(): String {
// // Standard form of loop over progression
// var inductionVar = 1
// val last = 4
// val step = 1
// if (inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// inductionVar += 1
// // Loop body
// } while (inductionVar <= last)
// }
@@ -19,12 +19,11 @@ fun box(): String {
// // Standard form of loop over progression
// var inductionVar = 1
// val last = getProgressionLastElement(1, outerNestedLast, 2)
// val step = 2
// if (inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// inductionVar += 2
// // Loop body
// } while (i != last)
// }
@@ -15,12 +15,11 @@ fun box(): String {
// // Standard form of loop over progression
// val last = 1
// var inductionVar = getProgressionLastElement(1, 8, 2)
// val step = -2
// if (last <= inductionVar) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// inductionVar += -2
// // Loop body
// } while (last <= inductionVar)
// }
@@ -16,12 +16,11 @@ fun box(): String {
// // Standard form of loop over progression
// var inductionVar = 1
// val last = getProgressionLastElement(1, 8, 2)
// val step = 2
// if (inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// inductionVar += 2
// // Loop body
// } while (i != last)
// }
@@ -13,24 +13,21 @@ fun box(): String {
//
// Expected lowered form of loop:
//
// // Additional variables:
// val innerStepArg = one()
// val innerNewStep = if (innerStepArg > 0) innerStepArg
// else throw IllegalArgumentException("Step must be positive, was: $innerStepArg.")
// val outerNestedLast = getProgressionLastElement(1, 6, innerNewStep)
// val outerStepArg = one()
// val outerNewStep = if (outerStepArg > 0) outerStepArg
// else throw IllegalArgumentException("Step must be positive, was: $outerStepArg.")
// // Additional statments:
// var innerStepArg = one()
// if (innerStepArg <= 0) throw IllegalArgumentException("Step must be positive, was: $innerStepArg.")
// val outerNestedLast = getProgressionLastElement(1, 6, innerStepArg)
// var outerStepArg = one()
// if (outerStepArg <= 0) throw IllegalArgumentException("Step must be positive, was: $outerStepArg.")
//
// // Standard form of loop over progression
// var inductionVar = 1
// val last = getProgressionLastElement(1, outerNestedLast, outerNewStep)
// val step = outerNewStep
// val last = getProgressionLastElement(1, outerNestedLast, outerStepArg)
// if (inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// inductionVar += outerStepArg
// // Loop body
// } while (i != last)
// }
@@ -46,5 +43,5 @@ fun box(): String {
// 2 ATHROW
// 1 IF_ICMPGT
// 1 IF_ICMPNE
// 2 IFLE
// 2 IFGT
// 4 IF
@@ -19,12 +19,11 @@ fun box(): String {
// // Standard form of loop over progression
// var inductionVar = 1
// val last = innerNestedLast
// val step = 1
// if (inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// inductionVar += 1
// // Loop body
// } while (i != last)
// }
@@ -12,19 +12,18 @@ fun box(): String {
//
// Expected lowered form of loop (before bytecode optimizations):
//
// // Additional variables:
// // Additional statements:
// val untilArg = nine()
// val nestedLast = untilArg - 1
//
// // Standard form of loop over progression
// var inductionVar = 1
// val last = getProgressionLastElement(1, nestedLast, 2)
// val step = 2
// if (untilArg != Int.MIN_VALUE && inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// inductionVar += 2
// // Loop body
// } while (i != last)
// }
@@ -16,12 +16,11 @@ fun box(): String {
// // Standard form of loop over progression
// var inductionVar = 2u
// val last = getProgressionLastElement(2u, UInt.MIN_VALUE - 1, 2) // `(UInt.MIN_VALUE - 1)` underflows to UInt.MAX_VALUE
// val step = 2
// if (false && inductionVar <= last) { // `false` comes from constant folding of `Int.MIN_VALUE != Int.MIN_VALUE`
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// inductionVar += 2
// // Loop body
// } while (i != last)
// }
@@ -12,18 +12,17 @@ fun box(): String {
//
// Expected lowered form of loop (before bytecode optimization):
//
// // Additional variables:
// val newStep = throw IllegalArgumentException("Step must be positive, was: 0.")
// // Additional statements:
// throw IllegalArgumentException("Step must be positive, was: 0.")
//
// // Standard form of loop over progression
// var inductionVar = 1u
// val last = getProgressionLastElement(1u, 6u, newStep)
// val step = newStep
// val last = getProgressionLastElement(1u, 6u, 0)
// if (inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// inductionVar += 0
// // Loop body
// } while (i != last)
// }
@@ -16,12 +16,11 @@ fun box(): String {
// // Standard form of loop over progression
// val last = getProgressionLastElement(8u, 1u, -2)
// var inductionVar = 8u
// val step = -2
// if (last <= inductionVar) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// inductionVar += -2
// // Loop body
// } while (i != last)
// }
@@ -15,22 +15,21 @@ fun box(): String {
//
// Expected lowered form of loop:
//
// // Additional variables:
// // Additional statements:
// val progression = intProgression
// val nestedFirst = progression.first
// val nestedLast = progression.last
// val nestedStep = progression.step
// val newStep = if (nestedStep > 0) 2 else -2
// val maybeNegatedStep = if (nestedStep <= 0) -2 else 2
//
// // Standard form of loop over progression
// var inductionVar = nestedFirst
// val last = getProgressionLastElement(nestedFirst, nestedLast, newStep)
// val step = newStep
// if ((step > 0 && inductionVar <= last) || (step < 0 && last <= inductionVar)) {
// val last = getProgressionLastElement(nestedFirst, nestedLast, maybeNegatedStep)
// if ((maybeNegatedStep > 0 && inductionVar <= last) || (maybeNegatedStep < 0 && last <= inductionVar)) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// inductionVar += maybeNegatedStep
// // Loop body
// } while (i != last)
// }
@@ -45,9 +44,9 @@ fun box(): String {
// 0 NEW java/lang/IllegalArgumentException
// 0 ATHROW
// 2 INVOKESTATIC kotlin/UnsignedKt.uintCompare
// 1 IFGT
// 2 IFGT
// 1 IF_ICMPNE
// 3 IFLE
// 2 IFLE
// 1 IFGE
// 6 IF
// 0 INEG
@@ -16,25 +16,23 @@ fun box(): String {
//
// Expected lowered form of loop:
//
// // Additional variables:
// // Additional statements:
// val progression = intProgression
// val nestedFirst = progression.first
// val nestedLast = progression.last
// val nestedStep = progression.step
// val stepArg = one()
// val checkedStep = if (stepArg > 0) stepArg
// else throw IllegalArgumentException("Step must be positive, was: $stepArg.")
// val newStep = if (nestedStep > 0) checkedStep else -checkedStep
// var stepArg = one()
// if (stepArg <= 0) throw IllegalArgumentException("Step must be positive, was: $stepArg.")
// if (nestedStep <= 0) stepArg = -stepArg
//
// // Standard form of loop over progression
// var inductionVar = nestedFirst
// val last = getProgressionLastElement(nestedFirst, nestedLast, newStep)
// val step = newStep
// if ((step > 0 && inductionVar <= last) || (step < 0 && last <= inductionVar)) {
// val last = getProgressionLastElement(nestedFirst, nestedLast, stepArg)
// if ((stepArg > 0 && inductionVar <= last) || (stepArg < 0 && last <= inductionVar)) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// inductionVar += stepArg
// // Loop body
// } while (i != last)
// }
@@ -49,9 +47,9 @@ fun box(): String {
// 1 NEW java/lang/IllegalArgumentException
// 1 ATHROW
// 2 INVOKESTATIC kotlin/UnsignedKt.uintCompare
// 1 IFGT
// 3 IFGT
// 1 IF_ICMPNE
// 4 IFLE
// 2 IFLE
// 1 IFGE
// 7 IF
// 1 INEG
@@ -14,18 +14,17 @@ fun box(): String {
//
// Expected lowered form of loop:
//
// // Additional variables:
// // Additional statements:
// val outerNestedLast = getProgressionLastElement(1u, 7u, 3)
//
// // Standard form of loop over progression
// var inductionVar = 1u
// val last = getProgressionLastElement(1u, outerNestedLast, 2)
// val step = 2
// if (inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// inductionVar += 2
// // Loop body
// } while (i != last)
// }
@@ -13,19 +13,18 @@ fun box(): String {
//
// Expected lowered form of loop (before bytecode optimizations):
//
// // Additional variables:
// // Additional statements:
// val untilArg = nine()
// val nestedLast = untilArg - 1u
//
// // Standard form of loop over progression
// var inductionVar = 1
// val last = getProgressionLastElement(1u, nestedLast, 2)
// val step = 2
// if (untilArg != UInt.MIN_VALUE && inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// inductionVar += 2
// // Loop body
// } while (i != last)
// }