]> git.lizzy.rs Git - rust.git/blobdiff - src/base.rs
Use don't unroll loop in Rvalue::Repeat
[rust.git] / src / base.rs
index 342c12ffc4118206688b01e380924cdbe896d23a..cae2115ab181b630f67b1e090df951313c953e0c 100644 (file)
@@ -105,11 +105,12 @@ pub(crate) fn trans_fn<'tcx, B: Backend + 'static>(
         );
     });
 
-    // If the return block is not reachable, then the SSA builder may have inserted a `iconst.i128`
+    // If the return block is not reachable, then the SSA builder may have inserted an `iconst.i128`
     // instruction, which doesn't have an encoding.
     context.compute_cfg();
     context.compute_domtree();
     context.eliminate_unreachable_code(cx.module.isa()).unwrap();
+    context.dce(cx.module.isa()).unwrap();
 
     // Define function
     let module = &mut cx.module;
@@ -280,7 +281,7 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, impl Backend>) {
                         let len = trans_operand(fx, len).load_scalar(fx);
                         let index = trans_operand(fx, index).load_scalar(fx);
                         args = [index, len, location];
-                        rustc_hir::lang_items::PanicBoundsCheckFnLangItem
+                        rustc_hir::LangItem::PanicBoundsCheck
                     }
                     _ => {
                         let msg_str = msg.description();
@@ -290,7 +291,7 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, impl Backend>) {
                             .ins()
                             .iconst(fx.pointer_type, i64::try_from(msg_str.len()).unwrap());
                         args = [msg_ptr, msg_len, location];
-                        rustc_hir::lang_items::PanicFnLangItem
+                        rustc_hir::LangItem::Panic
                     }
                 };
 
