From d839d83f7e159cb9c5408bd0d4c554c074815af0 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Fri, 3 Jul 2020 09:44:59 +0300 Subject: [PATCH 1/7] add reminder by a power of two peephole rule --- cranelift/codegen/src/preopt.peepmatic | 5 +++++ cranelift/codegen/src/preopt.serialized | Bin 5438 -> 5543 bytes 2 files changed, 5 insertions(+) diff --git a/cranelift/codegen/src/preopt.peepmatic b/cranelift/codegen/src/preopt.peepmatic index 604460dd57..de9dc8342a 100644 --- a/cranelift/codegen/src/preopt.peepmatic +++ b/cranelift/codegen/src/preopt.peepmatic @@ -191,3 +191,8 @@ (=> (when (udiv_imm $C $x) (is-power-of-two $C)) (ushr_imm $(log2 $C) $x)) + +;; Reminder by a power of two -> bitwise and with decreased by one constant. +(=> (when (urem_imm $C $x) + (is-power-of-two $C)) + (band_imm $(iadd -1 $C) $x)) \ No newline at end of file diff --git a/cranelift/codegen/src/preopt.serialized b/cranelift/codegen/src/preopt.serialized index bae915dab008d2ecd380e99793768123a1317186..9b9bc3f01f3a3aac7274b463d5c7dae78ef005b1 100644 GIT binary patch delta 727 zcmZuvJxtqR5cP$8#!ehN+@LH6AUT78}>5jgQ(PWzi8f4WVi+ z;7Cu?FusRve9+4zJ%9B&jj-o|Fu6~<$GTg#0~%VYi8DP&<8I^cP`B|}c*7e$^*{SG z+iTuCR*gD6!f_;vFNWr#6z+`YG>f@N-s5i~hKsi7F}|`YH+jN)Pa>*@uwo8T*I>k>9fS3^ID!V-oc9G4Inf=5szxXI}DYA?8J& z7G_?M+N)m4U^LTX5=8iz^l8qInDA*9^HFRL>?iZSN|JdF-v(Y%8rLEz(#$ic#vJ}O gR+B1jV->!cTt_*6z-U)bHE-fM>UY{X@F!9G2dvzJwEzGB delta 683 zcmZ8fJx{_=6zv6DDNqWQ253e3P*4y=1O;NEn}&qQW^{2d4kjcEIt=bkb=21K!sHY5J0zch9-!o}8xdAv?%)-n~_h<7!mwU>@|l&oCAsf+;A; z0&&2B!@K~`Ac0*NLai(4{aZ)ZiES~=vvCk(LK=s>zyl$*KM?)^-&|Xu;ftu^ym$q( z*pYRJVoxsPm#D!UHoZC~-NlJUzzcT^;&?5FcpurA8%eSbVa}t$g0XSpv2Hwinj`1* zM7Kn`hQ;;S+kz~9OHq+bITn|p2%btiu!8qe+?YlrV{B0zLLT>g@sWRllCw{A;gc^0 zYetq|pEb+2JkC-g?LL$ZO~^mi9Q!Y!f*<~Ddy@sRoGI!zESR;^RO>d*MYw3=Sn5?9 z=O&!9aUQ}MldDfgUh0-7$ZRCKQZ|nLjzt?M6Hef3;9QN_CMw~1tk3MyJNn^OUL&ky jZ;Vk?D&_j78BxO&C9VZpwk=pnN)*@|O From 1033cf57488d1f79d4298d6bdc0d5aeb850635ff Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Fri, 3 Jul 2020 11:01:35 +0300 Subject: [PATCH 2/7] use (isub 1 $C) instead (iadd -1 $C) --- cranelift/codegen/src/preopt.peepmatic | 2 +- cranelift/codegen/src/preopt.serialized | Bin 5543 -> 5543 bytes .../peepmatic/crates/runtime/src/operator.rs | 4 ++++ .../peepmatic/crates/runtime/src/optimizer.rs | 2 ++ 4 files changed, 7 insertions(+), 1 deletion(-) diff --git a/cranelift/codegen/src/preopt.peepmatic b/cranelift/codegen/src/preopt.peepmatic index de9dc8342a..c6e18ec695 100644 --- a/cranelift/codegen/src/preopt.peepmatic +++ b/cranelift/codegen/src/preopt.peepmatic @@ -195,4 +195,4 @@ ;; Reminder by a power of two -> bitwise and with decreased by one constant. (=> (when (urem_imm $C $x) (is-power-of-two $C)) - (band_imm $(iadd -1 $C) $x)) \ No newline at end of file + (band_imm $(isub 1 $C) $x)) \ No newline at end of file diff --git a/cranelift/codegen/src/preopt.serialized b/cranelift/codegen/src/preopt.serialized index 9b9bc3f01f3a3aac7274b463d5c7dae78ef005b1..7ef6d1264230b7f91c82b8f861a5549f3fef6ea4 100644 GIT binary patch delta 49 wcmZ3kyaP%h+NW**0Hct7c+kpB%t>nUQs~3Rg2X0NqFllK=n! delta 49 xcmZ3ky unreachable!("not a unary unquote operator: {:?}", operator), } } @@ -98,6 +99,7 @@ where UnquoteOperator::Bor => fold_ints!(a, b, |x, y| x | y), UnquoteOperator::Bxor => fold_ints!(a, b, |x, y| x ^ y), UnquoteOperator::Iadd => fold_ints!(a, b, |x, y| x.wrapping_add(y)), + UnquoteOperator::Isub => fold_ints!(a, b, |x, y| x.wrapping_sub(y)), UnquoteOperator::Imul => fold_ints!(a, b, |x, y| x.wrapping_mul(y)), UnquoteOperator::Log2 | UnquoteOperator::Neg => { unreachable!("not a binary unquote operator: {:?}", operator) From 1fcb215895346d3d6552260f1ab15cb141211883 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Fri, 3 Jul 2020 11:03:03 +0300 Subject: [PATCH 3/7] trailing new line --- cranelift/codegen/src/preopt.peepmatic | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cranelift/codegen/src/preopt.peepmatic b/cranelift/codegen/src/preopt.peepmatic index c6e18ec695..c0f6d0862e 100644 --- a/cranelift/codegen/src/preopt.peepmatic +++ b/cranelift/codegen/src/preopt.peepmatic @@ -195,4 +195,4 @@ ;; Reminder by a power of two -> bitwise and with decreased by one constant. (=> (when (urem_imm $C $x) (is-power-of-two $C)) - (band_imm $(isub 1 $C) $x)) \ No newline at end of file + (band_imm $(isub 1 $C) $x)) From 03b6c97e15ca45a4143d783191d43c1fb571c557 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Fri, 3 Jul 2020 11:05:55 +0300 Subject: [PATCH 4/7] typo --- cranelift/peepmatic/crates/runtime/src/operator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cranelift/peepmatic/crates/runtime/src/operator.rs b/cranelift/peepmatic/crates/runtime/src/operator.rs index 22a70e2a9d..71fc2c4ca4 100644 --- a/cranelift/peepmatic/crates/runtime/src/operator.rs +++ b/cranelift/peepmatic/crates/runtime/src/operator.rs @@ -233,7 +233,7 @@ pub enum UnquoteOperator { #[peepmatic(params(iNN, iNN), result(iNN))] Iadd, - /// Compile-time `iadd` of two constant values. + /// Compile-time `isub` of two constant values. #[peepmatic(params(iNN, iNN), result(iNN))] Isub, From d21ca7f2a3936e3a0286ba9d8ba2c78e09a3a240 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Fri, 3 Jul 2020 11:46:37 +0300 Subject: [PATCH 5/7] fixed added fits-in-native-word contrain --- cranelift/codegen/src/preopt.peepmatic | 3 ++- cranelift/codegen/src/preopt.serialized | Bin 5543 -> 5613 bytes 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/cranelift/codegen/src/preopt.peepmatic b/cranelift/codegen/src/preopt.peepmatic index c0f6d0862e..791b1c30de 100644 --- a/cranelift/codegen/src/preopt.peepmatic +++ b/cranelift/codegen/src/preopt.peepmatic @@ -194,5 +194,6 @@ ;; Reminder by a power of two -> bitwise and with decreased by one constant. (=> (when (urem_imm $C $x) - (is-power-of-two $C)) + (is-power-of-two $C) + (fits-in-native-word $C)) (band_imm $(isub 1 $C) $x)) diff --git a/cranelift/codegen/src/preopt.serialized b/cranelift/codegen/src/preopt.serialized index 7ef6d1264230b7f91c82b8f861a5549f3fef6ea4..27023cfad28a1079ae5b5812c900be3f4ab4fa69 100644 GIT binary patch delta 753 zcmZ8fO-tNR6wMt?G?P!A#Bq|^89%2Q6~Crht7unK1W{2G7ve%(2!hnL;QRq$^ehEe zLZP@35$UFM)lF#^{(&yLvgkkXzQh^QJb1i&&OP_!JYGHr4?*>}J|jueEN`(eO#bQL zc8pLCeKgV2$Q((OiVlhn<$n@;)Q_!{hD-WIvuBEbbCSAfz`13UgMv~b7wgIgT&D*q z%eaU?vLXws_M2}?K7~NBDTD8-i@bV7c~mW*3b?XLcu+m+#Io+AJ3Us{DEO4#ryf+) ztZcGPG||V_Sqy0&6~l=gEk1FrEx$Mq)O7*t7Gtho^?e$~KchosQ%PJ|pd5CMd6dmo z+!=P*&l#a0rrD%Xd^9^>zA(<&#U-0@YZhoCO!H*M)67}5G?T1dqNz}mwQHJf`;gw_ z-VTnZMU>bQ@9{2%McKsrZGvlNTuN{v^;m*SGagBB8snjeTd22mt_JHUDhxjQ6P$RE zHwn&S+>0MA2X1$w<1+5TLTi~i@F=^o$M`j_Y8Yx}D(YZ^t5>LIa^&Gl=74L@S#kY@ bUy-qgiW9J^;^gtundcu_cgxg>9p_1TJma zufD`Hma!8u@!KA!Wv|0}%b~Ap4L3gp;VfVd_x<^#w+b7UjGLd;&A-| zdQpV^N`O<7KMrsP Date: Fri, 3 Jul 2020 21:45:40 +0300 Subject: [PATCH 6/7] typo --- cranelift/codegen/src/preopt.peepmatic | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cranelift/codegen/src/preopt.peepmatic b/cranelift/codegen/src/preopt.peepmatic index 791b1c30de..a571d82947 100644 --- a/cranelift/codegen/src/preopt.peepmatic +++ b/cranelift/codegen/src/preopt.peepmatic @@ -192,7 +192,7 @@ (is-power-of-two $C)) (ushr_imm $(log2 $C) $x)) -;; Reminder by a power of two -> bitwise and with decreased by one constant. +;; Remainder by a power of two -> bitwise and with decreased by one constant. (=> (when (urem_imm $C $x) (is-power-of-two $C) (fits-in-native-word $C)) From 305659427d66a6a356b1471678fa0bd6df38c36e Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Tue, 7 Jul 2020 01:55:18 +0300 Subject: [PATCH 7/7] changes according review --- cranelift/codegen/src/preopt.peepmatic | 2 +- cranelift/codegen/src/preopt.serialized | Bin 5613 -> 5613 bytes .../peepmatic/crates/runtime/src/operator.rs | 8 ++++---- .../peepmatic/crates/runtime/src/optimizer.rs | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cranelift/codegen/src/preopt.peepmatic b/cranelift/codegen/src/preopt.peepmatic index a571d82947..cd988ff7d9 100644 --- a/cranelift/codegen/src/preopt.peepmatic +++ b/cranelift/codegen/src/preopt.peepmatic @@ -196,4 +196,4 @@ (=> (when (urem_imm $C $x) (is-power-of-two $C) (fits-in-native-word $C)) - (band_imm $(isub 1 $C) $x)) + (band_imm $(isub $C 1) $x)) diff --git a/cranelift/codegen/src/preopt.serialized b/cranelift/codegen/src/preopt.serialized index 27023cfad28a1079ae5b5812c900be3f4ab4fa69..1bb11b24ad37aacd868cb3aee8c1ad33109707d7 100644 GIT binary patch delta 41 ocmaE>{Z@NJH=8H}5HK<@14$+j6HKyhUch#enUQ647FRPj0Hz2Ba{vGU delta 41 ncmaE>{Z@NJH=8Ik0|NsS5JLbXn9Z_z0ozGtM%K+)T+Q47rWFTs diff --git a/cranelift/peepmatic/crates/runtime/src/operator.rs b/cranelift/peepmatic/crates/runtime/src/operator.rs index 71fc2c4ca4..2915f36290 100644 --- a/cranelift/peepmatic/crates/runtime/src/operator.rs +++ b/cranelift/peepmatic/crates/runtime/src/operator.rs @@ -233,14 +233,14 @@ pub enum UnquoteOperator { #[peepmatic(params(iNN, iNN), result(iNN))] Iadd, - /// Compile-time `isub` of two constant values. - #[peepmatic(params(iNN, iNN), result(iNN))] - Isub, - /// Compile-time `imul` of two constant values. #[peepmatic(params(iNN, iNN), result(iNN))] Imul, + /// Compile-time `isub` of two constant values. + #[peepmatic(params(iNN, iNN), result(iNN))] + Isub, + /// Take the base-2 log of a power of two integer. #[peepmatic(params(iNN), result(iNN))] Log2, diff --git a/cranelift/peepmatic/crates/runtime/src/optimizer.rs b/cranelift/peepmatic/crates/runtime/src/optimizer.rs index ce4a6b8e57..f477db85ac 100644 --- a/cranelift/peepmatic/crates/runtime/src/optimizer.rs +++ b/cranelift/peepmatic/crates/runtime/src/optimizer.rs @@ -77,8 +77,8 @@ where | UnquoteOperator::Bor | UnquoteOperator::Bxor | UnquoteOperator::Iadd - | UnquoteOperator::Isub - | UnquoteOperator::Imul => unreachable!("not a unary unquote operator: {:?}", operator), + | UnquoteOperator::Imul + | UnquoteOperator::Isub => unreachable!("not a unary unquote operator: {:?}", operator), } } @@ -99,8 +99,8 @@ where UnquoteOperator::Bor => fold_ints!(a, b, |x, y| x | y), UnquoteOperator::Bxor => fold_ints!(a, b, |x, y| x ^ y), UnquoteOperator::Iadd => fold_ints!(a, b, |x, y| x.wrapping_add(y)), - UnquoteOperator::Isub => fold_ints!(a, b, |x, y| x.wrapping_sub(y)), UnquoteOperator::Imul => fold_ints!(a, b, |x, y| x.wrapping_mul(y)), + UnquoteOperator::Isub => fold_ints!(a, b, |x, y| x.wrapping_sub(y)), UnquoteOperator::Log2 | UnquoteOperator::Neg => { unreachable!("not a binary unquote operator: {:?}", operator) }