17ec681f3Smrg/*
27ec681f3Smrg * Copyright (C) 2019 Collabora, Ltd.
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
207ec681f3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
217ec681f3Smrg * SOFTWARE.
227ec681f3Smrg *
237ec681f3Smrg * Authors (Collabora):
247ec681f3Smrg *      Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
257ec681f3Smrg */
267ec681f3Smrg
277ec681f3Smrg#ifndef __LCRA_H
287ec681f3Smrg#define __LCRA_H
297ec681f3Smrg
307ec681f3Smrg#include <stdbool.h>
317ec681f3Smrg#include <stdint.h>
327ec681f3Smrg
337ec681f3Smrgstruct lcra_state {
347ec681f3Smrg        unsigned node_count;
357ec681f3Smrg
367ec681f3Smrg        /* Alignment for node in log2(bytes)+1. Since alignment must be
377ec681f3Smrg         * non-negative power-of-two, the elements are strictly positive
387ec681f3Smrg         * integers. Zero is the sentinel for a missing node. In upper word,
397ec681f3Smrg         * bound. */
407ec681f3Smrg        unsigned *alignment;
417ec681f3Smrg
427ec681f3Smrg        /* Linear constraints imposed. Nested array sized upfront, organized as
437ec681f3Smrg         * linear[node_left][node_right]. That is, calculate indices as:
447ec681f3Smrg         *
457ec681f3Smrg         * Each element is itself a bit field denoting whether (c_j - c_i) bias
467ec681f3Smrg         * is present or not, including negative biases.
477ec681f3Smrg         *
487ec681f3Smrg         * Note for Midgard, there are 16 components so the bias is in range
497ec681f3Smrg         * [-15, 15] so encoded by 32-bit field. */
507ec681f3Smrg
517ec681f3Smrg        uint32_t *linear;
527ec681f3Smrg
537ec681f3Smrg        /* Per node max modulus constraints */
547ec681f3Smrg        uint8_t *modulus;
557ec681f3Smrg
567ec681f3Smrg        /* Classes allow nodes to be partitioned with a starting register.
577ec681f3Smrg         * Classes cannot interfere; that is, they are true partitions in the
587ec681f3Smrg         * usual sense of the word. class_count is the number of classes.
597ec681f3Smrg         * class[] is indexed by a node to get the mapped class. class_start is
607ec681f3Smrg         * biased to all solutions in the class. */
617ec681f3Smrg
627ec681f3Smrg        unsigned class_count;
637ec681f3Smrg        unsigned *class;
647ec681f3Smrg        unsigned *class_start;
657ec681f3Smrg        unsigned *class_size;
667ec681f3Smrg        bool *class_disjoint;
677ec681f3Smrg
687ec681f3Smrg        /* Before solving, forced registers; after solving, solutions. */
697ec681f3Smrg        unsigned *solutions;
707ec681f3Smrg
717ec681f3Smrg        /* For register spilling, the costs to spill nodes (as set by the user)
727ec681f3Smrg         * are in spill_cost[], negative if a node is unspillable. Internally,
737ec681f3Smrg         * spill_class specifies which class to spill (whichever class failed
747ec681f3Smrg         * to allocate) */
757ec681f3Smrg
767ec681f3Smrg        signed *spill_cost;
777ec681f3Smrg        unsigned spill_class;
787ec681f3Smrg};
797ec681f3Smrg
807ec681f3Smrgstruct lcra_state *
817ec681f3Smrglcra_alloc_equations(
827ec681f3Smrg                unsigned node_count, unsigned class_count);
837ec681f3Smrg
847ec681f3Smrgvoid
857ec681f3Smrglcra_free(struct lcra_state *l);
867ec681f3Smrg
877ec681f3Smrgvoid
887ec681f3Smrglcra_set_disjoint_class(struct lcra_state *l, unsigned c1, unsigned c2);
897ec681f3Smrg
907ec681f3Smrgvoid
917ec681f3Smrglcra_set_alignment(struct lcra_state *l, unsigned node, unsigned align_log2, unsigned bound);
927ec681f3Smrg
937ec681f3Smrgvoid
947ec681f3Smrglcra_restrict_range(struct lcra_state *l, unsigned node, unsigned len);
957ec681f3Smrg
967ec681f3Smrgvoid
977ec681f3Smrglcra_add_node_interference(struct lcra_state *l, unsigned i, unsigned cmask_i, unsigned j, unsigned cmask_j);
987ec681f3Smrg
997ec681f3Smrgbool
1007ec681f3Smrglcra_solve(struct lcra_state *l);
1017ec681f3Smrg
1027ec681f3Smrgvoid
1037ec681f3Smrglcra_set_node_spill_cost(struct lcra_state *l, unsigned node, signed cost);
1047ec681f3Smrg
1057ec681f3Smrgsigned
1067ec681f3Smrglcra_get_best_spill_node(struct lcra_state *l);
1077ec681f3Smrg
1087ec681f3Smrg#endif
109