Frequency Reduction in Code Optimization Last Updated : 21 Nov, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report Prerequisite - Compiler Design | Code Optimization Frequency reduction is a type in loop optimization process which is machine independent. In frequency reduction code inside a loop is optimized to improve the running time of program. Frequency reduction is used to decrease the amount of code in a loop. A statement or expression, which can be moved outside the loop body without affecting the semantics of the program, is moved outside the loop. Frequency Reduction is also called Code Motion. Objective of Frequency Reduction: The objective of frequency reduction is: To reduce the evaluation frequency of expression. To bring loop invariant statements out of the loop. Below is the example of Frequency Reduction: Program 1: CPP // This program does not uses frequency reduction. #include <bits/stdc++.h> using namespace std; int main() { int a = 2, b = 3, c, i = 0; while (i < 5) { // c is calculated 5 times c = pow(a, b) + pow(b, a); // print the value of c 5 times cout << c << endl; i++; } return 0; } Program 2: CPP // This program uses frequency reduction. #include <bits/stdc++.h> using namespace std; int main() { int a = 2, b = 3, c, i = 0; // c is calculated outside the loop c = pow(a, b) + pow(b, a); while (i < 5) { // print the value of c 5 times cout << c << endl; i++; } return 0; } Output: 17 17 17 17 17 Explanation: Program 2 is more efficient than Program 1 as in Program 1 the value of c is calculated each time the while loop is executed. Hence the value of c is calculated outside the loop only once and it reduces the amount of code in the loop. Comment More infoAdvertise with us P pp_pankaj Follow Improve Article Tags : Compiler Design Similar Reads Phases of a Compiler A compiler is a software tool that converts high-level programming code into machine code that a computer can understand and execute. It acts as a bridge between human-readable code and machine-level instructions, enabling efficient program execution. The process of compilation is divided into six p 10 min read Three address code in Compiler TAC is an intermediate representation of three-address code utilized by compilers to ease the process of code generation. Complex expressions are, therefore, decomposed into simple steps comprising, at most, three addresses: two operands and one result using this code. The results from TAC are alway 6 min read Code Optimization in Compiler Design Code optimization is a crucial phase in compiler design aimed at enhancing the performance and efficiency of the executable code. By improving the quality of the generated machine code optimizations can reduce execution time, minimize resource usage, and improve overall system performance. This proc 9 min read Introduction of Compiler Design A compiler is software that translates or converts a program written in a high-level language (Source Language) into a low-level language (Machine Language or Assembly Language). Compiler design is the process of developing a compiler.The development of compilers is closely tied to the evolution of 9 min read Directed Acyclic Graph in Compiler Design (with examples) In compiler design, a Directed Acyclic Graph (DAG) plays a crucial role in representing expressions and optimizing code. A DAG is a graph containing directed edges but no cycles, ensuring no path leads back to the starting node. DAGs are particularly useful in eliminating redundant computations and 4 min read Introduction of Lexical Analysis Lexical analysis, also known as scanning is the first phase of a compiler which involves reading the source program character by character from left to right and organizing them into tokens. Tokens are meaningful sequences of characters. There are usually only a small number of tokens for a programm 6 min read Storage Allocation Strategies in Compiler Design A compiler is a program that converts HLL(High-Level Language) to LLL(Low-Level Language) like machine language. It is also responsible for organizing the memory layout of a program by allocating space for global and local variables, constants, and dynamic data structures.As the program begins execu 6 min read Symbol Table in Compiler Every compiler uses a symbol table to track all variables, functions, and identifiers in a program. It stores information such as the name, type, scope, and memory location of each identifier. Built during the early stages of compilation, the symbol table supports error checking, scope management, a 8 min read Intermediate Code Generation in Compiler Design In the analysis-synthesis model of a compiler, the front end of a compiler translates a source program into an independent intermediate code, then the back end of the compiler uses this intermediate code to generate the target code (which can be understood by the machine). The benefits of using mach 6 min read Difference Between Compiler and Interpreter The Compiler and Interpreter, both have similar works to perform. Interpreters and Compilers convert the Source Code (HLL) to Machine Code (understandable by Computer). In general, computer programs exist in High-Level Language that a human being can easily understand. But computers cannot understan 6 min read Like