1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 use llvm::{BasicBlockRef, ValueRef};
17 pub fn slice_for_each<'a, 'tcx, F>(
18 bcx: &Builder<'a, 'tcx>,
23 ) -> Builder<'a, 'tcx> where F: FnOnce(&Builder<'a, 'tcx>, ValueRef, BasicBlockRef) {
24 // Special-case vectors with elements of size 0 so they don't go out of bounds (#9890)
25 let zst = type_is_zero_size(bcx.ccx, unit_ty);
26 let add = |bcx: &Builder, a, b| if zst {
29 bcx.inbounds_gep(a, &[b])
32 let body_bcx = bcx.build_sibling_block("slice_loop_body");
33 let next_bcx = bcx.build_sibling_block("slice_loop_next");
34 let header_bcx = bcx.build_sibling_block("slice_loop_header");
37 C_uint(bcx.ccx, 0usize)
41 let end = add(&bcx, start, len);
43 bcx.br(header_bcx.llbb());
44 let current = header_bcx.phi(val_ty(start), &[start], &[bcx.llbb()]);
46 let keep_going = header_bcx.icmp(llvm::IntNE, current, end);
47 header_bcx.cond_br(keep_going, body_bcx.llbb(), next_bcx.llbb());
49 let next = add(&body_bcx, current, C_uint(bcx.ccx, 1usize));
50 f(&body_bcx, if zst { data_ptr } else { current }, header_bcx.llbb());
51 header_bcx.add_incoming_to_phi(current, next, body_bcx.llbb());