home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcl / src-16f.lha / compiler / knownfun.lisp < prev    next >
Encoding:
Text File  |  1992-02-19  |  10.3 KB  |  279 lines

  1. ;;; -*- Package: C; Log: C.Log -*-
  2. ;;;
  3. ;;; **********************************************************************
  4. ;;; This code was written as part of the CMU Common Lisp project at
  5. ;;; Carnegie Mellon University, and has been placed in the public domain.
  6. ;;; If you want to use this code or any part of CMU Common Lisp, please contact
  7. ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
  8. ;;;
  9. (ext:file-comment
  10.   "$Header: knownfun.lisp,v 1.15 92/02/19 16:14:21 wlott Exp $")
  11. ;;;
  12. ;;; **********************************************************************
  13. ;;;
  14. ;;;    This file contains stuff for maintaining a database of special
  15. ;;; information about functions known to the compiler.  This includes semantic
  16. ;;; information such as side-effects and type inference functions as well as
  17. ;;; transforms and IR2 translators.
  18. ;;;
  19. ;;; Written by Rob MacLachlan
  20. ;;;
  21. (in-package 'c)
  22.  
  23. (export '(call unsafe unwind any foldable flushable movable predicate))
  24.  
  25. ;;;; IR1 boolean function attributes:
  26. ;;;
  27. ;;;    There are a number of boolean attributes of known functions which we
  28. ;;; like to have in IR1.  This information is mostly side effect information of
  29. ;;; a sort, but it is different from the kind of information we want in IR2.
  30. ;;; We aren't interested in a fine breakdown of side effects, since we do very
  31. ;;; little code motion on IR1.  We are interested in some deeper semantic
  32. ;;; properties such as whether it is safe to pass stack closures to.
  33. ;;;
  34. (def-boolean-attribute ir1
  35.   ;;
  36.   ;; May call functions that are passed as arguments.  In order to determine
  37.   ;; what other effects are present, we must find the effects of all arguments
  38.   ;; that may be functions.
  39.   call
  40.   ;;
  41.   ;; May incorporate function or number arguments into the result or somehow
  42.   ;; pass them upward.  Note that this applies to any argument that *might* be
  43.   ;; a function or number, not just the arguments that always are.
  44.   unsafe
  45.   ;;
  46.   ;; May fail to return during correct execution.  Errors are O.K.
  47.   unwind
  48.   ;;
  49.   ;; The (default) worst case.  Includes all the other bad things, plus any
  50.   ;; other possible bad thing.  If this is present, the above bad attributes
  51.   ;; will be explicitly present as well.
  52.   any
  53.   ;;
  54.   ;; May be constant-folded.  The function has no side effects, but may be
  55.   ;; affected by side effects on the arguments.  e.g. SVREF, MAPC.  Functions
  56.   ;; that side-effect their arguments are not considered to be foldable.
  57.   ;; Although it would be "legal" to constant fold them (since it "is an error"
  58.   ;; to modify a constant), we choose not to mark theses functions as foldable
  59.   ;; in this database.
  60.   foldable
  61.   ;;
  62.   ;; May be eliminated if value is unused.  The function has no side effects
  63.   ;; except possibly CONS.  If a function is defined to signal errors, then it
  64.   ;; is not flushable even if it is movable or foldable.
  65.   flushable
  66.   ;;
  67.   ;; May be moved with impunity.  Has no side effects except possibly CONS, and
  68.   ;; is affected only by its arguments.
  69.   movable
  70.   ;;
  71.   ;; Function is a true predicate likely to be open-coded.  Convert any
  72.   ;; non-conditional uses into (IF <pred> T NIL).
  73.   predicate
  74.   ;;
  75.   ;; Inhibit any warning for compiling a recursive definition.  [Normally the
  76.   ;; compiler warns when compiling a recursive definition for a known function,
  77.   ;; since it might be a botched interpreter stub.]
  78.   recursive
  79.   ;;
  80.   ;; Function does explicit argument type checking, so the declared type should
  81.   ;; not be asserted when a definition is compiled.
  82.   explicit-check)
  83.  
  84. (defstruct (function-info
  85.         (:print-function %print-function-info))
  86.   ;;
  87.   ;; Boolean attributes of this function.
  88.   (attributes (required-argument) :type attributes)
  89.   ;;
  90.   ;; A list of Transform structures describing transforms for this function.
  91.   (transforms () :type list)
  92.   ;;
  93.   ;; A function which computes the derived type for a call to this function by
  94.   ;; examining the arguments.  This is null when there is no special method for
  95.   ;; this function.
  96.   (derive-type nil :type (or function null))
  97.   ;;
  98.   ;; A function that does random unspecified code transformations by directly
  99.   ;; hacking the IR.  Returns true if further optimizations of the call
  100.   ;; shouldn't be attempted.
  101.   (optimizer nil :type (or function null))
  102.   ;;
  103.   ;; If true, a special-case LTN annotation method that is used in place of the
  104.   ;; standard type/policy template selection.  It may use arbitrary code to
  105.   ;; choose a template, decide to do a full call, or conspire with the
  106.   ;; IR2-Convert method to do almost anything.  The Combination node is passed
  107.   ;; as the argument.
  108.   (ltn-annotate nil :type (or function null))
  109.   ;;
  110.   ;; If true, the special-case IR2 conversion method for this function.  This
  111.   ;; deals with funny functions, and anything else that can't be handled using
  112.   ;; the template mechanism.  The Combination node and the IR2-Block are passed
  113.   ;; as arguments.
  114.   (ir2-convert nil :type (or function null))
  115.   ;;
  116.   ;; A list of all the templates that could be used to translate this function
  117.   ;; into IR2, sorted by increasing cost.
  118.   (templates nil :type list)
  119.   ;;
  120.   ;; If non-null, then this function is a unary type predicate for this type.
  121.   (predicate-type nil :type (or ctype null)))
  122.  
  123. (defprinter function-info
  124.   (transforms :test transforms)
  125.   (derive-type :test derive-type)
  126.   (optimizer :test optimizer)
  127.   (ltn-annotate :test ltn-annotate)
  128.   (ir2-convert :test ir2-convert)
  129.   (templates :test templates)
  130.   (predicate-type :test predicate-type))
  131.  
  132.  
  133. ;;;; Interfaces to defining macros:
  134.  
  135. ;;; The TRANSFORM structure represents an IR1 transform.
  136. ;;;
  137. (defstruct (transform (:print-function %print-transform))
  138.   ;;
  139.   ;; The function-type which enables this transform.
  140.   (type (required-argument) :type ctype)
  141.   ;;
  142.   ;; The transformation function.  Takes the Combination node and Returns a
  143.   ;; lambda, or throws out.
  144.   (function (required-argument) :type function)
  145.   ;;
  146.   ;; String used in efficency notes.
  147.   (note (required-argument) :type string)
  148.   ;;
  149.   ;; T if we should spew a failure note even if speed=brevity.
  150.   (important nil :type (member t nil)))
  151.  
  152. (defprinter transform type note)
  153.  
  154.  
  155. ;;; %Deftransform  --  Internal
  156. ;;;
  157. ;;;    Grab the Function-Info and enter the function, replacing any old one
  158. ;;; with the same type and note.
  159. ;;;
  160. (proclaim '(function %deftransform
  161.              (t list function &optional (or string null)
  162.             (member t nil))))
  163. (defun %deftransform (name type fun &optional note important)
  164.   (let* ((ctype (specifier-type type))
  165.      (note (or note "optimize"))
  166.      (info (function-info-or-lose name))
  167.      (old (find-if #'(lambda (x)
  168.                (and (type= (transform-type x) ctype)
  169.                 (string-equal (transform-note x) note)
  170.                 (eq (transform-important x) important)))
  171.                (function-info-transforms info))))
  172.     (if old
  173.     (setf (transform-function old) fun (transform-note old) note)
  174.     (push (make-transform :type ctype :function fun :note note
  175.                   :important important)
  176.           (function-info-transforms info)))
  177.     name))
  178.  
  179.  
  180. ;;; %Defknown  --  Internal
  181. ;;;
  182. ;;;    Make a function-info structure with the specified type, attributes and
  183. ;;; optimizers.
  184. ;;;
  185. (proclaim '(function %defknown (list list attributes &key (derive-type function)
  186.                      (optimizer function))))
  187. (defun %defknown (names type attributes &key derive-type optimizer)
  188.   (let ((ctype (specifier-type type))
  189.     (info (make-function-info :attributes attributes
  190.                   :derive-type derive-type
  191.                   :optimizer optimizer))
  192.     (*info-environment* (or (backend-info-environment *target-backend*)
  193.                 *info-environment*)))
  194.     (dolist (name names)
  195.       (setf (info function type name) ctype)
  196.       (setf (info function where-from name) :declared)
  197.       (setf (info function kind name) :function)
  198.       (setf (info function info name) info)))
  199.   names)
  200.  
  201.  
  202. ;;; Function-Info-Or-Lose  --  Internal
  203. ;;;
  204. ;;;    Return the Function-Info for name or die trying.  Since this is used by
  205. ;;; people who want to modify the info, and the info may be shared, we copy it.
  206. ;;; We don't have to copy the lists, since each function that has generators or
  207. ;;; transforms has already been through here.
  208. ;;;
  209. (proclaim '(function function-info-or-lose (t) function-info))
  210. (defun function-info-or-lose (name)
  211.   (let ((*info-environment* (or (backend-info-environment *target-backend*)
  212.                 *info-environment*)))
  213.     (let ((old (info function info name)))
  214.       (unless old (error "~S is not a known function." name))
  215.       (setf (info function info name) (copy-function-info old)))))
  216.  
  217.  
  218. ;;;; Generic type inference methods:
  219.  
  220. ;;; RESULT-TYPE-xxx-ARG  --  Interface
  221. ;;;
  222. ;;;    Derive the type to be the type of the xxx'th arg.  This can normally
  223. ;;; only be done when the result value is that argument.
  224. ;;;
  225. (defun result-type-first-arg (call)
  226.   (declare (type combination call))
  227.   (let ((cont (first (combination-args call))))
  228.     (when cont (continuation-type cont))))
  229. ;;;
  230. (defun result-type-last-arg (call)
  231.   (declare (type combination call))
  232.   (let ((cont (car (last (combination-args call)))))
  233.     (when cont (continuation-type cont))))
  234.  
  235.  
  236. ;;; RESULT-TYPE-FLOAT-CONTAGION  --  Interface
  237. ;;;
  238. ;;;    Derive the result type according to the float contagion rules, but
  239. ;;; always return a float.  This is used for irrational functions that preserve
  240. ;;; realness of their arguments.
  241. ;;;
  242. (defun result-type-float-contagion (call)
  243.   (declare (type combination call))
  244.   (reduce #'numeric-contagion (combination-args call)
  245.       :key #'continuation-type
  246.       :initial-value (specifier-type 'single-float)))
  247.  
  248.  
  249. ;;; SEQUENCE-RESULT-NTH-ARG  --  Internal
  250. ;;;
  251. ;;;    Return a closure usable as a derive-type method for accessing the N'th
  252. ;;; argument.  If arg is a list, result is a list.  If arg is a vector, result
  253. ;;; is a vector with the same element type.
  254. ;;;
  255. (defun sequence-result-nth-arg (n)
  256.   #'(lambda (call)
  257.       (declare (type combination call))
  258.       (let ((cont (nth (1- n) (combination-args call))))
  259.     (when cont
  260.       (let ((type (continuation-type cont)))
  261.         (if (array-type-p type)
  262.         (specifier-type
  263.          `(vector ,(type-specifier (array-type-element-type type))))
  264.         (let ((ltype (specifier-type 'list)))
  265.           (when (csubtypep type ltype)
  266.             ltype))))))))
  267.  
  268.  
  269. ;;; RESULT-TYPE-SPECIFIER-NTH-ARG  --  Interface
  270. ;;;
  271. ;;;    Derive the type to be the type specifier which is the N'th arg.
  272. ;;; 
  273. (defun result-type-specifier-nth-arg (n)
  274.   #'(lambda (call)
  275.       (declare (type combination call))
  276.       (let ((cont (nth (1- n) (combination-args call))))
  277.     (when (and cont (constant-continuation-p cont))
  278.       (specifier-type (continuation-value cont))))))
  279.