Skip to content

Commit 0958407

Browse files
committed
Pass around i1 as is instead of going through u8
1 parent a272684 commit 0958407

File tree

10 files changed

+32
-71
lines changed

10 files changed

+32
-71
lines changed

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
@@ -204,7 +204,7 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
204204
let (otherwise, targets) = targets.split_last().unwrap();
205205
let switch = bx.switch(discr.immediate(),
206206
llblock(self, *otherwise), values.len());
207-
let switch_llty = bx.cx.layout_of(switch_ty).immediate_llvm_type(bx.cx);
207+
let switch_llty = bx.cx.layout_of(switch_ty).llvm_type(bx.cx);
208208
for (&value, target) in values.iter().zip(targets) {
209209
let llval = C_uint_big(switch_llty, value);
210210
let llbb = llblock(self, *target);
@@ -651,8 +651,6 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
651651
bx.range_metadata(llval, 0..2);
652652
}
653653
}
654-
// We store bools as i8 so we need to truncate to i1.
655-
llval = base::to_immediate(bx, llval, arg.layout);
656654
}
657655
}
658656

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
@@ -93,7 +93,7 @@ impl<'a, 'tcx> PlaceRef<'tcx> {
9393
let scalar_load_metadata = |load, scalar: &layout::Scalar| {
9494
let vr = scalar.valid_range.clone();
9595
match scalar.value {
96-
layout::Int(..) => {
96+
layout::Int(..) if !scalar.is_bool() => {
9797
let range = scalar.valid_range_exclusive(bx.cx);
9898
if range.start != range.end {
9999
bx.range_metadata(load, range);
@@ -124,21 +124,13 @@ impl<'a, 'tcx> PlaceRef<'tcx> {
124124
}
125125
load
126126
};
127-
OperandValue::Immediate(base::to_immediate(bx, llval, self.layout))
127+
OperandValue::Immediate(llval)
128128
} else if let layout::Abi::ScalarPair(ref a, ref b) = self.layout.abi {
129129
let load = |i, scalar: &layout::Scalar| {
130-
let mut llptr = bx.struct_gep(self.llval, i as u64);
131-
// Make sure to always load i1 as i8.
132-
if scalar.is_bool() {
133-
llptr = bx.pointercast(llptr, Type::i8p(bx.cx));
134-
}
130+
let llptr = bx.struct_gep(self.llval, i as u64);
135131
let load = bx.load(llptr, self.align);
136132
scalar_load_metadata(load, scalar);
137-
if scalar.is_bool() {
138-
bx.trunc(load, Type::i1(bx.cx))
139-
} else {
140-
load
141-
}
133+
load
142134
};
143135
OperandValue::Pair(load(0, a), load(1, b))
144136
} else {
@@ -254,7 +246,7 @@ impl<'a, 'tcx> PlaceRef<'tcx> {
254246

255247
/// Obtain the actual discriminant of a value.
256248
pub fn trans_get_discr(self, bx: &Builder<'a, 'tcx>, cast_to: Ty<'tcx>) -> ValueRef {
257-
let cast_to = bx.cx.layout_of(cast_to).immediate_llvm_type(bx.cx);
249+
let cast_to = bx.cx.layout_of(cast_to).llvm_type(bx.cx);
258250
if self.layout.abi == layout::Abi::Uninhabited {
259251
return C_undef(cast_to);
260252
}
@@ -286,7 +278,7 @@ impl<'a, 'tcx> PlaceRef<'tcx> {
286278
niche_start,
287279
..
288280
} => {
289-
let niche_llty = discr.layout.immediate_llvm_type(bx.cx);
281+
let niche_llty = discr.layout.llvm_type(bx.cx);
290282
if niche_variants.start() == niche_variants.end() {
291283
// FIXME(eddyb) Check the actual primitive type here.
292284
let niche_llval = if niche_start == 0 {
@@ -351,7 +343,7 @@ impl<'a, 'tcx> PlaceRef<'tcx> {
351343
}
352344

353345
let niche = self.project_field(bx, 0);
354-
let niche_llty = niche.layout.immediate_llvm_type(bx.cx);
346+
let niche_llty = niche.layout.llvm_type(bx.cx);
355347
let niche_value = ((variant_index - *niche_variants.start()) as u128)
356348
.wrapping_add(niche_start);
357349
// FIXME(eddyb) Check the actual primitive type here.

src/librustc_trans/mir/rvalue.rs

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

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

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

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

115115
// Use llvm.memset.p0i8.* to initialize byte arrays
116-
let v = base::from_immediate(&bx, v);
116+
if common::val_ty(v) == Type::i1(bx.cx) {
117+
v = bx.zext(v, Type::i8(bx.cx))
118+
}
117119
if common::val_ty(v) == Type::i8(bx.cx) {
118120
base::call_memset(&bx, start, v, size, align, false);
119121
return bx;
@@ -256,7 +258,7 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
256258
} else { // cast to thin-ptr
257259
// Cast of fat-ptr to thin-ptr is an extraction of data-ptr and
258260
// pointer-cast of that pointer to desired pointer type.
259-
let llcast_ty = cast.immediate_llvm_type(bx.cx);
261+
let llcast_ty = cast.llvm_type(bx.cx);
260262
let llval = bx.pointercast(data_ptr, llcast_ty);
261263
OperandValue::Immediate(llval)
262264
}
@@ -266,7 +268,7 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
266268
}
267269
mir::CastKind::Misc => {
268270
assert!(cast.is_llvm_immediate());
269-
let ll_t_out = cast.immediate_llvm_type(bx.cx);
271+
let ll_t_out = cast.llvm_type(bx.cx);
270272
if operand.layout.abi == layout::Abi::Uninhabited {
271273
return (bx, OperandRef {
272274
val: OperandValue::Immediate(C_undef(ll_t_out)),
@@ -276,7 +278,7 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
276278
let r_t_in = CastTy::from_ty(operand.layout.ty)
277279
.expect("bad input type for cast");
278280
let r_t_out = CastTy::from_ty(cast.ty).expect("bad output type for cast");
279-
let ll_t_in = operand.layout.immediate_llvm_type(bx.cx);
281+
let ll_t_in = operand.layout.llvm_type(bx.cx);
280282
match operand.layout.variants {
281283
layout::Variants::Single { index } => {
282284
if let Some(def) = operand.layout.ty.ty_adt_def() {

src/librustc_trans/type_of.rs

+3-17
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,13 +352,6 @@ 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,
363-
// to avoid dealing with two types and the associated conversions.
364-
// This means that `(bool, bool)` is represented as `{i1, i1}`,
365-
// both in memory and as an immediate, while `bool` is typically
366-
// `i8` in memory and only `i1` when immediate. While we need to
367-
// load/store `bool` as `i8` to avoid crippling LLVM optimizations,
368-
// `i1` in a LLVM aggregate is valid and mostly equivalent to `i8`.
369355
if scalar.is_bool() {
370356
return Type::i1(cx);
371357
}

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)