Debian Codegraph @

gcc/testsuite/rust/execute/torture/for-loop2.rs

raw
1 // { dg-output "loop1\r*\nloop2\r*\n" }
2 #![feature(intrinsics)]
3
4 pub use option::Option::{self, None, Some};
5 pub use result::Result::{self, Err, Ok};
6
7 extern "C" {
8 fn printf(s: *const i8, ...);
9 }
10
11 mod option {
12 pub enum Option<T> {
13 #[lang = "None"]
14 None,
15 #[lang = "Some"]
16 Some(T),
17 }
18 }
19
20 mod result {
21 enum Result<T, E> {
22 Ok(T),
23 Err(E),
24 }
25 }
26
27 #[lang = "sized"]
28 pub trait Sized {}
29
30 #[lang = "clone"]
31 pub trait Clone: Sized {
32 fn clone(&self) -> Self;
33
34 fn clone_from(&mut self, source: &Self) {
35 *self = source.clone()
36 }
37 }
38
39 mod impls {
40 use super::Clone;
41
42 macro_rules! impl_clone {
43 ($($t:ty)*) => {
44 $(
45 impl Clone for $t {
46 fn clone(&self) -> Self {
47 *self
48 }
49 }
50 )*
51 }
52 }
53
54 impl_clone! {
55 usize u8 u16 u32 u64 // u128
56 isize i8 i16 i32 i64 // i128
57 f32 f64
58 bool char
59 }
60 }
61
62 #[lang = "copy"]
63 pub trait Copy: Clone {
64 // Empty.
65 }
66
67 mod copy_impls {
68 use super::Copy;
69
70 macro_rules! impl_copy {
71 ($($t:ty)*) => {
72 $(
73 impl Copy for $t {}
74 )*
75 }
76 }
77
78 impl_copy! {
79 usize u8 u16 u32 u64 // u128
80 isize i8 i16 i32 i64 // i128
81 f32 f64
82 bool char
83 }
84 }
85
86 mod intrinsics {
87 extern "rust-intrinsic" {
88 pub fn add_with_overflow<T>(x: T, y: T) -> (T, bool);
89 pub fn wrapping_add<T>(a: T, b: T) -> T;
90 pub fn wrapping_sub<T>(a: T, b: T) -> T;
91 pub fn rotate_left<T>(a: T, b: T) -> T;
92 pub fn rotate_right<T>(a: T, b: T) -> T;
93 pub fn offset<T>(ptr: *const T, count: isize) -> *const T;
94 pub fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
95 pub fn move_val_init<T>(dst: *mut T, src: T);
96 pub fn uninit<T>() -> T;
97 }
98 }
99
100 mod ptr {
101 #[lang = "const_ptr"]
102 impl<T> *const T {
103 pub unsafe fn offset(self, count: isize) -> *const T {
104 intrinsics::offset(self, count)
105 }
106 }
107
108 #[lang = "mut_ptr"]
109 impl<T> *mut T {
110 pub unsafe fn offset(self, count: isize) -> *mut T {
111 intrinsics::offset(self, count) as *mut T
112 }
113 }
114
115 pub unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
116 let x = x as *mut u8;
117 let y = y as *mut u8;
118 let len = mem::size_of::<T>() * count;
119 swap_nonoverlapping_bytes(x, y, len)
120 }
121
122 pub unsafe fn swap_nonoverlapping_one<T>(x: *mut T, y: *mut T) {
123 // For types smaller than the block optimization below,
124 // just swap directly to avoid pessimizing codegen.
125 if mem::size_of::<T>() < 32 {
126 let z = read(x);
127 intrinsics::copy_nonoverlapping(y, x, 1);
128 write(y, z);
129 } else {
130 swap_nonoverlapping(x, y, 1);
131 }
132 }
133
134 pub unsafe fn write<T>(dst: *mut T, src: T) {
135 intrinsics::move_val_init(&mut *dst, src)
136 }
137
138 pub unsafe fn read<T>(src: *const T) -> T {
139 let mut tmp: T = mem::uninitialized();
140 intrinsics::copy_nonoverlapping(src, &mut tmp, 1);
141 tmp
142 }
143
144 pub unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) {
145 struct Block(u64, u64, u64, u64);
146 struct UnalignedBlock(u64, u64, u64, u64);
147
148 let block_size = mem::size_of::<Block>();
149
150 // Loop through x & y, copying them `Block` at a time
151 // The optimizer should unroll the loop fully for most types
152 // N.B. We can't use a for loop as the `range` impl calls `mem::swap` recursively
153 let mut i: usize = 0;
154 while i + block_size <= len {
155 // Create some uninitialized memory as scratch space
156 // Declaring `t` here avoids aligning the stack when this loop is unused
157 let mut t: Block = mem::uninitialized();
158 let t = &mut t as *mut _ as *mut u8;
159 let x = x.offset(i as isize);
160 let y = y.offset(i as isize);
161
162 // Swap a block of bytes of x & y, using t as a temporary buffer
163 // This should be optimized into efficient SIMD operations where available
164 intrinsics::copy_nonoverlapping(x, t, block_size);
165 intrinsics::copy_nonoverlapping(y, x, block_size);
166 intrinsics::copy_nonoverlapping(t, y, block_size);
167 i += block_size;
168 }
169
170 if i < len {
171 // Swap any remaining bytes
172 let mut t: UnalignedBlock = mem::uninitialized();
173 let rem = len - i;
174
175 let t = &mut t as *mut _ as *mut u8;
176 let x = x.offset(i as isize);
177 let y = y.offset(i as isize);
178
179 intrinsics::copy_nonoverlapping(x, t, rem);
180 intrinsics::copy_nonoverlapping(y, x, rem);
181 intrinsics::copy_nonoverlapping(t, y, rem);
182 }
183 }
184 }
185
186 mod mem {
187 extern "rust-intrinsic" {
188 #[rustc_const_stable(feature = "const_transmute", since = "1.46.0")]
189 pub fn transmute<T, U>(_: T) -> U;
190 #[rustc_const_stable(feature = "const_size_of", since = "1.40.0")]
191 pub fn size_of<T>() -> usize;
192 }
193
194 pub fn swap<T>(x: &mut T, y: &mut T) {
195 unsafe {
196 ptr::swap_nonoverlapping_one(x, y);
197 }
198 }
199
200 pub fn replace<T>(dest: &mut T, mut src: T) -> T {
201 swap(dest, &mut src);
202 src
203 }
204
205 pub unsafe fn uninitialized<T>() -> T {
206 intrinsics::uninit()
207 }
208 }
209
210 macro_rules! impl_uint {
211 ($($ty:ident = $lang:literal),*) => {
212 $(
213 impl $ty {
214 pub fn wrapping_add(self, rhs: Self) -> Self {
215 unsafe {
216 intrinsics::wrapping_add(self, rhs)
217 }
218 }
219
220 pub fn wrapping_sub(self, rhs: Self) -> Self {
221 unsafe {
222 intrinsics::wrapping_sub(self, rhs)
223 }
224 }
225
226 pub fn rotate_left(self, n: u32) -> Self {
227 unsafe {
228 intrinsics::rotate_left(self, n as Self)
229 }
230 }
231
232 pub fn rotate_right(self, n: u32) -> Self {
233 unsafe {
234 intrinsics::rotate_right(self, n as Self)
235 }
236 }
237
238 pub fn to_le(self) -> Self {
239 #[cfg(target_endian = "little")]
240 {
241 self
242 }
243 }
244
245 pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
246 Self::from_le(Self::from_ne_bytes(bytes))
247 }
248
249 pub const fn from_le(x: Self) -> Self {
250 #[cfg(target_endian = "little")]
251 {
252 x
253 }
254 }
255
256 pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
257 unsafe { mem::transmute(bytes) }
258 }
259
260 pub fn checked_add(self, rhs: Self) -> Option<Self> {
261 let (a, b) = self.overflowing_add(rhs);
262 if b {
263 Option::None
264 } else {
265 Option::Some(a)
266 }
267 }
268
269 pub fn overflowing_add(self, rhs: Self) -> (Self, bool) {
270 let (a, b) = unsafe { intrinsics::add_with_overflow(self as $ty, rhs as $ty) };
271 (a as Self, b)
272 }
273 }
274 )*
275 }
276 }
277
278 impl_uint!(
279 u8 = "u8",
280 u16 = "u16",
281 u32 = "u32",
282 u64 = "u64",
283 usize = "usize"
284 );
285
286 #[lang = "add"]
287 pub trait Add<RHS = Self> {
288 type Output;
289
290 fn add(self, rhs: RHS) -> Self::Output;
291 }
292 macro_rules! add_impl {
293 ($($t:ty)*) => ($(
294 impl Add for $t {
295 type Output = $t;
296
297 fn add(self, other: $t) -> $t { self + other }
298 }
299 )*)
300 }
301
302 add_impl! { usize u8 u16 u32 u64 /*isize i8 i16 i32 i64*/ f32 f64 }
303
304 #[lang = "sub"]
305 pub trait Sub<RHS = Self> {
306 type Output;
307
308 fn sub(self, rhs: RHS) -> Self::Output;
309 }
310 macro_rules! sub_impl {
311 ($($t:ty)*) => ($(
312 impl Sub for $t {
313 type Output = $t;
314
315 fn sub(self, other: $t) -> $t { self - other }
316 }
317 )*)
318 }
319
320 sub_impl! { usize u8 u16 u32 u64 /*isize i8 i16 i32 i64*/ f32 f64 }
321
322 #[lang = "Range"]
323 pub struct Range<Idx> {
324 pub start: Idx,
325 pub end: Idx,
326 }
327
328 pub trait TryFrom<T>: Sized {
329 /// The type returned in the event of a conversion error.
330 type Error;
331
332 /// Performs the conversion.
333 fn try_from(value: T) -> Result<Self, Self::Error>;
334 }
335
336 pub trait From<T>: Sized {
337 fn from(_: T) -> Self;
338 }
339
340 impl<T> From<T> for T {
341 fn from(t: T) -> T {
342 t
343 }
344 }
345
346 impl<T, U> TryFrom<U> for T
347 where
348 T: From<U>,
349 {
350 type Error = !;
351
352 fn try_from(value: U) -> Result<Self, Self::Error> {
353 Ok(T::from(value))
354 }
355 }
356
357 trait Step {
358 /// Returns the number of steps between two step objects. The count is
359 /// inclusive of `start` and exclusive of `end`.
360 ///
361 /// Returns `None` if it is not possible to calculate `steps_between`
362 /// without overflow.
363 fn steps_between(start: &Self, end: &Self) -> Option<usize>;
364
365 /// Replaces this step with `1`, returning itself
366 fn replace_one(&mut self) -> Self;
367
368 /// Replaces this step with `0`, returning itself
369 fn replace_zero(&mut self) -> Self;
370
371 /// Adds one to this step, returning the result
372 fn add_one(&self) -> Self;
373
374 /// Subtracts one to this step, returning the result
375 fn sub_one(&self) -> Self;
376
377 /// Add an usize, returning None on overflow
378 fn add_usize(&self, n: usize) -> Option<Self>;
379 }
380
381 // These are still macro-generated because the integer literals resolve to different types.
382 macro_rules! step_identical_methods {
383 () => {
384 #[inline]
385 fn replace_one(&mut self) -> Self {
386 mem::replace(self, 1)
387 }
388
389 #[inline]
390 fn replace_zero(&mut self) -> Self {
391 mem::replace(self, 0)
392 }
393
394 #[inline]
395 fn add_one(&self) -> Self {
396 Add::add(*self, 1)
397 }
398
399 #[inline]
400 fn sub_one(&self) -> Self {
401 Sub::sub(*self, 1)
402 }
403 };
404 }
405
406 macro_rules! step_impl_unsigned {
407 ($($t:ty)*) => ($(
408 impl Step for $t {
409 fn steps_between(start: &$t, end: &$t) -> Option<usize> {
410 if *start < *end {
411 // Note: We assume $t <= usize here
412 Option::Some((*end - *start) as usize)
413 } else {
414 Option::Some(0)
415 }
416 }
417
418 fn add_usize(&self, n: usize) -> Option<Self> {
419 match <$t>::try_from(n) {
420 Result::Ok(n_as_t) => self.checked_add(n_as_t),
421 Result::Err(_) => Option::None,
422 }
423 }
424
425 step_identical_methods!();
426 }
427 )*)
428 }
429 macro_rules! step_impl_signed {
430 ($( [$t:ty : $unsigned:ty] )*) => ($(
431 impl Step for $t {
432 #[inline]
433 #[allow(trivial_numeric_casts)]
434 fn steps_between(start: &$t, end: &$t) -> Option<usize> {
435 if *start < *end {
436 // Note: We assume $t <= isize here
437 // Use .wrapping_sub and cast to usize to compute the
438 // difference that may not fit inside the range of isize.
439 Option::Some((*end as isize).wrapping_sub(*start as isize) as usize)
440 } else {
441 Option::Some(0)
442 }
443 }
444
445 #[inline]
446 #[allow(unreachable_patterns)]
447 fn add_usize(&self, n: usize) -> Option<Self> {
448 match <$unsigned>::try_from(n) {
449 Result::Ok(n_as_unsigned) => {
450 // Wrapping in unsigned space handles cases like
451 // `-120_i8.add_usize(200) == Option::Some(80_i8)`,
452 // even though 200_usize is out of range for i8.
453 let wrapped = (*self as $unsigned).wrapping_add(n_as_unsigned) as $t;
454 if wrapped >= *self {
455 Option::Some(wrapped)
456 } else {
457 Option::None // Addition overflowed
458 }
459 }
460 Result::Err(_) => Option::None,
461 }
462 }
463
464 step_identical_methods!();
465 }
466 )*)
467 }
468
469 macro_rules! step_impl_no_between {
470 ($($t:ty)*) => ($(
471 impl Step for $t {
472 #[inline]
473 fn steps_between(_start: &Self, _end: &Self) -> Option<usize> {
474 Option::None
475 }
476
477 #[inline]
478 fn add_usize(&self, n: usize) -> Option<Self> {
479 self.checked_add(n as $t)
480 }
481
482 step_identical_methods!();
483 }
484 )*)
485 }
486
487 step_impl_unsigned!(usize);
488
489 pub trait Iterator {
490 type Item;
491
492 #[lang = "next"]
493 fn next(&mut self) -> Option<Self::Item>;
494 }
495
496 impl<A: Step> Iterator for Range<A> {
497 type Item = A;
498
499 fn next(&mut self) -> Option<A> {
500 if self.start < self.end {
501 // We check for overflow here, even though it can't actually
502 // happen. Adding this check does however help llvm vectorize loops
503 // for some ranges that don't get vectorized otherwise,
504 // and this won't actually result in an extra check in an optimized build.
505 match self.start.add_usize(1) {
506 Option::Some(mut n) => {
507 mem::swap(&mut n, &mut self.start);
508 Option::Some(n)
509 }
510 Option::None => Option::None,
511 }
512 } else {
513 Option::None
514 }
515 }
516 }
517
518 pub trait IntoIterator {
519 type Item;
520
521 type IntoIter: Iterator<Item = Self::Item>;
522
523 #[lang = "into_iter"]
524 fn into_iter(self) -> Self::IntoIter;
525 }
526
527 impl<I: Iterator> IntoIterator for I {
528 type Item = I::Item;
529 type IntoIter = I;
530
531 fn into_iter(self) -> I {
532 self
533 }
534 }
535
536 pub fn main() -> i32 {
537 let a = 1usize..3usize;
538
539 for i in a {
540 unsafe { printf("loop%d\n\0" as *const str as *const i8, i); }
541 }
542
543 0
544 }