CompuServe Thread

#Optimizer Useless!?

5 messages in this thread
#7161From: Ken BroomfieldJul 19, 1993 2:29 PM
————————————————————————– I originally sent this message almost three weeks ago. I called the tech support line a week ago. I still haven't gotten an answer to what ought to be a straightforward question. ————————————————————————– Sir– I'm running into a problem with the C++ compiler. It seems to fail to optimize functions that contain calls to the Mac Toolbox. It's as if the "nooptimize" pragma (which I guess only exists in Think C) is turned on for such calls, but I can't find any explicit directive which would cause this. Using ResEdit, I have discovered a list of Mac Toobox trap names embedded in the C++ translator for _SOME_ reason. Does the C++ compiler do this by default for Toolbox calls? Why is it necessary for ALL traps to be handled this way, even traps which are stack-based (as opposed to register-based, which may understandably cause trouble for an optimizer)? This would seem to render the optimizer much less useful, since Toolbox calls are strewn throughout a typical program. Thanks for any info on this. –Ken
#7188From: Robert S. Mah[OSB]Jul 20, 1993 12:00 AM
Has anyone else experienced Ken's bug report? Not that I doubt him, per se, but it seems just so _inane_ that somthing like this could have been overlooked! Cheers, Rob
#7227From: Kevin IrlenJul 20, 1993 4:21 PM
<<< I'm running into a problem with the C++ compiler. It seems to fail to optimize functions that contain calls to the Mac Toolbox. >>> Please provide an example of this, specifying which optimizations you're using. This is not a "feature" of the compiler. Kevin Irlen Symantec Languages Support
#7243From: Ken BroomfieldJul 20, 1993 8:29 PM
Here is a short program which illustrates the problem with optimization not occurring in functions containing toolbox calls: class CBase { public: short a, b, c; inline CBase () { a = 1; b = 2; c = 3; } inline void foobar () { a = b + c * 8 + b * 7; } inline void btoc () { b = c; } inline void nothing () { } }; class CSub: public CBase { public: inline CSub () { } inline void foobar () { inherited::foobar (); } inline void btoc () { if (0) foobar (); inherited::btoc (); } }; void takebase (CBase &b) { } void localSysBeep (short s) { } void main (void) { CSub s; s.foobar (); s.btoc (); s.nothing (); takebase (s); // This call is needed so that the optimizer won't // remove the above member function calls as dead code localSysBeep (1); // Change this to "SysBeep (1);" and the code will // suddenly experience the _Oprah Effect_, growing // by from 74 to 114 bytes, an increase of 54%. // The code should actually SHRINK by two bytes // owing to the change from a JSR instruction to // a toolbox trap. } If you're wondering about all the various parts of this source code that should generate no object code, they do have an effect regarding this problem, though they don't account for all of it. I ran into this problem while developing classes for fixed point numbers (to allow the use of standard arithmetic operators with the Mac fixed point types) which required defining many small inline functions which did little or no actual work (such as converting from one type to another when the representation was internally the same). I noticed that the code which contained calls to FixMul() and FixDiv(), etc., contained a lot of useless busy-work. Then I discovered that ANY toolbox call had the same effect. Another unrelated problem I've noticed in the disassemblies below is that this compiler LOVES to allocate stack frames that are WAY TOO LARGE for what it needs. The first version allocates 44 bytes and uses 6 of them. The second version allocates 36 bytes and uses 22, mostly for unneeded store instructions. I'm running with these options: C++ Compiler: – Use global optimizer: on. All optimization options are on. Optimize for space selected – All debugging options are off except for Generate optimizer warnings – Generate 68020 instructions: on – Align to 2 byte boundaries – Relaxed ANSI conformance enabled TPM: – Optimize monomorphic methods: on Here is the disassembly of main() from the version that calls localSysBeep() (i.e., no toolbox calls are made). The code looks clean: main: 00000000: 4E56 FFD4 LINK A6,#$FFD4 00000004: 48E7 1800 MOVEM.L D3/D4,-(A7) 00000008: 7601 MOVEQ #$01,D3 0000000A: 3D43 FFD4 MOVE.W D3,$FFD4(A6) 0000000E: 7002 MOVEQ #$02,D0 00000010: 3D40 FFD6 MOVE.W D0,$FFD6(A6) 00000014: 7003 MOVEQ #$03,D0 00000016: 3D40 FFD8 MOVE.W D0,$FFD8(A6) 0000001A: 382E FFD6 MOVE.W $FFD6(A6),D4 0000001E: C9FC 0007 MULS.W #$0007,D4 00000022: 7018 MOVEQ #$18,D0 00000024: D840 ADD.W D0,D4 00000026: D86E FFD6 ADD.W $FFD6(A6),D4 0000002A: 3D44 FFD4 MOVE.W D4,$FFD4(A6) [continued in the reply]
#7244From: Ken BroomfieldJul 20, 1993 8:29 PM
[continued] 0000002E: 3D6E FFD8 FFD6 MOVE.W $FFD8(A6),$FFD6(A6) 00000034: 486E FFD4 PEA $FFD4(A6) 00000038: 4EBA 0000 JSR takebase(CBase&) 0000003C: 3F03 MOVE.W D3,-(A7) 0000003E: 4EBA 0000 JSR localSysBeep(short) 00000042: 4CDF 0018 MOVEM.L (A7)+,D3/D4 00000046: 4E5E UNLK A6 00000048: 4E75 RTS 0000004A Here is the disassembly of main() from the version that calls SysBeep() (i.e., a single toolbox call is being made). The code has lots of unneeded instructions: main: 00000000: 4E56 FFDC LINK A6,#$FFDC 00000004: 48E7 1C38 MOVEM.L D3-D5/A2-A4,-(A7) 00000008: 47EE FFDC LEA $FFDC(A6),A3 0000000C: 284B MOVEA.L A3,A4 0000000E: 7601 MOVEQ #$01,D3 00000010: 3883 MOVE.W D3,(A4) 00000012: 7002 MOVEQ #$02,D0 00000014: 3940 0002 MOVE.W D0,$0002(A4) 00000018: 7803 MOVEQ #$03,D4 0000001A: 3944 0004 MOVE.W D4,$0004(A4) 0000001E: 45EE FFDC LEA $FFDC(A6),A2 00000022: 2D4A FFE4 MOVE.L A2,$FFE4(A6) 00000026: 3A2A 0002 MOVE.W $0002(A2),D5 0000002A: 3D45 FFFC MOVE.W D5,$FFFC(A6) 0000002E: CBFC 0007 MULS.W #$0007,D5 00000032: 302A 0004 MOVE.W $0004(A2),D0 00000036: E960 ASL.W D4,D0 00000038: DA40 ADD.W D0,D5 0000003A: 302E FFFC MOVE.W $FFFC(A6),D0 0000003E: DA40 ADD.W D0,D5 00000040: 3485 MOVE.W D5,(A2) 00000042: 41EE FFDC LEA $FFDC(A6),A0 00000046: 2D48 FFE8 MOVE.L A0,$FFE8(A6) 0000004A: 2D48 FFF4 MOVE.L A0,$FFF4(A6) 0000004E: 3028 0004 MOVE.W $0004(A0),D0 00000052: 3140 0002 MOVE.W D0,$0002(A0) 00000056: 41EE FFDC LEA $FFDC(A6),A0 0000005A: 2D48 FFF8 MOVE.L A0,$FFF8(A6) 0000005E: 486E FFDC PEA $FFDC(A6) 00000062: 4EBA 0000 JSR takebase(CBase&) 00000066: 3F03 MOVE.W D3,-(A7) 00000068: A9C8 _SysBeep 0000006A: 4CDF 1C38 MOVEM.L (A7)+,D3-D5/A2-A4 0000006E: 4E5E UNLK A6 00000070: 4E75 RTS 00000072 Lastly, it sure would have been nice to have gotten a response from you three weeks ago. –Ken ~~~~