Debian Codegraph @

src/cmp/cmpbackend-cxx/cmpc-opt-num.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 2010, Juan Jose Garcia Ripoll
6 ;;;; Copyright (c) 2023, Daniel Kochmański
7 ;;;;
8 ;;;; See the file 'LICENSE' for the copyright details.
9 ;;;;
10
11 ;;;; C/C++ specific optimizer for numerical expressions.
12
13 (in-package "COMPILER")
14
15 ;;;
16 ;;; Bit fiddling. It is a bit tricky because C does not allow
17 ;;; shifts in << or >> which exceed the integer size. In those
18 ;;; cases the compiler may do whatever it wants (and gcc does!)
19 ;;;
20 (define-c-inliner shift (return-type argument orig-shift)
21 (let* ((arg-type (loc-lisp-type argument))
22 (arg-c-type (lisp-type->host-type arg-type))
23 (return-c-type (lisp-type->host-type return-type))
24 (shift (loc-immediate-value orig-shift)))
25 (if (or (not (c-integer-host-type-p arg-c-type))
26 (not (c-integer-host-type-p return-c-type)))
27 (produce-inline-loc (list argument orig-shift) '(:object :fixnum) '(:object)
28 "ecl_ash(#0,#1)" nil t)
29 (let* ((arg-bits (c-integer-host-type-bits arg-c-type))
30 (return-bits (c-integer-host-type-bits return-c-type))
31 (max-type (if (and (plusp shift)
32 (< arg-bits return-bits))
33 return-c-type
34 arg-c-type)))
35 (produce-inline-loc (list argument) (list max-type) (list return-type)
36 (format nil
37 (if (minusp shift)
38 "((#0) >> (~D))"
39 "((#0) << (~D))")
40 (abs shift))
41 nil t)))))
42
43 ;;;
44 ;;; Inliners for arithmetic operations.
45 ;;;
46
47 (defun most-generic-number-host-type (r1 r2)
48 (let* ((r1 (host-type-record r1))
49 (r2 (host-type-record r2)))
50 (host-type-name (if (< (host-type-index r1) (host-type-index r2))
51 r2
52 r1))))
53
54 (defun inline-binop (expected-type arg1 arg2 consing non-consing)
55 (let ((arg1-type (loc-lisp-type arg1))
56 (arg2-type (loc-lisp-type arg2)))
57 (if (and (policy-assume-right-type)
58 (c-number-type-p expected-type)
59 (c-number-type-p arg1-type)
60 (c-number-type-p arg2-type))
61 ;; The input arguments have to be coerced to a C
62 ;; type that fits the output, to avoid overflow which
63 ;; would happen if we used say, long c = (int)a * (int)b
64 ;; as the output would be an integer, not a long.
65 (let* ((arg1-rep (lisp-type->host-type arg1-type))
66 (arg2-rep (lisp-type->host-type arg2-type))
67 (out-rep (lisp-type->host-type expected-type))
68 (max-rep (most-generic-number-host-type
69 (most-generic-number-host-type
70 arg1-rep arg2-rep) out-rep))
71 (max-name (host-type->c-name max-rep)))
72 (produce-inline-loc
73 (list arg1 arg2)
74 (list arg1-rep arg2-rep)
75 (list max-rep)
76 (format nil "(~@[(~A)~]#0)~A(~@[(~A)~]#1)"
77 (unless (eq arg1-rep max-rep) max-name)
78 non-consing
79 (unless (eq arg2-rep max-rep) max-name))
80 nil t))
81 (produce-inline-loc (list arg1 arg2) '(:object :object) '(:object)
82 consing nil t))))
83
84 (defun inline-arith-unop (expected-type arg1 consing non-consing)
85 (let ((arg1-type (loc-lisp-type arg1)))
86 (if (and (policy-assume-right-type)
87 (c-number-type-p expected-type)
88 (c-number-type-p arg1-type))
89 (produce-inline-loc (list arg1)
90 (list (lisp-type->host-type arg1-type))
91 (list (lisp-type->host-type expected-type))
92 non-consing nil t)
93 (produce-inline-loc (list arg1) '(:object :object) '(:object)
94 consing nil t))))
95
96 (define-c-inliner * (return-type &rest arguments &aux arg1 arg2)
97 (when (null arguments)
98 (return (make-vv :host-type :fixnum :value 1)))
99 (setf arg1 (pop arguments))
100 (when (null arguments)
101 (return arg1))
102 (setf arg2 (pop arguments))
103 (when (null arguments)
104 (return (inline-binop return-type arg1 arg2 "ecl_times(#0,#1)" #\*)))
105 (cmperr "The C inliner for (FUNCTION *) expected at most 2 arguments."))
106
107 (define-c-inliner + (return-type &rest arguments &aux arg1 arg2)
108 (when (null arguments)
109 (return (make-vv :host-type :fixnum :value 0)))
110 (setf arg1 (pop arguments))
111 (when (null arguments)
112 (return arg1))
113 (setf arg2 (pop arguments))
114 (when (null arguments)
115 (return (inline-binop return-type arg1 arg2 "ecl_plus(#0,#1)" #\+)))
116 (cmperr "The C inliner for (FUNCTION +) expected at most 2 arguments."))
117
118 (define-c-inliner - (return-type arg1 &rest arguments &aux arg2)
119 (when (null arguments)
120 (return (inline-arith-unop return-type arg1 "ecl_negate(#0)" "-(#0)")))
121 (setf arg2 (pop arguments))
122 (when (null arguments)
123 (return (inline-binop return-type arg1 arg2 "ecl_minus(#0,#1)" #\-)))
124 (cmperr "The C inliner for (FUNCTION -) expected at most 2 arguments."))
125
126 (define-c-inliner / (return-type arg1 &rest arguments &aux arg2)
127 (when (null arguments)
128 (return (inline-arith-unop return-type arg1
129 "ecl_divide(ecl_make_fixnum(1),(#0))" "1/(#0)")))
130 (setf arg2 (pop arguments))
131 (when (null arguments)
132 (return (inline-binop return-type arg1 arg2 "ecl_divide(#0,#1)" #\/)))
133 (cmperr "The C inliner for (FUNCTION /) expected at most 2 arguments."))
134
135 (define-c-inliner float (return-type arg &optional float)
136 (let ((arg-c-type (lisp-type->host-type (loc-lisp-type arg)))
137 (flt-c-type (and float (lisp-type->host-type (loc-lisp-type float)))))
138 (if (member arg-c-type '(:float :double :long-double))
139 (when (or (null float) (eq arg-c-type flt-c-type))
140 arg)
141 (when (member flt-c-type '(:float :double :long-double))
142 (produce-inline-loc (list arg)
143 (list :object)
144 (list flt-c-type)
145 (ecase flt-c-type
146 (:float "ecl_to_float(#0)")
147 (:double "ecl_to_double(#0)")
148 (:long-double "ecl_to_long_double(#0)"))
149 nil t)))))