1
Fork 0

Generalized IntPredicate in the BuilderMethods trait

This commit is contained in:
Denis Merigoux 2018-08-20 18:16:51 +02:00 committed by Eduard-Mihai Burtescu
parent 14798d6937
commit 51b7f2739b
9 changed files with 73 additions and 38 deletions

View file

@ -75,7 +75,7 @@ use rustc_data_structures::small_c_str::SmallCStr;
use rustc_data_structures::sync::Lrc;
use rustc_data_structures::indexed_vec::Idx;
use traits::BuilderMethods;
use traits::{IntPredicate, BuilderMethods};
use llvm::BasicBlock;
use std::any::Any;
@ -127,14 +127,14 @@ impl Drop for StatRecorder<'a, 'll, 'tcx> {
pub fn bin_op_to_icmp_predicate(op: hir::BinOpKind,
signed: bool)
-> llvm::IntPredicate {
-> IntPredicate {
match op {
hir::BinOpKind::Eq => llvm::IntEQ,
hir::BinOpKind::Ne => llvm::IntNE,
hir::BinOpKind::Lt => if signed { llvm::IntSLT } else { llvm::IntULT },
hir::BinOpKind::Le => if signed { llvm::IntSLE } else { llvm::IntULE },
hir::BinOpKind::Gt => if signed { llvm::IntSGT } else { llvm::IntUGT },
hir::BinOpKind::Ge => if signed { llvm::IntSGE } else { llvm::IntUGE },
hir::BinOpKind::Eq => IntPredicate::IntEQ,
hir::BinOpKind::Ne => IntPredicate::IntNE,
hir::BinOpKind::Lt => if signed { IntPredicate::IntSLT } else { IntPredicate::IntULT },
hir::BinOpKind::Le => if signed { IntPredicate::IntSLE } else { IntPredicate::IntULE },
hir::BinOpKind::Gt => if signed { IntPredicate::IntSGT } else { IntPredicate::IntUGT },
hir::BinOpKind::Ge => if signed { IntPredicate::IntSGE } else { IntPredicate::IntUGE },
op => {
bug!("comparison_op_to_icmp_predicate: expected comparison operator, \
found {:?}",

View file

@ -9,7 +9,7 @@
// except according to those terms.
use llvm::{AtomicRmwBinOp, AtomicOrdering, SynchronizationScope, AsmDialect};
use llvm::{IntPredicate, RealPredicate, False, OperandBundleDef};
use llvm::{RealPredicate, False, OperandBundleDef};
use llvm::{self, BasicBlock};
use common::*;
use type_::Type;
@ -19,7 +19,7 @@ use rustc::ty::TyCtxt;
use rustc::ty::layout::{Align, Size};
use rustc::session::{config, Session};
use rustc_data_structures::small_c_str::SmallCStr;
use traits::BuilderMethods;
use traits::{self, BuilderMethods};
use std::borrow::Cow;
use std::ops::Range;
@ -689,8 +689,9 @@ impl BuilderMethods<'a, 'll, 'tcx, Value, BasicBlock>
}
/* Comparisons */
fn icmp(&self, op: IntPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
fn icmp(&self, op: traits::IntPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
self.count_insn("icmp");
let op : llvm::IntPredicate = traits::IntPredicateMethods::convert_to_backend_specific(op);
unsafe {
llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, noname())
}
@ -1048,8 +1049,9 @@ impl BuilderMethods<'a, 'll, 'tcx, Value, BasicBlock>
src: &'ll Value,
order: AtomicOrdering,
failure_order: AtomicOrdering,
weak: llvm::Bool,
weak: bool,
) -> &'ll Value {
let weak = if weak { llvm::True } else { llvm::False };
unsafe {
llvm::LLVMRustBuildAtomicCmpXchg(self.llbuilder, dst, cmp, src,
order, failure_order, weak)

View file

@ -16,12 +16,11 @@ use std;
use builder::Builder;
use common::*;
use llvm;
use meth;
use rustc::ty::layout::LayoutOf;
use rustc::ty::{self, Ty};
use value::Value;
use traits::BuilderMethods;
use traits::{IntPredicate,BuilderMethods};
pub fn size_and_align_of_dst(
bx: &Builder<'_, 'll, 'tcx>,
@ -100,7 +99,7 @@ pub fn size_and_align_of_dst(
// pick the correct alignment statically.
C_usize(cx, std::cmp::max(sized_align, unsized_align) as u64)
}
_ => bx.select(bx.icmp(llvm::IntUGT, sized_align, unsized_align),
_ => bx.select(bx.icmp(IntPredicate::IntUGT, sized_align, unsized_align),
sized_align,
unsized_align)
};

View file

@ -478,7 +478,7 @@ pub fn codegen_intrinsic_call(
"cxchg" | "cxchgweak" => {
let ty = substs.type_at(0);
if int_type_width_signed(ty, cx).is_some() {
let weak = if split[1] == "cxchgweak" { llvm::True } else { llvm::False };
let weak = split[1] == "cxchgweak";
let pair = bx.atomic_cmpxchg(
args[0].immediate(),
args[1].immediate(),

View file

@ -19,6 +19,7 @@ use libc::{c_uint, c_int, size_t, c_char};
use libc::{c_ulonglong, c_void};
use std::marker::PhantomData;
use traits;
use super::RustString;
@ -141,6 +142,23 @@ pub enum IntPredicate {
IntSLE = 41,
}
impl traits::IntPredicateMethods for IntPredicate {
fn convert_to_backend_specific(intpre: traits::IntPredicate) -> Self {
match intpre {
traits::IntPredicate::IntEQ => IntPredicate::IntEQ,
traits::IntPredicate::IntNE => IntPredicate::IntNE,
traits::IntPredicate::IntUGT => IntPredicate::IntUGT,
traits::IntPredicate::IntUGE => IntPredicate::IntUGE,
traits::IntPredicate::IntULT => IntPredicate::IntULT,
traits::IntPredicate::IntULE => IntPredicate::IntULE,
traits::IntPredicate::IntSGT => IntPredicate::IntSGT,
traits::IntPredicate::IntSGE => IntPredicate::IntSGE,
traits::IntPredicate::IntSLT => IntPredicate::IntSLT,
traits::IntPredicate::IntSLE => IntPredicate::IntSLE,
}
}
}
/// LLVMRealPredicate
#[derive(Copy, Clone)]
#[repr(C)]

View file

@ -26,7 +26,7 @@ use type_of::LayoutLlvmExt;
use type_::Type;
use value::Value;
use traits::BuilderMethods;
use traits::{IntPredicate,BuilderMethods};
use syntax::symbol::Symbol;
use syntax_pos::Pos;
@ -210,7 +210,7 @@ impl FunctionCx<'a, 'll, 'tcx, &'ll Value> {
} else {
let switch_llty = bx.cx.layout_of(switch_ty).immediate_llvm_type(bx.cx);
let llval = C_uint_big(switch_llty, values[0]);
let cmp = bx.icmp(llvm::IntEQ, discr.immediate(), llval);
let cmp = bx.icmp(IntPredicate::IntEQ, discr.immediate(), llval);
bx.cond_br(cmp, lltrue, llfalse);
}
} else {

View file

@ -23,7 +23,7 @@ use value::Value;
use glue;
use mir::constant::const_alloc_to_llvm;
use traits::BuilderMethods;
use traits::{IntPredicate,BuilderMethods};
use super::{FunctionCx, LocalRef};
use super::operand::{OperandRef, OperandValue};
@ -332,7 +332,7 @@ impl PlaceRef<'tcx, &'ll Value> {
} else {
C_uint_big(niche_llty, niche_start)
};
bx.select(bx.icmp(llvm::IntEQ, lldiscr, niche_llval),
bx.select(bx.icmp(IntPredicate::IntEQ, lldiscr, niche_llval),
C_uint(cast_to, niche_variants.start().as_u32() as u64),
C_uint(cast_to, dataful_variant.as_u32() as u64))
} else {
@ -340,7 +340,7 @@ impl PlaceRef<'tcx, &'ll Value> {
let delta = niche_start.wrapping_sub(niche_variants.start().as_u32() as u128);
let lldiscr = bx.sub(lldiscr, C_uint_big(niche_llty, delta));
let lldiscr_max = C_uint(niche_llty, niche_variants.end().as_u32() as u64);
bx.select(bx.icmp(llvm::IntULE, lldiscr, lldiscr_max),
bx.select(bx.icmp(IntPredicate::IntULE, lldiscr, lldiscr_max),
bx.intcast(lldiscr, cast_to, false),
C_uint(cast_to, dataful_variant.as_u32() as u64))
}

View file

@ -28,7 +28,7 @@ use type_::Type;
use type_of::LayoutLlvmExt;
use value::Value;
use traits::BuilderMethods;
use traits::{IntPredicate,BuilderMethods};
use super::{FunctionCx, LocalRef};
use super::operand::{OperandRef, OperandValue};
@ -135,7 +135,7 @@ impl FunctionCx<'a, 'll, 'tcx, &'ll Value> {
bx.br(header_bx.llbb());
let current = header_bx.phi(common::val_ty(start), &[start], &[bx.llbb()]);
let keep_going = header_bx.icmp(llvm::IntNE, current, end);
let keep_going = header_bx.icmp(IntPredicate::IntNE, current, end);
header_bx.cond_br(keep_going, body_bx.llbb(), next_bx.llbb());
cg_elem.val.store(&body_bx,
@ -337,7 +337,7 @@ impl FunctionCx<'a, 'll, 'tcx, &'ll Value> {
// convenient place to put the `assume`.
base::call_assume(&bx, bx.icmp(
llvm::IntULE,
IntPredicate::IntULE,
llval,
C_uint_big(ll_t_in, *scalar.valid_range.end())
));
@ -639,31 +639,31 @@ impl FunctionCx<'a, 'll, 'tcx, &'ll Value> {
match op {
mir::BinOp::Eq => {
bx.and(
bx.icmp(llvm::IntEQ, lhs_addr, rhs_addr),
bx.icmp(llvm::IntEQ, lhs_extra, rhs_extra)
bx.icmp(IntPredicate::IntEQ, lhs_addr, rhs_addr),
bx.icmp(IntPredicate::IntEQ, lhs_extra, rhs_extra)
)
}
mir::BinOp::Ne => {
bx.or(
bx.icmp(llvm::IntNE, lhs_addr, rhs_addr),
bx.icmp(llvm::IntNE, lhs_extra, rhs_extra)
bx.icmp(IntPredicate::IntNE, lhs_addr, rhs_addr),
bx.icmp(IntPredicate::IntNE, lhs_extra, rhs_extra)
)
}
mir::BinOp::Le | mir::BinOp::Lt |
mir::BinOp::Ge | mir::BinOp::Gt => {
// a OP b ~ a.0 STRICT(OP) b.0 | (a.0 == b.0 && a.1 OP a.1)
let (op, strict_op) = match op {
mir::BinOp::Lt => (llvm::IntULT, llvm::IntULT),
mir::BinOp::Le => (llvm::IntULE, llvm::IntULT),
mir::BinOp::Gt => (llvm::IntUGT, llvm::IntUGT),
mir::BinOp::Ge => (llvm::IntUGE, llvm::IntUGT),
mir::BinOp::Lt => (IntPredicate::IntULT, IntPredicate::IntULT),
mir::BinOp::Le => (IntPredicate::IntULE, IntPredicate::IntULT),
mir::BinOp::Gt => (IntPredicate::IntUGT, IntPredicate::IntUGT),
mir::BinOp::Ge => (IntPredicate::IntUGE, IntPredicate::IntUGT),
_ => bug!(),
};
bx.or(
bx.icmp(strict_op, lhs_addr, rhs_addr),
bx.and(
bx.icmp(llvm::IntEQ, lhs_addr, rhs_addr),
bx.icmp(IntPredicate::IntEQ, lhs_addr, rhs_addr),
bx.icmp(op, lhs_extra, rhs_extra)
)
)
@ -710,7 +710,7 @@ impl FunctionCx<'a, 'll, 'tcx, &'ll Value> {
let invert_mask = common::shift_mask_val(&bx, lhs_llty, rhs_llty, true);
let outer_bits = bx.and(rhs, invert_mask);
let of = bx.icmp(llvm::IntNE, outer_bits, C_null(rhs_llty));
let of = bx.icmp(IntPredicate::IntNE, outer_bits, C_null(rhs_llty));
let val = self.codegen_scalar_binop(bx, op, lhs, rhs, input_ty);
(val, of)
@ -838,7 +838,7 @@ fn cast_int_to_float(bx: &Builder<'_, 'll, '_>,
const MAX_F32_PLUS_HALF_ULP: u128 = ((1 << (Single::PRECISION + 1)) - 1)
<< (Single::MAX_EXP - Single::PRECISION as i16);
let max = C_uint_big(int_ty, MAX_F32_PLUS_HALF_ULP);
let overflow = bx.icmp(llvm::IntUGE, x, max);
let overflow = bx.icmp(IntPredicate::IntUGE, x, max);
let infinity_bits = C_u32(bx.cx, ieee::Single::INFINITY.to_bits() as u32);
let infinity = consts::bitcast(infinity_bits, float_ty);
bx.select(overflow, infinity, bx.uitofp(x, float_ty))

View file

@ -9,8 +9,7 @@
// except according to those terms.
use llvm::{AtomicRmwBinOp, AtomicOrdering, SynchronizationScope, AsmDialect};
use llvm::{IntPredicate, RealPredicate, OperandBundleDef};
use llvm;
use llvm::{RealPredicate, OperandBundleDef};
use common::*;
use type_::Type;
use libc::c_char;
@ -22,6 +21,23 @@ use builder::MemFlags;
use std::borrow::Cow;
use std::ops::Range;
pub enum IntPredicate {
IntEQ,
IntNE,
IntUGT,
IntUGE,
IntULT,
IntULE,
IntSGT,
IntSGE,
IntSLT,
IntSLE,
}
pub trait IntPredicateMethods {
fn convert_to_backend_specific(intpre : IntPredicate) -> Self;
}
pub trait BuilderMethods<'a, 'll :'a, 'tcx: 'll,
Value : ?Sized,
@ -251,7 +267,7 @@ pub trait BuilderMethods<'a, 'll :'a, 'tcx: 'll,
src: &'ll Value,
order: AtomicOrdering,
failure_order: AtomicOrdering,
weak: llvm::Bool,
weak: bool,
) -> &'ll Value;
fn atomic_rmw(
&self,