Beautified (indented) compiler source tree
[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 *
32 findPointerGetSet (iCode * sic, operand * op)
33 {
34   iCode *ic = sic;
35
36   for (; ic; ic = ic->next)
37     {
38       if ((POINTER_SET (ic) && isOperandEqual (op, IC_RESULT (ic))) ||
39           (POINTER_GET (ic) && isOperandEqual (op, IC_LEFT (ic))))
40         return ic;
41
42       /* if we find any other usage or definition of op null */
43       if (IC_RESULT (ic) && isOperandEqual (IC_RESULT (ic), op))
44         return NULL;
45
46       if (IC_RIGHT (ic) && isOperandEqual (IC_RIGHT (ic), op))
47         return NULL;
48
49       if (IC_LEFT (ic) && isOperandEqual (IC_LEFT (ic), op))
50         return NULL;
51
52     }
53
54   return NULL;
55 }
56
57 /*-----------------------------------------------------------------------*/
58 /* ptrPostIncDecOpts - will do some pointer post increment optimizations */
59 /*                     this will help register allocation amongst others */
60 /*-----------------------------------------------------------------------*/
61 void 
62 ptrPostIncDecOpt (iCode * sic)
63 {
64   /* this is what we do. look for sequences like
65
66      iTempX := _SOME_POINTER_;
67      iTempY := _SOME_POINTER_ + nn ;   nn  = sizeof (pointed to object)
68      _SOME_POINTER_ := iTempY;
69      either       
70      iTempZ := @[iTempX];
71      or
72      *(iTempX) := ..something..
73      if we find this then transform this to
74      iTempX := _SOME_POINTER_;
75      either       
76      iTempZ := @[iTempX];
77      or 
78      *(iTempX) := ..something..
79      iTempY := _SOME_POINTER_ + nn ;   nn  = sizeof (pointed to object)
80      _SOME_POINTER_ := iTempY; */
81
82   /* sounds simple enough so lets start , here I use -ve
83      tests all the way to return if any test fails */
84   iCode *pgs, *sh, *st;
85
86   if (!(sic->next && sic->next->next && sic->next->next->next))
87     return;
88   if (sic->next->op != '+' && sic->next->op != '-')
89     return;
90   if (!(sic->next->next->op == '=' &&
91         !POINTER_SET (sic->next->next)))
92     return;
93   if (!isOperandEqual (IC_LEFT (sic->next), IC_RIGHT (sic)) ||
94       !IS_OP_LITERAL (IC_RIGHT (sic->next)))
95     return;
96   if (operandLitValue (IC_RIGHT (sic->next)) !=
97       getSize (operandType (IC_RIGHT (sic))->next))
98     return;
99   if (!isOperandEqual (IC_RESULT (sic->next->next),
100                        IC_RIGHT (sic)))
101     return;
102   if (!isOperandEqual (IC_RESULT (sic->next), IC_RIGHT (sic->next->next)))
103     return;
104   if (!(pgs = findPointerGetSet (sic->next->next, IC_RESULT (sic))))
105     return;
106
107   /* found the patter .. now do the transformation */
108   sh = sic->next;
109   st = sic->next->next;
110
111   /* take the two out of the chain */
112   sic->next = st->next;
113   st->next->prev = sic;
114
115   /* and put them after the pointer get/set icode */
116   if ((st->next = pgs->next))
117     st->next->prev = st;
118   pgs->next = sh;
119   sh->prev = pgs;
120
121 }