Skip to content

Commit 6e60fdb

Browse files
committed
Remove unnecessary temporaries from compare_values()
1 parent 1806174 commit 6e60fdb

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

src/librustc_trans/trans/_match.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -875,19 +875,17 @@ fn compare_values<'blk, 'tcx>(cx: Block<'blk, 'tcx>,
875875
debug_loc: DebugLoc)
876876
-> Result<'blk, 'tcx> {
877877
fn compare_str<'blk, 'tcx>(cx: Block<'blk, 'tcx>,
878-
lhs: ValueRef,
879-
rhs: ValueRef,
878+
lhs_data: ValueRef,
879+
lhs_len: ValueRef,
880+
rhs_data: ValueRef,
881+
rhs_len: ValueRef,
880882
rhs_t: Ty<'tcx>,
881883
debug_loc: DebugLoc)
882884
-> Result<'blk, 'tcx> {
883885
let did = langcall(cx,
884886
None,
885887
&format!("comparison of `{}`", rhs_t),
886888
StrEqFnLangItem);
887-
let lhs_data = Load(cx, expr::get_dataptr(cx, lhs));
888-
let lhs_len = Load(cx, expr::get_meta(cx, lhs));
889-
let rhs_data = Load(cx, expr::get_dataptr(cx, rhs));
890-
let rhs_len = Load(cx, expr::get_meta(cx, rhs));
891889
callee::trans_lang_call(cx, did, &[lhs_data, lhs_len, rhs_data, rhs_len], None, debug_loc)
892890
}
893891

@@ -899,32 +897,38 @@ fn compare_values<'blk, 'tcx>(cx: Block<'blk, 'tcx>,
899897

900898
match rhs_t.sty {
901899
ty::TyRef(_, mt) => match mt.ty.sty {
902-
ty::TyStr => compare_str(cx, lhs, rhs, rhs_t, debug_loc),
900+
ty::TyStr => {
901+
let lhs_data = Load(cx, expr::get_dataptr(cx, lhs));
902+
let lhs_len = Load(cx, expr::get_meta(cx, lhs));
903+
let rhs_data = Load(cx, expr::get_dataptr(cx, rhs));
904+
let rhs_len = Load(cx, expr::get_meta(cx, rhs));
905+
compare_str(cx, lhs_data, lhs_len, rhs_data, rhs_len, rhs_t, debug_loc)
906+
}
903907
ty::TyArray(ty, _) | ty::TySlice(ty) => match ty.sty {
904908
ty::TyUint(ast::TyU8) => {
905909
// NOTE: cast &[u8] and &[u8; N] to &str and abuse the str_eq lang item,
906910
// which calls memcmp().
907911
let pat_len = val_ty(rhs).element_type().array_length();
908912
let ty_str_slice = cx.tcx().mk_static_str();
909913

910-
let rhs_str = alloc_ty(cx, ty_str_slice, "rhs_str");
911-
Store(cx, expr::get_dataptr(cx, rhs), expr::get_dataptr(cx, rhs_str));
912-
Store(cx, C_uint(cx.ccx(), pat_len), expr::get_meta(cx, rhs_str));
914+
let rhs_data = GEPi(cx, rhs, &[0, 0]);
915+
let rhs_len = C_uint(cx.ccx(), pat_len);
913916

914-
let lhs_str;
917+
let lhs_data;
918+
let lhs_len;
915919
if val_ty(lhs) == val_ty(rhs) {
916920
// Both the discriminant and the pattern are thin pointers
917-
lhs_str = alloc_ty(cx, ty_str_slice, "lhs_str");
918-
Store(cx, expr::get_dataptr(cx, lhs), expr::get_dataptr(cx, lhs_str));
919-
Store(cx, C_uint(cx.ccx(), pat_len), expr::get_meta(cx, lhs_str));
920-
}
921-
else {
921+
lhs_data = GEPi(cx, lhs, &[0, 0]);
922+
lhs_len = C_uint(cx.ccx(), pat_len);
923+
} else {
922924
// The discriminant is a fat pointer
923925
let llty_str_slice = type_of::type_of(cx.ccx(), ty_str_slice).ptr_to();
924-
lhs_str = PointerCast(cx, lhs, llty_str_slice);
926+
let lhs_str = PointerCast(cx, lhs, llty_str_slice);
927+
lhs_data = Load(cx, expr::get_dataptr(cx, lhs_str));
928+
lhs_len = Load(cx, expr::get_meta(cx, lhs_str));
925929
}
926930

927-
compare_str(cx, lhs_str, rhs_str, rhs_t, debug_loc)
931+
compare_str(cx, lhs_data, lhs_len, rhs_data, rhs_len, rhs_t, debug_loc)
928932
},
929933
_ => cx.sess().bug("only byte strings supported in compare_values"),
930934
},

0 commit comments

Comments
 (0)