Skip to content

Commit 0e51b22

Browse files
committed
minor changes and force inline
1 parent 60ca2b2 commit 0e51b22

3 files changed

Lines changed: 46 additions & 33 deletions

File tree

src/data_struct.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ typedef struct {
5050

5151
typedef S_VALUE S_NAME(_value);
5252

53-
static void S_NAME(check_size)( hl_alloc *alloc, S_TYPE *st ) {
53+
INLINE static void S_NAME(check_size)( hl_alloc *alloc, S_TYPE *st ) {
5454
if( st->cur == st->max ) {
5555
int n = st->max ? (st->max << 1) : STRUCT_DEF_SIZE;
5656
S_KEY *keys = (S_KEY*)hl_malloc(alloc,sizeof(S_KEY) * n);
@@ -67,7 +67,7 @@ static void S_NAME(check_size)( hl_alloc *alloc, S_TYPE *st ) {
6767

6868
#ifndef S_SORTED
6969

70-
static void S_NAME(add_impl)( hl_alloc *alloc, S_TYPE *st, S_ARGS ) {
70+
INLINE static void S_NAME(add_impl)( hl_alloc *alloc, S_TYPE *st, S_ARGS ) {
7171
S_NAME(check_size)(alloc,st);
7272
st->keys[st->cur] = k;
7373
# ifdef S_MAP
@@ -76,7 +76,7 @@ static void S_NAME(add_impl)( hl_alloc *alloc, S_TYPE *st, S_ARGS ) {
7676
st->cur++;
7777
}
7878

79-
static bool S_NAME(exists)( S_TYPE st, S_KEY k ) {
79+
INLINE static bool S_NAME(exists)( S_TYPE st, S_KEY k ) {
8080
for(int i=0;i<st.cur;i++)
8181
if( st.keys[i] == k )
8282
return true;
@@ -95,7 +95,7 @@ static S_VALUE S_NAME(find)( S_TYPE st, S_KEY k ) {
9595

9696
#else
9797

98-
static bool S_NAME(add_impl)( hl_alloc *alloc, S_TYPE *st, S_ARGS ) {
98+
INLINE static bool S_NAME(add_impl)( hl_alloc *alloc, S_TYPE *st, S_ARGS ) {
9999
int min = 0;
100100
int max = st->cur;
101101
int pos;
@@ -119,7 +119,7 @@ static bool S_NAME(add_impl)( hl_alloc *alloc, S_TYPE *st, S_ARGS ) {
119119
}
120120

121121
#ifdef S_MAP
122-
static void S_NAME(replace_impl)( hl_alloc *alloc, S_TYPE *st, S_ARGS ) {
122+
INLINE static void S_NAME(replace_impl)( hl_alloc *alloc, S_TYPE *st, S_ARGS ) {
123123
int min = 0;
124124
int max = st->cur;
125125
int pos;
@@ -141,7 +141,7 @@ static void S_NAME(replace_impl)( hl_alloc *alloc, S_TYPE *st, S_ARGS ) {
141141
}
142142
#endif
143143

144-
static bool S_NAME(exists)( S_TYPE st, S_KEY k ) {
144+
INLINE static bool S_NAME(exists)( S_TYPE st, S_KEY k ) {
145145
int min = 0;
146146
int max = st.cur;
147147
while( min < max ) {
@@ -153,19 +153,19 @@ static bool S_NAME(exists)( S_TYPE st, S_KEY k ) {
153153
}
154154

155155
#ifdef S_MAP
156-
static S_VALUE S_NAME(find)( S_TYPE st, S_KEY k ) {
156+
INLINE static S_VALUE S_NAME(find)( S_TYPE st, S_KEY k ) {
157157
int min = 0;
158158
int max = st.cur;
159159
while( min < max ) {
160160
int mid = (min + max) >> 1;
161161
S_KEY k2 = st.keys[mid];
162162
if( k2 < k ) min = mid + 1; else if( k2 > k ) max = mid; else return st.values[mid];
163163
}
164-
return (S_VALUE)0;
164+
return S_DEFVAL;
165165
}
166166
#endif
167167

168-
static bool S_NAME(remove)( S_TYPE *st, S_KEY k ) {
168+
INLINE static bool S_NAME(remove)( S_TYPE *st, S_KEY k ) {
169169
int min = 0;
170170
int max = st->cur;
171171
while( min < max ) {
@@ -186,11 +186,11 @@ static bool S_NAME(remove)( S_TYPE *st, S_KEY k ) {
186186

187187
#endif
188188

189-
static void S_NAME(reset)( S_TYPE *st ) {
189+
INLINE static void S_NAME(reset)( S_TYPE *st ) {
190190
st->cur = 0;
191191
}
192192

193-
static S_VALUE *S_NAME(free)( S_TYPE *st ) {
193+
INLINE static S_VALUE *S_NAME(free)( S_TYPE *st ) {
194194
st->cur = 0;
195195
st->max = 0;
196196
S_VALUE *vals = st->values;
@@ -201,19 +201,19 @@ static S_VALUE *S_NAME(free)( S_TYPE *st ) {
201201
return vals;
202202
}
203203

204-
static int S_NAME(count)( S_TYPE st ) {
204+
INLINE static int S_NAME(count)( S_TYPE st ) {
205205
return st.cur;
206206
}
207207

208-
static S_VALUE S_NAME(get)( S_TYPE st, int idx ) {
208+
INLINE static S_VALUE S_NAME(get)( S_TYPE st, int idx ) {
209209
return st.values[idx];
210210
}
211211

212-
static S_VALUE S_NAME(first)( S_TYPE st ) {
212+
INLINE static S_VALUE S_NAME(first)( S_TYPE st ) {
213213
return st.cur == 0 ? S_DEFVAL : st.values[0];
214214
}
215215

216-
static bool S_NAME(iter_next)( S_TYPE st, S_VALUE *val, int idx ) {
216+
INLINE static bool S_NAME(iter_next)( S_TYPE st, S_VALUE *val, int idx ) {
217217
if( idx < st.cur ) *val = st.values[idx];
218218
return idx < st.cur;
219219
}

src/data_struct.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424

2525
#include <hl.h>
2626

27+
#if defined(__GNUC__) || defined(__clang__)
28+
#define INLINE inline __attribute__((always_inline))
29+
#elif defined(_MSC_VER)
30+
#define INLINE __forceinline
31+
#else
32+
#define INLINE inline
33+
#endif
34+
2735
#define STRUCT_DEF_SIZE 2
2836
#define for_iter(name,var,set) name##__value var; for(int __idx=0;name##_iter_next(set,&var,__idx);__idx++)
2937

src/jit_emit.c

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "data_struct.h"
2525

2626
static ereg ENULL = {VAL_NULL};
27+
static void __ignore( void *value ) {}
2728

2829
typedef struct {
2930
hl_type *t;
@@ -46,7 +47,14 @@ typedef struct _tmp_phi tmp_phi;
4647
#include "data_struct.c"
4748
#define blocks_add(set,v) blocks_add_impl(DEF_ALLOC,&(set),v)
4849

50+
#define S_TYPE phi_arr
51+
#define S_NAME(name) phi_##name
52+
#define S_VALUE tmp_phi*
53+
#include "data_struct.c"
54+
#define phi_add(set,v) phi_add_impl(DEF_ALLOC,&(set),v)
55+
4956
#define S_SORTED
57+
5058
#define S_DEFVAL ENULL
5159
#define S_CMP(a,b) a.index > b.index
5260
#define S_TYPE ereg_map
@@ -55,11 +63,15 @@ typedef struct _tmp_phi tmp_phi;
5563
#include "data_struct.c"
5664
#define ereg_add(set,v) ereg_add_impl(DEF_ALLOC,&(set),v)
5765

58-
#define S_TYPE phi_arr
59-
#define S_NAME(name) phi_##name
60-
#define S_VALUE tmp_phi*
66+
#define S_MAP
67+
68+
#define S_DEFVAL ENULL
69+
#define S_TYPE vreg_map
70+
#define S_NAME(name) vreg_##name
71+
#define S_KEY int
72+
#define S_VALUE ereg
6173
#include "data_struct.c"
62-
#define phi_add(set,v) phi_add_impl(DEF_ALLOC,&(set),v)
74+
#define vreg_replace(set,k,v) vreg_replace_impl(DEF_ALLOC,&(set),k,v)
6375

6476
struct _linked_inf {
6577
int id;
@@ -76,7 +88,7 @@ struct _emit_block {
7688
bool sealed;
7789
blocks nexts;
7890
blocks preds;
79-
int_map written_vars;
91+
vreg_map written_vars;
8092
phi_arr phis;
8193
emit_block *wait_seal_next;
8294
};
@@ -399,21 +411,13 @@ static void block_add_pred( emit_ctx *ctx, emit_block *b, emit_block *p ) {
399411

400412
static void store_block_var( emit_ctx *ctx, emit_block *b, vreg *r, ereg v ) {
401413
if( IS_NULL(v) ) jit_assert();
402-
int_map_replace(b->written_vars,r->id,v.index < 0 ? v.index : v.index + 1);
414+
vreg_replace(b->written_vars,r->id,v);
403415
if( v.index < 0 ) {
404416
tmp_phi *p = GET_PHI(v);
405417
p->ref_blocks = link_add_sort_unique(ctx,b->id,b,p->ref_blocks);
406418
}
407419
}
408420

409-
static ereg lookup_block_var( emit_block *b, vreg *r ) {
410-
int e = int_map_find(b->written_vars,r->id);
411-
if( !e ) return ENULL;
412-
ereg v;
413-
v.index = e < 0 ? e : e-1;
414-
return v;
415-
}
416-
417421
static void split_block( emit_ctx *ctx ) {
418422
emit_block *b = alloc_block(ctx);
419423
b->sealed = true;
@@ -530,7 +534,7 @@ static ereg optimize_phi_rec( emit_ctx *ctx, tmp_phi *p ) {
530534
linked_inf *l = p->ref_blocks;
531535
while( l ) {
532536
emit_block *b = (emit_block*)l->ptr;
533-
if( lookup_block_var(b,p->r).index == p->value.index )
537+
if( vreg_find(b->written_vars,p->r->id).index == p->value.index )
534538
store_block_var(ctx,b,p->r,same);
535539
l = l->next;
536540
}
@@ -561,7 +565,7 @@ static ereg gather_phis( emit_ctx *ctx, tmp_phi *p ) {
561565
}
562566

563567
static ereg emit_load_reg_block( emit_ctx *ctx, emit_block *b, vreg *r ) {
564-
ereg v = lookup_block_var(b,r);
568+
ereg v = vreg_find(b->written_vars,r->id);
565569
if( !IS_NULL(v) )
566570
return v;
567571
if( !b->sealed ) {
@@ -1058,6 +1062,7 @@ static void emit_opcode( emit_ctx *ctx, hl_opcode *o ) {
10581062
hl_module *m = ctx->mod;
10591063
#ifdef HL_DEBUG
10601064
int uid = (ctx->fun->findex << 16) | ctx->op_pos;
1065+
__ignore(&uid);
10611066
#endif
10621067
switch( o->op ) {
10631068
case OMov:
@@ -1614,10 +1619,10 @@ static void emit_opcode( emit_ctx *ctx, hl_opcode *o ) {
16141619
{
16151620
ereg ref = resolve_ref(ctx, ra->id);
16161621
if( IS_NULL(ref) ) jit_assert();
1617-
ereg r = lookup_block_var(ctx->current_block, ra);
1622+
ereg r = vreg_find(ctx->current_block->written_vars, ra->id);
16181623
if( !IS_NULL(r) ) {
16191624
STORE_MEM(ref, 0, LOAD(ra));
1620-
int_map_remove(&ctx->current_block->written_vars, ra->id);
1625+
vreg_remove(&ctx->current_block->written_vars, ra->id);
16211626
}
16221627
STORE(dst, ref);
16231628
}

0 commit comments

Comments
 (0)