public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Paulo J. Matos" <paulo@matos-sorge.com>
To: gcc@gcc.gnu.org
Subject: Expanding instructions with condition codes inter-deps
Date: Mon, 17 Oct 2011 15:58:00 -0000	[thread overview]
Message-ID: <j7h191$hnc$1@dough.gmane.org> (raw)

Hi,

To negate a double word (HImode) register, I used to take the 
instruction all the way to assembly generation and then expand into 
three assembly instructions like so:
   xor  %t0, #ffff ; invert bits in top word of op0
   nadd %b0, #0    ; negate bottom bits of op0
   addc %t0, #0    ; add carry to top bits of op0

The interesting thing about this sequence is that the carry added to the 
top bits of op0, is the carry generated by the previous instruction.

If I instead of taking the neghi rule all the way to assembly, and 
instead expand it I get:
(define_expand "neghi2"
   [(set (match_operand:HI 0 "register_operand")
         (neg:HI (match_operand:HI 1 "general_operand")))
    (clobber (reg:CC RCC))]
   ""
{
     rtx op0_low = gen_lowpart(QImode, operands[0]);
     rtx op0_high = gen_highpart(QImode, operands[0]);
     emit_move_insn(operands[0], operands[1]);
     emit_insn(gen_xorqi3(op0_high, op0_high, GEN_INT(-1)));
     emit_insn(gen_negqi2(op0_low, op0_low));
     emit_insn(gen_addc_internal(op0_high, op0_high, const0_rtx));
     DONE;
})


addc_internal looks like:
(define_insn "addc_internal"
   [(set (match_operand:QI 0 "nonimmediate_operand" "=c")
         (plus:QI
           (plus:QI
             (ltu:QI (reg:CC RCC) (const_int 0))
             (match_operand:QI 1 "nonimmediate_operand" "%0"))
           (match_operand:QI 2 "general_operand" "cwmi")))
    (clobber (reg:CC RCC))]
   ""
   "addc\\t%0,%f2")


The problem is that GCC sometimes moves nadd and addc around. It might 
swap their order or insert instructions in-between that set the carry 
flag. This ends up generating code which won't work properly.

What's the best way to instruct GCC during neghi2 expansion that the 
carry read by addc internal is the one generated by negqi2 and so no 
instructions setting carry should be inserted between them and that 
their order should not be changed? (for example, it would be ok to 
output negqi2, xorqi3 and addc_internal since xorqi3 only sets N and Z, 
not the Carry bit).

Cheers,

-- 
PMatos

             reply	other threads:[~2011-10-17 10:50 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-10-17 15:58 Paulo J. Matos [this message]
2011-10-17 17:23 ` Andrew Pinski
2011-10-18 13:44   ` Paulo J. Matos
2011-10-19  5:22 ` Richard Henderson
2011-10-19  5:45   ` Paul_Koning
     [not found]     ` <CAPOJ94M2XrqM_kG98v1dC1=K2fEkHpuNLSkZBrQyzJ9ncmaQXg@mail.gmail.com>
2011-10-21 20:57       ` Richard Henderson
2011-10-21 21:42     ` Paulo J. Matos
2011-10-22  0:13       ` Richard Henderson
2011-10-22  5:13         ` Peter Bigot
2011-10-22  5:21         ` Paul_Koning
2011-10-24  7:04           ` Richard Henderson
2011-10-24 12:12             ` Paulo J. Matos
2011-10-24 12:07         ` Paulo J. Matos
2011-10-20 12:46   ` Paulo J. Matos

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='j7h191$hnc$1@dough.gmane.org' \
    --to=paulo@matos-sorge.com \
    --cc=gcc@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).