Skip to content

Commit 016b300

Browse files
committed
i386: Improve stack protector patterns and peephole2s even more
Improve stack protector patterns and peephole2s even more: a. Use unrelated register clears with integer mode size <= word mode size to clear stack protector scratch register. b. Use unrelated register initializations in front of stack protector sequence to clear stack protector scratch register. c. Use unrelated register initializations using LEA instructions to clear stack protector scratch register. These stack protector improvements reuse 6914 unrelated register initializations to substitute the clear of stack protector scratch register in 12034 instances of stack protector sequence in recent linux defconfig build. gcc/ChangeLog: * config/i386/i386.md (@stack_protect_set_1_<PTR:mode>_<W:mode>): Use W mode iterator instead of SWI48. Output MOV instead of XOR for TARGET_USE_MOV0. (stack_protect_set_1 peephole2): Use integer modes with mode size <= word mode size for operand 3. (stack_protect_set_1 peephole2 rust-lang#2): New peephole2 pattern to substitute stack protector scratch register clear with unrelated register initialization, originally in front of stack protector sequence. (*stack_protect_set_3_<PTR:mode>_<SWI48:mode>): New insn pattern. (stack_protect_set_1 peephole2): New peephole2 pattern to substitute stack protector scratch register clear with unrelated register initialization involving LEA instruction.
1 parent a99f6bb commit 016b300

File tree

1 file changed

+66
-9
lines changed

1 file changed

+66
-9
lines changed

gcc/config/i386/i386.md

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6172,7 +6172,7 @@
61726172
gcc_assert (TARGET_64BIT);
61736173
return "lea{l}\t{%E1, %k0|%k0, %E1}";
61746174
}
6175-
else
6175+
else
61766176
return "lea{<imodesuffix>}\t{%E1, %0|%0, %E1}";
61776177
}
61786178
[(set_attr "type" "lea")
@@ -24306,19 +24306,22 @@
2430624306
DONE;
2430724307
})
2430824308

24309-
(define_insn "@stack_protect_set_1_<PTR:mode>_<SWI48:mode>"
24309+
(define_insn "@stack_protect_set_1_<PTR:mode>_<W:mode>"
2431024310
[(set (match_operand:PTR 0 "memory_operand" "=m")
2431124311
(unspec:PTR [(match_operand:PTR 1 "memory_operand" "m")]
2431224312
UNSPEC_SP_SET))
24313-
(set (match_operand:SWI48 2 "register_operand" "=&r") (const_int 0))
24313+
(set (match_operand:W 2 "register_operand" "=&r") (const_int 0))
2431424314
(clobber (reg:CC FLAGS_REG))]
2431524315
""
2431624316
{
2431724317
output_asm_insn ("mov{<PTR:imodesuffix>}\t{%1, %<PTR:k>2|%<PTR:k>2, %1}",
2431824318
operands);
2431924319
output_asm_insn ("mov{<PTR:imodesuffix>}\t{%<PTR:k>2, %0|%0, %<PTR:k>2}",
2432024320
operands);
24321-
return "xor{l}\t%k2, %k2";
24321+
if (!TARGET_USE_MOV0 || optimize_insn_for_size_p ())
24322+
return "xor{l}\t%k2, %k2";
24323+
else
24324+
return "mov{l}\t{$0, %k2|%k2, 0}";
2432224325
}
2432324326
[(set_attr "type" "multi")])
2432424327

@@ -24334,15 +24337,16 @@
2433424337
UNSPEC_SP_SET))
2433524338
(set (match_operand:W 2 "general_reg_operand") (const_int 0))
2433624339
(clobber (reg:CC FLAGS_REG))])
24337-
(parallel [(set (match_operand:SWI48 3 "general_reg_operand")
24338-
(match_operand:SWI48 4 "const0_operand"))
24339-
(clobber (reg:CC FLAGS_REG))])]
24340-
"peep2_reg_dead_p (0, operands[3])
24340+
(set (match_operand 3 "general_reg_operand")
24341+
(match_operand 4 "const0_operand"))]
24342+
"GET_MODE_SIZE (GET_MODE (operands[3])) <= UNITS_PER_WORD
24343+
&& peep2_reg_dead_p (0, operands[3])
2434124344
&& peep2_reg_dead_p (1, operands[2])"
2434224345
[(parallel [(set (match_dup 0)
2434324346
(unspec:PTR [(match_dup 1)] UNSPEC_SP_SET))
2434424347
(set (match_dup 3) (const_int 0))
24345-
(clobber (reg:CC FLAGS_REG))])])
24348+
(clobber (reg:CC FLAGS_REG))])]
24349+
"operands[3] = gen_lowpart (word_mode, operands[3]);")
2434624350

