@@ -6682,7 +6682,7 @@ riscv_legitimize_call_address (rtx addr)
66826682 rtx reg = RISCV_CALL_ADDRESS_TEMP (Pmode);
66836683 riscv_emit_move (reg, addr);
66846684
6685- if (TARGET_ZICFILP )
6685+ if (is_zicfilp_p () )
66866686 {
66876687 rtx sw_guarded = RISCV_CALL_ADDRESS_LPAD (Pmode);
66886688 emit_insn (gen_set_guarded (Pmode, reg));
@@ -6692,7 +6692,7 @@ riscv_legitimize_call_address (rtx addr)
66926692 return reg;
66936693 }
66946694
6695- if (TARGET_ZICFILP && REG_P (addr))
6695+ if (is_zicfilp_p () && REG_P (addr))
66966696 emit_insn (gen_set_lpl (Pmode, const0_rtx));
66976697
66986698 return addr;
@@ -7508,7 +7508,7 @@ riscv_save_reg_p (unsigned int regno)
75087508 if (regno == GP_REGNUM || regno == THREAD_POINTER_REGNUM)
75097509 return false ;
75107510
7511- if (regno == RETURN_ADDR_REGNUM && TARGET_ZICFISS )
7511+ if (regno == RETURN_ADDR_REGNUM && is_zicfiss_p () )
75127512 return true ;
75137513
75147514 /* We must save every register used in this function. If this is not a
@@ -10340,10 +10340,10 @@ riscv_file_end ()
1034010340 long GNU_PROPERTY_RISCV_FEATURE_1_AND = 0 ;
1034110341 unsigned long feature_1_and = 0 ;
1034210342
10343- if (TARGET_ZICFISS )
10343+ if (is_zicfilp_p () )
1034410344 feature_1_and |= 0x1 << 0 ;
1034510345
10346- if (TARGET_ZICFILP )
10346+ if (is_zicfiss_p () )
1034710347 feature_1_and |= 0x1 << 1 ;
1034810348
1034910349 if (feature_1_and)
@@ -10403,7 +10403,7 @@ riscv_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
1040310403 /* Mark the end of the (empty) prologue. */
1040410404 emit_note (NOTE_INSN_PROLOGUE_END);
1040510405
10406- if (TARGET_ZICFILP )
10406+ if (is_zicfilp_p () )
1040710407 emit_insn (gen_lpad (const0_rtx));
1040810408
1040910409 /* Determine if we can use a sibcall to call FUNCTION directly. */
@@ -10630,6 +10630,20 @@ riscv_override_options_internal (struct gcc_options *opts)
1063010630
1063110631 /* Convert -march and -mrvv-vector-bits to a chunks count. */
1063210632 riscv_vector_chunks = riscv_convert_vector_chunks (opts);
10633+
10634+ if (opts->x_flag_cf_protection != CF_NONE)
10635+ {
10636+ if ((opts->x_flag_cf_protection & CF_RETURN) == CF_RETURN
10637+ && !TARGET_ZICFISS)
10638+ error (" %<-fcf-protection%> is not compatible with this target" );
10639+
10640+ if ((opts->x_flag_cf_protection & CF_BRANCH) == CF_BRANCH
10641+ && !TARGET_ZICFILP)
10642+ error (" %<-fcf-protection%> is not compatible with this target" );
10643+
10644+ opts->x_flag_cf_protection
10645+ = (cf_protection_level) (opts->x_flag_cf_protection | CF_SET);
10646+ }
1063310647}
1063410648
1063510649/* Implement TARGET_OPTION_OVERRIDE. */
@@ -10924,7 +10938,7 @@ riscv_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
1092410938
1092510939 /* Work out the offsets of the pointers from the start of the
1092610940 trampoline code. */
10927- if (!TARGET_ZICFILP )
10941+ if (!is_zicfilp_p () )
1092810942 gcc_assert (ARRAY_SIZE (trampoline) * 4 == TRAMPOLINE_CODE_SIZE);
1092910943 else
1093010944 gcc_assert (ARRAY_SIZE (trampoline_cfi) * 4 == TRAMPOLINE_CODE_SIZE);
@@ -10952,7 +10966,7 @@ riscv_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
1095210966 unsigned insn_count = 0 ;
1095310967
1095410968 /* Insert lpad, if zicfilp is enabled. */
10955- if (TARGET_ZICFILP )
10969+ if (is_zicfilp_p () )
1095610970 {
1095710971 unsigned HOST_WIDE_INT lpad_code;
1095810972 lpad_code = OPCODE_AUIPC | (0 << SHIFT_RD) | (lp_value << IMM_BITS);
@@ -11014,7 +11028,7 @@ riscv_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
1101411028 insn_count++;
1101511029
1101611030 /* For zicfilp only, insert lui t2, 1, because use jr t0. */
11017- if (TARGET_ZICFILP )
11031+ if (is_zicfilp_p () )
1101811032 {
1101911033 unsigned HOST_WIDE_INT set_lpl_code;
1102011034 set_lpl_code = OPCODE_LUI
@@ -11044,7 +11058,7 @@ riscv_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
1104411058 static_chain_offset = TRAMPOLINE_CODE_SIZE;
1104511059 target_function_offset = static_chain_offset + GET_MODE_SIZE (ptr_mode);
1104611060
11047- if (!TARGET_ZICFILP )
11061+ if (!is_zicfilp_p () )
1104811062 {
1104911063 /* auipc t2, 0
1105011064 l[wd] t0, (target_function_offset)(t2)
@@ -13959,9 +13973,25 @@ expand_reversed_crc_using_clmul (scalar_mode crc_mode, scalar_mode data_mode,
1395913973 riscv_emit_move (operands[0 ], gen_lowpart (crc_mode, a0));
1396013974}
1396113975
13976+ bool is_zicfiss_p ()
13977+ {
13978+ if (TARGET_ZICFISS && (flag_cf_protection & CF_RETURN))
13979+ return true ;
13980+
13981+ return false ;
13982+ }
13983+
13984+ bool is_zicfilp_p ()
13985+ {
13986+ if (TARGET_ZICFILP && (flag_cf_protection & CF_BRANCH))
13987+ return true ;
13988+
13989+ return false ;
13990+ }
13991+
1396213992bool need_shadow_stack_push_pop_p ()
1396313993{
13964- return TARGET_ZICFISS && riscv_save_return_addr_reg_p ();
13994+ return is_zicfiss_p () && riscv_save_return_addr_reg_p ();
1396513995}
1396613996
1396713997/* Initialize the GCC target structure. */
0 commit comments