home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11635 < prev    next >
Encoding:
Text File  |  1992-07-27  |  5.8 KB  |  156 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!timbuk.cray.com!hemlock.cray.com!dsf
  3. From: dsf@cray.com (Dan Frankowski)
  4. Subject: Multi-operator expressions
  5. Message-ID: <1992Jul27.154135.17551@hemlock.cray.com>
  6. Summary: How to do function calls for multi-operator expressions?
  7. Lines: 145
  8. Organization: Cray Research, Inc.
  9. Date: 27 Jul 92 15:41:35 CDT
  10.  
  11. This posting is long.  :-(  I ask, "How do we optimize expressions
  12. involving more than one operator, such as A * B + C, x = x + y,
  13. and list = one + two + three + four?"
  14.  
  15. There is a statement of the problem, for which I yearn to see a clean
  16. solution.  There is also the beginnings of a proposal for a language
  17. extension to allow user-specified compile-time expression
  18. optimization.  I'm not proposing a language extension!  It's just for
  19. discussion, folks.
  20.  
  21. DISCLAIMER: This ain't official Cray business.
  22.  
  23. Dan Frankowski            dsf@palm.cray.com     (612) 683-5099
  24.  
  25. **********************************************************************
  26.  
  27. Title: Optimizing expressions involving many operators
  28. Alternate title: Function calls for expressions involving many operators
  29.  
  30. Motivation
  31. ----------
  32.  
  33. In many cases there are important optimizations for expressions that
  34. involve more than one operator.  For example:
  35.  
  36.   A + B * C (where A, B, and C are matrices) on a Cray should be a
  37.   Cray Assembly Language (CAL) routine to pipeline the results of
  38.   multiplication to the adder while more multiplication is done.
  39.  
  40. This is a serious optimization, resulting in noticeable improvement in
  41. performance.  It is inhibited by the fact that in this expression,
  42. operator* doesn't know about operator+ nor vice-versa.  But to
  43. implement it properly, _something_ must know that both addition and
  44. multiplication are taking place.
  45.  
  46. Two present-day solutions:
  47.  
  48. - Specify the expression as addMult(A,B,C).
  49.  
  50. - Overload + and * to build a parse tree, and then make = optimize
  51.   the parse tree and perform all of the necessary calculations.
  52.   (See Jim Adcock's 1992 June 15 posting to comp.lang.c++)
  53.  
  54. The first solution is completely counter to the style of C++.  The
  55. second has runtime overhead for evaluating a parse tree, which is the
  56. bread and butter of a compiler.  It also does not account for function
  57. calls with A + B * C as a parameter, or any other places where an
  58. expression may be used.
  59.  
  60. Wouldn't it be nice to have a simple notation to allow the programmer
  61. to specify functions to be put in the place of expressions that
  62. involve more than one operator?  The programmer could then tell the
  63. compiler, "Make A + B * C into AddMult(A,B,C)" much as we now say
  64. "Make A + B into operator+(const Matrix&, const Matrix&)" (which can
  65. be an inlined call to Add(A,B)).
  66.  
  67. Proposal
  68. --------
  69.  
  70. For the matrix example above, we could use
  71.  
  72. Matrix operator+*(const Matrix&, const Matrix&, const Matrix&);
  73.  
  74. The compiler would transform A + B * C into operator+*(A,B,C).  In
  75. other words, use operator<op1><op2>(<arguments>) to specify a two
  76. operator expression.  This is easily extended to any fixed number of
  77. operators.
  78.  
  79. This does not conflict with -=, +=, .., OP= because a OP b is not an
  80. lvalue, so a OP b = c isn't legal.  For example, operator+= is
  81. unambiguously a += b; it cannot be a + b = c.  (See below for many
  82. problems, however.)
  83.  
  84. Other examples
  85. --------------
  86.  
  87. Most classes should optimize x = x + y in user code to x += y
  88. automatically, because then the "addition" may be done in place-- that
  89. is, without allocating additional storage.  The same holds for x = x / y,
  90. x = x * y, x = x - y, x = x & y, and so on.  This is a considerable
  91. optimization, and one which the user of the class may not (and ideally
  92. should not need to) know about.
  93.  
  94. Two concrete examples similar to this:
  95.  
  96. - In the string routines of libg++,
  97.  
  98.     cat(x, y, z) for z = x + y
  99.     cat(z, y, x, x) for x = z + y + x
  100.     y.prepend(x) for y = x + y
  101.  
  102. - A "+" overloaded to mean concatenation, when encountering
  103.  
  104.     list = one + two + three + four + five + six
  105.  
  106.   does not need to generate five temporaries and delete them.
  107.  
  108. This seems a continuation of the ideas of C++: it has allowed
  109. programmers to specify operators, traditionally the domain of the
  110. compiler; why not optimizations on those operators?  I prefer to view
  111. these optimizations as function calls for multi-operator expressions.
  112. Does anyone have other examples where it would be very useful to
  113. replace a multi-operator expression with a single function call?
  114.  
  115. Problems
  116. --------
  117.  
  118. - This ruins parsing completely, does it not?  Currently, when a + token
  119.   is passed from the lexical analyzer to the parser, we are sure that it
  120.   is a binary operator.  For the ternary operator ?:, when we encounter
  121.   a ? token, we are sure it is part of a ternary operator.  Now, with
  122.   operator+*() defined, when we encounter a + token, we are not sure how
  123.   many operators and operands are left on the input stream for a match.
  124.   Has the grammar for C++ now become ambiguous as well?  Help!
  125.  
  126. - This doesn't solve the linked list example above.  How could
  127.   we specify a variable number of operands?
  128.  
  129. - Is operator++() then for a + b + c or a++?  The syntax for
  130.   operator()++ is already complicated by distinguishing prefix and
  131.   postfix.
  132.  
  133. - This does not allow us to restrict the operands of an expression,
  134.   so that only x = x + y would translate to operator=+() (which could
  135.   be inlined to call operator+=() ), and not x = y + z.
  136.  
  137. - Is a + b = c legal in any extended versions of C or C++?  Then the
  138.   proposal would break programs that use such expressions.
  139.  
  140. Questions
  141. ---------
  142.  
  143. Are there other examples?
  144. Is this worth the trouble?
  145. Isn't there a better syntax?
  146. What is a notation to specify restrictions on the operands?
  147.  
  148. References
  149. ----------
  150. <aldavi01.708583569@starbase.spd.louisville.edu>
  151.   "operator+ operator* for N objects, ?"
  152. <1992Jun15.210513.11679@microsoft.com>
  153.   "Re: operator+ operator* for N objects, ?"
  154. -- 
  155. Dan Frankowski            dsf@palm.cray.com     (612) 683-5099
  156.