2434724351
(define_insn "*stack_protect_set_2_<mode>_si"
2434824352
[(set (match_operand:PTR 0 "memory_operand" "=m")
@@ -24401,6 +24405,59 @@
2440124405
(unspec:PTR [(match_dup 1)] UNSPEC_SP_SET))
2440224406
(set (match_dup 3) (match_dup 4))])])
2440324407

24408+
(define_peephole2
24409+
[(set (match_operand:SWI48 3 "general_reg_operand")
24410+
(match_operand:SWI48 4 "general_gr_operand"))
24411+
(parallel [(set (match_operand:PTR 0 "memory_operand")
24412+
(unspec:PTR [(match_operand:PTR 1 "memory_operand")]
24413+
UNSPEC_SP_SET))
24414+
(set (match_operand:W 2 "general_reg_operand") (const_int 0))
24415+
(clobber (reg:CC FLAGS_REG))])]
24416+
"peep2_reg_dead_p (0, operands[3])
24417+
&& peep2_reg_dead_p (2, operands[2])
24418+
&& !reg_mentioned_p (operands[3], operands[0])
24419+
&& !reg_mentioned_p (operands[3], operands[1])"
24420+
[(parallel [(set (match_dup 0)
24421+
(unspec:PTR [(match_dup 1)] UNSPEC_SP_SET))
24422+
(set (match_dup 3) (match_dup 4))])])
24423+
24424+
(define_insn "*stack_protect_set_3_<PTR:mode>_<SWI48:mode>"
24425+
[(set (match_operand:PTR 0 "memory_operand" "=m")
24426+
(unspec:PTR [(match_operand:PTR 3 "memory_operand" "m")]
24427+
UNSPEC_SP_SET))
24428+
(set (match_operand:SWI48 1 "register_operand" "=&r")
24429+
(match_operand:SWI48 2 "address_no_seg_operand" "Ts"))]
24430+
""
24431+
{
24432+
output_asm_insn ("mov{<PTR:imodesuffix>}\t{%3, %<PTR:k>1|%<PTR:k>1, %3}",
24433+
operands);
24434+
output_asm_insn ("mov{<PTR:imodesuffix>}\t{%<PTR:k>1, %0|%0, %<PTR:k>1}",
24435+
operands);
24436+
if (SImode_address_operand (operands[2], VOIDmode))
24437+
{
24438+
gcc_assert (TARGET_64BIT);
24439+
return "lea{l}\t{%E2, %k1|%k1, %E2}";
24440+
}
24441+
else
24442+
return "lea{<SWI48:imodesuffix>}\t{%E2, %1|%1, %E2}";
24443+
}
24444+
[(set_attr "type" "multi")
24445+
(set_attr "length" "24")])
24446+
24447+
(define_peephole2
24448+
[(parallel [(set (match_operand:PTR 0 "memory_operand")
24449+
(unspec:PTR [(match_operand:PTR 1 "memory_operand")]
24450+
UNSPEC_SP_SET))
24451+
(set (match_operand:W 2 "general_reg_operand") (const_int 0))
24452+
(clobber (reg:CC FLAGS_REG))])
24453+
(set (match_operand:SWI48 3 "general_reg_operand")
24454+
(match_operand:SWI48 4 "address_no_seg_operand"))]
24455+
"peep2_reg_dead_p (0, operands[3])
24456+
&& peep2_reg_dead_p (1, operands[2])"
24457+
[(parallel [(set (match_dup 0)
24458+
(unspec:PTR [(match_dup 1)] UNSPEC_SP_SET))
24459+
(set (match_dup 3) (match_dup 4))])])
24460+
2440424461
(define_expand "stack_protect_test"
2440524462
[(match_operand 0 "memory_operand")
2440624463
(match_operand 1 "memory_operand")

0 commit comments

Comments
 (0)