-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCODE_OPT.CPP
150 lines (105 loc) · 2.19 KB
/
CODE_OPT.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include<process.h>
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char a[]={'=','-',',','@',';','#','%','+','^','!','{','[',']','}'};
char lhs[100][10];
char rhs[100][10];
int n,b=0,p=0,k,count=0;
static int flag = 1;
char y;
cout<<"Enter the no of 3 instructions code";
cin>>n;
cout<<"\nTo Abort press at LHS symbols =,@,#,$,%,+,-";
for(int i=0;i<n;i++)
{ cout<<"\n\nEnter the instruction no:"<<i+1;
cout<<"\nLHS followed by RHS\n";
for(int j=0;j<2;j++)
{
printf("%c",lhs[i][j]=getch());
for(int l=0;l<strlen(a);l++)
if(lhs[i][j] == a[l])
{
clrscr();
cout<<"\n\n\t\tYou Entered Wrong input (Aborting...)";
getch();
exit(1);
}
}
cout<<"\t=\t";
gets(rhs[i]);
}
cout<<"\nOptimal Code optimization for Entered code";
//COPY PROPAGATION
for(i=0;i<n;i++)
{
// checking each r.h.s
/* if(rhs[i][k] == NULL)
{ i=i+1;
continue;
} */
count=0;
for(p=i+1;p<n;p++)
{ /*if(lhs [p][0] == NULL)
p=p+1; */
for(k=0;rhs[i][k] != '\0';k++)
{
if(rhs[i][k] == rhs[p][k])
{ count++;
}
else
{break;}
}
if(count == strlen(rhs[p]))
{ cout<<"\nCopy Propagation Found at";
cout<<"\nInstructions:"<<i+1<<" and "<<p+1;
flag=10;
}
}
}
// DEAD CODE
for(p=0;p<n-1;p++)
{ // Taking L.H.S first
//implimenting KMP algorithm
flag=0;
for(i=p+1;i<n;i++)
{ b=strlen(rhs[i]);
for(k=0;k<b; k++)
{ if ((lhs[p][0] == rhs[i][k]) && (lhs[p][1] == rhs[i][k+1]))
{ flag=1;
}
}
}
if(flag == 0)
{ cout<<"\nDead code found with instruction :"<<p+1;
cout<<"\nRemoving ...";
lhs[p][1]=lhs[p][0]=NULL;
b=strlen(rhs[p]);
for(k=0;k<b; k++)
rhs[p][k]=NULL;
flag = 10;
}
}
if(flag == 1)
{
cout<<"\n\nNO optimization Required already otimized";
}
else
for(i=0;i<n;i++)
{
cout<<"\n\nInstruction no:"<<i+1<<endl;
//cout<<"\nLHS followed by RHS\n";
for(int j=0;j<2;j++)
{if(lhs[i][j] == NULL)
{ i=i+1;}
printf("%c",lhs[i][j]);
}
cout<<"\t=\t";
puts(rhs[i]);
}
getch();
}