1 |
879 |
markom |
/* cuc.h -- OpenRISC Custom Unit Compiler, main header file
|
2 |
|
|
* Copyright (C) 2002 Marko Mlinar, markom@opencores.org
|
3 |
|
|
*
|
4 |
|
|
* This file is part of OpenRISC 1000 Architectural Simulator.
|
5 |
|
|
*
|
6 |
|
|
* This program is free software; you can redistribute it and/or modify
|
7 |
|
|
* it under the terms of the GNU General Public License as published by
|
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
9 |
|
|
* (at your option) any later version.
|
10 |
|
|
*
|
11 |
|
|
* This program is distributed in the hope that it will be useful,
|
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
|
|
* GNU General Public License for more details.
|
15 |
|
|
*
|
16 |
|
|
* You should have received a copy of the GNU General Public License
|
17 |
|
|
* along with this program; if not, write to the Free Software
|
18 |
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
19 |
|
|
|
20 |
|
|
#ifndef __DATAF_H__
|
21 |
|
|
#define __DATAF_H__
|
22 |
|
|
|
23 |
|
|
/* Maximum number of instructions per function */
|
24 |
|
|
#define MAX_INSNS 0x10000
|
25 |
|
|
#define MAX_OPERANDS 4
|
26 |
|
|
#define MAX_BB 0x100
|
27 |
|
|
#define MAX_REGS 34
|
28 |
|
|
#define FLAG_REG (MAX_REGS - 2)
|
29 |
|
|
#define LRBB_REG (MAX_REGS - 1)
|
30 |
|
|
#define MAX_STACK 0x1000 /* if more, not converted */
|
31 |
|
|
#define MAX_PREROLL 32
|
32 |
|
|
#define MAX_UNROLL 32
|
33 |
|
|
|
34 |
|
|
#define IT_BRANCH 0x0001 /* Branch instruction */
|
35 |
|
|
#define IT_INDELAY 0x0002 /* Instruction is in delay slot */
|
36 |
|
|
#define IT_BBSTART 0x0004 /* BB start marker */
|
37 |
|
|
#define IT_BBEND 0x0008 /* BB end marker */
|
38 |
|
|
#define IT_OUTPUT 0x0010 /* this instruction holds final value of the register */
|
39 |
|
|
#define IT_SIGNED 0x0020 /* Instruction is signed */
|
40 |
|
|
#define IT_MEMORY 0x0040 /* Instruction does memory access */
|
41 |
|
|
#define IT_UNUSED 0x0080 /* dead instruction marker */
|
42 |
|
|
#define IT_FLAG1 0x0100 /* misc flags */
|
43 |
|
|
#define IT_FLAG2 0x0200
|
44 |
|
|
#define IT_VOLATILE 0x0400 /* Should not be moved/removed */
|
45 |
|
|
#define IT_MEMADD 0x0800 /* add before the load -- should not be removed */
|
46 |
|
|
#define IT_COND 0x1000 /* Conditional */
|
47 |
|
|
#define IT_LATCHED 0x2000 /* Output of this instruction is latched/registered */
|
48 |
|
|
#define IT_CUT 0x4000 /* After this instruction register is placed */
|
49 |
|
|
|
50 |
|
|
#define OPT_NONE 0x00
|
51 |
|
|
#define OPT_CONST 0x01
|
52 |
|
|
#define OPT_REGISTER 0x02
|
53 |
|
|
#define OPT_REF 0x04
|
54 |
|
|
#define OPT_JUMP 0x08 /* Jump to an instruction index */
|
55 |
|
|
#define OPT_DEST 0x10 /* This operand is dest */
|
56 |
|
|
#define OPT_BB 0x20 /* Jumpt to BB */
|
57 |
|
|
#define OPT_LRBB 0x40 /* 0 if we came in from left BB, or 1 otherwise */
|
58 |
|
|
|
59 |
907 |
markom |
#define MT_WIDTH 0x007 /* These bits hold memory access width in bytes 1 << x */
|
60 |
|
|
#define MT_BURST 0x008 /* burst start & end markers */
|
61 |
|
|
#define MT_BURSTE 0x010
|
62 |
|
|
#define MT_CALL 0x020 /* This is a call */
|
63 |
|
|
#define MT_LOAD 0x040 /* This memory access does a read */
|
64 |
|
|
#define MT_STORE 0x080 /* This memory access does a write */
|
65 |
|
|
#define MT_SIGNED 0x100 /* Signed memory access */
|
66 |
879 |
markom |
|
67 |
897 |
markom |
#define MO_NONE 0 /* different memory ordering, even if there are dependencies,
|
68 |
|
|
burst can be made, width can change */
|
69 |
|
|
#define MO_WEAK 1 /* different memory ordering, if there cannot be dependencies,
|
70 |
|
|
burst can be made, width can change */
|
71 |
|
|
#define MO_STRONG 2 /* Same memory ordering, burst can be made, width can change */
|
72 |
|
|
#define MO_EXACT 3 /* Exacltly the same memory ordering and widths */
|
73 |
|
|
|
74 |
879 |
markom |
#define BB_INLOOP 0x01 /* This block is inside a loop */
|
75 |
|
|
#define BB_OPTIONAL 0x02
|
76 |
925 |
markom |
#define BB_END 0x04 /* Whether this block is last */
|
77 |
879 |
markom |
#define BB_DEAD 0x08 /* This block is unaccessible -> to be removed */
|
78 |
|
|
|
79 |
932 |
markom |
#define BBID_START MAX_BB /* Start BB pointer */
|
80 |
|
|
#define BBID_END (MAX_BB + 1) /* End BB pointer */
|
81 |
925 |
markom |
|
82 |
879 |
markom |
/* Various macros to minimize code size */
|
83 |
|
|
#define REF(bb,i) (((bb) * MAX_INSNS) + (i))
|
84 |
|
|
#define REF_BB(r) ((r) / MAX_INSNS)
|
85 |
|
|
#define REF_I(r) ((r) % MAX_INSNS)
|
86 |
|
|
#define INSN(ref) bb[REF_BB(ref)].insn[REF_I(ref)]
|
87 |
904 |
markom |
|
88 |
|
|
#ifndef MIN
|
89 |
|
|
# define MIN(x,y) ((x) < (y) ? (x) : (y))
|
90 |
|
|
#endif
|
91 |
879 |
markom |
|
92 |
904 |
markom |
#ifndef MAX
|
93 |
|
|
# define MAX(x,y) ((x) > (y) ? (x) : (y))
|
94 |
|
|
#endif
|
95 |
|
|
|
96 |
879 |
markom |
#define log(x...) fprintf (flog, x)
|
97 |
|
|
|
98 |
883 |
markom |
#define cucdebug(x,s...) {if ((x) <= cuc_debug) printf (s);}
|
99 |
|
|
|
100 |
937 |
markom |
#define CUC_WIDTH_ITERATIONS 256
|
101 |
936 |
markom |
|
102 |
879 |
markom |
/* Options */
|
103 |
883 |
markom |
/* Whether we are debugging cuc (0-9) */
|
104 |
|
|
extern int cuc_debug;
|
105 |
879 |
markom |
|
106 |
|
|
/* Temporary registers by software convention */
|
107 |
939 |
markom |
extern const int caller_saved[MAX_REGS];
|
108 |
879 |
markom |
|
109 |
|
|
typedef struct _dep_list_t {
|
110 |
|
|
unsigned long ref;
|
111 |
|
|
struct _dep_list_t *next;
|
112 |
|
|
} dep_list;
|
113 |
|
|
|
114 |
883 |
markom |
/* Shared list, if marked dead, entry is not used */
|
115 |
|
|
typedef struct _csm_list {
|
116 |
|
|
int ref;
|
117 |
|
|
int cnt;
|
118 |
|
|
int cmovs;
|
119 |
|
|
double size, osize;
|
120 |
|
|
int cmatch;
|
121 |
|
|
int dead;
|
122 |
|
|
int ninsn; /* Number of associated instructions */
|
123 |
|
|
struct _csm_list *from;
|
124 |
|
|
struct _csm_list *next;
|
125 |
897 |
markom |
} cuc_shared_list;
|
126 |
883 |
markom |
|
127 |
897 |
markom |
/* Shared resource item definition */
|
128 |
|
|
typedef struct {
|
129 |
|
|
int ref;
|
130 |
|
|
int cmatch;
|
131 |
|
|
} cuc_shared_item;
|
132 |
|
|
|
133 |
879 |
markom |
/* Implementation specific timings */
|
134 |
|
|
typedef struct {
|
135 |
|
|
int b; /* Basic block # this timing is referring to */
|
136 |
|
|
int preroll; /* How many times was this BB pre/unrolled */
|
137 |
|
|
int unroll;
|
138 |
883 |
markom |
int nshared;
|
139 |
897 |
markom |
cuc_shared_item *shared; /* List of shared resources */
|
140 |
879 |
markom |
int new_time;
|
141 |
|
|
double size;
|
142 |
|
|
} cuc_timings;
|
143 |
|
|
|
144 |
|
|
/* Instructionn entity */
|
145 |
|
|
typedef struct {
|
146 |
|
|
int type; /* type of the instruction */
|
147 |
|
|
int index; /* Instruction index */
|
148 |
|
|
int opt[MAX_OPERANDS]; /* operand types */
|
149 |
|
|
unsigned long op[MAX_OPERANDS]; /* operand values */
|
150 |
|
|
dep_list *dep; /* instruction dependencies */
|
151 |
|
|
unsigned long insn; /* Instruction opcode */
|
152 |
|
|
char disasm[40]; /* disassembled string */
|
153 |
936 |
markom |
unsigned long max; /* max result value */
|
154 |
879 |
markom |
int tmp;
|
155 |
|
|
} cuc_insn;
|
156 |
|
|
|
157 |
|
|
/* Basic block entity */
|
158 |
|
|
typedef struct {
|
159 |
|
|
unsigned long type; /* Type of the bb */
|
160 |
|
|
int first, last; /* Where this block lies */
|
161 |
|
|
int prev[2], next[2];
|
162 |
|
|
int tmp;
|
163 |
|
|
cuc_insn *insn; /* Instructions lie here */
|
164 |
|
|
int ninsn; /* Number of instructions */
|
165 |
|
|
int last_used_reg[MAX_REGS];
|
166 |
|
|
dep_list *mdep; /* Last memory access dependencies */
|
167 |
|
|
int nmemory;
|
168 |
|
|
int cnt; /* how many times was this block executed */
|
169 |
|
|
int unrolled; /* how many times has been this block unrolled */
|
170 |
897 |
markom |
|
171 |
|
|
int ntim; /* Basic block options */
|
172 |
879 |
markom |
cuc_timings *tim;
|
173 |
897 |
markom |
int selected_tim; /* Selected option, -1 if none */
|
174 |
879 |
markom |
} cuc_bb;
|
175 |
|
|
|
176 |
|
|
/* Function entity */
|
177 |
906 |
markom |
typedef struct _cuc_func {
|
178 |
879 |
markom |
/* Basic blocks */
|
179 |
|
|
int num_bb;
|
180 |
|
|
cuc_bb bb[MAX_BB];
|
181 |
883 |
markom |
int saved_regs[MAX_REGS];/* Whether this register was saved */
|
182 |
|
|
int lur[MAX_REGS]; /* Location of last use */
|
183 |
|
|
int used_regs[MAX_REGS]; /* Nonzero if it was used */
|
184 |
879 |
markom |
|
185 |
|
|
/* Schedule of memory instructions */
|
186 |
|
|
int nmsched;
|
187 |
|
|
int msched[MAX_INSNS];
|
188 |
|
|
int mtype[MAX_INSNS];
|
189 |
|
|
|
190 |
|
|
/* initial bb and their relocations to new block numbers */
|
191 |
|
|
int num_init_bb;
|
192 |
|
|
int *init_bb_reloc;
|
193 |
|
|
int orig_time; /* time in cyc required for SW implementation */
|
194 |
883 |
markom |
int num_runs; /* Number times this function was run */
|
195 |
|
|
cuc_timings timings; /* Base timings */
|
196 |
879 |
markom |
unsigned long start_addr; /* Address of first instruction inn function */
|
197 |
|
|
unsigned long end_addr; /* Address of last instruction inn function */
|
198 |
897 |
markom |
int memory_order; /* Memory order */
|
199 |
906 |
markom |
|
200 |
|
|
int nfdeps; /* Function dependencies */
|
201 |
|
|
struct _cuc_func **fdeps;
|
202 |
|
|
|
203 |
|
|
int tmp;
|
204 |
879 |
markom |
} cuc_func;
|
205 |
|
|
|
206 |
|
|
/* Instructions from function */
|
207 |
|
|
extern cuc_insn insn[MAX_INSNS];
|
208 |
|
|
extern int num_insn;
|
209 |
|
|
extern int reloc[MAX_INSNS];
|
210 |
|
|
extern FILE *flog;
|
211 |
|
|
|
212 |
933 |
markom |
/* returns log2(x) */
|
213 |
|
|
int log2 (unsigned long x);
|
214 |
|
|
|
215 |
879 |
markom |
/* Loads from file into global array insn */
|
216 |
897 |
markom |
int cuc_load (char *in_fn);
|
217 |
879 |
markom |
|
218 |
905 |
markom |
/* Negates conditional instruction */
|
219 |
|
|
void negate_conditional (cuc_insn *ii);
|
220 |
|
|
|
221 |
879 |
markom |
/* Scans sequence of BBs and set bb[].cnt */
|
222 |
|
|
void generate_bb_seq (cuc_func *f, char *mp_filename, char *bb_filename);
|
223 |
|
|
|
224 |
|
|
/* Prints out instructions */
|
225 |
|
|
void print_insns (cuc_insn *insn, int size, int verbose);
|
226 |
|
|
|
227 |
925 |
markom |
/* prints out bb string */
|
228 |
|
|
void print_bb_num (int num);
|
229 |
|
|
|
230 |
879 |
markom |
/* Print out basic blocks */
|
231 |
|
|
void print_cuc_bb (cuc_func *func, char *s);
|
232 |
|
|
|
233 |
|
|
/* Duplicates function */
|
234 |
|
|
cuc_func *dup_func (cuc_func *f);
|
235 |
|
|
|
236 |
|
|
/* Releases memory allocated by function */
|
237 |
|
|
void free_func (cuc_func *f);
|
238 |
|
|
|
239 |
897 |
markom |
/* Common subexpression matching -- resource sharing, analysis pass */
|
240 |
883 |
markom |
void csm (cuc_func *f);
|
241 |
|
|
|
242 |
897 |
markom |
/* Common subexpression matching -- resource sharing, generation pass */
|
243 |
|
|
void csm_gen (cuc_func *f, cuc_func *rf, cuc_shared_item *shared, int nshared);
|
244 |
|
|
|
245 |
879 |
markom |
/* Set the BB limits */
|
246 |
|
|
void detect_bb (cuc_func *func);
|
247 |
|
|
|
248 |
|
|
/* Optimize basic blocks */
|
249 |
931 |
markom |
int optimize_bb (cuc_func *func);
|
250 |
879 |
markom |
|
251 |
931 |
markom |
/* Search and optimize complex cmov assignments */
|
252 |
|
|
int optimize_cmovs (cuc_func *func);
|
253 |
|
|
|
254 |
|
|
/* Optimizes dataflow tree */
|
255 |
|
|
int optimize_tree (cuc_func *func);
|
256 |
|
|
|
257 |
|
|
/* Remove nop instructions */
|
258 |
|
|
int remove_nops (cuc_func *func);
|
259 |
|
|
|
260 |
|
|
/* Removes dead instruction */
|
261 |
|
|
int remove_dead (cuc_func *func);
|
262 |
|
|
|
263 |
|
|
/* Removes trivial register assignments */
|
264 |
|
|
int remove_trivial_regs (cuc_func *f);
|
265 |
|
|
|
266 |
|
|
/* Determine inputs and outputs */
|
267 |
|
|
void set_io (cuc_func *func);
|
268 |
|
|
|
269 |
879 |
markom |
/* Removes BBs marked as dead */
|
270 |
931 |
markom |
int remove_dead_bb (cuc_func *func);
|
271 |
879 |
markom |
|
272 |
931 |
markom |
/* Common subexpression elimination */
|
273 |
|
|
int cse (cuc_func *f);
|
274 |
|
|
|
275 |
879 |
markom |
/* Detect register dependencies */
|
276 |
|
|
void reg_dep (cuc_func *func);
|
277 |
|
|
|
278 |
|
|
/* Cuts the tree and marks registers */
|
279 |
|
|
void mark_cut (cuc_func *f);
|
280 |
|
|
|
281 |
|
|
/* Unroll loop b times times and return new function. Original
|
282 |
|
|
function is unmodified. */
|
283 |
|
|
cuc_func *preunroll_loop (cuc_func *func, int b, int preroll, int unroll, char *bb_filename);
|
284 |
883 |
markom |
|
285 |
879 |
markom |
/* Schedule memory accesses
|
286 |
|
|
|
287 |
|
|
void schedule_memory (cuc_func *func, int otype);
|
288 |
|
|
|
289 |
|
|
/* Generates verilog file out of insn dataflow */
|
290 |
940 |
markom |
void output_verilog (cuc_func *func, char *filename, char *funcname);
|
291 |
879 |
markom |
|
292 |
|
|
/* Recalculates bb[].cnt values, based on generated profile file */
|
293 |
|
|
void recalc_cnts (cuc_func *f, char *bb_filename);
|
294 |
|
|
|
295 |
|
|
/* Calculate timings */
|
296 |
|
|
void analyse_timings (cuc_func *func, cuc_timings *timings);
|
297 |
|
|
|
298 |
933 |
markom |
/* Calculates facts, that are determined by conditionals */
|
299 |
|
|
void insert_conditional_facts (cuc_func *func);
|
300 |
936 |
markom |
|
301 |
|
|
/* Width optimization -- detect maximum values */
|
302 |
|
|
void detect_max_values (cuc_func *f);
|
303 |
|
|
|
304 |
879 |
markom |
#endif /* __DATAF_H__ */
|