Skip to content

Commit 9efe5e7

Browse files
committed
Promote unchecked_add/sub/mul/shl/shr to mir::BinOp
1 parent f429b3e commit 9efe5e7

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

src/codegen_i128.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ pub(crate) fn maybe_codegen<'tcx>(
2222

2323
match bin_op {
2424
BinOp::BitAnd | BinOp::BitOr | BinOp::BitXor => None,
25-
BinOp::Add | BinOp::Sub => None,
26-
BinOp::Mul => {
25+
BinOp::Add | BinOp::AddUnchecked | BinOp::Sub | BinOp::SubUnchecked => None,
26+
BinOp::Mul | BinOp::MulUnchecked => {
2727
let args = [lhs.load_scalar(fx), rhs.load_scalar(fx)];
2828
let ret_val = fx.lib_call(
2929
"__multi3",
@@ -69,7 +69,7 @@ pub(crate) fn maybe_codegen<'tcx>(
6969
}
7070
}
7171
BinOp::Lt | BinOp::Le | BinOp::Eq | BinOp::Ge | BinOp::Gt | BinOp::Ne => None,
72-
BinOp::Shl | BinOp::Shr => None,
72+
BinOp::Shl | BinOp::ShlUnchecked | BinOp::Shr | BinOp::ShrUnchecked => None,
7373
}
7474
}
7575

@@ -131,9 +131,10 @@ pub(crate) fn maybe_codegen_checked<'tcx>(
131131
fx.lib_call(name, param_types, vec![], &args);
132132
Some(out_place.to_cvalue(fx))
133133
}
134+
BinOp::AddUnchecked | BinOp::SubUnchecked | BinOp::MulUnchecked => unreachable!(),
134135
BinOp::Offset => unreachable!("offset should only be used on pointers, not 128bit ints"),
135136
BinOp::Div | BinOp::Rem => unreachable!(),
136137
BinOp::Lt | BinOp::Le | BinOp::Eq | BinOp::Ge | BinOp::Gt | BinOp::Ne => unreachable!(),
137-
BinOp::Shl | BinOp::Shr => unreachable!(),
138+
BinOp::Shl | BinOp::ShlUnchecked | BinOp::Shr | BinOp::ShrUnchecked => unreachable!(),
138139
}
139140
}

src/num.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,11 @@ pub(crate) fn codegen_int_binop<'tcx>(
128128
let rhs = in_rhs.load_scalar(fx);
129129

130130
let b = fx.bcx.ins();
131+
// FIXME trap on overflow for the Unchecked versions
131132
let val = match bin_op {
132-
BinOp::Add => b.iadd(lhs, rhs),
133-
BinOp::Sub => b.isub(lhs, rhs),
134-
BinOp::Mul => b.imul(lhs, rhs),
133+
BinOp::Add | BinOp::AddUnchecked => b.iadd(lhs, rhs),
134+
BinOp::Sub | BinOp::SubUnchecked => b.isub(lhs, rhs),
135+
BinOp::Mul | BinOp::MulUnchecked => b.imul(lhs, rhs),
135136
BinOp::Div => {
136137
if signed {
137138
b.sdiv(lhs, rhs)
@@ -149,16 +150,19 @@ pub(crate) fn codegen_int_binop<'tcx>(
149150
BinOp::BitXor => b.bxor(lhs, rhs),
150151
BinOp::BitAnd => b.band(lhs, rhs),
151152
BinOp::BitOr => b.bor(lhs, rhs),
152-
BinOp::Shl => b.ishl(lhs, rhs),
153-
BinOp::Shr => {
153+
BinOp::Shl | BinOp::ShlUnchecked => b.ishl(lhs, rhs),
154+
BinOp::Shr | BinOp::ShrUnchecked => {
154155
if signed {
155156
b.sshr(lhs, rhs)
156157
} else {
157158
b.ushr(lhs, rhs)
158159
}
159160
}
161+
BinOp::Offset => unreachable!("Offset is not an integer operation"),
160162
// Compare binops handles by `codegen_binop`.
161-
_ => unreachable!("{:?}({:?}, {:?})", bin_op, in_lhs.layout().ty, in_rhs.layout().ty),
163+
BinOp::Eq | BinOp::Ne | BinOp::Lt | BinOp::Le | BinOp::Gt | BinOp::Ge => {
164+
unreachable!("{:?}({:?}, {:?})", bin_op, in_lhs.layout().ty, in_rhs.layout().ty);
165+
}
162166
};
163167

164168
CValue::by_val(val, in_lhs.layout())

0 commit comments

Comments
 (0)