embodiments of an effective method to continuously profile loop trip counts and to dynamically optimize the loop throughout the life of the program using the profile results are disclosed . in the following description , numerous specific details are set forth . however , it is understood that embodiments may be practiced without these specific details . in other instances , well - known elements , applications , and techniques have not been discussed in detail in order to avoid obscuring the present invention . the terms “ optimize ” and “ optimization ” referred to throughout this document specifically refer to performing modifications to code . these optimizations are normally performed with the intent to improve the performance throughput of the code , although there are certain optimizations that either have other intents or other results . in certain cases optimizations may be performed on code to modify the resulting output data collected upon execution of the code rather than attempting to improve any performance throughput of the code . additionally , optimizations could also be performed to modify the data collection capability of performance monitoring code among other goals . in other potential cases , code optimizations could be introduced into the code with the intent to achieve a gain in performance throughput of the code , only to not achieve a gain whatsoever and possibly result in an unwanted performance degradation due to unforeseen circumstances . therefore , the terms “ optimize ” and “ optimization ” do not specifically refer to an optimal configuration of code that will achieve ideal performance . it is not necessarily the case , nor is it likely possible , that any “ optimization ”, as referred to within this document , performed on a segment of code could possibly result in truly optimal performance ( i . e . the best theoretical performance throughput ). rather , references to “ optimizing ” and “ optimizations ” within this document connote attempts at restructuring code to either potentially gain some level of performance improvement over the original code or to modify some other aspect of the code for the benefit of the user ( e . g . modifying the resulting output as referred to above ). if the “ optimization ” was meant to achieve a performance improvement , then whether a real performance improvement is achieved is entirely dependent upon the types of modifications performed , the specific structure and behavior of the original code , and the environment in which the code is executed within . embodiments of the present invention relate to the trip count of a loop during execution within a body of code . a loop , as defined within a region of code , is a sequence of code instructions that are usually carried out several times but written just once . the trip count of a loop is the number of times a loop body is executed after the loop is entered . therefore , the average trip count can be described as the average number of times a loop body is executed each time the loop is entered averaged over a given execution period of time . fig1 illustrates an example of a segment of code 100 within which a loop pre - head block 102 , a loop entry block 104 , a loop body 106 , and a loop post - exit block 108 are located . arrows 110 , 112 , and 114 detail the control flow of the loop within the segment of code 100 . in one embodiment during the execution of the program in which code segment 100 is located the control flow of the program reaches pre - head block 102 . after the code in pre - head block is executed the program control flow 110 enters into the loop body 106 by first executing the loop entry block 104 . once the loop entry block has been executed , the loop body 106 is executed and the control flow 112 loops back to the beginning of the loop body 106 by re - entering the loop entry block 104 . the control flow loops for a given number of iterations and eventually exits out of the loop body to enter ( 114 ) the loop post - exit block 108 . during the execution lifetime of the program , the entire segment of code 100 could be executed numerous times . for example , segment of code 100 could be nested in multiple larger loops that lie within the program . in this example , over a given execution period the average trip count ( atc ) can be calculated as follows : atc = frequency ( loop entry block ( 104 ) execution )/ frequency ( loop pre - head block ( 102 7 ) execution ) for example , if for every time the loop pre - head block 102 was executed the loop entry block 104 executed 50 times , then the average trip count would be 50 . the loop entry block 104 will not necessarily execute the same number of times each time the loop is entered . thus , the average trip count is calculated to obtain a representative trip count over a given execution period . in one embodiment the average trip count could be calculated over a specific period of time , such as one second . in another embodiment the average trip count could be calculated over a specific number of loop iterations , such as 50 , 000 loop iterations . obtaining accurate trip count information in two - phase dynamic binary translators however poses a challenge . during the profiling phase , a loop may have a very different trip count from its execution behavior in the late stages of execution . thus , the average trip count of the loop collected during the profiling phase may not be representative of the loop &# 39 ; s trip count during late execution stages and the resulting loop optimizations ( i . e . loop transformations ) that were well - suited for the beginning stages of execution are not as effective , or possibly even damaging to performance , during many later critical stages of execution . using unrepresentative trip count information to optimize a loop may increase the optimization overhead as well as miss many optimization opportunities . an effective way to maintain the prediction accuracy of the average trip count is to continuously monitor the trip count over the entire lifespan of the program . the loop body code can be dynamically updated many times during execution using the monitoring results to determine the specific optimizations . the average trip count data is helpful in determining the type of optimizations to perform on the loop . there are many possible optimizations that can be performed on a loop including loop peeling , loop unrolling , data prefetching , and software pipelining , among others . loop peeling can simplify a loop with a problematic first iteration by performing that iteration separately before entering the loop . loop unrolling duplicates the body of the loop multiple times in order to decrease the number of times the loop condition is tested as well as the number of jumps taken , which hurt performance by impairing the instruction pipeline . data prefetching calls data into the cache before the processor needs it . software pipelining is a technique that reforms the loop so that a faster execution rate is realized because iterations are executed in overlapped fashion to increase parallelism . in one embodiment , a loop classified as having a low average trip count ( e . g ., under 10 ) potentially would be a good candidate for loop peeling and loop unrolling optimizations , but a poor candidate for data prefetching and software pipelining optimizations . a loop classified as having a medium average trip count ( e . g ., between 10 and 50 ) potentially would be a good candidate for loop unrolling and software pipelining , but a poor candidate for software prefetching . whereas a loop classified as having a high average trip count ( e . g . above 50 ) potentially would be a good candidate for both software pipelining and data prefetching . the loop optimization techniques specified are some of the many potential loop optimizations that would benefit from accurate trip count predictions . thus , in one embodiment of the present invention two threshold values are determined ( tlow and thigh ). an average trip count less than tlow would be considered a low trip count and low trip count specific optimizations could be performed accordingly . an average trip count greater than thigh would be considered a high trip count and high trip count specific optimizations could be performed . an average trip count between tlow and thigh or equal to one of the two numbers would be considered a medium trip count and medium trip count specific optimizations could then be performed . the specific threshold numbers given in the examples above could be modified based on the software application ( s ) that were in need of optimizations . in another embodiment , one threshold value that distinguished only between low and high average trip count values is used . in yet another embodiment , three or more threshold values that could discretely distinguish between many average trip count levels . an additional trip count issue arises when a loop has an average trip count that changes significantly multiple times throughout the lifespan of the program . not only can a loop change its trip count classification between the profiling phase and the later stages of the program , but the program could have significant periods of time where the trip count is stable within different ranges . for example , a loop with an average trip count of 30 in the profiling phase could then jump to an average trip count of 70 for a significant portion of the program execution , then drop to an average trip count of 5 in the late stages of the program . if the trip count was monitored over the course of the entire program the average might be somewhere around the profiling phase average of 30 but that average , even though taken over the entire lifespan of the program , would not be representative of the loop for significant portions of the program . thus , in one embodiment the present invention , as the average trip count of a loop moves up and down throughout the program lifespan the loop &# 39 ; s trip count classification changes if the movements cross a trip count classification threshold boundary . alternatively , in another embodiment the trip count classification will not immediately change upon crossing a threshold boundary . the threshold boundary conditions implements a grace value to tolerate slight changes in trip counts without modifying the optimization policies of the loop . if a loop &# 39 ; s average trip count value is hovering very close to a threshold value for a great deal of the program &# 39 ; s execution lifespan slight perturbations could change the loop modification policies fairly regularly , which would slow down execution with much optimization change overhead . such an unwanted anomaly could be remedied with a grace value , which doesn &# 39 ; t change the optimization policies unless a threshold value is passed by a certain percentage ( e . g . 10 %). the general nature of a loop reveals that its trip count normally does not vary greatly each time the loop is entered . most loops actually have relatively stable trip counts for significant periods of time , although not necessarily for the entire program lifespan . thus , the average trip count can be checked intermittently . in one embodiment of the present invention , the average trip count is checked after the loop has iterated a given number of times . once the loop has iterated this preset number of times ( an iteration threshold ), the average is calculated and then the regular program execution continues . fig2 illustrates an embodiment of a method that continuously monitors the trip count of a loop by taking the running average of the trip count after the iteration threshold has been passed and then using the results to dynamically optimize the loop during execution . at the start 200 of the method the program begins execution by initializing two counters , the loop - entry - count and the loop - body - count 202 . these two counters can determine the average trip count of a loop over a given time with this equation : as detailed earlier , the average trip count is the number of times a loop body is executed each time the loop is entered averaged over a given execution period . in one embodiment , the execution period can equal a given amount of time . in another embodiment , the execution period can equal a given number of iterations through the loop body , as is the case in this example . the loop is then entered in the next stage of fig2 and the loop - entry - count is incremented 204 . once in the loop the loop - body - count is incremented 206 and a check is performed to determine if the loop - body - count is greater than or equal to the iteration threshold value 208 . if the loop - body - count is less than the iteration threshold value the rest of the code within the loop is processed 210 and a check is made to determine whether or not a loop exit condition has taken place 222 . once the loop - body - count has equaled or exceeded the iteration threshold the average trip count will calculated 212 . next , the average trip count will be classified to determine the applicable optimizations to apply to the loop ( e . g ., loop unrolling , software pipelining , data prefetching , etc .) 214 . after the determination is made as to the average trip count classification for the loop a check is made to determine if the loop trip count classification has changed 216 . if a classification change has taken place then the relevant optimizations are applied dynamically to the loop body code 218 , and the loop will be retranslated ( i . e . recompiled ) in real - time . in another embodiment of the present invention , code that is related to the loop ( possibly located in the pre - header block of code , the post - exit block of code , or in other relevant locations in the loop region or other regions of code ) is optimized to increase the performance of the loop . in yet another embodiment , the code that is optimized is not necessarily meant to improve the performance throughput of the loop but rather to improve the resulting data output from the loop among other reasons . if the classification has not changed since the last average trip count classification no optimizations would need to be implemented and the rest of the code within the loop will be processed 210 . after any needed optimizations are in place the loop - entry - count and loop - body - count counters are reinitialized 220 and the control flow returns to the entry point of the loop 204 . when the exit condition for the loop does take place and the loop exits at 222 the control flow continues and the code that is below the loop is executed . in one embodiment the loop could be nested within one or more larger loops and the control flow eventually returns to the loop entry point 204 . regardless , each time the loop is exited the program either will eventually enter the loop again at 204 or eventually exit the program without re - entering the loop and finish at 226 , this decision is determined at 224 of the flow diagram . if the program does return to the loop entry point at 204 it might immediately return ( e . g . in a tight nested loop ) or return at a much later point in the program lifespan . it would be beneficial for the iteration threshold to be set to a significantly high value . this allows for less overhead during program execution . in one embodiment , the iteration threshold could equal 50 , 000 iterations through the loop . this specific number of iterations could be modified up or down to pinpoint the most efficient frequency for checking the average trip count . in another embodiment , the iteration threshold could be set to a time of 1 second of absolute program time . thus , once every second the average trip count of the loop could be checked and the code reoptimized . again , this absolute time could be adjusted up and down based on the most efficient frequency for checking the average trip count . some programs have loop trip counts that are quite uniform throughout the entire program lifespan . if this is the case it is unnecessary and inefficient to continually monitor the loop &# 39 ; s trip count . thus , it would be beneficial in one embodiment of the present invention to stop monitoring the loop trip count if after a certain number of iterations the average trip count has not significantly changed . fig3 illustrates one embodiment of a method capable of monitoring a loop &# 39 ; s trip count , optimizing the loop if the trip count classification changes , and discontinuing the monitoring if the trip count classification does not change after a given period of time . at the start 300 of the method the give - up - threshold counter is initialized 302 . next , the loop is executed for a pre - determined number of iterations 304 ( i . e . the iteration threshold detailed in fig2 ). once the loop has hit the iteration threshold the average loop trip count is collected 306 . then a check is made to determine whether the loop classification changed 308 . if the classification did change the new optimizations based on the classification results are applied to the loop 310 and the control flow returns to the beginning of the method to reinitialize the give - up - threshold counter 302 and go on from there . otherwise , if the classification did not change in 308 then the give - up - threshold counter is incremented 312 and a check is performed to determine if the give - up - threshold counter is equal to or greater than the actual threshold value determined to give up on further monitoring the loop 314 . if the threshold value has been met or exceeded the loop instrumentation is removed from the loop 316 , otherwise the loop begins executing again for the predetermined number of iterations 304 . the give - up - threshold counter increments once per iteration threshold number of loop iterations , therefore the give - up - threshold is related to the number of times the loop &# 39 ; s average trip count is calculated consecutively without a classification change prior to giving up on the monitoring effort . for example , if the loop give - up - threshold counter were 100 , the loop would need to have the average trip count calculated 100 consecutive times without a classification change to initiate the removal of the monitoring instrumentation . in another embodiment , the give - up - threshold counter could be equal to the total number of loop iterations ( relating to the loop - body - count counter in fig2 ) instead of the total number of average trip count calculations . in that embodiment the loop - body - count counter would need to be added to the current give - up - threshold counter each time the average trip count was calculated without a classification change . for example , in this embodiment , if the give - up - threshold were 5 , 000 , 000 , the loop would need to iterate 5 , 000 , 000 times consecutively without a classification change to initiate the removal of the monitoring instrumentation . in either embodiment the give - up - threshold counter could be modified to any given larger or smaller value for efficiency purposes depending on the specific software being monitored . in one embodiment of the present invention , dynamic optimizations ( also referred to as transformations ) are separated into two classes to efficiently monitor the trip count continuously : scalar optimizations ( i . e . scalar transformations ) that do not need accurate trip count information ( such as common subexpression elimination ( cse ) where a subexpression ( e . g . a + b ) referred to frequently is calculated only once ) and loop optimizations that do require accurate trip count information to be effective ( such as loop unrolling , software pipelining , data prefetching , etc .). in one embodiment , the scalar optimizations are applied at the end of the profiling phase using the initial profile information and the loop optimizations are postponed until more accurate trip count information is available . in another embodiment , loops can be classified as being easy or hard . loops with a regular control flow graph often can be identified with few early exits and the loop conditions are controlled by simple induction variables . many floating - point intensive programs usually have simple control flows such as this . these can be referred to as easy loops . other loops would therefore be referred to as hard loops . easy loops are fairly predictable so their trip counts from the initial profiles in the profiling phase are usually fairly accurate . thus , in one embodiment , if loops can be partitioned into these two categories then at the end of the profiling phase both scalar and loop optimizations can be applied effectively to easy loops . for hard loops , since most of the trip count information from the initial profile is likely incorrect , the loop optimizations are postponed until more accurate trip count information is available . fig4 illustrates one embodiment of a method capable of classifying easy loops and hard loops and applying the relevant optimizations to each class . at the start 400 of the method the program begins execution 402 . the initial profiling phase is run 404 and the initial profiling phase average loop trip count is collected 406 . other information can also be gathered during the initial profiling phase as needed . next , scalar optimizations are applied to the loop based on the information obtained from the initial profiling phase 408 . the loop is then classified as easy or hard 410 based on the information collected from the initial profiling phase . a determination is made as to whether the loop is easy or hard 412 . if the loop is easy the relevant loop optimizations are applied to the loop based on the initial profile information 414 . otherwise , if the loop is hard code is inserted to monitor the loop &# 39 ; s trip count for potential future dynamic loop optimizations 416 and the method is finished 418 . monitoring code that is inserted into the loop body code must be as small as possible . the key is to have very lightweight instrumentation so the trip count information can be collected without performance impact on the scalar - optimized loops . fig5 details the code necessary within ( and near ) the loop body to effectively monitor the trip count in one embodiment of the present invention . a segment of code is displayed with a pre - head block of code 500 , a loop entry block of code 502 , a loop body 504 , and a post - exit block of code 506 . each time the pre - head block 500 is entered the loop pre - head monitoring code 508 is executed . the loop - entry - count counter is loaded into one register , that register is incremented , and the new value of the register is stored back into the loop - entry - count counter . finally , the loop - body - counter is loaded into a second register . next , as the control flow of the program enters 510 the loop entry block 502 the loop entry block monitoring code 512 is executed . first , the second register is incremented . next , a check is made to determine if the iteration threshold has been met or exceeded . if the threshold has been met or exceeded the average trip count is checked by calling the check trip count function ( this is detailed in fig3 ). finally , the new value in the second register is stored back into the loop - body - count counter . once the loop entry block monitoring code 512 has been executed the rest of the code in the loop body 504 is executed . the loop either exits 516 into the post - exit block 506 or loops back 514 to the top of the loop body . the goal is to have the least number of lines of monitoring code located within the loop entry block 502 and the loop body 504 itself . in this embodiment , there are four lines of code within the loop entry block that are used for monitoring the trip count . fig6 details another implementation of monitoring code used to determine the loop &# 39 ; s trip count in an embodiment of the present invention . a segment of code is displayed with a pre - head block of code 600 , a loop entry block of code 602 , a loop body 604 , and a post - exit block of code 606 . each time the pre - head block 600 is entered the loop pre - head monitoring code 608 is executed . initially , the loop - body - counter is loaded into a second register . next , a check is made to determine if the iteration threshold has been met or exceeded . if the threshold has been met or exceeded the average trip count is checked by calling the check trip count function ( this is detailed in fig3 ). then the loop - entry - count counter is loaded into the first register , that register is incremented , and the new value of the register is stored back into the loop - entry - count counter . next , as the control flow of the program enters 610 the loop entry block 602 , the loop entry block monitoring code 612 is executed . first , the second register is incremented . finally , the new value in the second register is stored back into the loop - body - count counter . once the loop entry block monitoring code 612 has been executed the rest of the code in the loop body 604 is executed . the loop either exits 616 into the post - exit block 606 or loops back 614 to the top of the loop entry block 602 . in this embodiment , there are only two lines of code within the loop entry block that are used for monitoring the trip count . the downside of this monitoring code implementation scheme is that the loop may iterate too many times ( i . e . higher than the iteration threshold ). when this happens , the average trip count calculation will not be performed until the next time the loop is entered , which may delay the optimizations for the loop . to determine the most efficient loop optimization scheme would require weighing the instrumentation overhead penalty against the benefits of more accurate trip count monitoring . fig7 details yet another implementation of monitoring code used to determine the loop &# 39 ; s trip count in an embodiment of the present invention . a segment of code is displayed with a pre - head block of code 700 , a loop entry block of code 702 , a loop body 704 , and a post - exit block of code 706 . each time the pre - head block 700 is entered the loop pre - head monitoring code 708 is executed . initially , the loop - body - counter is loaded into a second register . next , a check is made to determine if the iteration threshold has been met or exceeded . if the threshold has been met or exceeded the average trip count is checked by calling the check trip count function ( this is detailed in fig3 ). then the loop - entry - count counter is loaded into the first register , that register is incremented , and the new value of the register is stored back into the loop - entry - count counter . next , as the control flow of the program enters 710 the loop entry block 702 , the loop entry block monitoring code 712 is executed . in this embodiment , there is only one line of monitoring code within the loop entry block , incrementing the second register . once the loop entry block monitoring code 712 has been executed the rest of the code in the loop body 704 is executed . the loop either exits 716 into the post - exit block 706 or loops back 714 to the top of the loop entry block . when the loop exits into the post - exit block 706 the post - exit monitoring code 718 is executed . this entails storing the value currently in the second register back into the loop - body - count counter . as mentioned , in this embodiment , there is only one line of code within the loop entry block used for monitoring the trip count . this implementation has the same downside as the implementation detailed in fig7 , the loop may iterate too many times . again , to determine the most efficient loop optimization scheme would require weighing the instrumentation overhead penalty against the benefits of more accurate trip count monitoring . thus , embodiments for an effective method to continuously profile loop trip counts and to dynamically optimize the loop throughout the life of the program using the profile results are disclosed . these embodiments have been described with reference to specific exemplary embodiments thereof . it will , however , be evident to persons having the benefit of this disclosure that various modifications and changes may be made to these embodiments without departing from the broader spirit and scope of the embodiments described herein . the specification and drawings are , accordingly , to be regarded in an illustrative rather than a restrictive sense .