@@ -315,18 +316,45 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, impl Backend>) {
 
             TerminatorKind::SwitchInt {
                 discr,
-                switch_ty: _,
+                switch_ty,
                 values,
                 targets,
             } => {
                 let discr = trans_operand(fx, discr).load_scalar(fx);
-                let mut switch = ::cranelift_frontend::Switch::new();
-                for (i, value) in values.iter().enumerate() {
-                    let block = fx.get_block(targets[i]);
-                    switch.set_entry(*value, block);
+
+                if switch_ty.kind() == fx.tcx.types.bool.kind() {
+                    assert_eq!(targets.len(), 2);
+                    let then_block = fx.get_block(targets[0]);
+                    let else_block = fx.get_block(targets[1]);
+                    let test_zero = match **values {
+                        [0] => true,
+                        [1] => false,
+                        _ => unreachable!("{:?}", values),
+                    };
+
+                    let discr = crate::optimize::peephole::maybe_unwrap_bint(&mut fx.bcx, discr);
+                    let (discr, is_inverted) =
+                        crate::optimize::peephole::maybe_unwrap_bool_not(&mut fx.bcx, discr);
+                    let test_zero = if is_inverted { !test_zero } else { test_zero };
+                    let discr = crate::optimize::peephole::maybe_unwrap_bint(&mut fx.bcx, discr);
+                    let discr =
+                        crate::optimize::peephole::make_branchable_value(&mut fx.bcx, discr);
+                    if test_zero {
+                        fx.bcx.ins().brz(discr, then_block, &[]);
+                        fx.bcx.ins().jump(else_block, &[]);
+                    } else {
+                        fx.bcx.ins().brnz(discr, then_block, &[]);
+                        fx.bcx.ins().jump(else_block, &[]);
+                    }
+                } else {
+                    let mut switch = ::cranelift_frontend::Switch::new();
+                    for (i, value) in values.iter().enumerate() {
+                        let block = fx.get_block(targets[i]);
+                        switch.set_entry(*value, block);
+                    }
+                    let otherwise_block = fx.get_block(targets[targets.len() - 1]);
+                    switch.emit(&mut fx.bcx, discr, otherwise_block);
                 }
-                let otherwise_block = fx.get_block(targets[targets.len() - 1]);
-                switch.emit(&mut fx.bcx, discr, otherwise_block);
             }
             TerminatorKind::Call {
                 func,
@@ -476,7 +504,7 @@ fn trans_stmt<'tcx>(
                     let layout = operand.layout();
                     let val = operand.load_scalar(fx);
                     let res = match un_op {
-                        UnOp::Not => match layout.ty.kind {
+                        UnOp::Not => match layout.ty.kind() {
                             ty::Bool => {
                                 let res = fx.bcx.ins().icmp_imm(IntCC::Equal, val, 0);
                                 CValue::by_val(fx.bcx.ins().bint(types::I8, res), layout)
@@ -486,7 +514,7 @@ fn trans_stmt<'tcx>(
                             }
                             _ => unreachable!("un op Not for {:?}", layout.ty),
                         },
-                        UnOp::Neg => match layout.ty.kind {
+                        UnOp::Neg => match layout.ty.kind() {
                             ty::Int(IntTy::I128) => {
                                 // FIXME remove this case once ineg.i128 works
                                 let zero = CValue::const_val(fx, layout, 0);
@@ -502,7 +530,7 @@ fn trans_stmt<'tcx>(
                 Rvalue::Cast(CastKind::Pointer(PointerCast::ReifyFnPointer), operand, to_ty) => {
                     let from_ty = fx.monomorphize(&operand.ty(&fx.mir.local_decls, fx.tcx));
                     let to_layout = fx.layout_of(fx.monomorphize(to_ty));
-                    match from_ty.kind {
+                    match *from_ty.kind() {
                         ty::FnDef(def_id, substs) => {
                             let func_ref = fx.get_function_ref(
                                 Instance::resolve_for_fn_ptr(
@@ -557,10 +585,10 @@ fn is_fat_ptr<'tcx>(
                             let (ptr, _extra) = operand.load_scalar_pair(fx);
                             lval.write_cvalue(fx, CValue::by_val(ptr, dest_layout))
                         }
-                    } else if let ty::Adt(adt_def, _substs) = from_ty.kind {
+                    } else if let ty::Adt(adt_def, _substs) = from_ty.kind() {
                         // enum -> discriminant value
                         assert!(adt_def.is_enum());
-                        match to_ty.kind {
+                        match to_ty.kind() {
                             ty::Uint(_) | ty::Int(_) => {}
                             _ => unreachable!("cast adt {} -> {}", from_ty, to_ty),
                         }
@@ -630,7 +658,7 @@ fn is_fat_ptr<'tcx>(
                     _to_ty,
                 ) => {
                     let operand = trans_operand(fx, operand);
-                    match operand.layout().ty.kind {
+                    match *operand.layout().ty.kind() {
                         ty::Closure(def_id, substs) => {
                             let instance = Instance::resolve_closure(
                                 fx.tcx,
@@ -665,10 +693,29 @@ fn is_fat_ptr<'tcx>(
                         .val
                         .try_to_bits(fx.tcx.data_layout.pointer_size)
                         .unwrap();
-                    for i in 0..times {
-                        let index = fx.bcx.ins().iconst(fx.pointer_type, i as i64);
+                    if fx.clif_type(operand.layout().ty) == Some(types::I8) {
+                        let times = fx.bcx.ins().iconst(fx.pointer_type, times as i64);
+                        // FIXME use emit_small_memset where possible
+                        let addr = lval.to_ptr().get_addr(fx);
+                        let val = operand.load_scalar(fx);
+                        fx.bcx.call_memset(fx.cx.module.target_config(), addr, val, times);
+                    } else {
+                        let loop_block = fx.bcx.create_block();
+                        let done_block = fx.bcx.create_block();
+                        let index = fx.bcx.append_block_param(loop_block, fx.pointer_type);
+                        let zero = fx.bcx.ins().iconst(fx.pointer_type, 0);
+                        fx.bcx.ins().jump(loop_block, &[zero]);
+
+                        fx.bcx.switch_to_block(loop_block);
                         let to = lval.place_index(fx, index);
                         to.write_cvalue(fx, operand);
+
+                        let index = fx.bcx.ins().iadd_imm(index, 1);
+                        let done = fx.bcx.ins().icmp_imm(IntCC::Equal, index, times as i64);
+                        fx.bcx.ins().brz(done, loop_block, &[index]);
+                        fx.bcx.ins().jump(done_block, &[]);
+
+                        fx.bcx.switch_to_block(done_block);
                     }
                 }
                 Rvalue::Len(place) => {
@@ -678,8 +725,6 @@ fn is_fat_ptr<'tcx>(
                     lval.write_cvalue(fx, CValue::by_val(len, usize_layout));
                 }
                 Rvalue::NullaryOp(NullOp::Box, content_ty) => {
-                    use rustc_hir::lang_items::ExchangeMallocFnLangItem;
-
                     let usize_type = fx.clif_type(fx.tcx.types.usize).unwrap();
                     let content_ty = fx.monomorphize(content_ty);
                     let layout = fx.layout_of(content_ty);
@@ -691,7 +736,11 @@ fn is_fat_ptr<'tcx>(
                     let box_layout = fx.layout_of(fx.tcx.mk_box(content_ty));
 
                     // Allocate space:
-                    let def_id = match fx.tcx.lang_items().require(ExchangeMallocFnLangItem) {
+                    let def_id = match fx
+                        .tcx
+                        .lang_items()
+                        .require(rustc_hir::LangItem::ExchangeMalloc)
+                    {
                         Ok(id) => id,
                         Err(s) => {
                             fx.tcx
@@ -840,7 +889,7 @@ fn codegen_array_len<'tcx>(
     fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
     place: CPlace<'tcx>,
 ) -> Value {
-    match place.layout().ty.kind {
+    match *place.layout().ty.kind() {
         ty::Array(_elem_ty, len) => {
             let len = fx
                 .monomorphize(&len)
@@ -879,11 +928,12 @@ pub(crate) fn trans_place<'tcx>(
                 min_length: _,
                 from_end,
             } => {
+                let offset: u64 = offset;
                 let index = if !from_end {
-                    fx.bcx.ins().iconst(fx.pointer_type, i64::from(offset))
+                    fx.bcx.ins().iconst(fx.pointer_type, offset as i64)
                 } else {
                     let len = codegen_array_len(fx, cplace);
-                    fx.bcx.ins().iadd_imm(len, -i64::from(offset))
+                    fx.bcx.ins().iadd_imm(len, -(offset as i64))
                 };
                 cplace = cplace.place_index(fx, index);
             }
@@ -891,13 +941,16 @@ pub(crate) fn trans_place<'tcx>(
                 // These indices are generated by slice patterns.
                 // slice[from:-to] in Python terms.
 
-                match cplace.layout().ty.kind {
+                let from: u64 = from;
+                let to: u64 = to;
+
+                match cplace.layout().ty.kind() {
                     ty::Array(elem_ty, _len) => {
                         assert!(!from_end, "array subslices are never `from_end`");
                         let elem_layout = fx.layout_of(elem_ty);
                         let ptr = cplace.to_ptr();
                         cplace = CPlace::for_ptr(
-                            ptr.offset_i64(fx, elem_layout.size.bytes() as i64 * i64::from(from)),
+                            ptr.offset_i64(fx, elem_layout.size.bytes() as i64 * (from as i64)),
                             fx.layout_of(fx.tcx.mk_array(elem_ty, u64::from(to) - u64::from(from))),
                         );
                     }
@@ -907,10 +960,8 @@ pub(crate) fn trans_place<'tcx>(
                         let (ptr, len) = cplace.to_ptr_maybe_unsized();
                         let len = len.unwrap();
                         cplace = CPlace::for_ptr_with_extra(
-                            ptr.offset_i64(fx, elem_layout.size.bytes() as i64 * i64::from(from)),
-                            fx.bcx
-                                .ins()
-                                .iadd_imm(len, -(i64::from(from) + i64::from(to))),
+                            ptr.offset_i64(fx, elem_layout.size.bytes() as i64 * (from as i64)),
+                            fx.bcx.ins().iadd_imm(len, -(from as i64 + to as i64)),
                             cplace.layout(),
                         );
                     }