Skip to content

Commit 0af7914

Browse files
committed
codegen_llvm: improve common patterns
1 parent cd41765 commit 0af7914

File tree

8 files changed

+86
-115
lines changed

8 files changed

+86
-115
lines changed

src/librustc_codegen_llvm/abi.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -695,14 +695,13 @@ impl<'tcx> FnTypeExt<'tcx> for FnType<'tcx, Ty<'tcx>> {
695695
// If the value is a boolean, the range is 0..2 and that ultimately
696696
// become 0..0 when the type becomes i1, which would be rejected
697697
// by the LLVM verifier.
698-
match scalar.value {
699-
layout::Int(..) if !scalar.is_bool() => {
698+
if let layout::Int(..) = scalar.value {
699+
if !scalar.is_bool() {
700700
let range = scalar.valid_range_exclusive(bx.cx);
701701
if range.start != range.end {
702702
bx.range_metadata(callsite, range);
703703
}
704704
}
705-
_ => {}
706705
}
707706
}
708707
for arg in &self.args {

src/librustc_codegen_llvm/attributes.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,8 @@ pub fn set_probestack(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
9494
// Currently stack probes seem somewhat incompatible with the address
9595
// sanitizer. With asan we're already protected from stack overflow anyway
9696
// so we don't really need stack probes regardless.
97-
match cx.sess().opts.debugging_opts.sanitizer {
98-
Some(Sanitizer::Address) => return,
99-
_ => {}
97+
if let Some(Sanitizer::Address) = cx.sess().opts.debugging_opts.sanitizer {
98+
return
10099
}
101100

102101
// probestack doesn't play nice either with pgo-gen.

src/librustc_codegen_llvm/base.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -495,10 +495,8 @@ pub fn codegen_instance<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>, instance: Instance<'
495495
let sig = common::ty_fn_sig(cx, fn_ty);
496496
let sig = cx.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
497497

498-
let lldecl = match cx.instances.borrow().get(&instance) {
499-
Some(&val) => val,
500-
None => bug!("Instance `{:?}` not already declared", instance)
501-
};
498+
let lldecl = cx.instances.borrow().get(&instance).cloned().unwrap_or_else(||
499+
bug!("Instance `{:?}` not already declared", instance));
502500

503501
cx.stats.borrow_mut().n_closures += 1;
504502

@@ -836,12 +834,7 @@ pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
836834
.iter()
837835
.any(|(_, list)| {
838836
use rustc::middle::dependency_format::Linkage;
839-
list.iter().any(|linkage| {
840-
match linkage {
841-
Linkage::Dynamic => true,
842-
_ => false,
843-
}
844-
})
837+
list.iter().any(|&linkage| linkage == Linkage::Dynamic)
845838
});
846839
let allocator_module = if any_dynamic_crate {
847840
None

src/librustc_codegen_llvm/common.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,13 @@ pub fn langcall(tcx: TyCtxt,
336336
msg: &str,
337337
li: LangItem)
338338
-> DefId {
339-
match tcx.lang_items().require(li) {
340-
Ok(id) => id,
341-
Err(s) => {
342-
let msg = format!("{} {}", msg, s);
343-
match span {
344-
Some(span) => tcx.sess.span_fatal(span, &msg[..]),
345-
None => tcx.sess.fatal(&msg[..]),
346-
}
339+
tcx.lang_items().require(li).unwrap_or_else(|s| {
340+
let msg = format!("{} {}", msg, s);
341+
match span {
342+
Some(span) => tcx.sess.span_fatal(span, &msg[..]),
343+
None => tcx.sess.fatal(&msg[..]),
347344
}
348-
}
345+
})
349346
}
350347

351348
// To avoid UB from LLVM, these two functions mask RHS with an

src/librustc_codegen_llvm/consts.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,13 @@ fn check_and_apply_linkage(
249249
// extern "C" fn() from being non-null, so we can't just declare a
250250
// static and call it a day. Some linkages (like weak) will make it such
251251
// that the static actually has a null value.
252-
let llty2 = match ty.sty {
253-
ty::RawPtr(ref mt) => cx.layout_of(mt.ty).llvm_type(cx),
254-
_ => {
255-
if span.is_some() {
256-
cx.sess().span_fatal(span.unwrap(), "must have type `*const T` or `*mut T`")
257-
} else {
258-
bug!("must have type `*const T` or `*mut T`")
259-
}
252+
let llty2 = if let ty::RawPtr(ref mt) = ty.sty {
253+
cx.layout_of(mt.ty).llvm_type(cx)
254+
} else {
255+
if let Some(span) = span {
256+
cx.sess().span_fatal(span, "must have type `*const T` or `*mut T`")
257+
} else {
258+
bug!("must have type `*const T` or `*mut T`")
260259
}
261260
};
262261
unsafe {
@@ -273,9 +272,9 @@ fn check_and_apply_linkage(
273272
let mut real_name = "_rust_extern_with_linkage_".to_string();
274273
real_name.push_str(&sym);
275274
let g2 = declare::define_global(cx, &real_name, llty).unwrap_or_else(||{
276-
if span.is_some() {
275+
if let Some(span) = span {
277276
cx.sess().span_fatal(
278-
span.unwrap(),
277+
span,
279278
&format!("symbol `{}` is already defined", &sym)
280279
)
281280
} else {

src/librustc_codegen_llvm/context.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,8 @@ impl<'b, 'tcx> CodegenCx<'b, 'tcx> {
318318
if let Some(v) = self.intrinsics.borrow().get(key).cloned() {
319319
return v;
320320
}
321-
match declare_intrinsic(self, key) {
322-
Some(v) => return v,
323-
None => bug!("unknown intrinsic '{}'", key)
324-
}
321+
322+
declare_intrinsic(self, key).unwrap_or_else(|| bug!("unknown intrinsic '{}'", key))
325323
}
326324

327325
/// Generate a new symbol name with the given prefix. This symbol name must
@@ -465,9 +463,10 @@ impl LayoutOf for &'a CodegenCx<'ll, 'tcx> {
465463

466464
fn layout_of(self, ty: Ty<'tcx>) -> Self::TyLayout {
467465
self.tcx.layout_of(ty::ParamEnv::reveal_all().and(ty))
468-
.unwrap_or_else(|e| match e {
469-
LayoutError::SizeOverflow(_) => self.sess().fatal(&e.to_string()),
470-
_ => bug!("failed to get layout for `{}`: {}", ty, e)
466+
.unwrap_or_else(|e| if let LayoutError::SizeOverflow(_) = e {
467+
self.sess().fatal(&e.to_string())
468+
} else {
469+
bug!("failed to get layout for `{}`: {}", ty, e)
471470
})
472471
}
473472
}

src/librustc_codegen_llvm/intrinsic.rs

+52-66
Original file line numberDiff line numberDiff line change
@@ -539,10 +539,9 @@ pub fn codegen_intrinsic_call(
539539
}
540540

541541
_ => {
542-
let intr = match Intrinsic::find(&name) {
543-
Some(intr) => intr,
544-
None => bug!("unknown intrinsic '{}'", name),
545-
};
542+
let intr = Intrinsic::find(&name).unwrap_or_else(||
543+
bug!("unknown intrinsic '{}'", name));
544+
546545
fn one<T>(x: Vec<T>) -> T {
547546
assert_eq!(x.len(), 1);
548547
x.into_iter().next().unwrap()
@@ -1071,11 +1070,8 @@ fn generic_simd_intrinsic(
10711070
}
10721071

10731072
if name.starts_with("simd_shuffle") {
1074-
let n: usize = match name["simd_shuffle".len()..].parse() {
1075-
Ok(n) => n,
1076-
Err(_) => span_bug!(span,
1077-
"bad `simd_shuffle` instruction only caught in codegen?")
1078-
};
1073+
let n: usize = name["simd_shuffle".len()..].parse().unwrap_or_else(|_|
1074+
span_bug!(span, "bad `simd_shuffle` instruction only caught in codegen?"));
10791075

10801076
require_simd!(ret_ty, "return");
10811077

@@ -1216,63 +1212,53 @@ fn generic_simd_intrinsic(
12161212
&args.iter().map(|arg| arg.immediate()).collect::<Vec<_>>(),
12171213
None);
12181214
unsafe { llvm::LLVMRustSetHasUnsafeAlgebra(c) };
1219-
return Ok(c);
1220-
}
1221-
1222-
if name == "simd_fsqrt" {
1223-
return simd_simple_float_intrinsic("sqrt", in_elem, in_ty, in_len, bx, span, args);
1224-
}
1225-
1226-
if name == "simd_fsin" {
1227-
return simd_simple_float_intrinsic("sin", in_elem, in_ty, in_len, bx, span, args);
1228-
}
1229-
1230-
if name == "simd_fcos" {
1231-
return simd_simple_float_intrinsic("cos", in_elem, in_ty, in_len, bx, span, args);
1232-
}
1233-
1234-
if name == "simd_fabs" {
1235-
return simd_simple_float_intrinsic("fabs", in_elem, in_ty, in_len, bx, span, args);
1215+
Ok(c)
12361216
}
12371217

1238-
if name == "simd_floor" {
1239-
return simd_simple_float_intrinsic("floor", in_elem, in_ty, in_len, bx, span, args);
1240-
}
1241-
1242-
if name == "simd_ceil" {
1243-
return simd_simple_float_intrinsic("ceil", in_elem, in_ty, in_len, bx, span, args);
1244-
}
1245-
1246-
if name == "simd_fexp" {
1247-
return simd_simple_float_intrinsic("exp", in_elem, in_ty, in_len, bx, span, args);
1248-
}
1249-
1250-
if name == "simd_fexp2" {
1251-
return simd_simple_float_intrinsic("exp2", in_elem, in_ty, in_len, bx, span, args);
1252-
}
1253-
1254-
if name == "simd_flog10" {
1255-
return simd_simple_float_intrinsic("log10", in_elem, in_ty, in_len, bx, span, args);
1256-
}
1257-
1258-
if name == "simd_flog2" {
1259-
return simd_simple_float_intrinsic("log2", in_elem, in_ty, in_len, bx, span, args);
1260-
}
1261-
1262-
if name == "simd_flog" {
1263-
return simd_simple_float_intrinsic("log", in_elem, in_ty, in_len, bx, span, args);
1264-
}
1265-
1266-
if name == "simd_fpowi" {
1267-
return simd_simple_float_intrinsic("powi", in_elem, in_ty, in_len, bx, span, args);
1268-
}
1269-
1270-
if name == "simd_fpow" {
1271-
return simd_simple_float_intrinsic("pow", in_elem, in_ty, in_len, bx, span, args);
1272-
}
1273-
1274-
if name == "simd_fma" {
1275-
return simd_simple_float_intrinsic("fma", in_elem, in_ty, in_len, bx, span, args);
1218+
match name {
1219+
"simd_fsqrt" => {
1220+
return simd_simple_float_intrinsic("sqrt", in_elem, in_ty, in_len, bx, span, args);
1221+
}
1222+
"simd_fsin" => {
1223+
return simd_simple_float_intrinsic("sin", in_elem, in_ty, in_len, bx, span, args);
1224+
}
1225+
"simd_fcos" => {
1226+
return simd_simple_float_intrinsic("cos", in_elem, in_ty, in_len, bx, span, args);
1227+
}
1228+
"simd_fabs" => {
1229+
return simd_simple_float_intrinsic("fabs", in_elem, in_ty, in_len, bx, span, args);
1230+
}
1231+
"simd_floor" => {
1232+
return simd_simple_float_intrinsic("floor", in_elem, in_ty, in_len, bx, span, args);
1233+
}
1234+
"simd_ceil" => {
1235+
return simd_simple_float_intrinsic("ceil", in_elem, in_ty, in_len, bx, span, args);
1236+
}
1237+
"simd_fexp" => {
1238+
return simd_simple_float_intrinsic("exp", in_elem, in_ty, in_len, bx, span, args);
1239+
}
1240+
"simd_fexp2" => {
1241+
return simd_simple_float_intrinsic("exp2", in_elem, in_ty, in_len, bx, span, args);
1242+
}
1243+
"simd_flog10" => {
1244+
return simd_simple_float_intrinsic("log10", in_elem, in_ty, in_len, bx, span, args);
1245+
}
1246+
"simd_flog2" => {
1247+
return simd_simple_float_intrinsic("log2", in_elem, in_ty, in_len, bx, span, args);
1248+
}
1249+
"simd_flog" => {
1250+
return simd_simple_float_intrinsic("log", in_elem, in_ty, in_len, bx, span, args);
1251+
}
1252+
"simd_fpowi" => {
1253+
return simd_simple_float_intrinsic("powi", in_elem, in_ty, in_len, bx, span, args);
1254+
}
1255+
"simd_fpow" => {
1256+
return simd_simple_float_intrinsic("pow", in_elem, in_ty, in_len, bx, span, args);
1257+
}
1258+
"simd_fma" => {
1259+
return simd_simple_float_intrinsic("fma", in_elem, in_ty, in_len, bx, span, args);
1260+
}
1261+
_ => { /* fallthrough */ }
12761262
}
12771263

12781264
// FIXME: use:
@@ -1364,7 +1350,7 @@ fn generic_simd_intrinsic(
13641350
}
13651351
};
13661352
assert!(pointer_count > 0);
1367-
assert!(pointer_count - 1 == ptr_count(arg_tys[0].simd_type(tcx)));
1353+
assert_eq!(pointer_count - 1, ptr_count(arg_tys[0].simd_type(tcx)));
13681354
assert_eq!(underlying_ty, non_ptr(arg_tys[0].simd_type(tcx)));
13691355

13701356
// The element type of the third argument must be a signed integer type of any width:
@@ -1461,7 +1447,7 @@ fn generic_simd_intrinsic(
14611447
}
14621448
};
14631449
assert!(pointer_count > 0);
1464-
assert!(pointer_count - 1 == ptr_count(arg_tys[0].simd_type(tcx)));
1450+
assert_eq!(pointer_count - 1, ptr_count(arg_tys[0].simd_type(tcx)));
14651451
assert_eq!(underlying_ty, non_ptr(arg_tys[0].simd_type(tcx)));
14661452

14671453
// The element type of the third argument must be a signed integer type of any width:

src/librustc_codegen_llvm/type_of.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,12 @@ fn uncached_llvm_type<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
6565
let mut name = String::with_capacity(32);
6666
let printer = DefPathBasedNames::new(cx.tcx, true, true);
6767
printer.push_type_name(layout.ty, &mut name);
68-
match (&layout.ty.sty, &layout.variants) {
69-
(&ty::Adt(def, _), &layout::Variants::Single { index }) => {
70-
if def.is_enum() && !def.variants.is_empty() {
71-
write!(&mut name, "::{}", def.variants[index].name).unwrap();
72-
}
68+
if let (&ty::Adt(def, _), &layout::Variants::Single { index })
69+
= (&layout.ty.sty, &layout.variants)
70+
{
71+
if def.is_enum() && !def.variants.is_empty() {
72+
write!(&mut name, "::{}", def.variants[index].name).unwrap();
7373
}
74-
_ => {}
7574
}
7675
Some(name)
7776
}
@@ -155,7 +154,7 @@ fn struct_llfields<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
155154
debug!("struct_llfields: pad_bytes: {:?} offset: {:?} stride: {:?}",
156155
padding, offset, layout.size);
157156
result.push(Type::padding_filler(cx, padding, padding_align));
158-
assert!(result.len() == 1 + field_count * 2);
157+
assert_eq!(result.len(), 1 + field_count * 2);
159158
} else {
160159
debug!("struct_llfields: offset: {:?} stride: {:?}",
161160
offset, layout.size);

0 commit comments

Comments
 (0)