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 /*-----------------------------------------------------------------------*/
31 static iCode *findPointerGetSet(iCode *sic,operand *op)
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))))
40 /* if we find any other usage or definition of op null */
41 if (IC_RESULT(ic) && isOperandEqual(IC_RESULT(ic),op))
44 if (IC_RIGHT(ic) && isOperandEqual(IC_RIGHT(ic),op))
47 if (IC_LEFT(ic) && isOperandEqual(IC_LEFT(ic),op))
55 /*-----------------------------------------------------------------------*/
56 /* ptrPostIncDecOpts - will do some pointer post increment optimizations */
57 /* this will help register allocation amongst others */
58 /*-----------------------------------------------------------------------*/
59 void ptrPostIncDecOpt (iCode *sic)
61 /* this is what we do. look for sequences like
63 iTempX := _SOME_POINTER_;
64 iTempY := _SOME_POINTER_ + nn ; nn = sizeof (pointed to object)
65 _SOME_POINTER_ := iTempY;
69 *(iTempX) := ..something..
70 if we find this then transform this to
71 iTempX := _SOME_POINTER_;
75 *(iTempX) := ..something..
76 iTempY := _SOME_POINTER_ + nn ; nn = sizeof (pointed to object)
77 _SOME_POINTER_ := iTempY; */
79 /* sounds simple enough so lets start , here I use -ve
80 tests all the way to return if any test fails */
83 if (!( sic->next && sic->next->next && sic->next->next->next))
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))))
98 /* found the patter .. now do the transformation */
99 sh = sic->next; st = sic->next->next ;
101 /* take the two out of the chain */
102 sic->next = st->next;
103 st->next->prev = sic;
105 /* and put them after the pointer get/set icode */
106 if ((st->next = pgs->next))