Generate a defmacro form that transforms code like this:
(with-constant-values (start end) env ((start end) (let* ((counted? (or (> start 0) end)) (test (if counted? `(and ,more? (cl:< ,index ,size)) more?)) (inc (when counted? `((cl:incf ,index))))) (values `((,table ,form) ,@(when counted? `((,index ,start) (,size ,(or end `(hash-table-count ,table)))))) `((with-custom-hash-table (with-hash-table-iterator (,next ,table) ,@body))) (make-iter-value test inc) (make-iter-place test inc)))) (nil (values `((,table ,form) (,index ,start) (,size (or ,end (hash-table-count ,table)))) `((with-custom-hash-table (with-hash-table-iterator (,next ,table) ,@body))) (make-iter-value `(and ,more? (cl:< ,index ,size)) `((cl:incf ,index))) (make-iter-place `(and ,more? (cl:< ,index ,size)) `((cl:incf ,index))))))

Pattern to recognize: Macro call that should be transformed by with-constant-values

Macro definition:
(defmacro with-constant-values ((&rest things) env &body clauses)
  "Check whether one or more forms are constant and retrieve their values.

   THINGS:

     A list of bindings each of the form (VAR INITFORM).

     Each INITFORM is evaluated and the result is checked whether it
     is a constant form in the environment ENV. If so the constant
     value is stored in VAR otherwise the resulting form (to which
     INITFORM evaluates) itself is stored in VAR.

     Alternatively each element of THINGS can be a symbol in which
     case it is a shorthand for (VAR VAR).

   ENV:

     The environment in which to check whether the forms are
     constants.

   CLAUSES:

     A list of clauses each of the form (VARS . FORMS), where
     VARS is a list of variables, listed in THINGS, which should be
     constant in order for the corresponding FORMS to be evaluated.

     If all variables in VARS are constants FORMS are evaluated in an
     implicit PROGN, with the result of the last form returned.

     If not all of VARS are constant, FORMS are not evaluated and the
     next clause is tried. If no clause succeeds NIL is returned."

  (let ((const-vars (pairlis (mapcar #'ensure-car things)
                             (make-gensym-list (length things) "CONST?"))))

    (labels ((make-get-value (thing body)
               (destructuring-bind (var &optional (form var))
                   (ensure-list thing)

                 (let ((const-var (cdr (assoc var const-vars))))
                   (assert const-var)

                   `(multiple-value-bind (,var ,const-var)
                        (constant-form-value ,form ,env)

                      ,body))))

             (const-var (var)
               (let ((cvar (cdr (assoc var const-vars))))
                 (unless cvar
                   (error "Variable ~s in clause not one of ~s."
                          var (mapcar #'ensure-car things)))

                 cvar))

             (make-clause (clause)
               (destructuring-bind (vars &rest body) clause
                 `((and ,@(mapcar #'const-var vars))
                   ,@body))))

      (cl:reduce
       #'make-get-value
       things
       :from-end t
       :initial-value
       `(cond ,@(mapcar #'make-clause clauses))))))

Macroexpansion:
(multiple-value-bind (start #:const?144) (constant-form-value start env) (multiple-value-bind (end #:const?145) (constant-form-value end env) (cond ((and #:const?144 #:const?145) (let* ((counted? (or (> start 0) end)) (test (if counted? (sb-int:quasiquote (and #S(sb-impl::comma :expr more? :kind 0) (< #S(sb-impl::comma :expr index :kind 0) #S(sb-impl::comma :expr size :kind 0)))) more?)) (inc (when counted? (sb-int:quasiquote ((incf #S(sb-impl::comma :expr index :kind 0))))))) (values (sb-int:quasiquote ((#S(sb-impl::comma :expr table :kind 0) #S(sb-impl::comma :expr form :kind 0)) #S(sb-impl::comma :expr (when counted? (sb-int:quasiquote ((#S(sb-impl::comma :expr index :kind 0) #S(sb-impl::comma :expr start :kind 0)) (#S(sb-impl::comma :expr size :kind 0) #S(sb-impl::comma :expr (or end (sb-int:quasiquote (hash-table-count #S(sb-impl::comma :expr table :kind 0)))) :kind 0))))) :kind 2))) (sb-int:quasiquote ((with-custom-hash-table (with-hash-table-iterator (#S(sb-impl::comma :expr next :kind 0) #S(sb-impl::comma :expr table :kind 0)) #S(sb-impl::comma :expr body :kind 2))))) (make-iter-value test inc) (make-iter-place test inc)))) ((and) (values (sb-int:quasiquote ((#S(sb-impl::comma :expr table :kind 0) #S(sb-impl::comma :expr form :kind 0)) (#S(sb-impl::comma :expr index :kind 0) #S(sb-impl::comma :expr start :kind 0)) (#S(sb-impl::comma :expr size :kind 0) (or #S(sb-impl::comma :expr end :kind 0) (hash-table-count #S(sb-impl::comma :expr table :kind 0)))))) (sb-int:quasiquote ((with-custom-hash-table (with-hash-table-iterator (#S(sb-impl::comma :expr next :kind 0) #S(sb-impl::comma :expr table :kind 0)) #S(sb-impl::comma :expr body :kind 2))))) (make-iter-value (sb-int:quasiquote (and #S(sb-impl::comma :expr more? :kind 0) (< #S(sb-impl::comma :expr index :kind 0) #S(sb-impl::comma :expr size :kind 0)))) (sb-int:quasiquote ((incf #S(sb-impl::comma :expr index :kind 0))))) (make-iter-place (sb-int:quasiquote (and #S(sb-impl::comma :expr more? :kind 0) (< #S(sb-impl::comma :expr index :kind 0) #S(sb-impl::comma :expr size :kind 0)))) (sb-int:quasiquote ((incf #S(sb-impl::comma :expr index :kind 0))))))))))