
Strength Reduction for Loop-Invariant Types Phung Hua Nguyen and Jingling Xue Compiler Research Group School of Computer Science and Engineering University of New South Wales Sydney, NSW 2032, Australia Abstract an adaptive compilation system for Java, consists of several devirtualisation techniques including class test Types are fundamental for enforcing levels of abstrac- (Calder & Grunwald 1994), method test (Detlefs & tion in modern high-level programming languages Agesen 1999) and code patching (Ishizaki et al. 2000) and their lower-level representations. However, some for researchers to experiment with. In the class type-related features such as dynamic method calls (method) test approach, a guard test is generated to and dynamic type casts can contribute substantially test the receiver of the class (method) to ensure that to the performance of a program. Loop-invariant type it is valid to execute an assumed target method. In is a concept relating to an object whose dynamic type addition, Jikes maintains several data structures op- never changes inside a loop. In this case, operations erationally close to every object to facilitate dynamic on the type of the object may be redundant in the type checking required by Java’s type system. loop. As these operations often cause exceptions, ex- Precise type information is invaluable for analysis isting redundancy elimination techniques usually fail and optimisation of programs. For example, both dy- to optimise them. This paper proposes a new ap- namic method dispatching and dynamic type check- proach to reducing the cost of two important opera- ing benefit from class hierarchy analysis (Dean, Grove tions on loop-invariant types: method tests and dy- & Chamber 1995) and type analysis (Sundaresan namic type checking. We demonstrate its usefulness et al. 2000, Bacon, Wegman & Zadeck 1996, Bacon and benefit in IBM’s Jikes RVM, a dynamic compila- & Sweeney 1996). By deducing the set of plausible tion system for Java. dynamic (or concrete) types of an object at compile time, the compiler can reduce the number of class Keywords: Loop-Invariant Type, Strength Reduction, and method tests required in devirtualisation and PRE, Type Checking, Inlining eliminate some redundant dynamic type checks. In the special case when the receiver x at a call site 1 Introduction x.f() has been proved to have a single implemen- tation, the callee f can be dispatched as a direct Types are fundamental for enforcing levels of ab- call or directly inlined if the “closed-world” assump- straction in modern programming languages and their tion is made. Otherwise, in languages that embrace lower-level representations (Morrisett, Walker, Crary dynamic class loading such as Java, only one single & Glew 1998). However, some type-related features class/method test is needed to guard the compile- pose challenges for language implementors to remove time “unique” implementation if it is inlined. But these abstraction boundaries to permit efficient exe- the caller will be recompiled (Detlefs & Agesen 1999) cution. In object-oriented languages, two examples or patched (Ishizaki et al. 2000) later if the single- are dynamic method dispatching and dynamic type implementation assumption is invalidated due to, for checking. To reduce the overhead in the former case, example, dynamic class loading. the compiler typically applies a number of devirtual- Obviously, it is imperative for the tests required in isation techniques (Calder & Grunwald 1994, Cham- devirtualisation and dynamic type checking to be ex- bers & Ungar 1990, Detlefs & Agesen 1999, Gold- ecuted as efficiently as possible, especially when they berg & Robson 1983, Chambers, Dean & Grove are executed frequently. This paper proposes a new 1996, H¨olzle, Chambers & Ungar 1991, Ishizaki, approach to reducing the cost of these tests in a loop Kawahito, Yasue, Komatsu & Nakatani 2000, Krasner in an important special case that has not been ad- 1983, Deutsch & Schiffman 1984, Sundaresan, Hen- dressed before. A variable (i.e., object) in a loop is dren, Razafimahefa, Vall´ee-Rai, Lam, Gagnon & said to have a loop-invariant dynamic (i.e., concrete) Godin 2000) in tandem with inlining (Hazelwood type if the dynamic type never changes during the ex- & Grove 2003). To reduce the cost in the latter ecution time of the loop. In this paper, such an object case, various type encoding techniques have been de- is said to have a loop-invariant type. Our proposed vised (Alpern, Cocchi & Grove 2001, Krall, Vitek compiler technique, called strength reduction for loop- & Horspool 1997). For example, IBM’s Jikes RVM invariant types, aims at reducing the cost of the type (Alpern, Attanasio, Barton, Burke, P.Cheng, Choi, operations whose operands have loop-invariant types. Cocchi, Fink, Grove, Hind, Hummel, Lieber, Litvi- In this case, we will execute these operations only nov, Mergen, Ngo, Russell, Sarkar, Serrano, Shep- once, at the first iteration of the loop and cache its herd, Smith, Sreedhar, Srinivasan, & Whaley 2000), value in a new temporary. The executions of these operations in the remaining iterations of the loop are “Copyright (c)2004, Australian Computer Society, Inc. This redundant; they will be replaced with cheaper opera- paper appeared at the 27th Australasian Computer Science tions that perform a test on the new temporary only. Conference, The University of Otago, Dunedin, New Zealand. We apply our technique to reduce the overhead of Conferences in Research and Practice in Information Technol- two important operations: method tests in devirtual- ogy, Vol. 26. V. Estivill-Castro, Ed. Reproduction for aca- demic, not-for profit purposes permitted provided this text is isation and dynamic type checking. Our technique is included.” particularly useful in a compilation system that sup- R0 = <receiver object> 1public void printRec() { R1 = load(R0 + <offset-of-VMT-in-object>) 2 ... R2 = load(R1 + <offset-of-method-in-VMT>) 3 Enumeration i = entry.items.elements(); R3 = load(VMT of expected class 1) 4 Enumeration f = fmt.elements(); R4 = load(R3 + <offset-of-method-in-VMT>) 5 if (R2== R4){ // testcase1 6 while ( f.hasMoreElements() ) { ... //inlinedmethod1 7 s = ((String)f.nextElement()); } else { 8 R3 = load(VMT of expected class 2) 9 if(s!=null) R4 = load(R3 + <offset-of-method-in-VMT>) 10 spec.harness.Context.out.print(s); if (R2 == R4> { // test case 2 11 else { ... //inlinedmethod2 } else 12 s = (String)i.nextElement(); CALL R2 13 spec.harness.Context.out.println(s); } 14 } 15 } Figure 2: Pseudo code of a method test (assuming 16 ... that there are two target implementations inlined). 17 } tifying loop-invariant types. Section 4 presents some Figure 1: An example from db in SPECjvm98 where experimental results. Section 5 discusses the related our strength reduction succeeds but PRE fails. work. Section 6 concludes the paper. 2 Strength Reduction for Loop-Invariant ports (a) adaptive recompilation, (b) dynamic class Types loading and (c) precise exception semantics. Because of (a), the address of an inlined method must be In this section, we present our compiler technique loaded dynamically at run time, making a method for performing strength reduction on loop-invariant test more expensive than a class test. For this rea- types. We show how to reduce the overhead of two im- son and since class tests are rarely used (as Table 1 portant operations on loop-invariant types: method shows), we do not also optimise class tests in this tests (Section 2.1) and dynamic type checking (Sec- work. Because of (b), class and method tests are fre- tion 2.2). In each case, we introduce the operation quently necessary, especially if a call site is known to in pseudo code, discuss Jikes’s implementation, and have more than one implementation. Because of (c), finally, present our optimisation. In addition, we will existing partial redundancy elimination (PRE) tech- use examples to explain in detail why existing PRE niques (Bodik, Gupta & Soffa 1998, Knoop, R¨uthing techniques fail to optimise these two cases. & Steffen 1994, Cai & Xue 2003) usually fail to re- move the partial redundancy removed by our tech- 2.1 Method Tests nique. Method tests and dynamic type checks involve potential exception instructions (PEIs). Consider a Object-oriented languages such as Java complicate in- while loop shown in Figure 1, which is contained in a lining, because methods are usually virtual. A virtual method taken from db, a program from SPECjvm98. method is defined in one class, but may be overrid- Some irrelevant lines indicated by the “...” have been den in subclasses of that class. The method actually deleted. In line 12, i has a loop-invariant type. By invoked at a call site depends on the dynamic type of applying our technique, the type check on i will be the receiver object. performed only once during the first iteration of the Method test (Detlefs & Agesen 1999) is one while loop. PRE cannot move this operation out of of many devirtualisation techniques (Calder & the loop since doing so may cause an exception even Grunwald 1994, Chambers & Ungar 1990, Detlefs & though it may not be thrown in the original code. Agesen 1999, Goldberg & Robson 1983, Chambers Converting the loop into a do-while would not en- et al. 1996, H¨olzle et al. 1991, Ishizaki et al. 2000, able the desired code motion since the else branch Krasner 1983, Deutsch & Schiffman 1984, Sundare- may or may not be executed as before. If the call site san et al. 2000) proposed to reduce the overhead of at line 12 is inlined and guarded by method test(s), dynamic method calls for various object-oriented lan- our strength reduction technique will perform these guages. A method test is generated to test the target expensive tests only once. Again PRE fails for the method of a virtual call with an expected method to above reasons. In addition, PRE fails in both cases ensure that it is valid to make a direct call to the ex- because some earlier instructions are PEIs.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages10 Page
-
File Size-