An enabling optimization for C++ virtual functions

Gaining the code re-use advantages of object oriented programming requires dynamic function binding, which allows a new subclass to override a function of a superclass. Dynamic binding is obtained in C-Hthrough the use of vir tual functions. Unfortunately~ vir tual functions have two negative impacts on performance. First, they axe t radi t ional ly compiled into indirect call instructions, which take longer to execute than direct call instructions. Second, it is difficult for the compiler to perform optimization since summary information from called procedures is hard or even impossible to obtain. The net effect is that C + + programmers avoid the use of vir tual functions. We present a new optimizat ion that (1) removes the indirect function calls used for vir tual functions, (2) enables other compiler opt imizat ions such as inlining and constant propagation, and (3) requires no extensive dataflow analysis or profile information and thus is easily implemented in an existing compiler. We include experimental evidence that this opt imizat ion reduces execution time. Not surprisingly, the greatest benefits are obtained with programs that include a high proport ion of vir tual functions calls. *supported in part by the NationalScienceFoundation under grant CCR-9411861 "Permission to make digital/hard copy of all or part of this material without fee is granted provided that . copies are not made or distributed for profit or commercial advantage, the ACM copyright/server notice, the title of the publication and its date appear, and notice is given that copying is by permission of the Association for Computing Machinery, Inc.(ACM). To copy otherwise, to republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. " © 1996 ACM0-89791-820-7 96 0002 3.50 1 I N T R O D U C T I O N Compiler opt imizat ions that a t t empt to reduce program execution t ime have become increasingly more aggressive. Modern compilers perform a variety of intraprocedural and interprocedural opt imizat ions (e.g., branch prediction, code motion, constant propagation, and subprogram inlining). To safely apply these optimizat ions requires summary information from called procedures; thus, a call to a function that cannot be resolved at compile t ime will invalidate or severely restr ict optimization. C + + virtual functions produce such calls. This is unfortunate, as many functions in object-oriented programs are small and therefore excellent candidates for inlining. Because optimizat ion is hampered by vir tual functions, programmers hesi tate to use them. Reducing the cost of vi r tual functions is becoming increasingly impor tant as programmers become more familiar with object-oriented design and programming techniques and, consequently, include more vir tual functions in their code. For example, recent versions of the Interviews framework use more vir tual functions and vir tual function calls that a previous version [9]. This paper describes an opt imizat ion tha t improves the performance of C + + programs that contain vir tual functions. The opt imizat ion is an enabling optimizat ion: it a t t empts to enable other opt imizat ions by replacing indirect function calls with direct functions calls. This allows existing compiler opt imizat ions to be more effective. For example, enabling inlining can increase basic block size, which makes it easier to keep mult iple pipelined functional units busy in today 's modern super-scalar pipelined architectures such as the Pent ium and powerPC. Finally, unlike other opt imizat ions for the same problem [3, 10],