Skip to content

Commit 51f8ac3

Browse files
hjl-toolsH.J. Lu
authored andcommitted
x86-64: Find a scratch register for large model profiling
2 scratch registers, %r10 and %r11, are available at function entry for large model profiling. But %r10 may be used by stack realignment and we can't use %r10 in this case. Add x86_64_select_profile_regnum to find a caller-saved register which isn't live or a callee-saved register which has been saved on stack in the prologue at entry for large model profiling and sorry if we can't find one. gcc/ PR target/113689 * config/i386/i386.cc (x86_64_select_profile_regnum): New. (x86_function_profiler): Call x86_64_select_profile_regnum to get a scratch register for large model profiling. gcc/testsuite/ PR target/113689 * gcc.target/i386/pr113689-1.c: New file. * gcc.target/i386/pr113689-2.c: Likewise. * gcc.target/i386/pr113689-3.c: Likewise.
1 parent 62babed commit 51f8ac3

File tree

4 files changed

+214
-15
lines changed

4 files changed

+214
-15
lines changed

gcc/config/i386/i386.cc

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22749,6 +22749,48 @@ current_fentry_section (const char **name)
2274922749
return true;
2275022750
}
2275122751

22752+
/* Return a caller-saved register which isn't live or a callee-saved
22753+
register which has been saved on stack in the prologue at entry for
22754+
profile. */
22755+
22756+
static int
22757+
x86_64_select_profile_regnum (bool r11_ok ATTRIBUTE_UNUSED)
22758+
{
22759+
/* Use %r10 if the profiler is emitted before the prologue or it isn't
22760+
used by DRAP. */
22761+
if (ix86_profile_before_prologue ()
22762+
|| !crtl->drap_reg
22763+
|| REGNO (crtl->drap_reg) != R10_REG)
22764+
return R10_REG;
22765+
22766+
/* The profiler is emitted after the prologue. If there is a
22767+
caller-saved register which isn't live or a callee-saved
22768+
register saved on stack in the prologue, use it. */
22769+
22770+
bitmap reg_live = df_get_live_out (ENTRY_BLOCK_PTR_FOR_FN (cfun));
22771+
22772+
int i;
22773+
for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
22774+
if (GENERAL_REGNO_P (i)
22775+
&& i != R10_REG
22776+
#ifdef NO_PROFILE_COUNTERS
22777+
&& (r11_ok || i != R11_REG)
22778+
#else
22779+
&& i != R11_REG
22780+
#endif
22781+
&& TEST_HARD_REG_BIT (accessible_reg_set, i)
22782+
&& (ix86_save_reg (i, true, true)
22783+
|| (call_used_regs[i]
22784+
&& !fixed_regs[i]
22785+
&& !REGNO_REG_SET_P (reg_live, i))))
22786+
return i;
22787+
22788+
sorry ("no register available for profiling %<-mcmodel=large%s%>",
22789+
ix86_cmodel == CM_LARGE_PIC ? " -fPIC" : "");
22790+
22791+
return INVALID_REGNUM;
22792+
}
22793+
2275222794
/* Output assembler code to FILE to increment profiler label # LABELNO
2275322795
for profiling a function entry. */
2275422796
void
@@ -22783,42 +22825,61 @@ x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED)
2278322825
fprintf (file, "\tleaq\t%sP%d(%%rip), %%r11\n", LPREFIX, labelno);
2278422826
#endif
2278522827

