]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/mir/rvalue.rs
Rollup merge of #39604 - est31:i128_tests, r=alexcrichton
[rust.git] / src / librustc_trans / mir / rvalue.rs
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.
4 //
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.
10
11 use llvm::{self, ValueRef};
12 use rustc::ty::{self, Ty};
13 use rustc::ty::cast::{CastTy, IntTy};
14 use rustc::ty::layout::Layout;
15 use rustc::mir::tcx::LvalueTy;
16 use rustc::mir;
17 use middle::lang_items::ExchangeMallocFnLangItem;
18
19 use asm;
20 use base;
21 use builder::Builder;
22 use callee::Callee;
23 use common::{self, val_ty, C_bool, C_null, C_uint};
24 use common::{C_integral};
25 use adt;
26 use machine;
27 use type_::Type;
28 use type_of;
29 use tvec;
30 use value::Value;
31 use Disr;
32
33 use super::MirContext;
34 use super::constant::const_scalar_checked_binop;
35 use super::operand::{OperandRef, OperandValue};
36 use super::lvalue::LvalueRef;
37
38 impl<'a, 'tcx> MirContext<'a, 'tcx> {
39     pub fn trans_rvalue(&mut self,
40                         bcx: Builder<'a, 'tcx>,
41                         dest: LvalueRef<'tcx>,
42                         rvalue: &mir::Rvalue<'tcx>)
43                         -> Builder<'a, 'tcx>
44     {
45         debug!("trans_rvalue(dest.llval={:?}, rvalue={:?})",
46                Value(dest.llval), rvalue);
47
48         match *rvalue {
49            mir::Rvalue::Use(ref operand) => {
50                let tr_operand = self.trans_operand(&bcx, operand);
51                // FIXME: consider not copying constants through stack. (fixable by translating
52                // constants into OperandValue::Ref, why don’t we do that yet if we don’t?)
53                self.store_operand(&bcx, dest.llval, dest.alignment.to_align(), tr_operand);
54                bcx
55            }
56
57             mir::Rvalue::Cast(mir::CastKind::Unsize, ref source, cast_ty) => {
58                 let cast_ty = self.monomorphize(&cast_ty);
59
60                 if common::type_is_fat_ptr(bcx.ccx, cast_ty) {
61                     // into-coerce of a thin pointer to a fat pointer - just
62                     // use the operand path.
63                     let (bcx, temp) = self.trans_rvalue_operand(bcx, rvalue);
64                     self.store_operand(&bcx, dest.llval, dest.alignment.to_align(), temp);
65                     return bcx;
66                 }
67
68                 // Unsize of a nontrivial struct. I would prefer for
69                 // this to be eliminated by MIR translation, but
70                 // `CoerceUnsized` can be passed by a where-clause,
71                 // so the (generic) MIR may not be able to expand it.
72                 let operand = self.trans_operand(&bcx, source);
73                 let operand = operand.pack_if_pair(&bcx);
74                 let llref = match operand.val {
75                     OperandValue::Pair(..) => bug!(),
76                     OperandValue::Immediate(llval) => {
77                         // unsize from an immediate structure. We don't
78                         // really need a temporary alloca here, but
79                         // avoiding it would require us to have
80                         // `coerce_unsized_into` use extractvalue to
81                         // index into the struct, and this case isn't
82                         // important enough for it.
83                         debug!("trans_rvalue: creating ugly alloca");
84                         let scratch = LvalueRef::alloca(&bcx, operand.ty, "__unsize_temp");
85                         base::store_ty(&bcx, llval, scratch.llval, scratch.alignment, operand.ty);
86                         scratch
87                     }
88                     OperandValue::Ref(llref, align) => {
89                         LvalueRef::new_sized_ty(llref, operand.ty, align)
90                     }
91                 };
92                 base::coerce_unsized_into(&bcx, &llref, &dest);
93                 bcx
94             }
95
96             mir::Rvalue::Repeat(ref elem, ref count) => {
97                 let tr_elem = self.trans_operand(&bcx, elem);
98                 let size = count.value.as_u64(bcx.tcx().sess.target.uint_type);
99                 let size = C_uint(bcx.ccx, size);
100                 let base = base::get_dataptr(&bcx, dest.llval);
101                 tvec::slice_for_each(&bcx, base, tr_elem.ty, size, |bcx, llslot| {
102                     self.store_operand(bcx, llslot, dest.alignment.to_align(), tr_elem);
103                 })
104             }
105
106             mir::Rvalue::Aggregate(ref kind, ref operands) => {
107                 match *kind {
108                     mir::AggregateKind::Adt(adt_def, variant_index, substs, active_field_index) => {
109                         let disr = Disr::from(adt_def.variants[variant_index].disr_val);
110                         let dest_ty = dest.ty.to_ty(bcx.tcx());
111                         adt::trans_set_discr(&bcx, dest_ty, dest.llval, Disr::from(disr));
112                         for (i, operand) in operands.iter().enumerate() {
113                             let op = self.trans_operand(&bcx, operand);
114                             // Do not generate stores and GEPis for zero-sized fields.
115                             if !common::type_is_zero_size(bcx.ccx, op.ty) {
116                                 let mut val = LvalueRef::new_sized(
117                                     dest.llval, dest.ty, dest.alignment);
118                                 let field_index = active_field_index.unwrap_or(i);
119                                 val.ty = LvalueTy::Downcast {
120                                     adt_def: adt_def,
121                                     substs: self.monomorphize(&substs),
122                                     variant_index: disr.0 as usize,
123                                 };
124                                 let (lldest_i, align) = val.trans_field_ptr(&bcx, field_index);
125                                 self.store_operand(&bcx, lldest_i, align.to_align(), op);
126                             }
127                         }
128                     },
129                     _ => {
130                         // If this is a tuple or closure, we need to translate GEP indices.
131                         let layout = bcx.ccx.layout_of(dest.ty.to_ty(bcx.tcx()));
132                         let translation = if let Layout::Univariant { ref variant, .. } = *layout {
133                             Some(&variant.memory_index)
134                         } else {
135                             None
136                         };
137                         let alignment = dest.alignment;
138                         for (i, operand) in operands.iter().enumerate() {
139                             let op = self.trans_operand(&bcx, operand);
140                             // Do not generate stores and GEPis for zero-sized fields.
141                             if !common::type_is_zero_size(bcx.ccx, op.ty) {
142                                 // Note: perhaps this should be StructGep, but
143                                 // note that in some cases the values here will
144                                 // not be structs but arrays.
145                                 let i = if let Some(ref t) = translation {
146                                     t[i] as usize
147                                 } else {
148                                     i
149                                 };
150                                 let dest = bcx.gepi(dest.llval, &[0, i]);
151                                 self.store_operand(&bcx, dest, alignment.to_align(), op);
152                             }
153                         }
154                     }
155                 }
156                 bcx
157             }
158
159             mir::Rvalue::InlineAsm { ref asm, ref outputs, ref inputs } => {
160                 let outputs = outputs.iter().map(|output| {
161                     let lvalue = self.trans_lvalue(&bcx, output);
162                     (lvalue.llval, lvalue.ty.to_ty(bcx.tcx()))
163                 }).collect();
164
165                 let input_vals = inputs.iter().map(|input| {
166                     self.trans_operand(&bcx, input).immediate()
167                 }).collect();
168
169                 asm::trans_inline_asm(&bcx, asm, outputs, input_vals);
170                 bcx
171             }
172
173             _ => {
174                 assert!(rvalue_creates_operand(rvalue));
175                 let (bcx, temp) = self.trans_rvalue_operand(bcx, rvalue);
176                 self.store_operand(&bcx, dest.llval, dest.alignment.to_align(), temp);
177                 bcx
178             }
179         }
180     }
181
182     pub fn trans_rvalue_operand(&mut self,
183                                 bcx: Builder<'a, 'tcx>,
184                                 rvalue: &mir::Rvalue<'tcx>)
185                                 -> (Builder<'a, 'tcx>, OperandRef<'tcx>)
186     {
187         assert!(rvalue_creates_operand(rvalue), "cannot trans {:?} to operand", rvalue);
188
189         match *rvalue {
190             mir::Rvalue::Cast(ref kind, ref source, cast_ty) => {
191                 let operand = self.trans_operand(&bcx, source);
192                 debug!("cast operand is {:?}", operand);
193                 let cast_ty = self.monomorphize(&cast_ty);
194
195                 let val = match *kind {
196                     mir::CastKind::ReifyFnPointer => {
197                         match operand.ty.sty {
198                             ty::TyFnDef(def_id, substs, _) => {
199                                 OperandValue::Immediate(
200                                     Callee::def(bcx.ccx, def_id, substs)
201                                         .reify(bcx.ccx))
202                             }
203                             _ => {
204                                 bug!("{} cannot be reified to a fn ptr", operand.ty)
205                             }
206                         }
207                     }
208                     mir::CastKind::UnsafeFnPointer => {
209                         // this is a no-op at the LLVM level
210                         operand.val
211                     }
212                     mir::CastKind::Unsize => {
213                         // unsize targets other than to a fat pointer currently
214                         // can't be operands.
215                         assert!(common::type_is_fat_ptr(bcx.ccx, cast_ty));
216
217                         match operand.val {
218                             OperandValue::Pair(lldata, llextra) => {
219                                 // unsize from a fat pointer - this is a
220                                 // "trait-object-to-supertrait" coercion, for
221                                 // example,
222                                 //   &'a fmt::Debug+Send => &'a fmt::Debug,
223                                 // So we need to pointercast the base to ensure
224                                 // the types match up.
225                                 let llcast_ty = type_of::fat_ptr_base_ty(bcx.ccx, cast_ty);
226                                 let lldata = bcx.pointercast(lldata, llcast_ty);
227                                 OperandValue::Pair(lldata, llextra)
228                             }
229                             OperandValue::Immediate(lldata) => {
230                                 // "standard" unsize
231                                 let (lldata, llextra) = base::unsize_thin_ptr(&bcx, lldata,
232                                     operand.ty, cast_ty);
233                                 OperandValue::Pair(lldata, llextra)
234                             }
235                             OperandValue::Ref(..) => {
236                                 bug!("by-ref operand {:?} in trans_rvalue_operand",
237                                      operand);
238                             }
239                         }
240                     }
241                     mir::CastKind::Misc if common::type_is_fat_ptr(bcx.ccx, operand.ty) => {
242                         let ll_cast_ty = type_of::immediate_type_of(bcx.ccx, cast_ty);
243                         let ll_from_ty = type_of::immediate_type_of(bcx.ccx, operand.ty);
244                         if let OperandValue::Pair(data_ptr, meta_ptr) = operand.val {
245                             if common::type_is_fat_ptr(bcx.ccx, cast_ty) {
246                                 let ll_cft = ll_cast_ty.field_types();
247                                 let ll_fft = ll_from_ty.field_types();
248                                 let data_cast = bcx.pointercast(data_ptr, ll_cft[0]);
249                                 assert_eq!(ll_cft[1].kind(), ll_fft[1].kind());
250                                 OperandValue::Pair(data_cast, meta_ptr)
251                             } else { // cast to thin-ptr
252                                 // Cast of fat-ptr to thin-ptr is an extraction of data-ptr and
253                                 // pointer-cast of that pointer to desired pointer type.
254                                 let llval = bcx.pointercast(data_ptr, ll_cast_ty);
255                                 OperandValue::Immediate(llval)
256                             }
257                         } else {
258                             bug!("Unexpected non-Pair operand")
259                         }
260                     }
261                     mir::CastKind::Misc => {
262                         debug_assert!(common::type_is_immediate(bcx.ccx, cast_ty));
263                         let r_t_in = CastTy::from_ty(operand.ty).expect("bad input type for cast");
264                         let r_t_out = CastTy::from_ty(cast_ty).expect("bad output type for cast");
265                         let ll_t_in = type_of::immediate_type_of(bcx.ccx, operand.ty);
266                         let ll_t_out = type_of::immediate_type_of(bcx.ccx, cast_ty);
267                         let llval = operand.immediate();
268                         let l = bcx.ccx.layout_of(operand.ty);
269                         let signed = if let Layout::CEnum { signed, min, max, .. } = *l {
270                             if max > min {
271                                 // We want `table[e as usize]` to not
272                                 // have bound checks, and this is the most
273                                 // convenient place to put the `assume`.
274
275                                 base::call_assume(&bcx, bcx.icmp(
276                                     llvm::IntULE,
277                                     llval,
278                                     C_integral(common::val_ty(llval), max, false)
279                                 ));
280                             }
281
282                             signed
283                         } else {
284                             operand.ty.is_signed()
285                         };
286
287                         let newval = match (r_t_in, r_t_out) {
288                             (CastTy::Int(_), CastTy::Int(_)) => {
289                                 let srcsz = ll_t_in.int_width();
290                                 let dstsz = ll_t_out.int_width();
291                                 if srcsz == dstsz {
292                                     bcx.bitcast(llval, ll_t_out)
293                                 } else if srcsz > dstsz {
294                                     bcx.trunc(llval, ll_t_out)
295                                 } else if signed {
296                                     bcx.sext(llval, ll_t_out)
297                                 } else {
298                                     bcx.zext(llval, ll_t_out)
299                                 }
300                             }
301                             (CastTy::Float, CastTy::Float) => {
302                                 let srcsz = ll_t_in.float_width();
303                                 let dstsz = ll_t_out.float_width();
304                                 if dstsz > srcsz {
305                                     bcx.fpext(llval, ll_t_out)
306                                 } else if srcsz > dstsz {
307                                     bcx.fptrunc(llval, ll_t_out)
308                                 } else {
309                                     llval
310                                 }
311                             }
312                             (CastTy::Ptr(_), CastTy::Ptr(_)) |
313                             (CastTy::FnPtr, CastTy::Ptr(_)) |
314                             (CastTy::RPtr(_), CastTy::Ptr(_)) =>
315                                 bcx.pointercast(llval, ll_t_out),
316                             (CastTy::Ptr(_), CastTy::Int(_)) |
317                             (CastTy::FnPtr, CastTy::Int(_)) =>
318                                 bcx.ptrtoint(llval, ll_t_out),
319                             (CastTy::Int(_), CastTy::Ptr(_)) =>
320                                 bcx.inttoptr(llval, ll_t_out),
321                             (CastTy::Int(_), CastTy::Float) if signed =>
322                                 bcx.sitofp(llval, ll_t_out),
323                             (CastTy::Int(_), CastTy::Float) =>
324                                 bcx.uitofp(llval, ll_t_out),
325                             (CastTy::Float, CastTy::Int(IntTy::I)) =>
326                                 bcx.fptosi(llval, ll_t_out),
327                             (CastTy::Float, CastTy::Int(_)) =>
328                                 bcx.fptoui(llval, ll_t_out),
329                             _ => bug!("unsupported cast: {:?} to {:?}", operand.ty, cast_ty)
330                         };
331                         OperandValue::Immediate(newval)
332                     }
333                 };
334                 let operand = OperandRef {
335                     val: val,
336                     ty: cast_ty
337                 };
338                 (bcx, operand)
339             }
340
341             mir::Rvalue::Ref(_, bk, ref lvalue) => {
342                 let tr_lvalue = self.trans_lvalue(&bcx, lvalue);
343
344                 let ty = tr_lvalue.ty.to_ty(bcx.tcx());
345                 let ref_ty = bcx.tcx().mk_ref(
346                     bcx.tcx().mk_region(ty::ReErased),
347                     ty::TypeAndMut { ty: ty, mutbl: bk.to_mutbl_lossy() }
348                 );
349
350                 // Note: lvalues are indirect, so storing the `llval` into the
351                 // destination effectively creates a reference.
352                 let operand = if bcx.ccx.shared().type_is_sized(ty) {
353                     OperandRef {
354                         val: OperandValue::Immediate(tr_lvalue.llval),
355                         ty: ref_ty,
356                     }
357                 } else {
358                     OperandRef {
359                         val: OperandValue::Pair(tr_lvalue.llval,
360                                                 tr_lvalue.llextra),
361                         ty: ref_ty,
362                     }
363                 };
364                 (bcx, operand)
365             }
366
367             mir::Rvalue::Len(ref lvalue) => {
368                 let tr_lvalue = self.trans_lvalue(&bcx, lvalue);
369                 let operand = OperandRef {
370                     val: OperandValue::Immediate(tr_lvalue.len(bcx.ccx)),
371                     ty: bcx.tcx().types.usize,
372                 };
373                 (bcx, operand)
374             }
375
376             mir::Rvalue::BinaryOp(op, ref lhs, ref rhs) => {
377                 let lhs = self.trans_operand(&bcx, lhs);
378                 let rhs = self.trans_operand(&bcx, rhs);
379                 let llresult = if common::type_is_fat_ptr(bcx.ccx, lhs.ty) {
380                     match (lhs.val, rhs.val) {
381                         (OperandValue::Pair(lhs_addr, lhs_extra),
382                          OperandValue::Pair(rhs_addr, rhs_extra)) => {
383                             self.trans_fat_ptr_binop(&bcx, op,
384                                                      lhs_addr, lhs_extra,
385                                                      rhs_addr, rhs_extra,
386                                                      lhs.ty)
387                         }
388                         _ => bug!()
389                     }
390
391                 } else {
392                     self.trans_scalar_binop(&bcx, op,
393                                             lhs.immediate(), rhs.immediate(),
394                                             lhs.ty)
395                 };
396                 let operand = OperandRef {
397                     val: OperandValue::Immediate(llresult),
398                     ty: op.ty(bcx.tcx(), lhs.ty, rhs.ty),
399                 };
400                 (bcx, operand)
401             }
402             mir::Rvalue::CheckedBinaryOp(op, ref lhs, ref rhs) => {
403                 let lhs = self.trans_operand(&bcx, lhs);
404                 let rhs = self.trans_operand(&bcx, rhs);
405                 let result = self.trans_scalar_checked_binop(&bcx, op,
406                                                              lhs.immediate(), rhs.immediate(),
407                                                              lhs.ty);
408                 let val_ty = op.ty(bcx.tcx(), lhs.ty, rhs.ty);
409                 let operand_ty = bcx.tcx().intern_tup(&[val_ty, bcx.tcx().types.bool], false);
410                 let operand = OperandRef {
411                     val: result,
412                     ty: operand_ty
413                 };
414
415                 (bcx, operand)
416             }
417
418             mir::Rvalue::UnaryOp(op, ref operand) => {
419                 let operand = self.trans_operand(&bcx, operand);
420                 let lloperand = operand.immediate();
421                 let is_float = operand.ty.is_fp();
422                 let llval = match op {
423                     mir::UnOp::Not => bcx.not(lloperand),
424                     mir::UnOp::Neg => if is_float {
425                         bcx.fneg(lloperand)
426                     } else {
427                         bcx.neg(lloperand)
428                     }
429                 };
430                 (bcx, OperandRef {
431                     val: OperandValue::Immediate(llval),
432                     ty: operand.ty,
433                 })
434             }
435
436             mir::Rvalue::Box(content_ty) => {
437                 let content_ty: Ty<'tcx> = self.monomorphize(&content_ty);
438                 let llty = type_of::type_of(bcx.ccx, content_ty);
439                 let llsize = machine::llsize_of(bcx.ccx, llty);
440                 let align = type_of::align_of(bcx.ccx, content_ty);
441                 let llalign = C_uint(bcx.ccx, align);
442                 let llty_ptr = llty.ptr_to();
443                 let box_ty = bcx.tcx().mk_box(content_ty);
444
445                 // Allocate space:
446                 let def_id = match bcx.tcx().lang_items.require(ExchangeMallocFnLangItem) {
447                     Ok(id) => id,
448                     Err(s) => {
449                         bcx.sess().fatal(&format!("allocation of `{}` {}", box_ty, s));
450                     }
451                 };
452                 let r = Callee::def(bcx.ccx, def_id, bcx.tcx().intern_substs(&[]))
453                     .reify(bcx.ccx);
454                 let val = bcx.pointercast(bcx.call(r, &[llsize, llalign], None), llty_ptr);
455
456                 let operand = OperandRef {
457                     val: OperandValue::Immediate(val),
458                     ty: box_ty,
459                 };
460                 (bcx, operand)
461             }
462
463             mir::Rvalue::Use(ref operand) => {
464                 let operand = self.trans_operand(&bcx, operand);
465                 (bcx, operand)
466             }
467             mir::Rvalue::Repeat(..) |
468             mir::Rvalue::Aggregate(..) |
469             mir::Rvalue::InlineAsm { .. } => {
470                 bug!("cannot generate operand from rvalue {:?}", rvalue);
471
472             }
473         }
474     }
475
476     pub fn trans_scalar_binop(&mut self,
477                               bcx: &Builder<'a, 'tcx>,
478                               op: mir::BinOp,
479                               lhs: ValueRef,
480                               rhs: ValueRef,
481                               input_ty: Ty<'tcx>) -> ValueRef {
482         let is_float = input_ty.is_fp();
483         let is_signed = input_ty.is_signed();
484         let is_nil = input_ty.is_nil();
485         let is_bool = input_ty.is_bool();
486         match op {
487             mir::BinOp::Add => if is_float {
488                 bcx.fadd(lhs, rhs)
489             } else {
490                 bcx.add(lhs, rhs)
491             },
492             mir::BinOp::Sub => if is_float {
493                 bcx.fsub(lhs, rhs)
494             } else {
495                 bcx.sub(lhs, rhs)
496             },
497             mir::BinOp::Mul => if is_float {
498                 bcx.fmul(lhs, rhs)
499             } else {
500                 bcx.mul(lhs, rhs)
501             },
502             mir::BinOp::Div => if is_float {
503                 bcx.fdiv(lhs, rhs)
504             } else if is_signed {
505                 bcx.sdiv(lhs, rhs)
506             } else {
507                 bcx.udiv(lhs, rhs)
508             },
509             mir::BinOp::Rem => if is_float {
510                 bcx.frem(lhs, rhs)
511             } else if is_signed {
512                 bcx.srem(lhs, rhs)
513             } else {
514                 bcx.urem(lhs, rhs)
515             },
516             mir::BinOp::BitOr => bcx.or(lhs, rhs),
517             mir::BinOp::BitAnd => bcx.and(lhs, rhs),
518             mir::BinOp::BitXor => bcx.xor(lhs, rhs),
519             mir::BinOp::Shl => common::build_unchecked_lshift(bcx, lhs, rhs),
520             mir::BinOp::Shr => common::build_unchecked_rshift(bcx, input_ty, lhs, rhs),
521             mir::BinOp::Ne | mir::BinOp::Lt | mir::BinOp::Gt |
522             mir::BinOp::Eq | mir::BinOp::Le | mir::BinOp::Ge => if is_nil {
523                 C_bool(bcx.ccx, match op {
524                     mir::BinOp::Ne | mir::BinOp::Lt | mir::BinOp::Gt => false,
525                     mir::BinOp::Eq | mir::BinOp::Le | mir::BinOp::Ge => true,
526                     _ => unreachable!()
527                 })
528             } else if is_float {
529                 bcx.fcmp(
530                     base::bin_op_to_fcmp_predicate(op.to_hir_binop()),
531                     lhs, rhs
532                 )
533             } else {
534                 let (lhs, rhs) = if is_bool {
535                     // FIXME(#36856) -- extend the bools into `i8` because
536                     // LLVM's i1 comparisons are broken.
537                     (bcx.zext(lhs, Type::i8(bcx.ccx)),
538                      bcx.zext(rhs, Type::i8(bcx.ccx)))
539                 } else {
540                     (lhs, rhs)
541                 };
542
543                 bcx.icmp(
544                     base::bin_op_to_icmp_predicate(op.to_hir_binop(), is_signed),
545                     lhs, rhs
546                 )
547             }
548         }
549     }
550
551     pub fn trans_fat_ptr_binop(&mut self,
552                                bcx: &Builder<'a, 'tcx>,
553                                op: mir::BinOp,
554                                lhs_addr: ValueRef,
555                                lhs_extra: ValueRef,
556                                rhs_addr: ValueRef,
557                                rhs_extra: ValueRef,
558                                _input_ty: Ty<'tcx>)
559                                -> ValueRef {
560         match op {
561             mir::BinOp::Eq => {
562                 bcx.and(
563                     bcx.icmp(llvm::IntEQ, lhs_addr, rhs_addr),
564                     bcx.icmp(llvm::IntEQ, lhs_extra, rhs_extra)
565                 )
566             }
567             mir::BinOp::Ne => {
568                 bcx.or(
569                     bcx.icmp(llvm::IntNE, lhs_addr, rhs_addr),
570                     bcx.icmp(llvm::IntNE, lhs_extra, rhs_extra)
571                 )
572             }
573             mir::BinOp::Le | mir::BinOp::Lt |
574             mir::BinOp::Ge | mir::BinOp::Gt => {
575                 // a OP b ~ a.0 STRICT(OP) b.0 | (a.0 == b.0 && a.1 OP a.1)
576                 let (op, strict_op) = match op {
577                     mir::BinOp::Lt => (llvm::IntULT, llvm::IntULT),
578                     mir::BinOp::Le => (llvm::IntULE, llvm::IntULT),
579                     mir::BinOp::Gt => (llvm::IntUGT, llvm::IntUGT),
580                     mir::BinOp::Ge => (llvm::IntUGE, llvm::IntUGT),
581                     _ => bug!(),
582                 };
583
584                 bcx.or(
585                     bcx.icmp(strict_op, lhs_addr, rhs_addr),
586                     bcx.and(
587                         bcx.icmp(llvm::IntEQ, lhs_addr, rhs_addr),
588                         bcx.icmp(op, lhs_extra, rhs_extra)
589                     )
590                 )
591             }
592             _ => {
593                 bug!("unexpected fat ptr binop");
594             }
595         }
596     }
597
598     pub fn trans_scalar_checked_binop(&mut self,
599                                       bcx: &Builder<'a, 'tcx>,
600                                       op: mir::BinOp,
601                                       lhs: ValueRef,
602                                       rhs: ValueRef,
603                                       input_ty: Ty<'tcx>) -> OperandValue {
604         // This case can currently arise only from functions marked
605         // with #[rustc_inherit_overflow_checks] and inlined from
606         // another crate (mostly core::num generic/#[inline] fns),
607         // while the current crate doesn't use overflow checks.
608         if !bcx.ccx.check_overflow() {
609             let val = self.trans_scalar_binop(bcx, op, lhs, rhs, input_ty);
610             return OperandValue::Pair(val, C_bool(bcx.ccx, false));
611         }
612
613         // First try performing the operation on constants, which
614         // will only succeed if both operands are constant.
615         // This is necessary to determine when an overflow Assert
616         // will always panic at runtime, and produce a warning.
617         if let Some((val, of)) = const_scalar_checked_binop(bcx.tcx(), op, lhs, rhs, input_ty) {
618             return OperandValue::Pair(val, C_bool(bcx.ccx, of));
619         }
620
621         let (val, of) = match op {
622             // These are checked using intrinsics
623             mir::BinOp::Add | mir::BinOp::Sub | mir::BinOp::Mul => {
624                 let oop = match op {
625                     mir::BinOp::Add => OverflowOp::Add,
626                     mir::BinOp::Sub => OverflowOp::Sub,
627                     mir::BinOp::Mul => OverflowOp::Mul,
628                     _ => unreachable!()
629                 };
630                 let intrinsic = get_overflow_intrinsic(oop, bcx, input_ty);
631                 let res = bcx.call(intrinsic, &[lhs, rhs], None);
632
633                 (bcx.extract_value(res, 0),
634                  bcx.extract_value(res, 1))
635             }
636             mir::BinOp::Shl | mir::BinOp::Shr => {
637                 let lhs_llty = val_ty(lhs);
638                 let rhs_llty = val_ty(rhs);
639                 let invert_mask = common::shift_mask_val(&bcx, lhs_llty, rhs_llty, true);
640                 let outer_bits = bcx.and(rhs, invert_mask);
641
642                 let of = bcx.icmp(llvm::IntNE, outer_bits, C_null(rhs_llty));
643                 let val = self.trans_scalar_binop(bcx, op, lhs, rhs, input_ty);
644
645                 (val, of)
646             }
647             _ => {
648                 bug!("Operator `{:?}` is not a checkable operator", op)
649             }
650         };
651
652         OperandValue::Pair(val, of)
653     }
654 }
655
656 pub fn rvalue_creates_operand(rvalue: &mir::Rvalue) -> bool {
657     match *rvalue {
658         mir::Rvalue::Ref(..) |
659         mir::Rvalue::Len(..) |
660         mir::Rvalue::Cast(..) | // (*)
661         mir::Rvalue::BinaryOp(..) |
662         mir::Rvalue::CheckedBinaryOp(..) |
663         mir::Rvalue::UnaryOp(..) |
664         mir::Rvalue::Box(..) |
665         mir::Rvalue::Use(..) =>
666             true,
667         mir::Rvalue::Repeat(..) |
668         mir::Rvalue::Aggregate(..) |
669         mir::Rvalue::InlineAsm { .. } =>
670             false,
671     }
672
673     // (*) this is only true if the type is suitable
674 }
675
676 #[derive(Copy, Clone)]
677 enum OverflowOp {
678     Add, Sub, Mul
679 }
680
681 fn get_overflow_intrinsic(oop: OverflowOp, bcx: &Builder, ty: Ty) -> ValueRef {
682     use syntax::ast::IntTy::*;
683     use syntax::ast::UintTy::*;
684     use rustc::ty::{TyInt, TyUint};
685
686     let tcx = bcx.tcx();
687
688     let new_sty = match ty.sty {
689         TyInt(Is) => match &tcx.sess.target.target.target_pointer_width[..] {
690             "16" => TyInt(I16),
691             "32" => TyInt(I32),
692             "64" => TyInt(I64),
693             _ => panic!("unsupported target word size")
694         },
695         TyUint(Us) => match &tcx.sess.target.target.target_pointer_width[..] {
696             "16" => TyUint(U16),
697             "32" => TyUint(U32),
698             "64" => TyUint(U64),
699             _ => panic!("unsupported target word size")
700         },
701         ref t @ TyUint(_) | ref t @ TyInt(_) => t.clone(),
702         _ => panic!("tried to get overflow intrinsic for op applied to non-int type")
703     };
704
705     let name = match oop {
706         OverflowOp::Add => match new_sty {
707             TyInt(I8) => "llvm.sadd.with.overflow.i8",
708             TyInt(I16) => "llvm.sadd.with.overflow.i16",
709             TyInt(I32) => "llvm.sadd.with.overflow.i32",
710             TyInt(I64) => "llvm.sadd.with.overflow.i64",
711             TyInt(I128) => "llvm.sadd.with.overflow.i128",
712
713             TyUint(U8) => "llvm.uadd.with.overflow.i8",
714             TyUint(U16) => "llvm.uadd.with.overflow.i16",
715             TyUint(U32) => "llvm.uadd.with.overflow.i32",
716             TyUint(U64) => "llvm.uadd.with.overflow.i64",
717             TyUint(U128) => "llvm.uadd.with.overflow.i128",
718
719             _ => unreachable!(),
720         },
721         OverflowOp::Sub => match new_sty {
722             TyInt(I8) => "llvm.ssub.with.overflow.i8",
723             TyInt(I16) => "llvm.ssub.with.overflow.i16",
724             TyInt(I32) => "llvm.ssub.with.overflow.i32",
725             TyInt(I64) => "llvm.ssub.with.overflow.i64",
726             TyInt(I128) => "llvm.ssub.with.overflow.i128",
727
728             TyUint(U8) => "llvm.usub.with.overflow.i8",
729             TyUint(U16) => "llvm.usub.with.overflow.i16",
730             TyUint(U32) => "llvm.usub.with.overflow.i32",
731             TyUint(U64) => "llvm.usub.with.overflow.i64",
732             TyUint(U128) => "llvm.usub.with.overflow.i128",
733
734             _ => unreachable!(),
735         },
736         OverflowOp::Mul => match new_sty {
737             TyInt(I8) => "llvm.smul.with.overflow.i8",
738             TyInt(I16) => "llvm.smul.with.overflow.i16",
739             TyInt(I32) => "llvm.smul.with.overflow.i32",
740             TyInt(I64) => "llvm.smul.with.overflow.i64",
741             TyInt(I128) => "llvm.smul.with.overflow.i128",
742
743             TyUint(U8) => "llvm.umul.with.overflow.i8",
744             TyUint(U16) => "llvm.umul.with.overflow.i16",
745             TyUint(U32) => "llvm.umul.with.overflow.i32",
746             TyUint(U64) => "llvm.umul.with.overflow.i64",
747             TyUint(U128) => "llvm.umul.with.overflow.i128",
748
749             _ => unreachable!(),
750         },
751     };
752
753     bcx.ccx.get_intrinsic(&name)
754 }