Skip to content

Commit d72b617

Browse files
committed
Move IntrinsicCallMethods::call_overflow_intrinsics to BuilderMethods::checked_binop
1 parent 2677822 commit d72b617

File tree

6 files changed

+93
-92
lines changed

6 files changed

+93
-92
lines changed

src/librustc_codegen_llvm/builder.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,80 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
456456
}
457457
}
458458

459+
fn checked_binop(
460+
&mut self,
461+
oop: OverflowOp,
462+
ty: Ty,
463+
lhs: Self::Value,
464+
rhs: Self::Value,
465+
) -> (Self::Value, Self::Value) {
466+
use syntax::ast::IntTy::*;
467+
use syntax::ast::UintTy::*;
468+
use rustc::ty::{Int, Uint};
469+
470+
let new_sty = match ty.sty {
471+
Int(Isize) => Int(self.cx().tcx.sess.target.isize_ty),
472+
Uint(Usize) => Uint(self.cx().tcx.sess.target.usize_ty),
473+
ref t @ Uint(_) | ref t @ Int(_) => t.clone(),
474+
_ => panic!("tried to get overflow intrinsic for op applied to non-int type")
475+
};
476+
477+
let name = match oop {
478+
OverflowOp::Add => match new_sty {
479+
Int(I8) => "llvm.sadd.with.overflow.i8",
480+
Int(I16) => "llvm.sadd.with.overflow.i16",
481+
Int(I32) => "llvm.sadd.with.overflow.i32",
482+
Int(I64) => "llvm.sadd.with.overflow.i64",
483+
Int(I128) => "llvm.sadd.with.overflow.i128",
484+
485+
Uint(U8) => "llvm.uadd.with.overflow.i8",
486+
Uint(U16) => "llvm.uadd.with.overflow.i16",
487+
Uint(U32) => "llvm.uadd.with.overflow.i32",
488+
Uint(U64) => "llvm.uadd.with.overflow.i64",
489+
Uint(U128) => "llvm.uadd.with.overflow.i128",
490+
491+
_ => unreachable!(),
492+
},
493+
OverflowOp::Sub => match new_sty {
494+
Int(I8) => "llvm.ssub.with.overflow.i8",
495+
Int(I16) => "llvm.ssub.with.overflow.i16",
496+
Int(I32) => "llvm.ssub.with.overflow.i32",
497+
Int(I64) => "llvm.ssub.with.overflow.i64",
498+
Int(I128) => "llvm.ssub.with.overflow.i128",
499+
500+
Uint(U8) => "llvm.usub.with.overflow.i8",
501+
Uint(U16) => "llvm.usub.with.overflow.i16",
502+
Uint(U32) => "llvm.usub.with.overflow.i32",
503+
Uint(U64) => "llvm.usub.with.overflow.i64",
504+
Uint(U128) => "llvm.usub.with.overflow.i128",
505+
506+
_ => unreachable!(),
507+
},
508+
OverflowOp::Mul => match new_sty {
509+
Int(I8) => "llvm.smul.with.overflow.i8",
510+
Int(I16) => "llvm.smul.with.overflow.i16",
511+
Int(I32) => "llvm.smul.with.overflow.i32",
512+
Int(I64) => "llvm.smul.with.overflow.i64",
513+
Int(I128) => "llvm.smul.with.overflow.i128",
514+
515+
Uint(U8) => "llvm.umul.with.overflow.i8",
516+
Uint(U16) => "llvm.umul.with.overflow.i16",
517+
Uint(U32) => "llvm.umul.with.overflow.i32",
518+
Uint(U64) => "llvm.umul.with.overflow.i64",
519+
Uint(U128) => "llvm.umul.with.overflow.i128",
520+
521+
_ => unreachable!(),
522+
},
523+
};
524+
525+
let intrinsic = self.cx().get_intrinsic(&name);
526+
let res = self.call(intrinsic, &[lhs, rhs], None);
527+
(
528+
self.extract_value(res, 0),
529+
self.extract_value(res, 1),
530+
)
531+
}
532+
459533
fn alloca(&mut self, ty: &'ll Type, name: &str, align: Align) -> &'ll Value {
460534
let mut bx = Builder::with_cx(self.cx);
461535
bx.position_at_start(unsafe {

src/librustc_codegen_llvm/intrinsic.rs

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -778,80 +778,6 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
778778
let expect = self.cx().get_intrinsic(&"llvm.expect.i1");
779779
self.call(expect, &[cond, self.cx().const_bool(expected)], None)
780780
}
781-
782-
fn call_overflow_intrinsic(
783-
&mut self,
784-
oop: OverflowOp,
785-
ty: Ty,
786-
lhs: Self::Value,
787-
rhs: Self::Value,
788-
) -> (Self::Value, Self::Value) {
789-
use syntax::ast::IntTy::*;
790-
use syntax::ast::UintTy::*;
791-
use rustc::ty::{Int, Uint};
792-
793-
let new_sty = match ty.sty {
794-
Int(Isize) => Int(self.tcx().sess.target.isize_ty),
795-
Uint(Usize) => Uint(self.tcx().sess.target.usize_ty),
796-
ref t @ Uint(_) | ref t @ Int(_) => t.clone(),
797-
_ => panic!("tried to get overflow intrinsic for op applied to non-int type")
798-
};
799-
800-
let name = match oop {
801-
OverflowOp::Add => match new_sty {
802-
Int(I8) => "llvm.sadd.with.overflow.i8",
803-
Int(I16) => "llvm.sadd.with.overflow.i16",
804-
Int(I32) => "llvm.sadd.with.overflow.i32",
805-
Int(I64) => "llvm.sadd.with.overflow.i64",
806-
Int(I128) => "llvm.sadd.with.overflow.i128",
807-
808-
Uint(U8) => "llvm.uadd.with.overflow.i8",
809-
Uint(U16) => "llvm.uadd.with.overflow.i16",
810-
Uint(U32) => "llvm.uadd.with.overflow.i32",
811-
Uint(U64) => "llvm.uadd.with.overflow.i64",
812-
Uint(U128) => "llvm.uadd.with.overflow.i128",
813-
814-
_ => unreachable!(),
815-
},
816-
OverflowOp::Sub => match new_sty {
817-
Int(I8) => "llvm.ssub.with.overflow.i8",
818-
Int(I16) => "llvm.ssub.with.overflow.i16",
819-
Int(I32) => "llvm.ssub.with.overflow.i32",
820-
Int(I64) => "llvm.ssub.with.overflow.i64",
821-
Int(I128) => "llvm.ssub.with.overflow.i128",
822-
823-
Uint(U8) => "llvm.usub.with.overflow.i8",
824-
Uint(U16) => "llvm.usub.with.overflow.i16",
825-
Uint(U32) => "llvm.usub.with.overflow.i32",
826-
Uint(U64) => "llvm.usub.with.overflow.i64",
827-
Uint(U128) => "llvm.usub.with.overflow.i128",
828-
829-
_ => unreachable!(),
830-
},
831-
OverflowOp::Mul => match new_sty {
832-
Int(I8) => "llvm.smul.with.overflow.i8",
833-
Int(I16) => "llvm.smul.with.overflow.i16",
834-
Int(I32) => "llvm.smul.with.overflow.i32",
835-
Int(I64) => "llvm.smul.with.overflow.i64",
836-
Int(I128) => "llvm.smul.with.overflow.i128",
837-
838-
Uint(U8) => "llvm.umul.with.overflow.i8",
839-
Uint(U16) => "llvm.umul.with.overflow.i16",
840-
Uint(U32) => "llvm.umul.with.overflow.i32",
841-
Uint(U64) => "llvm.umul.with.overflow.i64",
842-
Uint(U128) => "llvm.umul.with.overflow.i128",
843-
844-
_ => unreachable!(),
845-
},
846-
};
847-
848-
let intrinsic = self.cx().get_intrinsic(&name);
849-
let res = self.call(intrinsic, &[lhs, rhs], None);
850-
(
851-
self.extract_value(res, 0),
852-
self.extract_value(res, 1),
853-
)
854-
}
855781
}
856782

857783
fn copy_intrinsic(

src/librustc_codegen_ssa/mir/rvalue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
693693
mir::BinOp::Mul => OverflowOp::Mul,
694694
_ => unreachable!()
695695
};
696-
bx.call_overflow_intrinsic(oop, input_ty, lhs, rhs)
696+
bx.checked_binop(oop, input_ty, lhs, rhs)
697697
}
698698
mir::BinOp::Shl | mir::BinOp::Shr => {
699699
let lhs_llty = bx.cx().val_ty(lhs);

src/librustc_codegen_ssa/traits/builder.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use super::HasCodegen;
1717
use common::{AtomicOrdering, AtomicRmwBinOp, IntPredicate, RealPredicate, SynchronizationScope};
1818
use mir::operand::OperandRef;
1919
use mir::place::PlaceRef;
20+
use rustc::ty::Ty;
2021
use rustc::ty::layout::{Align, Size};
2122
use std::ffi::CStr;
2223
use MemFlags;
@@ -25,6 +26,13 @@ use std::borrow::Cow;
2526
use std::ops::Range;
2627
use syntax::ast::AsmDialect;
2728

29+
#[derive(Copy, Clone)]
30+
pub enum OverflowOp {
31+
Add,
32+
Sub,
33+
Mul,
34+
}
35+
2836
pub trait BuilderMethods<'a, 'tcx: 'a>:
2937
HasCodegen<'tcx>
3038
+ DebugInfoBuilderMethods<'tcx>
@@ -97,6 +105,14 @@ pub trait BuilderMethods<'a, 'tcx: 'a>:
97105
fn fneg(&mut self, v: Self::Value) -> Self::Value;
98106
fn not(&mut self, v: Self::Value) -> Self::Value;
99107

108+
fn checked_binop(
109+
&mut self,
110+
oop: OverflowOp,
111+
ty: Ty,
112+
lhs: Self::Value,
113+
rhs: Self::Value,
114+
) -> (Self::Value, Self::Value);
115+
100116
fn alloca(&mut self, ty: Self::Type, name: &str, align: Align) -> Self::Value;
101117
fn dynamic_alloca(&mut self, ty: Self::Type, name: &str, align: Align) -> Self::Value;
102118
fn array_alloca(

src/librustc_codegen_ssa/traits/intrinsic.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@ use rustc::ty::Ty;
1414
use rustc_target::abi::call::FnType;
1515
use syntax_pos::Span;
1616

17-
#[derive(Copy, Clone)]
18-
pub enum OverflowOp {
19-
Add,
20-
Sub,
21-
Mul,
22-
}
23-
2417
pub trait IntrinsicCallMethods<'tcx>: BackendTypes {
2518
/// Remember to add all intrinsics here, in librustc_typeck/check/mod.rs,
2619
/// and in libcore/intrinsics.rs; if you need access to any llvm intrinsics,
@@ -37,12 +30,4 @@ pub trait IntrinsicCallMethods<'tcx>: BackendTypes {
3730
fn abort(&mut self);
3831
fn assume(&mut self, val: Self::Value);
3932
fn expect(&mut self, cond: Self::Value, expected: bool) -> Self::Value;
40-
41-
fn call_overflow_intrinsic(
42-
&mut self,
43-
oop: OverflowOp,
44-
ty: Ty,
45-
lhs: Self::Value,
46-
rhs: Self::Value,
47-
) -> (Self::Value, Self::Value);
4833
}

src/librustc_codegen_ssa/traits/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ mod write;
4040
pub use self::abi::{AbiBuilderMethods, AbiMethods};
4141
pub use self::asm::{AsmBuilderMethods, AsmMethods};
4242
pub use self::backend::{Backend, BackendTypes, ExtraBackendMethods};
43-
pub use self::builder::BuilderMethods;
43+
pub use self::builder::{BuilderMethods, OverflowOp};
4444
pub use self::consts::ConstMethods;
4545
pub use self::debuginfo::{DebugInfoBuilderMethods, DebugInfoMethods};
4646
pub use self::declare::{DeclareMethods, PreDefineMethods};
47-
pub use self::intrinsic::{IntrinsicCallMethods, OverflowOp};
47+
pub use self::intrinsic::IntrinsicCallMethods;
4848
pub use self::misc::MiscMethods;
4949
pub use self::statics::StaticMethods;
5050
pub use self::type_::{

0 commit comments

Comments
 (0)