dec83e5f58bcabdc4b4e8ab9cb3e23b4617c2931
[fw/sdcc] / src / SDCCptropt.c
1 /*-------------------------------------------------------------------------
2
3   SDCCptropt.c - source file for pointer arithmetic Optimizations
4
5              Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1998)
6
7    This program is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by the
9    Free Software Foundation; either version 2, or (at your option) any
10    later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20    
21    In other words, you are welcome to use, share and improve this program.
22    You are forbidden to forbid anyone else to use, share and improve
23    what you give them.   Help stamp out software-hoarding!  
24 -------------------------------------------------------------------------*/
25
26 #include "common.h"
27
28 /*-----------------------------------------------------------------------*/
29 /* findPointerGetSet - find the pointer get or set for a operand         */
30 /*-----------------------------------------------------------------------*/
31 static iCode *findPointerGetSet(iCode *sic,operand *op)
32 {
33     iCode *ic = sic;
34
35     for (; ic ; ic = ic->next) {
36         if ((POINTER_SET(ic) && isOperandEqual(op,IC_RESULT(ic))) ||
37             (POINTER_GET(ic) && isOperandEqual(op,IC_LEFT(ic))))
38             return ic;
39
40         /* if we find any other usage or definition of op null */
41         if (IC_RESULT(ic) && isOperandEqual(IC_RESULT(ic),op))
42             return NULL;
43
44         if (IC_RIGHT(ic) && isOperandEqual(IC_RIGHT(ic),op))
45             return NULL;
46         
47         if (IC_LEFT(ic) && isOperandEqual(IC_LEFT(ic),op))
48             return NULL;
49
50     }
51
52     return NULL;
53 }
54
55 /*-----------------------------------------------------------------------*/
56 /* ptrPostIncDecOpts - will do some pointer post increment optimizations */
57 /*                     this will help register allocation amongst others */
58 /*-----------------------------------------------------------------------*/
59 void ptrPostIncDecOpt (iCode *sic)
60 {
61     /* this is what we do. look for sequences like
62
63        iTempX := _SOME_POINTER_;
64        iTempY := _SOME_POINTER_ + nn ;   nn  = sizeof (pointed to object)
65        _SOME_POINTER_ := iTempY;
66        either       
67                iTempZ := @[iTempX];
68        or
69                *(iTempX) := ..something..
70        if we find this then transform this to
71        iTempX := _SOME_POINTER_;
72        either       
73                iTempZ := @[iTempX];
74        or 
75                *(iTempX) := ..something..
76        iTempY := _SOME_POINTER_ + nn ;   nn  = sizeof (pointed to object)
77        _SOME_POINTER_ := iTempY; */
78
79     /* sounds simple enough so lets start , here I use -ve
80        tests all the way to return if any test fails */
81     iCode *pgs, *sh,*st;
82
83     if (!( sic->next && sic->next->next && sic->next->next->next))
84         return ;    
85     if (sic->next->op != '+' && sic->next->op != '-') return;
86     if (!(sic->next->next->op == '=' &&
87           !POINTER_SET(sic->next->next))) return;    
88     if (!isOperandEqual(IC_LEFT(sic->next),IC_RIGHT(sic)) ||
89         !IS_OP_LITERAL(IC_RIGHT(sic->next))    ) return;
90     if (operandLitValue(IC_RIGHT(sic->next)) !=
91         getSize(operandType(IC_RIGHT(sic))->next)) return;
92     if (!isOperandEqual(IC_RESULT(sic->next->next),
93                         IC_RIGHT(sic))) return;
94     if (!isOperandEqual(IC_RESULT(sic->next),IC_RIGHT(sic->next->next))) return;
95     if (!(pgs = findPointerGetSet(sic->next->next,IC_RESULT(sic)))) 
96         return;
97
98     /* found the patter .. now do the transformation */
99     sh = sic->next; st = sic->next->next ;
100
101     /* take the two out of the chain */
102     sic->next = st->next;
103     st->next->prev = sic;
104     
105     /* and put them after the pointer get/set icode */
106     if ((st->next = pgs->next))
107         st->next->prev = st;
108     pgs->next = sh;
109     sh->prev  = pgs;
110     
111 }