Skip to content

Commit 1ae921d

Browse files
committed
i386: Clear stack protector scratch with zero/sign-extend instruction
Use unrelated register initializations using zero/sign-extend instructions to clear stack protector scratch register. Hanlde only SI -> DImode extensions for 64-bit targets, as this is the only extension that triggers the peephole in a non-negligible number. Also use explicit check for word_mode instead of mode iterator in peephole2 patterns to avoid pattern explosion. gcc/ChangeLog: * config/i386/i386.md (stack_protect_set_1 peephole2): Explicitly check operand 2 for word_mode. (stack_protect_set_1 peephole2 rust-lang#2): Ditto. (stack_protect_set_2 peephole2): Ditto. (stack_protect_set_3 peephole2): Ditto. (*stack_protect_set_4z_<mode>_di): New insn patter. (*stack_protect_set_4s_<mode>_di): Ditto. (stack_protect_set_4 peephole2): New peephole2 pattern to substitute stack protector scratch register clear with unrelated register initialization involving zero/sign-extend instruction.
1 parent 9917744 commit 1ae921d

File tree

1 file changed

+66
-8
lines changed

1 file changed

+66
-8
lines changed

gcc/config/i386/i386.md

+66-8
Original file line numberDiff line numberDiff line change
@@ -24335,11 +24335,12 @@
2433524335
[(parallel [(set (match_operand:PTR 0 "memory_operand")
2433624336
(unspec:PTR [(match_operand:PTR 1 "memory_operand")]
2433724337
UNSPEC_SP_SET))
24338-
(set (match_operand:W 2 "general_reg_operand") (const_int 0))
24338+
(set (match_operand 2 "general_reg_operand") (const_int 0))
2433924339
(clobber (reg:CC FLAGS_REG))])
2434024340
(set (match_operand 3 "general_reg_operand")
2434124341
(match_operand 4 "const0_operand"))]
24342-
"GET_MODE_SIZE (GET_MODE (operands[3])) <= UNITS_PER_WORD
24342+
"GET_MODE (operands[2]) == word_mode
24343+
&& GET_MODE_SIZE (GET_MODE (operands[3])) <= UNITS_PER_WORD
2434324344
&& peep2_reg_dead_p (0, operands[3])
2434424345
&& peep2_reg_dead_p (1, operands[2])"
2434524346
[(parallel [(set (match_dup 0)
@@ -24395,11 +24396,12 @@
2439524396
[(parallel [(set (match_operand:PTR 0 "memory_operand")
2439624397
(unspec:PTR [(match_operand:PTR 1 "memory_operand")]
2439724398
UNSPEC_SP_SET))
24398-
(set (match_operand:W 2 "general_reg_operand") (const_int 0))
24399+
(set (match_operand 2 "general_reg_operand") (const_int 0))
2439924400
(clobber (reg:CC FLAGS_REG))])
2440024401
(set (match_operand:SWI48 3 "general_reg_operand")
2440124402
(match_operand:SWI48 4 "general_gr_operand"))]
24402-
"peep2_reg_dead_p (0, operands[3])
24403+
"GET_MODE (operands[2]) == word_mode
24404+
&& peep2_reg_dead_p (0, operands[3])
2440324405
&& peep2_reg_dead_p (1, operands[2])"
2440424406
[(parallel [(set (match_dup 0)
2440524407
(unspec:PTR [(match_dup 1)] UNSPEC_SP_SET))
@@ -24411,9 +24413,10 @@
2441124413
(parallel [(set (match_operand:PTR 0 "memory_operand")
2441224414
(unspec:PTR [(match_operand:PTR 1 "memory_operand")]
2441324415
UNSPEC_SP_SET))
24414-
(set (match_operand:W 2 "general_reg_operand") (const_int 0))
24416+
(set (match_operand 2 "general_reg_operand") (const_int 0))
2441524417
(clobber (reg:CC FLAGS_REG))])]
24416-
"peep2_reg_dead_p (0, operands[3])
24418+
"GET_MODE (operands[2]) == word_mode
24419+
&& peep2_reg_dead_p (0, operands[3])
2441724420
&& peep2_reg_dead_p (2, operands[2])
2441824421
&& !reg_mentioned_p (operands[3], operands[0])
2441924422
&& !reg_mentioned_p (operands[3], operands[1])"
@@ -24448,16 +24451,71 @@
2444824451
[(parallel [(set (match_operand:PTR 0 "memory_operand")
2444924452
(unspec:PTR [(match_operand:PTR 1 "memory_operand")]
2445024453
UNSPEC_SP_SET))
24451-
(set (match_operand:W 2 "general_reg_operand") (const_int 0))
24454+
(set (match_operand 2 "general_reg_operand") (const_int 0))
2445224455
(clobber (reg:CC FLAGS_REG))])
2445324456
(set (match_operand:SWI48 3 "general_reg_operand")
2445424457
(match_operand:SWI48 4 "address_no_seg_operand"))]
24455-
"peep2_reg_dead_p (0, operands[3])
24458+
"GET_MODE (operands[2]) == word_mode
24459+
&& peep2_reg_dead_p (0, operands[3])
2445624460
&& peep2_reg_dead_p (1, operands[2])"
2445724461
[(parallel [(set (match_dup 0)
2445824462
(unspec:PTR [(match_dup 1)] UNSPEC_SP_SET))
2445924463
(set (match_dup 3) (match_dup 4))])])
2446024464

24465+
(define_insn "*stack_protect_set_4z_<mode>_di"
24466+
[(set (match_operand:PTR 0 "memory_operand" "=m")
24467+
(unspec:PTR [(match_operand:PTR 3 "memory_operand" "m")]
24468+
UNSPEC_SP_SET))
24469+
(set (match_operand:DI 1 "register_operand" "=&r")
24470+
(zero_extend:DI (match_operand:SI 2 "nonimmediate_operand" "rm")))]
24471+
"TARGET_64BIT && reload_completed"
24472+
{
24473+
output_asm_insn ("mov{<imodesuffix>}\t{%3, %<k>1|%<k>1, %3}", operands);
24474+
output_asm_insn ("mov{<imodesuffix>}\t{%<k>1, %0|%0, %<k>1}", operands);
24475+
if (ix86_use_lea_for_mov (insn, operands + 1))
24476+
return "lea{l}\t{%E2, %k1|%k1, %E2}";
24477+
else
24478+
return "mov{l}\t{%2, %k1|%k1, %2}";
24479+
}
24480+
[(set_attr "type" "multi")
24481+
(set_attr "length" "24")])
24482+
24483+
(define_insn "*stack_protect_set_4s_<mode>_di"
24484+
[(set (match_operand:PTR 0 "memory_operand" "=m,m")
24485+
(unspec:PTR [(match_operand:PTR 3 "memory_operand" "m,m")]
24486+
UNSPEC_SP_SET))
24487+
(set (match_operand:DI 1 "register_operand" "=&a,&r")
24488+
(sign_extend:DI (match_operand:SI 2 "nonimmediate_operand" "0,rm")))]
24489+
"TARGET_64BIT && reload_completed"
24490+
{
24491+
output_asm_insn ("mov{<imodesuffix>}\t{%3, %<k>1|%<k>1, %3}", operands);
24492+
output_asm_insn ("mov{<imodesuffix>}\t{%<k>1, %0|%0, %<k>1}", operands);
24493+
if (which_alternative)
24494+
return "movs{lq|x}\t{%2, %1|%1, %2}";
24495+
else
24496+
return "{cltq|cdqe}";
24497+
}
24498+
[(set_attr "type" "multi")
24499+
(set_attr "length" "24")])
24500+
24501+
(define_peephole2
24502+
[(parallel [(set (match_operand:PTR 0 "memory_operand")
24503+
(unspec:PTR [(match_operand:PTR 1 "memory_operand")]
24504+
UNSPEC_SP_SET))
24505+
(set (match_operand 2 "general_reg_operand") (const_int 0))
24506+
(clobber (reg:CC FLAGS_REG))])
24507+
(set (match_operand:DI 3 "general_reg_operand")
24508+
(any_extend:DI
24509+
(match_operand:SI 4 "nonimmediate_gr_operand")))]
24510+
"TARGET_64BIT
24511+
&& GET_MODE (operands[2]) == word_mode
24512+
&& peep2_reg_dead_p (0, operands[3])
24513+
&& peep2_reg_dead_p (1, operands[2])"
24514+
[(parallel [(set (match_dup 0)
24515+
(unspec:PTR [(match_dup 1)] UNSPEC_SP_SET))
24516+
(set (match_dup 3)
24517+
(any_extend:DI (match_dup 4)))])])
24518+
2446124519
(define_expand "stack_protect_test"
2446224520
[(match_operand 0 "memory_operand")
2446324521
(match_operand 1 "memory_operand")

0 commit comments

Comments
 (0)