Hi Tom, thank you.

I see what you mean, but I am not sure I agree: The compiler knows that the __asm statement mentions a pointer owned by the caller of asmFuntc (asmFunctParms).

This defines a visible communication mean between the caller of asmFunct and the assembler code within the __asm statement (especially true here given the "BALR 14,15").

Because of this, in my opinion, the fact that there is no c managed data flow from asmFunct back to its caller, is not enough to grant the compiler the right to optimize away the entire code block.

mario

On 11/21/21 5:44 PM, G. Tom Russell wrote:
I disagree Mario.  Your set of assembled instructions definitely produced some result, but you ended the C program with " return 0; "

So an optimized program will return a zero and terminate.  It seems to me that you should return functRC so that the compiler knows the function return code must be calculated.

regards, Tom

Date:    Fri, 19 Nov 2021 08:33:20 -0500
From:    Mario Bezzi<[email protected]>
Subject: Re: Curious compiler optimization

Resending as the original, nicely formatted post, was rejected because HTML.

Just for sharing..

The following simple C function:

int asmFunct(void *asmFunctPtr, void *asmFunctParms) {
                                                             int functRC;
__asm(" L    15,%1   \n"
           " LR   1,%2    \n"
           " BALR 14,15   \n"
           " ST 15,%0       "
           :"=m"(functRC)
           :"m"(asmFunctPtr), "r"(asmFunctParms)
           :"r1", "r14", "r15");
     return 0;
}

When compiled under z/OS 2.4 without optimization:

SOURCE,XREF,SSCOM,LIST,LANGLVL(EXTENDED),LONGNAME,ASM,RENT

Is properly translated to (Prolog and Epilog code omitted):

0000E4                    End of Prolog
0000E4  5010  D09C        000001 |                   ST r1,#SR_PARM_1(,r13
                           000002 |       *
                           000003 |       *      int functRC;
                           000004 |       *
                           000005 |       *      __asm(" L 15,%1   \n"
0000E8  5820  D09C        000005 |                   L r2,#SR_PARM_1(,r13 0000EC  5840  2004        000005 |                   L r4,asmFunctParms(,
0000F0  58F2  0000        000005 |                   L r15,0(r2,)
0000F4  1814              000005 |                   LR r1,r4
0000F6  05EF              000005 |                   BALR r14,r15
0000F8  50FD  0098        000005 |                   ST r15,152(r13,)
                           000006 |       *            " LR 1,%2    \n"
                           000007 |       *            " BALR 14,15   \n"                            000008 |       *            " ST 15,%0       "
                           000009 |       * :"=m"(functRC)
                           000010 |       * :"m"(asmFunctPtr), "r"(asmFunctParms)                            000011 |       *            :"r1", "r14", "r15");
                           000012 |       *      return 0;
0000FC  41F0  0000        000012 |                   LA r15,0
                           000013 |       *  }
000100                    000013 |        @1L1       DS       0H
000100                    Start of Epilog

When using optimization (any level), the entire __asm statement is removed leading to incorrout.

SOURCE,XREF,SSCOM,LIST,LANGLVL(EXTENDED),LONGNAME,ASM,RENT,OPT(3)

0000DE                    End of Prolog
                           000002 |       *
                           000003 |       *      int functRC;
                           000004 |       *
                           000005 |       *      __asm(" L 15,%1   \n"
                           000006 |       *            " LR 1,%2    \n"
                           000007 |       *            " BALR 14,15   \n"                            000008 |       *            " ST 15,%0       "
                           000009 |       * :"=m"(functRC)
                           000010 |       * :"m"(asmFunctPtr), "r"(asmFunctParms)                            000011 |       *            :"r1", "r14", "r15");
                           000012 |       *      return 0;
0000DE  41F0  0000        000012 |                   LA r15,0
                           000013 |       *  }
0000E2                    000013 |        @1L1       DS       0H
0000E2                    Start of Epilog

This is with:

5650ZOS V2.4 z/OS XL C

CCN0000(I) Product(5650-ZOS) Phase(CCNEOPTP) Level(D210317.Z2R4)
CCN0000(I) Product(5650-ZOS) Phase(CCNDRVR ) Level(D210317.Z2R4)
CCN0000(I) Product(5650-ZOS) Phase(CCNEP   ) Level(D210317.Z2R4)
CCN0000(I) Product(5650-ZOS) Phase(CCNETBY ) Level(D210317.Z2R4)
CCN0000(I) Product(5650-ZOS) Phase(CCNECWI ) Level(D210317.Z2R4)

And makes it unreliable to optimize C programs embedding assembler code..

I hope this helps,

mario

Reply via email to