Skip to content

Commit b288d56

Browse files
committed
Auto merge of #50277 - nox:free-the-ones, r=<try>
Pass around i1 as is instead of going through u8
2 parents 8a09bc6 + 666ddd8 commit b288d56

File tree

11 files changed

+34
-72
lines changed

11 files changed

+34
-72
lines changed

src/librustc/ty/layout.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -988,13 +988,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
988988
let mut abi = Abi::Aggregate { sized: true };
989989
if tag.value.size(dl) == size {
990990
abi = Abi::Scalar(tag.clone());
991-
} else if !tag.is_bool() {
992-
// HACK(nox): Blindly using ScalarPair for all tagged enums
993-
// where applicable leads to Option<u8> being handled as {i1, i8},
994-
// which later confuses SROA and some loop optimisations,
995-
// ultimately leading to the repeat-trusted-len test
996-
// failing. We make the trade-off of using ScalarPair only
997-
// for types where the tag isn't a boolean.
991+
} else {
998992
let mut common_prim = None;
999993
for (field_layouts, layout_variant) in variants.iter().zip(&layout_variants) {
1000994
let offsets = match layout_variant.fields {

src/librustc_trans/abi.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ impl<'a, 'tcx> FnTypeExt<'a, 'tcx> for FnType<'tcx, Ty<'tcx>> {
558558
let llreturn_ty = match self.ret.mode {
559559
PassMode::Ignore => Type::void(cx),
560560
PassMode::Direct(_) | PassMode::Pair(..) => {
561-
self.ret.layout.immediate_llvm_type(cx)
561+
self.ret.layout.llvm_type(cx)
562562
}
563563
PassMode::Cast(cast) => cast.llvm_type(cx),
564564
PassMode::Indirect(_) => {
@@ -575,7 +575,7 @@ impl<'a, 'tcx> FnTypeExt<'a, 'tcx> for FnType<'tcx, Ty<'tcx>> {
575575

576576
let llarg_ty = match arg.mode {
577577
PassMode::Ignore => continue,
578-
PassMode::Direct(_) => arg.layout.immediate_llvm_type(cx),
578+
PassMode::Direct(_) => arg.layout.llvm_type(cx),
579579
PassMode::Pair(..) => {
580580
llargument_tys.push(arg.layout.scalar_pair_element_llvm_type(cx, 0));
581581
llargument_tys.push(arg.layout.scalar_pair_element_llvm_type(cx, 1));

src/librustc_trans/base.rs

+1-18
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use rustc::middle::weak_lang_items;
4040
use rustc::mir::mono::{Linkage, Visibility, Stats};
4141
use rustc::middle::cstore::{EncodedMetadata};
4242
use rustc::ty::{self, Ty, TyCtxt};
43-
use rustc::ty::layout::{self, Align, TyLayout, LayoutOf};
43+
use rustc::ty::layout::{Align, TyLayout, LayoutOf};
4444
use rustc::ty::maps::Providers;
4545
use rustc::dep_graph::{DepNode, DepConstructor};
4646
use rustc::ty::subst::Kind;
@@ -387,23 +387,6 @@ pub fn call_assume<'a, 'tcx>(bx: &Builder<'a, 'tcx>, val: ValueRef) {
387387
bx.call(assume_intrinsic, &[val], None);
388388
}
389389

390-
pub fn from_immediate(bx: &Builder, val: ValueRef) -> ValueRef {
391-
if val_ty(val) == Type::i1(bx.cx) {
392-
bx.zext(val, Type::i8(bx.cx))
393-
} else {
394-
val
395-
}
396-
}
397-
398-
pub fn to_immediate(bx: &Builder, val: ValueRef, layout: layout::TyLayout) -> ValueRef {
399-
if let layout::Abi::Scalar(ref scalar) = layout.abi {
400-
if scalar.is_bool() {
401-
return bx.trunc(val, Type::i1(bx.cx));
402-
}
403-
}
404-
val
405-
}
406-
407390
pub fn call_memcpy(bx: &Builder,
408391
dst: ValueRef,
409392
src: ValueRef,

src/librustc_trans/intrinsic.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bx: &Builder<'a, 'tcx>,
244244
unsafe {
245245
llvm::LLVMSetAlignment(load, cx.align_of(tp_ty).abi() as u32);
246246
}
247-
to_immediate(bx, load, cx.layout_of(tp_ty))
247+
load
248248
},
249249
"volatile_store" => {
250250
let tp_ty = substs.type_at(0);
@@ -259,7 +259,7 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bx: &Builder<'a, 'tcx>,
259259
if dst.layout.is_zst() {
260260
return;
261261
}
262-
from_immediate(bx, args[1].immediate())
262+
args[1].immediate()
263263
};
264264
let ptr = bx.pointercast(dst.llval, val_ty(val).ptr_to());
265265
let store = bx.volatile_store(val, ptr);
@@ -556,7 +556,7 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bx: &Builder<'a, 'tcx>,
556556
let val = if let OperandValue::Ref(ptr, align) = args[1].val {
557557
bx.load(ptr, align)
558558
} else {
559-
from_immediate(bx, args[1].immediate())
559+
args[1].immediate()
560560
};
561561
let ptr = bx.pointercast(dst.llval, val_ty(val).ptr_to());
562562
let store = bx.nontemporal_store(val, ptr);

src/librustc_trans/mir/block.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
203203
let (otherwise, targets) = targets.split_last().unwrap();
204204
let switch = bx.switch(discr.immediate(),
205205
llblock(self, *otherwise), values.len());
206-
let switch_llty = bx.cx.layout_of(switch_ty).immediate_llvm_type(bx.cx);
206+
let switch_llty = bx.cx.layout_of(switch_ty).llvm_type(bx.cx);
207207
for (&value, target) in values.iter().zip(targets) {
208208
let llval = C_uint_big(switch_llty, value);
209209
let llbb = llblock(self, *target);
@@ -671,8 +671,6 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
671671
bx.range_metadata(llval, 0..2);
672672
}
673673
}
674-
// We store bools as i8 so we need to truncate to i1.
675-
llval = base::to_immediate(bx, llval, arg.layout);
676674
}
677675
}
678676

src/librustc_trans/mir/constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
215215
};
216216
Ok(primval_to_llvm(
217217
bx.cx, prim, scalar,
218-
layout.immediate_llvm_type(bx.cx),
218+
layout.llvm_type(bx.cx),
219219
))
220220
},
221221
other => bug!("simd shuffle field {:?}, {}", other, constant.ty),

src/librustc_trans/mir/operand.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl<'a, 'tcx> OperandRef<'tcx> {
8888
layout: TyLayout<'tcx>) -> OperandRef<'tcx> {
8989
assert!(layout.is_zst());
9090
OperandRef {
91-
val: OperandValue::Immediate(C_undef(layout.immediate_llvm_type(cx))),
91+
val: OperandValue::Immediate(C_undef(layout.llvm_type(cx))),
9292
layout
9393
}
9494
}
@@ -113,7 +113,7 @@ impl<'a, 'tcx> OperandRef<'tcx> {
113113
bx.cx,
114114
x,
115115
scalar,
116-
layout.immediate_llvm_type(bx.cx),
116+
layout.llvm_type(bx.cx),
117117
);
118118
OperandValue::Immediate(llval)
119119
},
@@ -226,7 +226,7 @@ impl<'a, 'tcx> OperandRef<'tcx> {
226226
// If we're uninhabited, or the field is ZST, it has no data.
227227
_ if self.layout.abi == layout::Abi::Uninhabited || field.is_zst() => {
228228
return OperandRef {
229-
val: OperandValue::Immediate(C_undef(field.immediate_llvm_type(bx.cx))),
229+
val: OperandValue::Immediate(C_undef(field.llvm_type(bx.cx))),
230230
layout: field
231231
};
232232
}
@@ -263,7 +263,7 @@ impl<'a, 'tcx> OperandRef<'tcx> {
263263
// HACK(eddyb) have to bitcast pointers until LLVM removes pointee types.
264264
match val {
265265
OperandValue::Immediate(ref mut llval) => {
266-
*llval = bx.bitcast(*llval, field.immediate_llvm_type(bx.cx));
266+
*llval = bx.bitcast(*llval, field.llvm_type(bx.cx));
267267
}
268268
OperandValue::Pair(ref mut a, ref mut b) => {
269269
*a = bx.bitcast(*a, field.scalar_pair_element_llvm_type(bx.cx, 0));
@@ -292,7 +292,7 @@ impl<'a, 'tcx> OperandValue {
292292
base::memcpy_ty(bx, dest.llval, r, dest.layout,
293293
source_align.min(dest.align)),
294294
OperandValue::Immediate(s) => {
295-
bx.store(base::from_immediate(bx, s), dest.llval, dest.align);
295+
bx.store(s, dest.llval, dest.align);
296296
}
297297
OperandValue::Pair(a, b) => {
298298
for (i, &x) in [a, b].iter().enumerate() {
@@ -301,7 +301,7 @@ impl<'a, 'tcx> OperandValue {
301301
if common::val_ty(x) == Type::i1(bx.cx) {
302302
llptr = bx.pointercast(llptr, Type::i8p(bx.cx));
303303
}
304-
bx.store(base::from_immediate(bx, x), llptr, dest.align);
304+
bx.store(x, llptr, dest.align);
305305
}
306306
}
307307
}

src/librustc_trans/mir/place.rs

+7-15
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl<'a, 'tcx> PlaceRef<'tcx> {
103103
// empty set, which is impossible, or the entire range of the
104104
// type, which is pointless).
105105
match scalar.value {
106-
layout::Int(..) if max_next & mask != min & mask => {
106+
layout::Int(..) if !scalar.is_bool() && max_next & mask != min & mask => {
107107
// llvm::ConstantRange can deal with ranges that wrap around,
108108
// so an overflow on (max + 1) is fine.
109109
bx.range_metadata(load, min..max_next);
@@ -133,21 +133,13 @@ impl<'a, 'tcx> PlaceRef<'tcx> {
133133
}
134134
load
135135
};
136-
OperandValue::Immediate(base::to_immediate(bx, llval, self.layout))
136+
OperandValue::Immediate(llval)
137137
} else if let layout::Abi::ScalarPair(ref a, ref b) = self.layout.abi {
138138
let load = |i, scalar: &layout::Scalar| {
139-
let mut llptr = bx.struct_gep(self.llval, i as u64);
140-
// Make sure to always load i1 as i8.
141-
if scalar.is_bool() {
142-
llptr = bx.pointercast(llptr, Type::i8p(bx.cx));
143-
}
139+
let llptr = bx.struct_gep(self.llval, i as u64);
144140
let load = bx.load(llptr, self.align);
145141
scalar_load_metadata(load, scalar);
146-
if scalar.is_bool() {
147-
bx.trunc(load, Type::i1(bx.cx))
148-
} else {
149-
load
150-
}
142+
load
151143
};
152144
OperandValue::Pair(load(0, a), load(1, b))
153145
} else {
@@ -263,7 +255,7 @@ impl<'a, 'tcx> PlaceRef<'tcx> {
263255

264256
/// Obtain the actual discriminant of a value.
265257
pub fn trans_get_discr(self, bx: &Builder<'a, 'tcx>, cast_to: Ty<'tcx>) -> ValueRef {
266-
let cast_to = bx.cx.layout_of(cast_to).immediate_llvm_type(bx.cx);
258+
let cast_to = bx.cx.layout_of(cast_to).llvm_type(bx.cx);
267259
if self.layout.abi == layout::Abi::Uninhabited {
268260
return C_undef(cast_to);
269261
}
@@ -295,7 +287,7 @@ impl<'a, 'tcx> PlaceRef<'tcx> {
295287
niche_start,
296288
..
297289
} => {
298-
let niche_llty = discr.layout.immediate_llvm_type(bx.cx);
290+
let niche_llty = discr.layout.llvm_type(bx.cx);
299291
if niche_variants.start == niche_variants.end {
300292
// FIXME(eddyb) Check the actual primitive type here.
301293
let niche_llval = if niche_start == 0 {
@@ -360,7 +352,7 @@ impl<'a, 'tcx> PlaceRef<'tcx> {
360352
}
361353

362354
let niche = self.project_field(bx, 0);
363-
let niche_llty = niche.layout.immediate_llvm_type(bx.cx);
355+
let niche_llty = niche.layout.llvm_type(bx.cx);
364356
let niche_value = ((variant_index - niche_variants.start) as u128)
365357
.wrapping_add(niche_start);
366358
// FIXME(eddyb) Check the actual primitive type here.

src/librustc_trans/mir/rvalue.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
102102

103103
let start = dest.project_index(&bx, C_usize(bx.cx, 0)).llval;
104104

105-
if let OperandValue::Immediate(v) = tr_elem.val {
105+
if let OperandValue::Immediate(mut v) = tr_elem.val {
106106
let align = C_i32(bx.cx, dest.align.abi() as i32);
107107
let size = C_usize(bx.cx, dest.layout.size.bytes());
108108

@@ -114,7 +114,9 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
114114
}
115115

116116
// Use llvm.memset.p0i8.* to initialize byte arrays
117-
let v = base::from_immediate(&bx, v);
117+
if common::val_ty(v) == Type::i1(bx.cx) {
118+
v = bx.zext(v, Type::i8(bx.cx))
119+
}
118120
if common::val_ty(v) == Type::i8(bx.cx) {
119121
base::call_memset(&bx, start, v, size, align, false);
120122
return bx;
@@ -257,7 +259,7 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
257259
} else { // cast to thin-ptr
258260
// Cast of fat-ptr to thin-ptr is an extraction of data-ptr and
259261
// pointer-cast of that pointer to desired pointer type.
260-
let llcast_ty = cast.immediate_llvm_type(bx.cx);
262+
let llcast_ty = cast.llvm_type(bx.cx);
261263
let llval = bx.pointercast(data_ptr, llcast_ty);
262264
OperandValue::Immediate(llval)
263265
}
@@ -267,7 +269,7 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
267269
}
268270
mir::CastKind::Misc => {
269271
assert!(cast.is_llvm_immediate());
270-
let ll_t_out = cast.immediate_llvm_type(bx.cx);
272+
let ll_t_out = cast.llvm_type(bx.cx);
271273
if operand.layout.abi == layout::Abi::Uninhabited {
272274
return (bx, OperandRef {
273275
val: OperandValue::Immediate(C_undef(ll_t_out)),
@@ -277,7 +279,7 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
277279
let r_t_in = CastTy::from_ty(operand.layout.ty)
278280
.expect("bad input type for cast");
279281
let r_t_out = CastTy::from_ty(cast.ty).expect("bad output type for cast");
280-
let ll_t_in = operand.layout.immediate_llvm_type(bx.cx);
282+
let ll_t_in = operand.layout.llvm_type(bx.cx);
281283
match operand.layout.variants {
282284
layout::Variants::Single { index } => {
283285
if let Some(def) = operand.layout.ty.ty_adt_def() {

src/librustc_trans/type_of.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ pub trait LayoutLlvmExt<'tcx> {
200200
fn is_llvm_immediate(&self) -> bool;
201201
fn is_llvm_scalar_pair<'a>(&self) -> bool;
202202
fn llvm_type<'a>(&self, cx: &CodegenCx<'a, 'tcx>) -> Type;
203-
fn immediate_llvm_type<'a>(&self, cx: &CodegenCx<'a, 'tcx>) -> Type;
204203
fn scalar_llvm_type_at<'a>(&self, cx: &CodegenCx<'a, 'tcx>,
205204
scalar: &layout::Scalar, offset: Size) -> Type;
206205
fn scalar_pair_element_llvm_type<'a>(&self, cx: &CodegenCx<'a, 'tcx>,
@@ -244,6 +243,9 @@ impl<'tcx> LayoutLlvmExt<'tcx> for TyLayout<'tcx> {
244243
/// that field and ensuring the struct has the right alignment.
245244
fn llvm_type<'a>(&self, cx: &CodegenCx<'a, 'tcx>) -> Type {
246245
if let layout::Abi::Scalar(ref scalar) = self.abi {
246+
if scalar.is_bool() {
247+
return Type::i1(cx);
248+
}
247249
// Use a different cache for scalars because pointers to DSTs
248250
// can be either fat or thin (data pointers of fat pointers).
249251
if let Some(&llty) = cx.scalar_lltypes.borrow().get(&self.ty) {
@@ -310,15 +312,6 @@ impl<'tcx> LayoutLlvmExt<'tcx> for TyLayout<'tcx> {
310312
llty
311313
}
312314

313-
fn immediate_llvm_type<'a>(&self, cx: &CodegenCx<'a, 'tcx>) -> Type {
314-
if let layout::Abi::Scalar(ref scalar) = self.abi {
315-
if scalar.is_bool() {
316-
return Type::i1(cx);
317-
}
318-
}
319-
self.llvm_type(cx)
320-
}
321-
322315
fn scalar_llvm_type_at<'a>(&self, cx: &CodegenCx<'a, 'tcx>,
323316
scalar: &layout::Scalar, offset: Size) -> Type {
324317
match scalar.value {
@@ -359,7 +352,7 @@ impl<'tcx> LayoutLlvmExt<'tcx> for TyLayout<'tcx> {
359352
};
360353
let scalar = [a, b][index];
361354

362-
// Make sure to return the same type `immediate_llvm_type` would,
355+
// Make sure to return the same type `llvm_type` would,
363356
// to avoid dealing with two types and the associated conversions.
364357
// This means that `(bool, bool)` is represented as `{i1, i1}`,
365358
// both in memory and as an immediate, while `bool` is typically

src/test/codegen/function-arguments.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ pub fn enum_id_1(x: Option<Result<u16, u16>>) -> Option<Result<u16, u16>> {
151151
x
152152
}
153153

154-
// CHECK: i16 @enum_id_2(i16)
154+
// CHECK: { i1, i8 } @enum_id_2(i1 zeroext %x.0, i8 %x.1)
155155
#[no_mangle]
156156
pub fn enum_id_2(x: Option<u8>) -> Option<u8> {
157157
x

0 commit comments

Comments
 (0)