This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
PATCH: simplify ~x + 1 into -x
- To: gcc-patches at gcc dot gnu dot org
- Subject: PATCH: simplify ~x + 1 into -x
- From: Lars Brinkhoff <lars at nocrew dot org>
- Date: 23 Apr 2001 10:18:22 +0200
- Organization: nocrew
This patch simlifies ~x + 1 into -x on two's complement machines
(probably all machines supported by GCC). It also adds a new target
macro to indicate the integer format, in case anyone would like to
port GCC to a machine without two's complement integers.
Bootstrapped on powerpc-unknown-linux-gnu and checked without
regressions.
2001-04-23 Lars Brinkhoff <lars@nocrew.org>
* defaults.h (TARGET_INT_FORMAT): New macro.
* expr.h (enum int_format): New enum.
* tm.texi (TARGET_INT_FORMAT): Document.
* simplify-rtx.c (simplify_binary_operation): Simplify
~a + 1 into -a on two's complement machines.
Index: defaults.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/defaults.h,v
retrieving revision 1.35
diff -c -3 -p -r1.35 defaults.h
*** defaults.h 2001/04/16 18:30:31 1.35
--- defaults.h 2001/04/23 08:04:43
*************** do { \
*** 290,295 ****
--- 290,299 ----
#define PIC_OFFSET_TABLE_REGNUM INVALID_REGNUM
#endif
+ #ifndef TARGET_INT_FORMAT
+ #define TARGET_INT_FORMAT TWOS_COMPLEMENT_INT_FORMAT
+ #endif
+
/* By default, the preprocessor should be invoked the same way in C++
as in C. */
#ifndef CPLUSPLUS_CPP_SPEC
Index: expr.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/expr.h,v
retrieving revision 1.80
diff -c -3 -p -r1.80 expr.h
*** expr.h 2001/04/14 03:39:22 1.80
--- expr.h 2001/04/23 08:04:44
*************** Boston, MA 02111-1307, USA. */
*** 43,48 ****
--- 43,53 ----
/* Next QUEUED in the queue. */
#define QUEUED_NEXT(P) XEXP (P, 4)
+ /* The format used for signed integers. */
+ enum int_format {TWOS_COMPLEMENT_INT_FORMAT,
+ ONES_COMPLEMENT_INT_FORMAT,
+ UNKNOWN_INT_FORMAT};
+
/* This is the 4th arg to `expand_expr'.
EXPAND_SUM means it is ok to return a PLUS rtx or MULT rtx.
EXPAND_INITIALIZER is similar but also record any labels on forced_labels.
Index: simplify-rtx.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/simplify-rtx.c,v
retrieving revision 1.45
diff -c -3 -p -r1.45 simplify-rtx.c
*** simplify-rtx.c 2001/04/05 02:27:45 1.45
--- simplify-rtx.c 2001/04/23 08:04:45
*************** simplify_binary_operation (code, mode, o
*** 915,920 ****
--- 915,928 ----
else if (GET_CODE (op1) == NEG)
return simplify_gen_binary (MINUS, mode, op0, XEXP (op1, 0));
+ /* (~a) + 1 -> -a on two's complement machines */
+ if (TARGET_INT_FORMAT == TWOS_COMPLEMENT_INT_FORMAT
+ && INTEGRAL_MODE_P (mode)
+ && GET_CODE (op0) == NOT
+ && GET_CODE (op1) == CONST_INT
+ && INTVAL (op1) == 1)
+ return gen_rtx_NEG (mode, XEXP (op0, 0));
+
/* Handle both-operands-constant cases. We can only add
CONST_INTs to constants since the sum of relocatable symbols
can't be handled by most assemblers. Don't add CONST_INT
Index: tm.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/tm.texi,v
retrieving revision 1.180
diff -c -3 -p -r1.180 tm.texi
*** tm.texi 2001/04/16 18:30:31 1.180
--- tm.texi 2001/04/23 08:04:50
*************** have the value 0. This macro need not b
*** 838,843 ****
--- 838,863 ----
You need not define this macro if the ordering is the same as for
multi-word integers.
+ @findex TARGET_INT_FORMAT
+ @item TARGET_INT_FORMAT
+ A code distinguishing the signed integer format of the target machine.
+ There are two defined values:
+
+ @table @code
+ @findex TWOS_COMPLEMENT_INT_FORMAT
+ @item TWOS_COMPLEMENT_INT_FORMAT
+ This code indicates two's complement. It is the default; there is no
+ need to define this macro when the format is two's complement.
+
+ @findex ONES_COMPLEMENT_INT_FORMAT
+ @item ONES_COMPLEMENT_INT_FORMAT
+ This code indicates one's complement.
+
+ @findex UNKNOWN_INT_FORMAT
+ @item UNKNOWN_INT_FORMAT
+ This code indicates any other format.
+ @end table
+
@findex BITS_PER_UNIT
@item BITS_PER_UNIT
Define this macro to be the number of bits in an addressable storage