Skip to content

Commit 7e795bd

Browse files
committed
Replace _with_overflow instrinsics in LowerIntrinsics.
1 parent f79db59 commit 7e795bd

File tree

3 files changed

+113
-3
lines changed

3 files changed

+113
-3
lines changed

compiler/rustc_mir_transform/src/lower_intrinsics.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,29 @@ impl<'tcx> MirPass<'tcx> for LowerIntrinsics {
107107
}
108108
}
109109
sym::add_with_overflow | sym::sub_with_overflow | sym::mul_with_overflow => {
110-
// The checked binary operations are not suitable target for lowering here,
111-
// since their semantics depend on the value of overflow-checks flag used
112-
// during codegen. Issue #35310.
110+
if let Some(target) = *target {
111+
let lhs;
112+
let rhs;
113+
{
114+
let mut args = args.drain(..);
115+
lhs = args.next().unwrap();
116+
rhs = args.next().unwrap();
117+
}
118+
let bin_op = match intrinsic_name {
119+
sym::add_with_overflow => BinOp::Add,
120+
sym::sub_with_overflow => BinOp::Sub,
121+
sym::mul_with_overflow => BinOp::Mul,
122+
_ => bug!("unexpected intrinsic"),
123+
};
124+
block.statements.push(Statement {
125+
source_info: terminator.source_info,
126+
kind: StatementKind::Assign(Box::new((
127+
*destination,
128+
Rvalue::CheckedBinaryOp(bin_op, Box::new((lhs, rhs))),
129+
))),
130+
});
131+
terminator.kind = TerminatorKind::Goto { target };
132+
}
113133
}
114134
sym::size_of | sym::min_align_of => {
115135
if let Some(target) = *target {

tests/mir-opt/lower_intrinsics.rs

+7
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,10 @@ pub fn assume() {
7272
std::intrinsics::assume(true);
7373
}
7474
}
75+
76+
// EMIT_MIR lower_intrinsics.with_overflow.LowerIntrinsics.diff
77+
pub fn with_overflow(a: i32, b: i32) {
78+
let _x = core::intrinsics::add_with_overflow(a, b);
79+
let _y = core::intrinsics::sub_with_overflow(a, b);
80+
let _z = core::intrinsics::mul_with_overflow(a, b);
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
- // MIR for `with_overflow` before LowerIntrinsics
2+
+ // MIR for `with_overflow` after LowerIntrinsics
3+
4+
fn with_overflow(_1: i32, _2: i32) -> () {
5+
debug a => _1; // in scope 0 at $DIR/lower_intrinsics.rs:+0:22: +0:23
6+
debug b => _2; // in scope 0 at $DIR/lower_intrinsics.rs:+0:30: +0:31
7+
let mut _0: (); // return place in scope 0 at $DIR/lower_intrinsics.rs:+0:38: +0:38
8+
let _3: (i32, bool); // in scope 0 at $DIR/lower_intrinsics.rs:+1:9: +1:11
9+
let mut _4: i32; // in scope 0 at $DIR/lower_intrinsics.rs:+1:50: +1:51
10+
let mut _5: i32; // in scope 0 at $DIR/lower_intrinsics.rs:+1:53: +1:54
11+
let mut _7: i32; // in scope 0 at $DIR/lower_intrinsics.rs:+2:50: +2:51
12+
let mut _8: i32; // in scope 0 at $DIR/lower_intrinsics.rs:+2:53: +2:54
13+
let mut _10: i32; // in scope 0 at $DIR/lower_intrinsics.rs:+3:50: +3:51
14+
let mut _11: i32; // in scope 0 at $DIR/lower_intrinsics.rs:+3:53: +3:54
15+
scope 1 {
16+
debug _x => _3; // in scope 1 at $DIR/lower_intrinsics.rs:+1:9: +1:11
17+
let _6: (i32, bool); // in scope 1 at $DIR/lower_intrinsics.rs:+2:9: +2:11
18+
scope 2 {
19+
debug _y => _6; // in scope 2 at $DIR/lower_intrinsics.rs:+2:9: +2:11
20+
let _9: (i32, bool); // in scope 2 at $DIR/lower_intrinsics.rs:+3:9: +3:11
21+
scope 3 {
22+
debug _z => _9; // in scope 3 at $DIR/lower_intrinsics.rs:+3:9: +3:11
23+
}
24+
}
25+
}
26+
27+
bb0: {
28+
StorageLive(_3); // scope 0 at $DIR/lower_intrinsics.rs:+1:9: +1:11
29+
StorageLive(_4); // scope 0 at $DIR/lower_intrinsics.rs:+1:50: +1:51
30+
_4 = _1; // scope 0 at $DIR/lower_intrinsics.rs:+1:50: +1:51
31+
StorageLive(_5); // scope 0 at $DIR/lower_intrinsics.rs:+1:53: +1:54
32+
_5 = _2; // scope 0 at $DIR/lower_intrinsics.rs:+1:53: +1:54
33+
- _3 = add_with_overflow::<i32>(move _4, move _5) -> bb1; // scope 0 at $DIR/lower_intrinsics.rs:+1:14: +1:55
34+
- // mir::Constant
35+
- // + span: $DIR/lower_intrinsics.rs:78:14: 78:49
36+
- // + literal: Const { ty: extern "rust-intrinsic" fn(i32, i32) -> (i32, bool) {add_with_overflow::<i32>}, val: Value(<ZST>) }
37+
+ _3 = CheckedAdd(move _4, move _5); // scope 0 at $DIR/lower_intrinsics.rs:+1:14: +1:55
38+
+ goto -> bb1; // scope 0 at $DIR/lower_intrinsics.rs:+1:14: +1:55
39+
}
40+
41+
bb1: {
42+
StorageDead(_5); // scope 0 at $DIR/lower_intrinsics.rs:+1:54: +1:55
43+
StorageDead(_4); // scope 0 at $DIR/lower_intrinsics.rs:+1:54: +1:55
44+
StorageLive(_6); // scope 1 at $DIR/lower_intrinsics.rs:+2:9: +2:11
45+
StorageLive(_7); // scope 1 at $DIR/lower_intrinsics.rs:+2:50: +2:51
46+
_7 = _1; // scope 1 at $DIR/lower_intrinsics.rs:+2:50: +2:51
47+
StorageLive(_8); // scope 1 at $DIR/lower_intrinsics.rs:+2:53: +2:54
48+
_8 = _2; // scope 1 at $DIR/lower_intrinsics.rs:+2:53: +2:54
49+
- _6 = sub_with_overflow::<i32>(move _7, move _8) -> bb2; // scope 1 at $DIR/lower_intrinsics.rs:+2:14: +2:55
50+
- // mir::Constant
51+
- // + span: $DIR/lower_intrinsics.rs:79:14: 79:49
52+
- // + literal: Const { ty: extern "rust-intrinsic" fn(i32, i32) -> (i32, bool) {sub_with_overflow::<i32>}, val: Value(<ZST>) }
53+
+ _6 = CheckedSub(move _7, move _8); // scope 1 at $DIR/lower_intrinsics.rs:+2:14: +2:55
54+
+ goto -> bb2; // scope 1 at $DIR/lower_intrinsics.rs:+2:14: +2:55
55+
}
56+
57+
bb2: {
58+
StorageDead(_8); // scope 1 at $DIR/lower_intrinsics.rs:+2:54: +2:55
59+
StorageDead(_7); // scope 1 at $DIR/lower_intrinsics.rs:+2:54: +2:55
60+
StorageLive(_9); // scope 2 at $DIR/lower_intrinsics.rs:+3:9: +3:11
61+
StorageLive(_10); // scope 2 at $DIR/lower_intrinsics.rs:+3:50: +3:51
62+
_10 = _1; // scope 2 at $DIR/lower_intrinsics.rs:+3:50: +3:51
63+
StorageLive(_11); // scope 2 at $DIR/lower_intrinsics.rs:+3:53: +3:54
64+
_11 = _2; // scope 2 at $DIR/lower_intrinsics.rs:+3:53: +3:54
65+
- _9 = mul_with_overflow::<i32>(move _10, move _11) -> bb3; // scope 2 at $DIR/lower_intrinsics.rs:+3:14: +3:55
66+
- // mir::Constant
67+
- // + span: $DIR/lower_intrinsics.rs:80:14: 80:49
68+
- // + literal: Const { ty: extern "rust-intrinsic" fn(i32, i32) -> (i32, bool) {mul_with_overflow::<i32>}, val: Value(<ZST>) }
69+
+ _9 = CheckedMul(move _10, move _11); // scope 2 at $DIR/lower_intrinsics.rs:+3:14: +3:55
70+
+ goto -> bb3; // scope 2 at $DIR/lower_intrinsics.rs:+3:14: +3:55
71+
}
72+
73+
bb3: {
74+
StorageDead(_11); // scope 2 at $DIR/lower_intrinsics.rs:+3:54: +3:55
75+
StorageDead(_10); // scope 2 at $DIR/lower_intrinsics.rs:+3:54: +3:55
76+
_0 = const (); // scope 0 at $DIR/lower_intrinsics.rs:+0:38: +4:2
77+
StorageDead(_9); // scope 2 at $DIR/lower_intrinsics.rs:+4:1: +4:2
78+
StorageDead(_6); // scope 1 at $DIR/lower_intrinsics.rs:+4:1: +4:2
79+
StorageDead(_3); // scope 0 at $DIR/lower_intrinsics.rs:+4:1: +4:2
80+
return; // scope 0 at $DIR/lower_intrinsics.rs:+4:2: +4:2
81+
}
82+
}
83+

0 commit comments

Comments
 (0)