Question

I have the following three address code, where n is some external constant:

   x = 0
   i = 0
L: t1 = i * 4
   t2 = a[t1]
   t3 = i * 4
   t4 = b[t3]
   t5 = t2 * t4
   x = x + t5
   i = i + 1
   if i < n goto L

I would like to optimize it as much as I can. Here is what I've come up with so far:

    x = 0
    i = 0
    t1 = -4
L:  t1 = t1+4
    t5 = a[t1] * b[t1]
    x = x + t5
    i = i + 1
    if i < n goto L

Can anyone offer corrections/additional optimizations?

Était-ce utile?

La solution

I'd probably do something like this:

    x = 0
    t1 = (n-1)*4
L:  t5 = a[t1] * b[t1]
    x = x + t5
    t1 = t1 - 4
    if t1 >= 0 goto L

I don't know what the target machine is, but the last two instructions could typically be done with something like SUB / JNS (which saves a comparison).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top