Skip to content

Commit 51b7f27

Browse files
denismerigouxeddyb
authored andcommitted
Generalized IntPredicate in the BuilderMethods trait
1 parent 14798d6 commit 51b7f27

File tree

9 files changed

+73
-38
lines changed

9 files changed

+73
-38
lines changed

src/librustc_codegen_llvm/base.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ use rustc_data_structures::small_c_str::SmallCStr;
7575
use rustc_data_structures::sync::Lrc;
7676
use rustc_data_structures::indexed_vec::Idx;
7777

78-
use traits::BuilderMethods;
78+
use traits::{IntPredicate, BuilderMethods};
7979
use llvm::BasicBlock;
8080

8181
use std::any::Any;
@@ -127,14 +127,14 @@ impl Drop for StatRecorder<'a, 'll, 'tcx> {
127127

128128
pub fn bin_op_to_icmp_predicate(op: hir::BinOpKind,
129129
signed: bool)
130-
-> llvm::IntPredicate {
130+
-> IntPredicate {
131131
match op {
132-
hir::BinOpKind::Eq => llvm::IntEQ,
133-
hir::BinOpKind::Ne => llvm::IntNE,
134-
hir::BinOpKind::Lt => if signed { llvm::IntSLT } else { llvm::IntULT },
135-
hir::BinOpKind::Le => if signed { llvm::IntSLE } else { llvm::IntULE },
136-
hir::BinOpKind::Gt => if signed { llvm::IntSGT } else { llvm::IntUGT },
137-
hir::BinOpKind::Ge => if signed { llvm::IntSGE } else { llvm::IntUGE },
132+
hir::BinOpKind::Eq => IntPredicate::IntEQ,
133+
hir::BinOpKind::Ne => IntPredicate::IntNE,
134+
hir::BinOpKind::Lt => if signed { IntPredicate::IntSLT } else { IntPredicate::IntULT },
135+
hir::BinOpKind::Le => if signed { IntPredicate::IntSLE } else { IntPredicate::IntULE },
136+
hir::BinOpKind::Gt => if signed { IntPredicate::IntSGT } else { IntPredicate::IntUGT },
137+
hir::BinOpKind::Ge => if signed { IntPredicate::IntSGE } else { IntPredicate::IntUGE },
138138
op => {
139139
bug!("comparison_op_to_icmp_predicate: expected comparison operator, \
140140
found {:?}",

src/librustc_codegen_llvm/builder.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use llvm::{AtomicRmwBinOp, AtomicOrdering, SynchronizationScope, AsmDialect};
12-
use llvm::{IntPredicate, RealPredicate, False, OperandBundleDef};
12+
use llvm::{RealPredicate, False, OperandBundleDef};
1313
use llvm::{self, BasicBlock};
1414
use common::*;
1515
use type_::Type;
@@ -19,7 +19,7 @@ use rustc::ty::TyCtxt;
1919
use rustc::ty::layout::{Align, Size};
2020
use rustc::session::{config, Session};
2121
use rustc_data_structures::small_c_str::SmallCStr;
22-
use traits::BuilderMethods;
22+
use traits::{self, BuilderMethods};
2323

2424
use std::borrow::Cow;
2525
use std::ops::Range;
@@ -689,8 +689,9 @@ impl BuilderMethods<'a, 'll, 'tcx, Value, BasicBlock>
689689
}
690690

691691
/* Comparisons */
692-
fn icmp(&self, op: IntPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
692+
fn icmp(&self, op: traits::IntPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
693693
self.count_insn("icmp");
694+
let op : llvm::IntPredicate = traits::IntPredicateMethods::convert_to_backend_specific(op);
694695
unsafe {
695696
llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, noname())
696697
}
@@ -1048,8 +1049,9 @@ impl BuilderMethods<'a, 'll, 'tcx, Value, BasicBlock>
10481049
src: &'ll Value,
10491050
order: AtomicOrdering,
10501051
failure_order: AtomicOrdering,
1051-
weak: llvm::Bool,
1052+
weak: bool,
10521053
) -> &'ll Value {
1054+
let weak = if weak { llvm::True } else { llvm::False };
10531055
unsafe {
10541056
llvm::LLVMRustBuildAtomicCmpXchg(self.llbuilder, dst, cmp, src,
10551057
order, failure_order, weak)

src/librustc_codegen_llvm/glue.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ use std;
1616

1717
use builder::Builder;
1818
use common::*;
19-
use llvm;
2019
use meth;
2120
use rustc::ty::layout::LayoutOf;
2221
use rustc::ty::{self, Ty};
2322
use value::Value;
24-
use traits::BuilderMethods;
23+
use traits::{IntPredicate,BuilderMethods};
2524

2625
pub fn size_and_align_of_dst(
2726
bx: &Builder<'_, 'll, 'tcx>,
@@ -100,7 +99,7 @@ pub fn size_and_align_of_dst(
10099
// pick the correct alignment statically.
101100
C_usize(cx, std::cmp::max(sized_align, unsized_align) as u64)
102101
}
103-
_ => bx.select(bx.icmp(llvm::IntUGT, sized_align, unsized_align),
102+
_ => bx.select(bx.icmp(IntPredicate::IntUGT, sized_align, unsized_align),
104103
sized_align,
105104
unsized_align)
106105
};

src/librustc_codegen_llvm/intrinsic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ pub fn codegen_intrinsic_call(
478478
"cxchg" | "cxchgweak" => {
479479
let ty = substs.type_at(0);
480480
if int_type_width_signed(ty, cx).is_some() {
481-
let weak = if split[1] == "cxchgweak" { llvm::True } else { llvm::False };
481+
let weak = split[1] == "cxchgweak";
482482
let pair = bx.atomic_cmpxchg(
483483
args[0].immediate(),
484484
args[1].immediate(),

src/librustc_codegen_llvm/llvm/ffi.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use libc::{c_uint, c_int, size_t, c_char};
1919
use libc::{c_ulonglong, c_void};
2020

2121
use std::marker::PhantomData;
22+
use traits;
2223

2324
use super::RustString;
2425

@@ -141,6 +142,23 @@ pub enum IntPredicate {
141142
IntSLE = 41,
142143
}
143144

145+
impl traits::IntPredicateMethods for IntPredicate {
146+
fn convert_to_backend_specific(intpre: traits::IntPredicate) -> Self {
147+
match intpre {
148+
traits::IntPredicate::IntEQ => IntPredicate::IntEQ,
149+
traits::IntPredicate::IntNE => IntPredicate::IntNE,
150+
traits::IntPredicate::IntUGT => IntPredicate::IntUGT,
151+
traits::IntPredicate::IntUGE => IntPredicate::IntUGE,
152+
traits::IntPredicate::IntULT => IntPredicate::IntULT,
153+
traits::IntPredicate::IntULE => IntPredicate::IntULE,
154+
traits::IntPredicate::IntSGT => IntPredicate::IntSGT,
155+
traits::IntPredicate::IntSGE => IntPredicate::IntSGE,
156+
traits::IntPredicate::IntSLT => IntPredicate::IntSLT,
157+
traits::IntPredicate::IntSLE => IntPredicate::IntSLE,
158+
}
159+
}
160+
}
161+
144162
/// LLVMRealPredicate
145163
#[derive(Copy, Clone)]
146164
#[repr(C)]

src/librustc_codegen_llvm/mir/block.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use type_of::LayoutLlvmExt;
2626
use type_::Type;
2727
use value::Value;
2828

29-
use traits::BuilderMethods;
29+
use traits::{IntPredicate,BuilderMethods};
3030

3131
use syntax::symbol::Symbol;
3232
use syntax_pos::Pos;
@@ -210,7 +210,7 @@ impl FunctionCx<'a, 'll, 'tcx, &'ll Value> {
210210
} else {
211211
let switch_llty = bx.cx.layout_of(switch_ty).immediate_llvm_type(bx.cx);
212212
let llval = C_uint_big(switch_llty, values[0]);
213-
let cmp = bx.icmp(llvm::IntEQ, discr.immediate(), llval);
213+
let cmp = bx.icmp(IntPredicate::IntEQ, discr.immediate(), llval);
214214
bx.cond_br(cmp, lltrue, llfalse);
215215
}
216216
} else {

src/librustc_codegen_llvm/mir/place.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use value::Value;
2323
use glue;
2424
use mir::constant::const_alloc_to_llvm;
2525

26-
use traits::BuilderMethods;
26+
use traits::{IntPredicate,BuilderMethods};
2727

2828
use super::{FunctionCx, LocalRef};
2929
use super::operand::{OperandRef, OperandValue};
@@ -332,15 +332,15 @@ impl PlaceRef<'tcx, &'ll Value> {
332332
} else {
333333
C_uint_big(niche_llty, niche_start)
334334
};
335-
bx.select(bx.icmp(llvm::IntEQ, lldiscr, niche_llval),
335+
bx.select(bx.icmp(IntPredicate::IntEQ, lldiscr, niche_llval),
336336
C_uint(cast_to, niche_variants.start().as_u32() as u64),
337337
C_uint(cast_to, dataful_variant.as_u32() as u64))
338338
} else {
339339
// Rebase from niche values to discriminant values.
340340
let delta = niche_start.wrapping_sub(niche_variants.start().as_u32() as u128);
341341
let lldiscr = bx.sub(lldiscr, C_uint_big(niche_llty, delta));
342342
let lldiscr_max = C_uint(niche_llty, niche_variants.end().as_u32() as u64);
343-
bx.select(bx.icmp(llvm::IntULE, lldiscr, lldiscr_max),
343+
bx.select(bx.icmp(IntPredicate::IntULE, lldiscr, lldiscr_max),
344344
bx.intcast(lldiscr, cast_to, false),
345345
C_uint(cast_to, dataful_variant.as_u32() as u64))
346346
}

src/librustc_codegen_llvm/mir/rvalue.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use type_::Type;
2828
use type_of::LayoutLlvmExt;
2929
use value::Value;
3030

31-
use traits::BuilderMethods;
31+
use traits::{IntPredicate,BuilderMethods};
3232

3333
use super::{FunctionCx, LocalRef};
3434
use super::operand::{OperandRef, OperandValue};
@@ -135,7 +135,7 @@ impl FunctionCx<'a, 'll, 'tcx, &'ll Value> {
135135
bx.br(header_bx.llbb());
136136
let current = header_bx.phi(common::val_ty(start), &[start], &[bx.llbb()]);
137137

138-
let keep_going = header_bx.icmp(llvm::IntNE, current, end);
138+
let keep_going = header_bx.icmp(IntPredicate::IntNE, current, end);
139139
header_bx.cond_br(keep_going, body_bx.llbb(), next_bx.llbb());
140140

141141
cg_elem.val.store(&body_bx,
@@ -337,7 +337,7 @@ impl FunctionCx<'a, 'll, 'tcx, &'ll Value> {
337337
// convenient place to put the `assume`.
338338

339339
base::call_assume(&bx, bx.icmp(
340-
llvm::IntULE,
340+
IntPredicate::IntULE,
341341
llval,
342342
C_uint_big(ll_t_in, *scalar.valid_range.end())
343343
));
@@ -639,31 +639,31 @@ impl FunctionCx<'a, 'll, 'tcx, &'ll Value> {
639639
match op {
640640
mir::BinOp::Eq => {
641641
bx.and(
642-
bx.icmp(llvm::IntEQ, lhs_addr, rhs_addr),
643-
bx.icmp(llvm::IntEQ, lhs_extra, rhs_extra)
642+
bx.icmp(IntPredicate::IntEQ, lhs_addr, rhs_addr),
643+
bx.icmp(IntPredicate::IntEQ, lhs_extra, rhs_extra)
644644
)
645645
}
646646
mir::BinOp::Ne => {
647647
bx.or(
648-
bx.icmp(llvm::IntNE, lhs_addr, rhs_addr),
649-
bx.icmp(llvm::IntNE, lhs_extra, rhs_extra)
648+
bx.icmp(IntPredicate::IntNE, lhs_addr, rhs_addr),
649+
bx.icmp(IntPredicate::IntNE, lhs_extra, rhs_extra)
650650
)
651651
}
652652
mir::BinOp::Le | mir::BinOp::Lt |
653653
mir::BinOp::Ge | mir::BinOp::Gt => {
654654
// a OP b ~ a.0 STRICT(OP) b.0 | (a.0 == b.0 && a.1 OP a.1)
655655
let (op, strict_op) = match op {
656-
mir::BinOp::Lt => (llvm::IntULT, llvm::IntULT),
657-
mir::BinOp::Le => (llvm::IntULE, llvm::IntULT),
658-
mir::BinOp::Gt => (llvm::IntUGT, llvm::IntUGT),
659-
mir::BinOp::Ge => (llvm::IntUGE, llvm::IntUGT),
656+
mir::BinOp::Lt => (IntPredicate::IntULT, IntPredicate::IntULT),
657+
mir::BinOp::Le => (IntPredicate::IntULE, IntPredicate::IntULT),
658+
mir::BinOp::Gt => (IntPredicate::IntUGT, IntPredicate::IntUGT),
659+
mir::BinOp::Ge => (IntPredicate::IntUGE, IntPredicate::IntUGT),
660660
_ => bug!(),
661661
};
662662

663663
bx.or(
664664
bx.icmp(strict_op, lhs_addr, rhs_addr),
665665
bx.and(
666-
bx.icmp(llvm::IntEQ, lhs_addr, rhs_addr),
666+
bx.icmp(IntPredicate::IntEQ, lhs_addr, rhs_addr),
667667
bx.icmp(op, lhs_extra, rhs_extra)
668668
)
669669
)
@@ -710,7 +710,7 @@ impl FunctionCx<'a, 'll, 'tcx, &'ll Value> {
710710
let invert_mask = common::shift_mask_val(&bx, lhs_llty, rhs_llty, true);
711711
let outer_bits = bx.and(rhs, invert_mask);
712712

713-
let of = bx.icmp(llvm::IntNE, outer_bits, C_null(rhs_llty));
713+
let of = bx.icmp(IntPredicate::IntNE, outer_bits, C_null(rhs_llty));
714714
let val = self.codegen_scalar_binop(bx, op, lhs, rhs, input_ty);
715715

716716
(val, of)
@@ -838,7 +838,7 @@ fn cast_int_to_float(bx: &Builder<'_, 'll, '_>,
838838
const MAX_F32_PLUS_HALF_ULP: u128 = ((1 << (Single::PRECISION + 1)) - 1)
839839
<< (Single::MAX_EXP - Single::PRECISION as i16);
840840
let max = C_uint_big(int_ty, MAX_F32_PLUS_HALF_ULP);
841-
let overflow = bx.icmp(llvm::IntUGE, x, max);
841+
let overflow = bx.icmp(IntPredicate::IntUGE, x, max);
842842
let infinity_bits = C_u32(bx.cx, ieee::Single::INFINITY.to_bits() as u32);
843843
let infinity = consts::bitcast(infinity_bits, float_ty);
844844
bx.select(overflow, infinity, bx.uitofp(x, float_ty))

src/librustc_codegen_llvm/traits.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
// except according to those terms.
1010

1111
use llvm::{AtomicRmwBinOp, AtomicOrdering, SynchronizationScope, AsmDialect};
12-
use llvm::{IntPredicate, RealPredicate, OperandBundleDef};
13-
use llvm;
12+
use llvm::{RealPredicate, OperandBundleDef};
1413
use common::*;
1514
use type_::Type;
1615
use libc::c_char;
@@ -22,6 +21,23 @@ use builder::MemFlags;
2221
use std::borrow::Cow;
2322
use std::ops::Range;
2423

24+
pub enum IntPredicate {
25+
IntEQ,
26+
IntNE,
27+
IntUGT,
28+
IntUGE,
29+
IntULT,
30+
IntULE,
31+
IntSGT,
32+
IntSGE,
33+
IntSLT,
34+
IntSLE,
35+
}
36+
37+
pub trait IntPredicateMethods {
38+
fn convert_to_backend_specific(intpre : IntPredicate) -> Self;
39+
}
40+
2541

2642
pub trait BuilderMethods<'a, 'll :'a, 'tcx: 'll,
2743
Value : ?Sized,
@@ -251,7 +267,7 @@ pub trait BuilderMethods<'a, 'll :'a, 'tcx: 'll,
251267
src: &'ll Value,
252268
order: AtomicOrdering,
253269
failure_order: AtomicOrdering,
254-
weak: llvm::Bool,
270+
weak: bool,
255271
) -> &'ll Value;
256272
fn atomic_rmw(
257273
&self,

0 commit comments

Comments
 (0)