1 /*-------------------------------------------------------------------------
3 SDCCptropt.c - source file for pointer arithmetic Optimizations
5 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
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
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.
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.
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 -------------------------------------------------------------------------*/
28 /*-----------------------------------------------------------------------*/
29 /* findPointerGetSet - find the pointer get or set for a operand */
30 /*-----------------------------------------------------------------------*/
32 findPointerGetSet (iCode * sic, operand * op)
36 for (; ic; ic = ic->next)
38 if ((POINTER_SET (ic) && isOperandEqual (op, IC_RESULT (ic))) ||
39 (POINTER_GET (ic) && isOperandEqual (op, IC_LEFT (ic))))
42 /* if we find any other usage or definition of op null */
43 if (IC_RESULT (ic) && isOperandEqual (IC_RESULT (ic), op))
46 if (IC_RIGHT (ic) && isOperandEqual (IC_RIGHT (ic), op))
49 if (IC_LEFT (ic) && isOperandEqual (IC_LEFT (ic), op))
57 /*-----------------------------------------------------------------------*/
58 /* ptrPostIncDecOpts - will do some pointer post increment optimizations */
59 /* this will help register allocation amongst others */
60 /*-----------------------------------------------------------------------*/
62 ptrPostIncDecOpt (iCode * sic)
64 /* this is what we do. look for sequences like
66 iTempX := _SOME_POINTER_;
67 iTempY := _SOME_POINTER_ + nn ; nn = sizeof (pointed to object)
68 _SOME_POINTER_ := iTempY;
72 *(iTempX) := ..something..
73 if we find this then transform this to
74 iTempX := _SOME_POINTER_;
78 *(iTempX) := ..something..
79 iTempY := _SOME_POINTER_ + nn ; nn = sizeof (pointed to object)
80 _SOME_POINTER_ := iTempY; */
82 /* sounds simple enough so lets start , here I use -ve
83 tests all the way to return if any test fails */
86 if (!(sic->next && sic->next->next && sic->next->next->next))
88 if (sic->next->op != '+' && sic->next->op != '-')
90 if (!(sic->next->next->op == '=' &&
91 !POINTER_SET (sic->next->next)))
93 if (!isOperandEqual (IC_LEFT (sic->next), IC_RIGHT (sic)) ||
94 !IS_OP_LITERAL (IC_RIGHT (sic->next)))
96 if (operandLitValue (IC_RIGHT (sic->next)) !=
97 getSize (operandType (IC_RIGHT (sic))->next))
99 if (!isOperandEqual (IC_RESULT (sic->next->next),
102 if (!isOperandEqual (IC_RESULT (sic->next), IC_RIGHT (sic->next->next)))
104 if (!(pgs = findPointerGetSet (sic->next->next, IC_RESULT (sic))))
107 /* found the patter .. now do the transformation */
109 st = sic->next->next;
111 /* take the two out of the chain */
112 sic->next = st->next;
113 st->next->prev = sic;
115 /* and put them after the pointer get/set icode */
116 if ((st->next = pgs->next))