/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {

  public static void assertIntEquals(int expected, int result) {
    if (expected != result) {
      throw new Error("Expected: " + expected + ", found: " + result);
    }
  }

  // This function always returns 1.
  public static int $opt$noinline$function_call(int arg) {
    return 1 % arg;
  }

  //                               | registers available to | regexp
  //                               | the register allocator |
  // ------------------------------|------------------------|-----------------
  // ARM64 callee-saved registers  | [x20-x29]              | x2[0-9]
  // ARM callee-saved registers    | [r5-r8,r10,r11]        | r([5-8]|10|11)
  // X86 callee-saved registers    | [ebp,esi,edi]          | e(bp|si|di)
  // X86_64 callee-saved registers | [rbx,rbp,r12-15]       | r(bx|bp|1[2-5])

  /**
   * Check that a value live across a function call is allocated in a callee
   * saved register.
   */

  /// CHECK-START-ARM:   int Main.$opt$LiveInCall(int) register (after)
  /// CHECK-DAG:   <<Arg:i\d+>>     ParameterValue
  /// CHECK-DAG:   <<Const1:i\d+>>  IntConstant 1
  /// CHECK:       <<t1:i\d+>>      Add [<<Arg>>,<<Const1>>] {{.*->r([5-8]|10|11)}}
  /// CHECK:       <<t2:i\d+>>      InvokeStaticOrDirect
  /// CHECK:                        Sub [<<t1>>,<<t2>>]
  /// CHECK:                        Return

  /// CHECK-START-ARM64: int Main.$opt$LiveInCall(int) register (after)
  /// CHECK-DAG:   <<Arg:i\d+>>     ParameterValue
  /// CHECK-DAG:   <<Const1:i\d+>>  IntConstant 1
  /// CHECK:       <<t1:i\d+>>      Add [<<Arg>>,<<Const1>>] {{.*->x2[0-9]}}
  /// CHECK:       <<t2:i\d+>>      InvokeStaticOrDirect
  /// CHECK:                        Sub [<<t1>>,<<t2>>]
  /// CHECK:                        Return

  /// CHECK-START-X86: int Main.$opt$LiveInCall(int) register (after)
  /// CHECK-DAG:   <<Arg:i\d+>>     ParameterValue
  /// CHECK-DAG:   <<Const1:i\d+>>  IntConstant 1
  /// CHECK:       <<t1:i\d+>>      Add [<<Arg>>,<<Const1>>] {{.*->e(bp|si|di)}}
  /// CHECK:       <<t2:i\d+>>      InvokeStaticOrDirect
  /// CHECK:                        Sub [<<t1>>,<<t2>>]
  /// CHECK:                        Return

  /// CHECK-START-X86_64: int Main.$opt$LiveInCall(int) register (after)
  /// CHECK-DAG:   <<Arg:i\d+>>     ParameterValue
  /// CHECK-DAG:   <<Const1:i\d+>>  IntConstant 1
  /// CHECK:       <<t1:i\d+>>      Add [<<Arg>>,<<Const1>>] {{.*->r(bx|bp|1[2-5])}}
  /// CHECK:       <<t2:i\d+>>      InvokeStaticOrDirect
  /// CHECK:                        Sub [<<t1>>,<<t2>>]
  /// CHECK:                        Return

  public static int $opt$LiveInCall(int arg) {
    int t1 = arg + 1;
    int t2 = $opt$noinline$function_call(arg);
    return t1 - t2;
  }

  public static void main(String[] args) {
    int arg = 123;
    assertIntEquals($opt$LiveInCall(arg), arg);
  }
}