22828+
int scratch;
22829+
const char *reg;
22830+
char legacy_reg[4] = { 0 };
22831+
2278622832
if (!TARGET_PECOFF)
2278722833
{
2278822834
switch (ix86_cmodel)
2278922835
{
2279022836
case CM_LARGE:
22791-
/* NB: R10 is caller-saved. Although it can be used as a
22792-
static chain register, it is preserved when calling
22793-
mcount for nested functions. */
22837+
scratch = x86_64_select_profile_regnum (true);
22838+
reg = hi_reg_name[scratch];
22839+
if (LEGACY_INT_REGNO_P (scratch))
22840+
{
22841+
legacy_reg[0] = 'r';
22842+
legacy_reg[1] = reg[0];
22843+
legacy_reg[2] = reg[1];
22844+
reg = legacy_reg;
22845+
}
2279422846
if (ASSEMBLER_DIALECT == ASM_INTEL)
22795-
fprintf (file, "1:\tmovabs\tr10, OFFSET FLAT:%s\n"
22796-
"\tcall\tr10\n", mcount_name);
22847+
fprintf (file, "1:\tmovabs\t%s, OFFSET FLAT:%s\n"
22848+
"\tcall\t%s\n", reg, mcount_name, reg);
2279722849
else
22798-
fprintf (file, "1:\tmovabsq\t$%s, %%r10\n\tcall\t*%%r10\n",
22799-
mcount_name);
22850+
fprintf (file, "1:\tmovabsq\t$%s, %%%s\n\tcall\t*%%%s\n",
22851+
mcount_name, reg, reg);
2280022852
break;
2280122853
case CM_LARGE_PIC:
2280222854
#ifdef NO_PROFILE_COUNTERS
22855+
scratch = x86_64_select_profile_regnum (false);
22856+
reg = hi_reg_name[scratch];
22857+
if (LEGACY_INT_REGNO_P (scratch))
22858+
{
22859+
legacy_reg[0] = 'r';
22860+
legacy_reg[1] = reg[0];
22861+
legacy_reg[2] = reg[1];
22862+
reg = legacy_reg;
22863+
}
2280322864
if (ASSEMBLER_DIALECT == ASM_INTEL)
2280422865
{
2280522866
fprintf (file, "1:movabs\tr11, "
2280622867
"OFFSET FLAT:_GLOBAL_OFFSET_TABLE_-1b\n");
22807-
fprintf (file, "\tlea\tr10, 1b[rip]\n");
22808-
fprintf (file, "\tadd\tr10, r11\n");
22868+
fprintf (file, "\tlea\t%s, 1b[rip]\n", reg);
22869+
fprintf (file, "\tadd\t%s, r11\n", reg);
2280922870
fprintf (file, "\tmovabs\tr11, OFFSET FLAT:%s@PLTOFF\n",
2281022871
mcount_name);
22811-
fprintf (file, "\tadd\tr10, r11\n");
22812-
fprintf (file, "\tcall\tr10\n");
22872+
fprintf (file, "\tadd\t%s, r11\n", reg);
22873+
fprintf (file, "\tcall\t%s\n", reg);
2281322874
break;
2281422875
}
2281522876
fprintf (file,
2281622877
"1:\tmovabsq\t$_GLOBAL_OFFSET_TABLE_-1b, %%r11\n");
22817-
fprintf (file, "\tleaq\t1b(%%rip), %%r10\n");
22818-
fprintf (file, "\taddq\t%%r11, %%r10\n");
22878+
fprintf (file, "\tleaq\t1b(%%rip), %%%s\n", reg);
22879+
fprintf (file, "\taddq\t%%r11, %%%s\n", reg);
2281922880
fprintf (file, "\tmovabsq\t$%s@PLTOFF, %%r11\n", mcount_name);
22820-
fprintf (file, "\taddq\t%%r11, %%r10\n");
22821-
fprintf (file, "\tcall\t*%%r10\n");
22881+
fprintf (file, "\taddq\t%%r11, %%%s\n", reg);
22882+
fprintf (file, "\tcall\t*%%%s\n", reg);
2282222883
#else
2282322884
sorry ("profiling %<-mcmodel=large%> with PIC is not supported");
2282422885
#endif
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* { dg-do run { target { lp64 && fpic } } } */
2+
/* { dg-options "-O2 -fno-pic -fprofile -mcmodel=large" } */
3+
4+
#include <stdarg.h>
5+
6+
__attribute__((noipa))
7+
void
8+
bar (int a1, int a2, int a3, int a4, int a5, int a6,
9+
char *x, char *y, int *z)
10+
{
11+
if (a1 != 1)
12+
__builtin_abort ();
13+
if (a2 != 2)
14+
__builtin_abort ();
15+
if (a3 != 3)
16+
__builtin_abort ();
17+
if (a4 != 4)
18+
__builtin_abort ();
19+
if (a5 != 5)
20+
__builtin_abort ();
21+
if (a6 != 6)
22+
__builtin_abort ();
23+
x[0] = 42;
24+
y[0] = 42;
25+
if (z[0] != 16)
26+
__builtin_abort ();
27+
}
28+
29+
__attribute__((noipa))
30+
void
31+
foo (int c, int d, int e, int f, int g, int h, int z, ...)
32+
{
33+
typedef char B[32];
34+
B b __attribute__((aligned (32)));
35+
va_list ap;
36+
va_start (ap, z);
37+
double x = va_arg (ap, double);
38+
if (x > 40.0)
39+
__builtin_abort ();
40+
bar (c, d, e, f, g, h, &b[0], __builtin_alloca (z), &z);
41+
va_end (ap);
42+
}
43+
44+
int
45+
main ()
46+
{
47+
foo (1, 2, 3, 4, 5, 6, 16, 38.0);
48+
return 0;
49+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* { dg-do run { target { lp64 && fpic } } } */
2+
/* { dg-options "-O2 -fpic -fprofile -mcmodel=large" } */
3+
4+
__attribute__((noipa))
5+
void
6+
bar (int a1, int a2, int a3, int a4, int a5, int a6,
7+
char *x, char *y, int *z)
8+
{
9+
if (a1 != 1)
10+
__builtin_abort ();
11+
if (a2 != 2)
12+
__builtin_abort ();
13+
if (a3 != 3)
14+
__builtin_abort ();
15+
if (a4 != 4)
16+
__builtin_abort ();
17+
if (a5 != 5)
18+
__builtin_abort ();
19+
if (a6 != 6)
20+
__builtin_abort ();
21+
x[0] = 42;
22+
y[0] = 42;
23+
if (z[0] != 16)
24+
__builtin_abort ();
25+
}
26+
27+
__attribute__((noipa))
28+
void
29+
foo (int c, int d, int e, int f, int g, int h, int z)
30+
{
31+
typedef char B[32];
32+
B b __attribute__((aligned (32)));
33+
bar (c, d, e, f, g, h, &b[0], __builtin_alloca (z), &z);
34+
}
35+
36+
int
37+
main ()
38+
{
39+
foo (1, 2, 3, 4, 5, 6, 16);
40+
return 0;
41+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* { dg-do run { target { lp64 && fpic } } } */
2+
/* { dg-options "-O2 -fpic -fprofile -mcmodel=large" } */
3+
4+
#include <stdarg.h>
5+
6+
__attribute__((noipa))
7+
void
8+
bar (char *x, char *y, int *z)
9+
{
10+
x[0] = 42;
11+
y[0] = 42;
12+
if (z[0] != 16)
13+
__builtin_abort ();
14+
}
15+
16+
__attribute__((noipa))
17+
void
18+
foo (int a1, int a2, int a3, int a4, int a5, int a6, int z, ...)
19+
{
20+
typedef char B[32];
21+
B b __attribute__((aligned (32)));
22+
va_list ap;
23+
va_start (ap, z);
24+
double x = va_arg (ap, double);
25+
if (x > 40.0)
26+
__builtin_abort ();
27+
if (a1 != 1)
28+
__builtin_abort ();
29+
if (a2 != 2)
30+
__builtin_abort ();
31+
if (a3 != 3)
32+
__builtin_abort ();
33+
if (a4 != 4)
34+
__builtin_abort ();
35+
if (a5 != 5)
36+
__builtin_abort ();
37+
if (a6 != 6)
38+
__builtin_abort ();
39+
bar (&b[0], __builtin_alloca (z), &z);
40+
va_end (ap);
41+
}
42+
43+
int
44+
main ()
45+
{
46+
foo (1, 2, 3, 4, 5, 6, 16, 38.0);
47+
return 0;
48+
}

0 commit comments

Comments
 (0)