Path: utzoo!utgpu!utstat!jarvis.csri.toronto.edu!mailrus!cornell!uw-beaver!uw-june!uw-entropy!dataio!bright From: bright@Data-IO.COM (Walter Bright) Newsgroups: comp.lang.c Subject: Re: C optimizer Keywords: C Ultrix Message-ID: <1877@dataio.Data-IO.COM> Date: 15 Feb 89 19:41:03 GMT References: <515@larry.UUCP> Reply-To: bright@dataio.Data-IO.COM (Walter Bright) Organization: Data I/O Corporation; Redmond, WA Lines: 26 In article <515@larry.UUCP> jwp@larry.UUCP (Jeffrey W Percival) writes: >I have a question about how much optimizing I should worry about when >writing programs in C. > x = (1 + cos(r)) / (cos(r) * sin(r)); > y = (cos(r) - sin(r)) / (1 + sin(r)); >Now, can I expect the compiler to form only one call to sin and >cos? In general, function calls are *not* regarded as common subexpressions, since the compiler usually knows nothing about the function, and it may have side effects. The cases where func() *might* be done only once are: 1) If func() is an inline function, like in C++, and the compiler recognizes the expansions as being common. 2) If func() is a compiler built-in function, so the compiler knows that there are no side effects, and so it's common. 3) If func() is a compiler built-in function, and the target machine implements func() as a small number of machine instructions, and a peephole optimizer in the code generator commons it. Obviously, if func() is a user-defined function, and its implementation is in another file, there is no way that the compiler can recognize it as common. Of course, the best way to determine if the compiler makes it common is to compile it and see!