xref: /aosp_15_r20/art/test/594-load-string-regression/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker public class Main {
18*795d594fSAndroid Build Coastguard Worker   // Note: We're not doing checker tests as we cannot do them specifically for a non-PIC
19*795d594fSAndroid Build Coastguard Worker   // configuration. The check here would be "prepare_for_register_allocation (before)"
20*795d594fSAndroid Build Coastguard Worker   //     CHECK:         LoadClass
21*795d594fSAndroid Build Coastguard Worker   //     CHECK-NEXT:    ClinitCheck
22*795d594fSAndroid Build Coastguard Worker   //     CHECK-NEXT:    LoadString load_kind:BootImageAddress
23*795d594fSAndroid Build Coastguard Worker   //     CHECK-NEXT:    NewInstance
24*795d594fSAndroid Build Coastguard Worker   // and "prepare_for_register_allocation (after)"
25*795d594fSAndroid Build Coastguard Worker   //     CHECK:         LoadString
26*795d594fSAndroid Build Coastguard Worker   //     CHECK-NEXT:    NewInstance
27*795d594fSAndroid Build Coastguard Worker   // but the order of instructions for non-PIC mode is different.
$noinline$test()28*795d594fSAndroid Build Coastguard Worker   public static int $noinline$test() {
29*795d594fSAndroid Build Coastguard Worker     int r = 0x12345678;
30*795d594fSAndroid Build Coastguard Worker     do {
31*795d594fSAndroid Build Coastguard Worker       // LICM pulls the LoadClass and ClinitCheck out of the loop, leaves NewInstance in the loop.
32*795d594fSAndroid Build Coastguard Worker       Helper h = new Helper();
33*795d594fSAndroid Build Coastguard Worker       // For non-PIC mode, LICM pulls the boot image LoadString out of the loop.
34*795d594fSAndroid Build Coastguard Worker       // (For PIC mode, the LoadString can throw and will not be moved out of the loop.)
35*795d594fSAndroid Build Coastguard Worker       String s = "";  // Empty string is known to be in the boot image.
36*795d594fSAndroid Build Coastguard Worker       r = r ^ (r >> 5);
37*795d594fSAndroid Build Coastguard Worker       h.$noinline$printString(s);
38*795d594fSAndroid Build Coastguard Worker       // During DCE after inlining, the loop back-edge disappears and the pre-header is
39*795d594fSAndroid Build Coastguard Worker       // merged with the body, leaving consecutive LoadClass, ClinitCheck, LoadString
40*795d594fSAndroid Build Coastguard Worker       // and NewInstance in non-PIC mode. The prepare_for_register_allocation pass
41*795d594fSAndroid Build Coastguard Worker       // merges the LoadClass and ClinitCheck with the NewInstance and checks that
42*795d594fSAndroid Build Coastguard Worker       // there are no instructions with side effects in between. This check used to
43*795d594fSAndroid Build Coastguard Worker       // fail because LoadString was always listing SideEffects::CanTriggerGC() even
44*795d594fSAndroid Build Coastguard Worker       // when it doesn't really have any side effects, i.e. for direct references to
45*795d594fSAndroid Build Coastguard Worker       // boot image Strings or for Strings known to be in the dex cache.
46*795d594fSAndroid Build Coastguard Worker     } while ($inline$shouldContinue());
47*795d594fSAndroid Build Coastguard Worker     return r;
48*795d594fSAndroid Build Coastguard Worker   }
49*795d594fSAndroid Build Coastguard Worker 
$inline$shouldContinue()50*795d594fSAndroid Build Coastguard Worker   static boolean $inline$shouldContinue() {
51*795d594fSAndroid Build Coastguard Worker     return false;
52*795d594fSAndroid Build Coastguard Worker   }
53*795d594fSAndroid Build Coastguard Worker 
main(String[] args)54*795d594fSAndroid Build Coastguard Worker   public static void main(String[] args) {
55*795d594fSAndroid Build Coastguard Worker     assertIntEquals(0x12345678 ^ (0x12345678 >> 5), $noinline$test());
56*795d594fSAndroid Build Coastguard Worker   }
57*795d594fSAndroid Build Coastguard Worker 
assertIntEquals(int expected, int result)58*795d594fSAndroid Build Coastguard Worker   public static void assertIntEquals(int expected, int result) {
59*795d594fSAndroid Build Coastguard Worker     if (expected != result) {
60*795d594fSAndroid Build Coastguard Worker       throw new Error("Expected: " + expected + ", found: " + result);
61*795d594fSAndroid Build Coastguard Worker     }
62*795d594fSAndroid Build Coastguard Worker   }
63*795d594fSAndroid Build Coastguard Worker }
64*795d594fSAndroid Build Coastguard Worker 
65*795d594fSAndroid Build Coastguard Worker class Helper {
$noinline$printString(String s)66*795d594fSAndroid Build Coastguard Worker   public void $noinline$printString(String s) {
67*795d594fSAndroid Build Coastguard Worker     System.out.println("String: \"" + s + "\"");
68*795d594fSAndroid Build Coastguard Worker   }
69*795d594fSAndroid Build Coastguard Worker }
70