about summary refs log tree commit diff
path: root/src/librustc_codegen_llvm
diff options
context:
space:
mode:
authorDenis Merigoux <denis.merigoux@gmail.com>2018-10-05 15:08:49 +0200
committerEduard-Mihai Burtescu <edy.burt@gmail.com>2018-11-16 15:08:18 +0200
commit54dd3a47fd54eb466dad7e47b41ec1b5b2dafd5a (patch)
treef2507cb57a8212eed8a963415ac5db2a4bccc8a8 /src/librustc_codegen_llvm
parent1ebdfbb02641676fb4f8efb1f87cfe8d0d29d2b3 (diff)
downloadrust-54dd3a47fd54eb466dad7e47b41ec1b5b2dafd5a.tar.gz
rust-54dd3a47fd54eb466dad7e47b41ec1b5b2dafd5a.zip
All Builder methods now take &mut self instead of &self
Diffstat (limited to 'src/librustc_codegen_llvm')
-rw-r--r--src/librustc_codegen_llvm/abi.rs34
-rw-r--r--src/librustc_codegen_llvm/asm.rs2
-rw-r--r--src/librustc_codegen_llvm/builder.rs252
-rw-r--r--src/librustc_codegen_llvm/debuginfo/gdb.rs2
-rw-r--r--src/librustc_codegen_llvm/debuginfo/mod.rs6
-rw-r--r--src/librustc_codegen_llvm/intrinsic.rs244
6 files changed, 276 insertions, 264 deletions
diff --git a/src/librustc_codegen_llvm/abi.rs b/src/librustc_codegen_llvm/abi.rs
index d0e22908728..89e9e12a8a3 100644
--- a/src/librustc_codegen_llvm/abi.rs
+++ b/src/librustc_codegen_llvm/abi.rs
@@ -171,13 +171,13 @@ pub trait ArgTypeExt<'ll, 'tcx> {
     fn memory_ty(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
     fn store(
         &self,
-        bx: &Builder<'_, 'll, 'tcx>,
+        bx: &mut Builder<'_, 'll, 'tcx>,
         val: &'ll Value,
         dst: PlaceRef<'tcx, &'ll Value>,
     );
     fn store_fn_arg(
         &self,
-        bx: &Builder<'_, 'll, 'tcx>,
+        bx: &mut Builder<'_, 'll, 'tcx>,
         idx: &mut usize,
         dst: PlaceRef<'tcx, &'ll Value>,
     );
@@ -196,14 +196,13 @@ impl ArgTypeExt<'ll, 'tcx> for ArgType<'tcx, Ty<'tcx>> {
     /// or results of call/invoke instructions into their destinations.
     fn store(
         &self,
-        bx: &Builder<'_, 'll, 'tcx>,
+        bx: &mut Builder<'_, 'll, 'tcx>,
         val: &'ll Value,
         dst: PlaceRef<'tcx, &'ll Value>,
     ) {
         if self.is_ignore() {
             return;
         }
-        let cx = bx.cx();
         if self.is_sized_indirect() {
             OperandValue::Ref(val, None, self.layout.align).store(bx, dst)
         } else if self.is_unsized_indirect() {
@@ -213,7 +212,8 @@ impl ArgTypeExt<'ll, 'tcx> for ArgType<'tcx, Ty<'tcx>> {
             // uses it for i16 -> {i8, i8}, but not for i24 -> {i8, i8, i8}.
             let can_store_through_cast_ptr = false;
             if can_store_through_cast_ptr {
-                let cast_dst = bx.pointercast(dst.llval, cx.type_ptr_to(cast.llvm_type(cx)));
+                let cast_ptr_llty = bx.cx().type_ptr_to(cast.llvm_type(bx.cx()));
+                let cast_dst = bx.pointercast(dst.llval, cast_ptr_llty);
                 bx.store(val, cast_dst, self.layout.align);
             } else {
                 // The actual return type is a struct, but the ABI
@@ -231,9 +231,9 @@ impl ArgTypeExt<'ll, 'tcx> for ArgType<'tcx, Ty<'tcx>> {
                 //   bitcasting to the struct type yields invalid cast errors.
 
                 // We instead thus allocate some scratch space...
-                let scratch_size = cast.size(cx);
-                let scratch_align = cast.align(cx);
-                let llscratch = bx.alloca(cast.llvm_type(cx), "abi_cast", scratch_align);
+                let scratch_size = cast.size(bx.cx());
+                let scratch_align = cast.align(bx.cx());
+                let llscratch = bx.alloca(cast.llvm_type(bx.cx()), "abi_cast", scratch_align);
                 bx.lifetime_start(llscratch, scratch_size);
 
                 // ...where we first store the value...
@@ -241,11 +241,11 @@ impl ArgTypeExt<'ll, 'tcx> for ArgType<'tcx, Ty<'tcx>> {
 
                 // ...and then memcpy it to the intended destination.
                 bx.memcpy(
-                    bx.pointercast(dst.llval, cx.type_i8p()),
+                    dst.llval,
                     self.layout.align,
-                    bx.pointercast(llscratch, cx.type_i8p()),
+                    llscratch,
                     scratch_align,
-                    cx.const_usize(self.layout.size.bytes()),
+                    bx.cx().const_usize(self.layout.size.bytes()),
                     MemFlags::empty()
                 );
 
@@ -258,7 +258,7 @@ impl ArgTypeExt<'ll, 'tcx> for ArgType<'tcx, Ty<'tcx>> {
 
     fn store_fn_arg(
         &self,
-        bx: &Builder<'a, 'll, 'tcx>,
+        bx: &mut Builder<'a, 'll, 'tcx>,
         idx: &mut usize,
         dst: PlaceRef<'tcx, &'ll Value>,
     ) {
@@ -284,14 +284,14 @@ impl ArgTypeExt<'ll, 'tcx> for ArgType<'tcx, Ty<'tcx>> {
 
 impl ArgTypeMethods<'tcx> for Builder<'a, 'll, 'tcx> {
     fn store_fn_arg(
-        &self,
+        &mut self,
         ty: &ArgType<'tcx, Ty<'tcx>>,
         idx: &mut usize, dst: PlaceRef<'tcx, Self::Value>
     ) {
         ty.store_fn_arg(self, idx, dst)
     }
     fn store_arg_ty(
-        &self,
+        &mut self,
         ty: &ArgType<'tcx, Ty<'tcx>>,
         val: &'ll Value,
         dst: PlaceRef<'tcx, &'ll Value>
@@ -324,7 +324,7 @@ pub trait FnTypeExt<'tcx> {
     fn ptr_to_llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
     fn llvm_cconv(&self) -> llvm::CallConv;
     fn apply_attrs_llfn(&self, llfn: &'ll Value);
-    fn apply_attrs_callsite(&self, bx: &Builder<'a, 'll, 'tcx>, callsite: &'ll Value);
+    fn apply_attrs_callsite(&self, bx: &mut Builder<'a, 'll, 'tcx>, callsite: &'ll Value);
 }
 
 impl<'tcx> FnTypeExt<'tcx> for FnType<'tcx, Ty<'tcx>> {
@@ -761,7 +761,7 @@ impl<'tcx> FnTypeExt<'tcx> for FnType<'tcx, Ty<'tcx>> {
         }
     }
 
-    fn apply_attrs_callsite(&self, bx: &Builder<'a, 'll, 'tcx>, callsite: &'ll Value) {
+    fn apply_attrs_callsite(&self, bx: &mut Builder<'a, 'll, 'tcx>, callsite: &'ll Value) {
         let mut i = 0;
         let mut apply = |attrs: &ArgAttributes| {
             attrs.apply_callsite(llvm::AttributePlace::Argument(i), callsite);
@@ -832,7 +832,7 @@ impl AbiMethods<'tcx> for CodegenCx<'ll, 'tcx> {
 
 impl AbiBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
     fn apply_attrs_callsite(
-        &self,
+        &mut self,
         ty: &FnType<'tcx, Ty<'tcx>>,
         callsite: Self::Value
     ) {
diff --git a/src/librustc_codegen_llvm/asm.rs b/src/librustc_codegen_llvm/asm.rs
index c0e7f949809..8bb88ba5a83 100644
--- a/src/librustc_codegen_llvm/asm.rs
+++ b/src/librustc_codegen_llvm/asm.rs
@@ -26,7 +26,7 @@ use libc::{c_uint, c_char};
 
 impl AsmBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
     fn codegen_inline_asm(
-        &self,
+        &mut self,
         ia: &hir::InlineAsm,
         outputs: Vec<PlaceRef<'tcx, &'ll Value>>,
         mut inputs: Vec<&'ll Value>
diff --git a/src/librustc_codegen_llvm/builder.rs b/src/librustc_codegen_llvm/builder.rs
index b013545d390..c10e98c5546 100644
--- a/src/librustc_codegen_llvm/builder.rs
+++ b/src/librustc_codegen_llvm/builder.rs
@@ -201,7 +201,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
     }
 
     fn switch(
-        &self,
+        &mut self,
         v: &'ll Value,
         else_llbb: &'ll BasicBlock,
         num_cases: usize,
@@ -212,7 +212,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
     }
 
     fn invoke(
-        &self,
+        &mut self,
         llfn: &'ll Value,
         args: &[&'ll Value],
         then: &'ll BasicBlock,
@@ -241,7 +241,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-    fn unreachable(&self) {
+    fn unreachable(&mut self) {
         self.count_insn("unreachable");
         unsafe {
             llvm::LLVMBuildUnreachable(self.llbuilder);
@@ -249,21 +249,21 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
     }
 
     /* Arithmetic */
-    fn add(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn add(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("add");
         unsafe {
             llvm::LLVMBuildAdd(self.llbuilder, lhs, rhs, noname())
         }
     }
 
-    fn fadd(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn fadd(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("fadd");
         unsafe {
             llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, noname())
         }
     }
 
-    fn fadd_fast(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn fadd_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("fadd");
         unsafe {
             let instr = llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, noname());
@@ -272,21 +272,21 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-    fn sub(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn sub(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("sub");
         unsafe {
             llvm::LLVMBuildSub(self.llbuilder, lhs, rhs, noname())
         }
     }
 
-    fn fsub(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn fsub(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("fsub");
         unsafe {
             llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, noname())
         }
     }
 
-    fn fsub_fast(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn fsub_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("fsub");
         unsafe {
             let instr = llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, noname());
@@ -295,21 +295,21 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-    fn mul(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn mul(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("mul");
         unsafe {
             llvm::LLVMBuildMul(self.llbuilder, lhs, rhs, noname())
         }
     }
 
-    fn fmul(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn fmul(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("fmul");
         unsafe {
             llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, noname())
         }
     }
 
-    fn fmul_fast(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn fmul_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("fmul");
         unsafe {
             let instr = llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, noname());
@@ -319,42 +319,42 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
     }
 
 
-    fn udiv(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn udiv(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("udiv");
         unsafe {
             llvm::LLVMBuildUDiv(self.llbuilder, lhs, rhs, noname())
         }
     }
 
-    fn exactudiv(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn exactudiv(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("exactudiv");
         unsafe {
             llvm::LLVMBuildExactUDiv(self.llbuilder, lhs, rhs, noname())
         }
     }
 
-    fn sdiv(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn sdiv(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("sdiv");
         unsafe {
             llvm::LLVMBuildSDiv(self.llbuilder, lhs, rhs, noname())
         }
     }
 
-    fn exactsdiv(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn exactsdiv(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("exactsdiv");
         unsafe {
             llvm::LLVMBuildExactSDiv(self.llbuilder, lhs, rhs, noname())
         }
     }
 
-    fn fdiv(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn fdiv(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("fdiv");
         unsafe {
             llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, noname())
         }
     }
 
-    fn fdiv_fast(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn fdiv_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("fdiv");
         unsafe {
             let instr = llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, noname());
@@ -363,28 +363,28 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-    fn urem(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn urem(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("urem");
         unsafe {
             llvm::LLVMBuildURem(self.llbuilder, lhs, rhs, noname())
         }
     }
 
-    fn srem(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn srem(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("srem");
         unsafe {
             llvm::LLVMBuildSRem(self.llbuilder, lhs, rhs, noname())
         }
     }
 
-    fn frem(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn frem(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("frem");
         unsafe {
             llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, noname())
         }
     }
 
-    fn frem_fast(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn frem_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("frem");
         unsafe {
             let instr = llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, noname());
@@ -393,70 +393,70 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-    fn shl(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn shl(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("shl");
         unsafe {
             llvm::LLVMBuildShl(self.llbuilder, lhs, rhs, noname())
         }
     }
 
-    fn lshr(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn lshr(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("lshr");
         unsafe {
             llvm::LLVMBuildLShr(self.llbuilder, lhs, rhs, noname())
         }
     }
 
-    fn ashr(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn ashr(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("ashr");
         unsafe {
             llvm::LLVMBuildAShr(self.llbuilder, lhs, rhs, noname())
         }
     }
 
-    fn and(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn and(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("and");
         unsafe {
             llvm::LLVMBuildAnd(self.llbuilder, lhs, rhs, noname())
         }
     }
 
-    fn or(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn or(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("or");
         unsafe {
             llvm::LLVMBuildOr(self.llbuilder, lhs, rhs, noname())
         }
     }
 
-    fn xor(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn xor(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("xor");
         unsafe {
             llvm::LLVMBuildXor(self.llbuilder, lhs, rhs, noname())
         }
     }
 
-    fn neg(&self, v: &'ll Value) -> &'ll Value {
+    fn neg(&mut self, v: &'ll Value) -> &'ll Value {
         self.count_insn("neg");
         unsafe {
             llvm::LLVMBuildNeg(self.llbuilder, v, noname())
         }
     }
 
-    fn fneg(&self, v: &'ll Value) -> &'ll Value {
+    fn fneg(&mut self, v: &'ll Value) -> &'ll Value {
         self.count_insn("fneg");
         unsafe {
             llvm::LLVMBuildFNeg(self.llbuilder, v, noname())
         }
     }
 
-    fn not(&self, v: &'ll Value) -> &'ll Value {
+    fn not(&mut self, v: &'ll Value) -> &'ll Value {
         self.count_insn("not");
         unsafe {
             llvm::LLVMBuildNot(self.llbuilder, v, noname())
         }
     }
 
-    fn alloca(&self, ty: &'ll Type, name: &str, align: Align) -> &'ll Value {
+    fn alloca(&mut self, ty: &'ll Type, name: &str, align: Align) -> &'ll Value {
         let mut bx = Builder::with_cx(self.cx);
         bx.position_at_start(unsafe {
             llvm::LLVMGetFirstBasicBlock(self.llfn())
@@ -464,7 +464,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         bx.dynamic_alloca(ty, name, align)
     }
 
-    fn dynamic_alloca(&self, ty: &'ll Type, name: &str, align: Align) -> &'ll Value {
+    fn dynamic_alloca(&mut self, ty: &'ll Type, name: &str, align: Align) -> &'ll Value {
         self.count_insn("alloca");
         unsafe {
             let alloca = if name.is_empty() {
@@ -479,7 +479,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-    fn array_alloca(&self,
+    fn array_alloca(&mut self,
                         ty: &'ll Type,
                         len: &'ll Value,
                         name: &str,
@@ -498,7 +498,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-    fn load(&self, ptr: &'ll Value, align: Align) -> &'ll Value {
+    fn load(&mut self, ptr: &'ll Value, align: Align) -> &'ll Value {
         self.count_insn("load");
         unsafe {
             let load = llvm::LLVMBuildLoad(self.llbuilder, ptr, noname());
@@ -507,7 +507,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-    fn volatile_load(&self, ptr: &'ll Value) -> &'ll Value {
+    fn volatile_load(&mut self, ptr: &'ll Value) -> &'ll Value {
         self.count_insn("load.volatile");
         unsafe {
             let insn = llvm::LLVMBuildLoad(self.llbuilder, ptr, noname());
@@ -517,7 +517,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
     }
 
     fn atomic_load(
-        &self,
+        &mut self,
         ptr: &'ll Value,
         order: rustc_codegen_ssa::common::AtomicOrdering,
         size: Size,
@@ -537,7 +537,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
     }
 
     fn load_operand(
-        &self,
+        &mut self,
         place: PlaceRef<'tcx, &'ll Value>
     ) -> OperandRef<'tcx, &'ll Value> {
         debug!("PlaceRef::load: {:?}", place);
@@ -548,21 +548,25 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
             return OperandRef::new_zst(self.cx(), place.layout);
         }
 
-        let scalar_load_metadata = |load, scalar: &layout::Scalar| {
+        fn scalar_load_metadata<'a, 'll, 'tcx>(
+            bx: &mut Builder<'a, 'll, 'tcx>,
+            load: &'ll Value,
+            scalar: &layout::Scalar
+        ) {
             let vr = scalar.valid_range.clone();
             match scalar.value {
                 layout::Int(..) => {
-                    let range = scalar.valid_range_exclusive(self.cx());
+                    let range = scalar.valid_range_exclusive(bx.cx());
                     if range.start != range.end {
-                        self.range_metadata(load, range);
+                        bx.range_metadata(load, range);
                     }
                 }
                 layout::Pointer if vr.start() < vr.end() && !vr.contains(&0) => {
-                    self.nonnull_metadata(load);
+                    bx.nonnull_metadata(load);
                 }
                 _ => {}
             }
-        };
+        }
 
         let val = if let Some(llextra) = place.llextra {
             OperandValue::Ref(place.llval, Some(llextra), place.align)
@@ -578,16 +582,16 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
             let llval = const_llval.unwrap_or_else(|| {
                 let load = self.load(place.llval, place.align);
                 if let layout::Abi::Scalar(ref scalar) = place.layout.abi {
-                    scalar_load_metadata(load, scalar);
+                    scalar_load_metadata(self, load, scalar);
                 }
                 load
             });
             OperandValue::Immediate(to_immediate(self, llval, place.layout))
         } else if let layout::Abi::ScalarPair(ref a, ref b) = place.layout.abi {
-            let load = |i, scalar: &layout::Scalar| {
+            let mut load = |i, scalar: &layout::Scalar| {
                 let llptr = self.struct_gep(place.llval, i as u64);
                 let load = self.load(llptr, place.align);
-                scalar_load_metadata(load, scalar);
+                scalar_load_metadata(self, load, scalar);
                 if scalar.is_bool() {
                     self.trunc(load, self.cx().type_i1())
                 } else {
@@ -604,7 +608,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
 
 
 
-    fn range_metadata(&self, load: &'ll Value, range: Range<u128>) {
+    fn range_metadata(&mut self, load: &'ll Value, range: Range<u128>) {
         if self.cx().sess().target.target.arch == "amdgpu" {
             // amdgpu/LLVM does something weird and thinks a i64 value is
             // split into a v2i32, halving the bitwidth LLVM expects,
@@ -627,19 +631,19 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-    fn nonnull_metadata(&self, load: &'ll Value) {
+    fn nonnull_metadata(&mut self, load: &'ll Value) {
         unsafe {
             llvm::LLVMSetMetadata(load, llvm::MD_nonnull as c_uint,
                                   llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0));
         }
     }
 
-    fn store(&self, val: &'ll Value, ptr: &'ll Value, align: Align) -> &'ll Value {
+    fn store(&mut self, val: &'ll Value, ptr: &'ll Value, align: Align) -> &'ll Value {
         self.store_with_flags(val, ptr, align, MemFlags::empty())
     }
 
     fn store_with_flags(
-        &self,
+        &mut self,
         val: &'ll Value,
         ptr: &'ll Value,
         align: Align,
@@ -672,7 +676,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-   fn atomic_store(&self, val: &'ll Value, ptr: &'ll Value,
+   fn atomic_store(&mut self, val: &'ll Value, ptr: &'ll Value,
                    order: rustc_codegen_ssa::common::AtomicOrdering, size: Size) {
         debug!("Store {:?} -> {:?}", val, ptr);
         self.count_insn("store.atomic");
@@ -689,7 +693,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-    fn gep(&self, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
+    fn gep(&mut self, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
         self.count_insn("gep");
         unsafe {
             llvm::LLVMBuildGEP(self.llbuilder, ptr, indices.as_ptr(),
@@ -697,7 +701,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-    fn inbounds_gep(&self, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
+    fn inbounds_gep(&mut self, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
         self.count_insn("inboundsgep");
         unsafe {
             llvm::LLVMBuildInBoundsGEP(
@@ -706,77 +710,77 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
     }
 
     /* Casts */
-    fn trunc(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
+    fn trunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
         self.count_insn("trunc");
         unsafe {
             llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty, noname())
         }
     }
 
-    fn sext(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
+    fn sext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
         self.count_insn("sext");
         unsafe {
             llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty, noname())
         }
     }
 
-    fn fptoui(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
+    fn fptoui(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
         self.count_insn("fptoui");
         unsafe {
             llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty, noname())
         }
     }
 
-    fn fptosi(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
+    fn fptosi(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
         self.count_insn("fptosi");
         unsafe {
             llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty,noname())
         }
     }
 
-    fn uitofp(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
+    fn uitofp(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
         self.count_insn("uitofp");
         unsafe {
             llvm::LLVMBuildUIToFP(self.llbuilder, val, dest_ty, noname())
         }
     }
 
-    fn sitofp(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
+    fn sitofp(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
         self.count_insn("sitofp");
         unsafe {
             llvm::LLVMBuildSIToFP(self.llbuilder, val, dest_ty, noname())
         }
     }
 
-    fn fptrunc(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
+    fn fptrunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
         self.count_insn("fptrunc");
         unsafe {
             llvm::LLVMBuildFPTrunc(self.llbuilder, val, dest_ty, noname())
         }
     }
 
-    fn fpext(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
+    fn fpext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
         self.count_insn("fpext");
         unsafe {
             llvm::LLVMBuildFPExt(self.llbuilder, val, dest_ty, noname())
         }
     }
 
-    fn ptrtoint(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
+    fn ptrtoint(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
         self.count_insn("ptrtoint");
         unsafe {
             llvm::LLVMBuildPtrToInt(self.llbuilder, val, dest_ty, noname())
         }
     }
 
-    fn inttoptr(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
+    fn inttoptr(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
         self.count_insn("inttoptr");
         unsafe {
             llvm::LLVMBuildIntToPtr(self.llbuilder, val, dest_ty, noname())
         }
     }
 
-    fn bitcast(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
+    fn bitcast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
         self.count_insn("bitcast");
         unsafe {
             llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty, noname())
@@ -784,14 +788,14 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
     }
 
 
-    fn intcast(&self, val: &'ll Value, dest_ty: &'ll Type, is_signed: bool) -> &'ll Value {
+    fn intcast(&mut self, val: &'ll Value, dest_ty: &'ll Type, is_signed: bool) -> &'ll Value {
         self.count_insn("intcast");
         unsafe {
             llvm::LLVMRustBuildIntCast(self.llbuilder, val, dest_ty, is_signed)
         }
     }
 
-    fn pointercast(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
+    fn pointercast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
         self.count_insn("pointercast");
         unsafe {
             llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty, noname())
@@ -799,7 +803,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
     }
 
     /* Comparisons */
-    fn icmp(&self, op: IntPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn icmp(&mut self, op: IntPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("icmp");
         let op = llvm::IntPredicate::from_generic(op);
         unsafe {
@@ -807,7 +811,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-    fn fcmp(&self, op: RealPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn fcmp(&mut self, op: RealPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("fcmp");
         unsafe {
             llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, noname())
@@ -815,14 +819,14 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
     }
 
     /* Miscellaneous instructions */
-    fn empty_phi(&self, ty: &'ll Type) -> &'ll Value {
+    fn empty_phi(&mut self, ty: &'ll Type) -> &'ll Value {
         self.count_insn("emptyphi");
         unsafe {
             llvm::LLVMBuildPhi(self.llbuilder, ty, noname())
         }
     }
 
-    fn phi(&self, ty: &'ll Type, vals: &[&'ll Value], bbs: &[&'ll BasicBlock]) -> &'ll Value {
+    fn phi(&mut self, ty: &'ll Type, vals: &[&'ll Value], bbs: &[&'ll BasicBlock]) -> &'ll Value {
         assert_eq!(vals.len(), bbs.len());
         let phi = self.empty_phi(ty);
         self.count_insn("addincoming");
@@ -834,7 +838,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-    fn inline_asm_call(&self, asm: *const c_char, cons: *const c_char,
+    fn inline_asm_call(&mut self, asm: *const c_char, cons: *const c_char,
                        inputs: &[&'ll Value], output: &'ll Type,
                        volatile: bool, alignstack: bool,
                        dia: syntax::ast::AsmDialect) -> Option<&'ll Value> {
@@ -867,7 +871,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-    fn memcpy(&self, dst: &'ll Value, dst_align: Align,
+    fn memcpy(&mut self, dst: &'ll Value, dst_align: Align,
                   src: &'ll Value, src_align: Align,
                   size: &'ll Value, flags: MemFlags) {
         if flags.contains(MemFlags::NONTEMPORAL) {
@@ -887,7 +891,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-    fn memmove(&self, dst: &'ll Value, dst_align: Align,
+    fn memmove(&mut self, dst: &'ll Value, dst_align: Align,
                   src: &'ll Value, src_align: Align,
                   size: &'ll Value, flags: MemFlags) {
         if flags.contains(MemFlags::NONTEMPORAL) {
@@ -908,7 +912,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
     }
 
     fn memset(
-        &self,
+        &mut self,
         ptr: &'ll Value,
         fill_byte: &'ll Value,
         size: &'ll Value,
@@ -924,14 +928,14 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         self.call(llintrinsicfn, &[ptr, fill_byte, size, align, volatile], None);
     }
 
-    fn minnum(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn minnum(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("minnum");
         unsafe {
             let instr = llvm::LLVMRustBuildMinNum(self.llbuilder, lhs, rhs);
             instr.expect("LLVMRustBuildMinNum is not available in LLVM version < 6.0")
         }
     }
-    fn maxnum(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
+    fn maxnum(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
         self.count_insn("maxnum");
         unsafe {
             let instr = llvm::LLVMRustBuildMaxNum(self.llbuilder, lhs, rhs);
@@ -940,7 +944,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
     }
 
     fn select(
-        &self, cond: &'ll Value,
+        &mut self, cond: &'ll Value,
         then_val: &'ll Value,
         else_val: &'ll Value,
     ) -> &'ll Value {
@@ -951,14 +955,14 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
     }
 
     #[allow(dead_code)]
-    fn va_arg(&self, list: &'ll Value, ty: &'ll Type) -> &'ll Value {
+    fn va_arg(&mut self, list: &'ll Value, ty: &'ll Type) -> &'ll Value {
         self.count_insn("vaarg");
         unsafe {
             llvm::LLVMBuildVAArg(self.llbuilder, list, ty, noname())
         }
     }
 
-    fn extract_element(&self, vec: &'ll Value, idx: &'ll Value) -> &'ll Value {
+    fn extract_element(&mut self, vec: &'ll Value, idx: &'ll Value) -> &'ll Value {
         self.count_insn("extractelement");
         unsafe {
             llvm::LLVMBuildExtractElement(self.llbuilder, vec, idx, noname())
@@ -966,7 +970,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
     }
 
     fn insert_element(
-        &self, vec: &'ll Value,
+        &mut self, vec: &'ll Value,
         elt: &'ll Value,
         idx: &'ll Value,
     ) -> &'ll Value {
@@ -976,14 +980,14 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-    fn shuffle_vector(&self, v1: &'ll Value, v2: &'ll Value, mask: &'ll Value) -> &'ll Value {
+    fn shuffle_vector(&mut self, v1: &'ll Value, v2: &'ll Value, mask: &'ll Value) -> &'ll Value {
         self.count_insn("shufflevector");
         unsafe {
             llvm::LLVMBuildShuffleVector(self.llbuilder, v1, v2, mask, noname())
         }
     }
 
-    fn vector_splat(&self, num_elts: usize, elt: &'ll Value) -> &'ll Value {
+    fn vector_splat(&mut self, num_elts: usize, elt: &'ll Value) -> &'ll Value {
         unsafe {
             let elt_ty = self.cx.val_ty(elt);
             let undef = llvm::LLVMGetUndef(self.cx().type_vector(elt_ty, num_elts as u64));
@@ -993,7 +997,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-    fn vector_reduce_fadd_fast(&self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
+    fn vector_reduce_fadd_fast(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
         self.count_insn("vector.reduce.fadd_fast");
         unsafe {
             // FIXME: add a non-fast math version once
@@ -1004,7 +1008,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
             instr
         }
     }
-    fn vector_reduce_fmul_fast(&self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
+    fn vector_reduce_fmul_fast(&mut self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
         self.count_insn("vector.reduce.fmul_fast");
         unsafe {
             // FIXME: add a non-fast math version once
@@ -1015,35 +1019,35 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
             instr
         }
     }
-    fn vector_reduce_add(&self, src: &'ll Value) -> &'ll Value {
+    fn vector_reduce_add(&mut self, src: &'ll Value) -> &'ll Value {
         self.count_insn("vector.reduce.add");
         unsafe { llvm::LLVMRustBuildVectorReduceAdd(self.llbuilder, src) }
     }
-    fn vector_reduce_mul(&self, src: &'ll Value) -> &'ll Value {
+    fn vector_reduce_mul(&mut self, src: &'ll Value) -> &'ll Value {
         self.count_insn("vector.reduce.mul");
         unsafe { llvm::LLVMRustBuildVectorReduceMul(self.llbuilder, src) }
     }
-    fn vector_reduce_and(&self, src: &'ll Value) -> &'ll Value {
+    fn vector_reduce_and(&mut self, src: &'ll Value) -> &'ll Value {
         self.count_insn("vector.reduce.and");
         unsafe { llvm::LLVMRustBuildVectorReduceAnd(self.llbuilder, src) }
     }
-    fn vector_reduce_or(&self, src: &'ll Value) -> &'ll Value {
+    fn vector_reduce_or(&mut self, src: &'ll Value) -> &'ll Value {
         self.count_insn("vector.reduce.or");
         unsafe { llvm::LLVMRustBuildVectorReduceOr(self.llbuilder, src) }
     }
-    fn vector_reduce_xor(&self, src: &'ll Value) -> &'ll Value {
+    fn vector_reduce_xor(&mut self, src: &'ll Value) -> &'ll Value {
         self.count_insn("vector.reduce.xor");
         unsafe { llvm::LLVMRustBuildVectorReduceXor(self.llbuilder, src) }
     }
-    fn vector_reduce_fmin(&self, src: &'ll Value) -> &'ll Value {
+    fn vector_reduce_fmin(&mut self, src: &'ll Value) -> &'ll Value {
         self.count_insn("vector.reduce.fmin");
         unsafe { llvm::LLVMRustBuildVectorReduceFMin(self.llbuilder, src, /*NoNaNs:*/ false) }
     }
-    fn vector_reduce_fmax(&self, src: &'ll Value) -> &'ll Value {
+    fn vector_reduce_fmax(&mut self, src: &'ll Value) -> &'ll Value {
         self.count_insn("vector.reduce.fmax");
         unsafe { llvm::LLVMRustBuildVectorReduceFMax(self.llbuilder, src, /*NoNaNs:*/ false) }
     }
-    fn vector_reduce_fmin_fast(&self, src: &'ll Value) -> &'ll Value {
+    fn vector_reduce_fmin_fast(&mut self, src: &'ll Value) -> &'ll Value {
         self.count_insn("vector.reduce.fmin_fast");
         unsafe {
             let instr = llvm::LLVMRustBuildVectorReduceFMin(self.llbuilder, src, /*NoNaNs:*/ true);
@@ -1051,7 +1055,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
             instr
         }
     }
-    fn vector_reduce_fmax_fast(&self, src: &'ll Value) -> &'ll Value {
+    fn vector_reduce_fmax_fast(&mut self, src: &'ll Value) -> &'ll Value {
         self.count_insn("vector.reduce.fmax_fast");
         unsafe {
             let instr = llvm::LLVMRustBuildVectorReduceFMax(self.llbuilder, src, /*NoNaNs:*/ true);
@@ -1059,16 +1063,16 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
             instr
         }
     }
-    fn vector_reduce_min(&self, src: &'ll Value, is_signed: bool) -> &'ll Value {
+    fn vector_reduce_min(&mut self, src: &'ll Value, is_signed: bool) -> &'ll Value {
         self.count_insn("vector.reduce.min");
         unsafe { llvm::LLVMRustBuildVectorReduceMin(self.llbuilder, src, is_signed) }
     }
-    fn vector_reduce_max(&self, src: &'ll Value, is_signed: bool) -> &'ll Value {
+    fn vector_reduce_max(&mut self, src: &'ll Value, is_signed: bool) -> &'ll Value {
         self.count_insn("vector.reduce.max");
         unsafe { llvm::LLVMRustBuildVectorReduceMax(self.llbuilder, src, is_signed) }
     }
 
-    fn extract_value(&self, agg_val: &'ll Value, idx: u64) -> &'ll Value {
+    fn extract_value(&mut self, agg_val: &'ll Value, idx: u64) -> &'ll Value {
         self.count_insn("extractvalue");
         assert_eq!(idx as c_uint as u64, idx);
         unsafe {
@@ -1076,7 +1080,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-    fn insert_value(&self, agg_val: &'ll Value, elt: &'ll Value,
+    fn insert_value(&mut self, agg_val: &'ll Value, elt: &'ll Value,
                        idx: u64) -> &'ll Value {
         self.count_insn("insertvalue");
         assert_eq!(idx as c_uint as u64, idx);
@@ -1086,7 +1090,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-    fn landing_pad(&self, ty: &'ll Type, pers_fn: &'ll Value,
+    fn landing_pad(&mut self, ty: &'ll Type, pers_fn: &'ll Value,
                        num_clauses: usize) -> &'ll Value {
         self.count_insn("landingpad");
         unsafe {
@@ -1095,27 +1099,27 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-    fn add_clause(&self, landing_pad: &'ll Value, clause: &'ll Value) {
+    fn add_clause(&mut self, landing_pad: &'ll Value, clause: &'ll Value) {
         unsafe {
             llvm::LLVMAddClause(landing_pad, clause);
         }
     }
 
-    fn set_cleanup(&self, landing_pad: &'ll Value) {
+    fn set_cleanup(&mut self, landing_pad: &'ll Value) {
         self.count_insn("setcleanup");
         unsafe {
             llvm::LLVMSetCleanup(landing_pad, llvm::True);
         }
     }
 
-    fn resume(&self, exn: &'ll Value) -> &'ll Value {
+    fn resume(&mut self, exn: &'ll Value) -> &'ll Value {
         self.count_insn("resume");
         unsafe {
             llvm::LLVMBuildResume(self.llbuilder, exn)
         }
     }
 
-    fn cleanup_pad(&self,
+    fn cleanup_pad(&mut self,
                        parent: Option<&'ll Value>,
                        args: &[&'ll Value]) -> Funclet<'ll> {
         self.count_insn("cleanuppad");
@@ -1131,7 +1135,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
     }
 
     fn cleanup_ret(
-        &self, funclet: &Funclet<'ll>,
+        &mut self, funclet: &Funclet<'ll>,
         unwind: Option<&'ll BasicBlock>,
     ) -> &'ll Value {
         self.count_insn("cleanupret");
@@ -1141,7 +1145,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         ret.expect("LLVM does not have support for cleanupret")
     }
 
-    fn catch_pad(&self,
+    fn catch_pad(&mut self,
                      parent: &'ll Value,
                      args: &[&'ll Value]) -> Funclet<'ll> {
         self.count_insn("catchpad");
@@ -1154,7 +1158,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         Funclet::new(ret.expect("LLVM does not have support for catchpad"))
     }
 
-    fn catch_ret(&self, funclet: &Funclet<'ll>, unwind: &'ll BasicBlock) -> &'ll Value {
+    fn catch_ret(&mut self, funclet: &Funclet<'ll>, unwind: &'ll BasicBlock) -> &'ll Value {
         self.count_insn("catchret");
         let ret = unsafe {
             llvm::LLVMRustBuildCatchRet(self.llbuilder, funclet.cleanuppad(), unwind)
@@ -1163,7 +1167,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
     }
 
     fn catch_switch(
-        &self,
+        &mut self,
         parent: Option<&'ll Value>,
         unwind: Option<&'ll BasicBlock>,
         num_handlers: usize,
@@ -1178,13 +1182,13 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         ret.expect("LLVM does not have support for catchswitch")
     }
 
-    fn add_handler(&self, catch_switch: &'ll Value, handler: &'ll BasicBlock) {
+    fn add_handler(&mut self, catch_switch: &'ll Value, handler: &'ll BasicBlock) {
         unsafe {
             llvm::LLVMRustAddHandler(catch_switch, handler);
         }
     }
 
-    fn set_personality_fn(&self, personality: &'ll Value) {
+    fn set_personality_fn(&mut self, personality: &'ll Value) {
         unsafe {
             llvm::LLVMSetPersonalityFn(self.llfn(), personality);
         }
@@ -1192,7 +1196,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
 
     // Atomic Operations
     fn atomic_cmpxchg(
-        &self,
+        &mut self,
         dst: &'ll Value,
         cmp: &'ll Value,
         src: &'ll Value,
@@ -1214,7 +1218,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
     fn atomic_rmw(
-        &self,
+        &mut self,
         op: rustc_codegen_ssa::common::AtomicRmwBinOp,
         dst: &'ll Value,
         src: &'ll Value,
@@ -1232,7 +1236,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
     }
 
     fn atomic_fence(
-        &self,
+        &mut self,
         order: rustc_codegen_ssa::common::AtomicOrdering,
         scope: rustc_codegen_ssa::common::SynchronizationScope
     ) {
@@ -1245,27 +1249,27 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-    fn add_case(&self, s: &'ll Value, on_val: &'ll Value, dest: &'ll BasicBlock) {
+    fn add_case(&mut self, s: &'ll Value, on_val: &'ll Value, dest: &'ll BasicBlock) {
         unsafe {
             llvm::LLVMAddCase(s, on_val, dest)
         }
     }
 
-    fn add_incoming_to_phi(&self, phi: &'ll Value, val: &'ll Value, bb: &'ll BasicBlock) {
+    fn add_incoming_to_phi(&mut self, phi: &'ll Value, val: &'ll Value, bb: &'ll BasicBlock) {
         self.count_insn("addincoming");
         unsafe {
             llvm::LLVMAddIncoming(phi, &val, &bb, 1 as c_uint);
         }
     }
 
-    fn set_invariant_load(&self, load: &'ll Value) {
+    fn set_invariant_load(&mut self, load: &'ll Value) {
         unsafe {
             llvm::LLVMSetMetadata(load, llvm::MD_invariant_load as c_uint,
                                   llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0));
         }
     }
 
-    fn check_store<'b>(&self,
+    fn check_store<'b>(&mut self,
                        val: &'ll Value,
                        ptr: &'ll Value) -> &'ll Value {
         let dest_ptr_ty = self.cx.val_ty(ptr);
@@ -1284,7 +1288,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-    fn check_call<'b>(&self,
+    fn check_call<'b>(&mut self,
                       typ: &str,
                       llfn: &'ll Value,
                       args: &'b [&'ll Value]) -> Cow<'b, [&'ll Value]> {
@@ -1326,15 +1330,15 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         Cow::Owned(casted_args)
     }
 
-    fn lifetime_start(&self, ptr: &'ll Value, size: Size) {
+    fn lifetime_start(&mut self, ptr: &'ll Value, size: Size) {
         self.call_lifetime_intrinsic("llvm.lifetime.start", ptr, size);
     }
 
-    fn lifetime_end(&self, ptr: &'ll Value, size: Size) {
+    fn lifetime_end(&mut self, ptr: &'ll Value, size: Size) {
         self.call_lifetime_intrinsic("llvm.lifetime.end", ptr, size);
     }
 
-    fn call_lifetime_intrinsic(&self, intrinsic: &str, ptr: &'ll Value, size: Size) {
+    fn call_lifetime_intrinsic(&mut self, intrinsic: &str, ptr: &'ll Value, size: Size) {
         if self.cx.sess().opts.optimize == config::OptLevel::No {
             return;
         }
@@ -1351,7 +1355,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
     }
 
     fn call(
-        &self,
+        &mut self,
         llfn: &'ll Value,
         args: &[&'ll Value],
         funclet: Option<&Funclet<'ll>>,
@@ -1377,14 +1381,14 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         }
     }
 
-    fn zext(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
+    fn zext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
         self.count_insn("zext");
         unsafe {
             llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty, noname())
         }
     }
 
-    fn struct_gep(&self, ptr: &'ll Value, idx: u64) -> &'ll Value {
+    fn struct_gep(&mut self, ptr: &'ll Value, idx: u64) -> &'ll Value {
         self.count_insn("structgep");
         assert_eq!(idx as c_uint as u64, idx);
         unsafe {
@@ -1396,13 +1400,13 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
         self.cx
     }
 
-    fn delete_basic_block(&self, bb: &'ll BasicBlock) {
+    fn delete_basic_block(&mut self, bb: &'ll BasicBlock) {
         unsafe {
             llvm::LLVMDeleteBasicBlock(bb);
         }
     }
 
-    fn do_not_inline(&self, llret: &'ll Value) {
+    fn do_not_inline(&mut self, llret: &'ll Value) {
         llvm::Attribute::NoInline.apply_callsite(llvm::AttributePlace::Function, llret);
     }
 }
diff --git a/src/librustc_codegen_llvm/debuginfo/gdb.rs b/src/librustc_codegen_llvm/debuginfo/gdb.rs
index 26e359f39cf..d6a7f04e163 100644
--- a/src/librustc_codegen_llvm/debuginfo/gdb.rs
+++ b/src/librustc_codegen_llvm/debuginfo/gdb.rs
@@ -23,7 +23,7 @@ use syntax::attr;
 
 /// Inserts a side-effect free instruction sequence that makes sure that the
 /// .debug_gdb_scripts global is referenced, so it isn't removed by the linker.
-pub fn insert_reference_to_gdb_debug_scripts_section_global(bx: &Builder) {
+pub fn insert_reference_to_gdb_debug_scripts_section_global(bx: &mut Builder) {
     if needs_gdb_debug_scripts_section(bx.cx()) {
         let gdb_debug_scripts_section = get_or_insert_gdb_debug_scripts_section_global(bx.cx());
         // Load just the first byte as that's all that's necessary to force
diff --git a/src/librustc_codegen_llvm/debuginfo/mod.rs b/src/librustc_codegen_llvm/debuginfo/mod.rs
index 67ef7918668..3a7e393f1dc 100644
--- a/src/librustc_codegen_llvm/debuginfo/mod.rs
+++ b/src/librustc_codegen_llvm/debuginfo/mod.rs
@@ -159,7 +159,7 @@ pub fn finalize(cx: &CodegenCx) {
 
 impl DebugInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
     fn declare_local(
-        &self,
+        &mut self,
         dbg_context: &FunctionDebugContext<&'ll DISubprogram>,
         variable_name: ast::Name,
         variable_type: Ty<'tcx>,
@@ -225,14 +225,14 @@ impl DebugInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
     }
 
     fn set_source_location(
-        &self,
+        &mut self,
         debug_context: &FunctionDebugContext<&'ll DISubprogram>,
         scope: Option<&'ll DIScope>,
         span: Span,
     ) {
         set_source_location(debug_context, &self, scope, span)
     }
-    fn insert_reference_to_gdb_debug_scripts_section_global(&self) {
+    fn insert_reference_to_gdb_debug_scripts_section_global(&mut self) {
         gdb::insert_reference_to_gdb_debug_scripts_section_global(self)
     }
 }
diff --git a/src/librustc_codegen_llvm/intrinsic.rs b/src/librustc_codegen_llvm/intrinsic.rs
index 7ea14055c3a..c2f00d60af2 100644
--- a/src/librustc_codegen_llvm/intrinsic.rs
+++ b/src/librustc_codegen_llvm/intrinsic.rs
@@ -89,15 +89,14 @@ fn get_simple_intrinsic(cx: &CodegenCx<'ll, '_>, name: &str) -> Option<&'ll Valu
 
 impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
     fn codegen_intrinsic_call(
-        &self,
+        &mut self,
         callee_ty: Ty<'tcx>,
         fn_ty: &FnType<'tcx, Ty<'tcx>>,
         args: &[OperandRef<'tcx, &'ll Value>],
         llresult: &'ll Value,
         span: Span,
     ) {
-        let cx = self.cx();
-        let tcx = cx.tcx;
+        let tcx = self.cx().tcx;
 
         let (def_id, substs) = match callee_ty.sty {
             ty::FnDef(def_id, substs) => (def_id, substs),
@@ -110,10 +109,10 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
         let ret_ty = sig.output();
         let name = &*tcx.item_name(def_id).as_str();
 
-        let llret_ty = cx.layout_of(ret_ty).llvm_type(cx);
+        let llret_ty = self.cx().layout_of(ret_ty).llvm_type(self.cx());
         let result = PlaceRef::new_sized(llresult, fn_ty.ret.layout, fn_ty.ret.layout.align);
 
-        let simple = get_simple_intrinsic(cx, name);
+        let simple = get_simple_intrinsic(self.cx(), name);
         let llval = match name {
             _ if simple.is_some() => {
                 self.call(simple.unwrap(),
@@ -124,15 +123,15 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
                 return;
             },
             "likely" => {
-                let expect = cx.get_intrinsic(&("llvm.expect.i1"));
-                self.call(expect, &[args[0].immediate(), cx.const_bool(true)], None)
+                let expect = self.cx().get_intrinsic(&("llvm.expect.i1"));
+                self.call(expect, &[args[0].immediate(), self.cx().const_bool(true)], None)
             }
             "unlikely" => {
-                let expect = cx.get_intrinsic(&("llvm.expect.i1"));
-                self.call(expect, &[args[0].immediate(), cx.const_bool(false)], None)
+                let expect = self.cx().get_intrinsic(&("llvm.expect.i1"));
+                self.call(expect, &[args[0].immediate(), self.cx().const_bool(false)], None)
             }
             "try" => {
-                try_intrinsic(self, cx,
+                try_intrinsic(self,
                               args[0].immediate(),
                               args[1].immediate(),
                               args[2].immediate(),
@@ -140,12 +139,12 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
                 return;
             }
             "breakpoint" => {
-                let llfn = cx.get_intrinsic(&("llvm.debugtrap"));
+                let llfn = self.cx().get_intrinsic(&("llvm.debugtrap"));
                 self.call(llfn, &[], None)
             }
             "size_of" => {
                 let tp_ty = substs.type_at(0);
-                cx.const_usize(cx.size_of(tp_ty).bytes())
+                self.cx().const_usize(self.cx().size_of(tp_ty).bytes())
             }
             "size_of_val" => {
                 let tp_ty = substs.type_at(0);
@@ -154,12 +153,12 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
                         glue::size_and_align_of_dst(self, tp_ty, Some(meta));
                     llsize
                 } else {
-                    cx.const_usize(cx.size_of(tp_ty).bytes())
+                    self.cx().const_usize(self.cx().size_of(tp_ty).bytes())
                 }
             }
             "min_align_of" => {
                 let tp_ty = substs.type_at(0);
-                cx.const_usize(cx.align_of(tp_ty).abi())
+                self.cx().const_usize(self.cx().align_of(tp_ty).abi())
             }
             "min_align_of_val" => {
                 let tp_ty = substs.type_at(0);
@@ -168,35 +167,35 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
                         glue::size_and_align_of_dst(self, tp_ty, Some(meta));
                     llalign
                 } else {
-                    cx.const_usize(cx.align_of(tp_ty).abi())
+                    self.cx().const_usize(self.cx().align_of(tp_ty).abi())
                 }
             }
             "pref_align_of" => {
                 let tp_ty = substs.type_at(0);
-                cx.const_usize(cx.align_of(tp_ty).pref())
+                self.cx().const_usize(self.cx().align_of(tp_ty).pref())
             }
             "type_name" => {
                 let tp_ty = substs.type_at(0);
                 let ty_name = Symbol::intern(&tp_ty.to_string()).as_str();
-                cx.const_str_slice(ty_name)
+                self.cx().const_str_slice(ty_name)
             }
             "type_id" => {
-                cx.const_u64(cx.tcx.type_id_hash(substs.type_at(0)))
+                self.cx().const_u64(self.cx().tcx.type_id_hash(substs.type_at(0)))
             }
             "init" => {
                 let ty = substs.type_at(0);
-                if !cx.layout_of(ty).is_zst() {
+                if !self.cx().layout_of(ty).is_zst() {
                     // Just zero out the stack slot.
                     // If we store a zero constant, LLVM will drown in vreg allocation for large
                     // data structures, and the generated code will be awful. (A telltale sign of
                     // this is large quantities of `mov [byte ptr foo],0` in the generated code.)
                     memset_intrinsic(
-                        &self,
+                        self,
                         false,
                         ty,
                         llresult,
-                        cx.const_u8(0),
-                        cx.const_usize(1)
+                        self.cx().const_u8(0),
+                        self.cx().const_usize(1)
                     );
                 }
                 return;
@@ -208,7 +207,7 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
             "needs_drop" => {
                 let tp_ty = substs.type_at(0);
 
-                cx.const_bool(cx.type_needs_drop(tp_ty))
+                self.cx().const_bool(self.cx().type_needs_drop(tp_ty))
             }
             "offset" => {
                 let ptr = args[0].immediate();
@@ -222,33 +221,33 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
             }
 
             "copy_nonoverlapping" => {
-                copy_intrinsic(&self, false, false, substs.type_at(0),
+                copy_intrinsic(self, false, false, substs.type_at(0),
                                args[1].immediate(), args[0].immediate(), args[2].immediate());
                 return;
             }
             "copy" => {
-                copy_intrinsic(&self, true, false, substs.type_at(0),
+                copy_intrinsic(self, true, false, substs.type_at(0),
                                args[1].immediate(), args[0].immediate(), args[2].immediate());
                 return;
             }
             "write_bytes" => {
-                memset_intrinsic(&self, false, substs.type_at(0),
+                memset_intrinsic(self, false, substs.type_at(0),
                                  args[0].immediate(), args[1].immediate(), args[2].immediate());
                 return;
             }
 
             "volatile_copy_nonoverlapping_memory" => {
-                copy_intrinsic(&self, false, true, substs.type_at(0),
+                copy_intrinsic(self, false, true, substs.type_at(0),
                                args[0].immediate(), args[1].immediate(), args[2].immediate());
                 return;
             }
             "volatile_copy_memory" => {
-                copy_intrinsic(&self, true, true, substs.type_at(0),
+                copy_intrinsic(self, true, true, substs.type_at(0),
                                args[0].immediate(), args[1].immediate(), args[2].immediate());
                 return;
             }
             "volatile_set_memory" => {
-                memset_intrinsic(&self, true, substs.type_at(0),
+                memset_intrinsic(self, true, substs.type_at(0),
                                  args[0].immediate(), args[1].immediate(), args[2].immediate());
                 return;
             }
@@ -256,32 +255,32 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
                 let tp_ty = substs.type_at(0);
                 let mut ptr = args[0].immediate();
                 if let PassMode::Cast(ty) = fn_ty.ret.mode {
-                    ptr = self.pointercast(ptr, cx.type_ptr_to(ty.llvm_type(cx)));
+                    ptr = self.pointercast(ptr, self.cx().type_ptr_to(ty.llvm_type(self.cx())));
                 }
                 let load = self.volatile_load(ptr);
                 let align = if name == "unaligned_volatile_load" {
                     1
                 } else {
-                    cx.align_of(tp_ty).abi() as u32
+                    self.cx().align_of(tp_ty).abi() as u32
                 };
                 unsafe {
                     llvm::LLVMSetAlignment(load, align);
                 }
-                to_immediate(self, load, cx.layout_of(tp_ty))
+                to_immediate(self, load, self.cx().layout_of(tp_ty))
             },
             "volatile_store" => {
-                let dst = args[0].deref(cx);
+                let dst = args[0].deref(self.cx());
                 args[1].val.volatile_store(self, dst);
                 return;
             },
             "unaligned_volatile_store" => {
-                let dst = args[0].deref(cx);
+                let dst = args[0].deref(self.cx());
                 args[1].val.unaligned_volatile_store(self, dst);
                 return;
             },
             "prefetch_read_data" | "prefetch_write_data" |
             "prefetch_read_instruction" | "prefetch_write_instruction" => {
-                let expect = cx.get_intrinsic(&("llvm.prefetch"));
+                let expect = self.cx().get_intrinsic(&("llvm.prefetch"));
                 let (rw, cache_type) = match name {
                     "prefetch_read_data" => (0, 1),
                     "prefetch_write_data" => (1, 1),
@@ -291,9 +290,9 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
                 };
                 self.call(expect, &[
                     args[0].immediate(),
-                    cx.const_i32(rw),
+                    self.cx().const_i32(rw),
                     args[1].immediate(),
-                    cx.const_i32(cache_type)
+                    self.cx().const_i32(cache_type)
                 ], None)
             },
             "ctlz" | "ctlz_nonzero" | "cttz" | "cttz_nonzero" | "ctpop" | "bswap" |
@@ -302,22 +301,24 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
             "unchecked_div" | "unchecked_rem" | "unchecked_shl" | "unchecked_shr" | "exact_div" |
             "rotate_left" | "rotate_right" => {
                 let ty = arg_tys[0];
-                match int_type_width_signed(ty, cx) {
+                match int_type_width_signed(ty, self.cx()) {
                     Some((width, signed)) =>
                         match name {
                             "ctlz" | "cttz" => {
-                                let y = cx.const_bool(false);
-                                let llfn = cx.get_intrinsic(&format!("llvm.{}.i{}", name, width));
+                                let y = self.cx().const_bool(false);
+                                let llfn = self.cx().get_intrinsic(
+                                    &format!("llvm.{}.i{}", name, width),
+                                );
                                 self.call(llfn, &[args[0].immediate(), y], None)
                             }
                             "ctlz_nonzero" | "cttz_nonzero" => {
-                                let y = cx.const_bool(true);
+                                let y = self.cx().const_bool(true);
                                 let llvm_name = &format!("llvm.{}.i{}", &name[..4], width);
-                                let llfn = cx.get_intrinsic(llvm_name);
+                                let llfn = self.cx().get_intrinsic(llvm_name);
                                 self.call(llfn, &[args[0].immediate(), y], None)
                             }
                             "ctpop" => self.call(
-                                cx.get_intrinsic(&format!("llvm.ctpop.i{}", width)),
+                                self.cx().get_intrinsic(&format!("llvm.ctpop.i{}", width)),
                                 &[args[0].immediate()],
                                 None
                             ),
@@ -325,19 +326,29 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
                                 if width == 8 {
                                     args[0].immediate() // byte swap a u8/i8 is just a no-op
                                 } else {
-                                    self.call(cx.get_intrinsic(&format!("llvm.bswap.i{}", width)),
-                                            &[args[0].immediate()], None)
+                                    self.call(
+                                        self.cx().get_intrinsic(
+                                            &format!("llvm.bswap.i{}", width),
+                                        ),
+                                        &[args[0].immediate()],
+                                        None,
+                                    )
                                 }
                             }
                             "bitreverse" => {
-                                self.call(cx.get_intrinsic(&format!("llvm.bitreverse.i{}", width)),
-                                    &[args[0].immediate()], None)
+                                self.call(
+                                    self.cx().get_intrinsic(
+                                        &format!("llvm.bitreverse.i{}", width),
+                                    ),
+                                    &[args[0].immediate()],
+                                    None,
+                                )
                             }
                             "add_with_overflow" | "sub_with_overflow" | "mul_with_overflow" => {
                                 let intrinsic = format!("llvm.{}{}.with.overflow.i{}",
                                                         if signed { 's' } else { 'u' },
                                                         &name[..3], width);
-                                let llfn = cx.get_intrinsic(&intrinsic);
+                                let llfn = self.cx().get_intrinsic(&intrinsic);
 
                                 // Convert `i1` to a `bool`, and write it to the out parameter
                                 let pair = self.call(llfn, &[
@@ -345,10 +356,8 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
                                     args[1].immediate()
                                 ], None);
                                 let val = self.extract_value(pair, 0);
-                                let overflow = self.zext(
-                                    self.extract_value(pair, 1),
-                                    cx.type_bool()
-                                );
+                                let overflow = self.extract_value(pair, 1);
+                                let overflow = self.zext(overflow, self.cx().type_bool());
 
                                 let dest = result.project_field(self, 0);
                                 self.store(val, dest.llval, dest.align);
@@ -393,14 +402,18 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
                                     // rotate = funnel shift with first two args the same
                                     let llvm_name = &format!("llvm.fsh{}.i{}",
                                                             if is_left { 'l' } else { 'r' }, width);
-                                    let llfn = cx.get_intrinsic(llvm_name);
+                                    let llfn = self.cx().get_intrinsic(llvm_name);
                                     self.call(llfn, &[val, val, raw_shift], None)
                                 } else {
                                     // rotate_left: (X << (S % BW)) | (X >> ((BW - S) % BW))
                                     // rotate_right: (X << ((BW - S) % BW)) | (X >> (S % BW))
-                                    let width = cx.const_uint(cx.type_ix(width), width);
+                                    let width = self.cx().const_uint(
+                                        self.cx().type_ix(width),
+                                        width,
+                                    );
                                     let shift = self.urem(raw_shift, width);
-                                    let inv_shift = self.urem(self.sub(width, raw_shift), width);
+                                    let width_minus_raw_shift = self.sub(width, raw_shift);
+                                    let inv_shift = self.urem(width_minus_raw_shift, width);
                                     let shift1 = self.shl(
                                         val,
                                         if is_left { shift } else { inv_shift },
@@ -448,11 +461,11 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
             },
 
             "discriminant_value" => {
-                args[0].deref(cx).codegen_get_discr(self, ret_ty)
+                args[0].deref(self.cx()).codegen_get_discr(self, ret_ty)
             }
 
             name if name.starts_with("simd_") => {
-                match generic_simd_intrinsic(&self, name,
+                match generic_simd_intrinsic(self, name,
                                              callee_ty,
                                              args,
                                              ret_ty, llret_ty,
@@ -483,16 +496,16 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
                             (SequentiallyConsistent, Monotonic),
                         "failacq" if is_cxchg =>
                             (SequentiallyConsistent, Acquire),
-                        _ => cx.sess().fatal("unknown ordering in atomic intrinsic")
+                        _ => self.cx().sess().fatal("unknown ordering in atomic intrinsic")
                     },
                     4 => match (split[2], split[3]) {
                         ("acq", "failrelaxed") if is_cxchg =>
                             (Acquire, Monotonic),
                         ("acqrel", "failrelaxed") if is_cxchg =>
                             (AcquireRelease, Monotonic),
-                        _ => cx.sess().fatal("unknown ordering in atomic intrinsic")
+                        _ => self.cx().sess().fatal("unknown ordering in atomic intrinsic")
                     },
-                    _ => cx.sess().fatal("Atomic intrinsic not in correct format"),
+                    _ => self.cx().sess().fatal("Atomic intrinsic not in correct format"),
                 };
 
                 let invalid_monomorphization = |ty| {
@@ -504,7 +517,7 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
                 match split[1] {
                     "cxchg" | "cxchgweak" => {
                         let ty = substs.type_at(0);
-                        if int_type_width_signed(ty, cx).is_some() {
+                        if int_type_width_signed(ty, self.cx()).is_some() {
                             let weak = split[1] == "cxchgweak";
                             let pair = self.atomic_cmpxchg(
                                 args[0].immediate(),
@@ -514,10 +527,8 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
                                 failorder,
                                 weak);
                             let val = self.extract_value(pair, 0);
-                            let success = self.zext(
-                                self.extract_value(pair, 1),
-                                cx.type_bool()
-                            );
+                            let success = self.extract_value(pair, 1);
+                            let success = self.zext(success, self.cx().type_bool());
 
                             let dest = result.project_field(self, 0);
                             self.store(val, dest.llval, dest.align);
@@ -531,8 +542,8 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
 
                     "load" => {
                         let ty = substs.type_at(0);
-                        if int_type_width_signed(ty, cx).is_some() {
-                            let size = cx.size_of(ty);
+                        if int_type_width_signed(ty, self.cx()).is_some() {
+                            let size = self.cx().size_of(ty);
                             self.atomic_load(args[0].immediate(), order, size)
                         } else {
                             return invalid_monomorphization(ty);
@@ -541,8 +552,8 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
 
                     "store" => {
                         let ty = substs.type_at(0);
-                        if int_type_width_signed(ty, cx).is_some() {
-                            let size = cx.size_of(ty);
+                        if int_type_width_signed(ty, self.cx()).is_some() {
+                            let size = self.cx().size_of(ty);
                             self.atomic_store(
                                 args[1].immediate(),
                                 args[0].immediate(),
@@ -579,11 +590,11 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
                             "min"   => AtomicRmwBinOp::AtomicMin,
                             "umax"  => AtomicRmwBinOp::AtomicUMax,
                             "umin"  => AtomicRmwBinOp::AtomicUMin,
-                            _ => cx.sess().fatal("unknown atomic operation")
+                            _ => self.cx().sess().fatal("unknown atomic operation")
                         };
 
                         let ty = substs.type_at(0);
-                        if int_type_width_signed(ty, cx).is_some() {
+                        if int_type_width_signed(ty, self.cx()).is_some() {
                             self.atomic_rmw(
                                 atom_op,
                                 args[0].immediate(),
@@ -598,7 +609,7 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
             }
 
             "nontemporal_store" => {
-                let dst = args[0].deref(cx);
+                let dst = args[0].deref(self.cx());
                 args[1].val.nontemporal_store(self, dst);
                 return;
             }
@@ -658,7 +669,7 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
                 // arguments to be truncated as needed and pointers to be
                 // cast.
                 fn modify_as_needed<'ll, 'tcx>(
-                    bx: &Builder<'_, 'll, 'tcx>,
+                    bx: &mut Builder<'_, 'll, 'tcx>,
                     t: &intrinsics::Type,
                     arg: &OperandRef<'tcx, &'ll Value>,
                 ) -> Vec<&'ll Value> {
@@ -677,7 +688,8 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
                             };
                             let arg = PlaceRef::new_sized(ptr, arg.layout, align);
                             (0..contents.len()).map(|i| {
-                                bx.load_operand(arg.project_field(bx, i)).immediate()
+                                let field = arg.project_field(bx, i);
+                                bx.load_operand(field).immediate()
                             }).collect()
                         }
                         intrinsics::Type::Pointer(_, Some(ref llvm_elem), _) => {
@@ -703,21 +715,21 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
 
 
                 let inputs = intr.inputs.iter()
-                                        .flat_map(|t| ty_to_type(cx, t))
+                                        .flat_map(|t| ty_to_type(self.cx(), t))
                                         .collect::<Vec<_>>();
 
-                let outputs = one(ty_to_type(cx, &intr.output));
+                let outputs = one(ty_to_type(self.cx(), &intr.output));
 
                 let llargs: Vec<_> = intr.inputs.iter().zip(args).flat_map(|(t, arg)| {
-                    modify_as_needed(&self, t, arg)
+                    modify_as_needed(self, t, arg)
                 }).collect();
                 assert_eq!(inputs.len(), llargs.len());
 
                 let val = match intr.definition {
                     intrinsics::IntrinsicDef::Named(name) => {
-                        let f = cx.declare_cfn(
+                        let f = self.cx().declare_cfn(
                             name,
-                            cx.type_func(&inputs, outputs),
+                            self.cx().type_func(&inputs, outputs),
                         );
                         self.call(f, &llargs, None)
                     }
@@ -742,7 +754,8 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
 
         if !fn_ty.ret.is_ignore() {
             if let PassMode::Cast(ty) = fn_ty.ret.mode {
-                let ptr = self.pointercast(result.llval, cx.type_ptr_to(ty.llvm_type(cx)));
+                let ptr_llty = self.cx().type_ptr_to(ty.llvm_type(self.cx()));
+                let ptr = self.pointercast(result.llval, ptr_llty);
                 self.store(llval, ptr, result.align);
             } else {
                 OperandRef::from_immediate_or_packed_pair(self, llval, result.layout)
@@ -753,7 +766,7 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
 }
 
 fn copy_intrinsic(
-    bx: &Builder<'a, 'll, 'tcx>,
+    bx: &mut Builder<'a, 'll, 'tcx>,
     allow_overlap: bool,
     volatile: bool,
     ty: Ty<'tcx>,
@@ -776,7 +789,7 @@ fn copy_intrinsic(
 }
 
 fn memset_intrinsic(
-    bx: &Builder<'a, 'll, 'tcx>,
+    bx: &mut Builder<'a, 'll, 'tcx>,
     volatile: bool,
     ty: Ty<'tcx>,
     dst: &'ll Value,
@@ -784,18 +797,17 @@ fn memset_intrinsic(
     count: &'ll Value
 ) {
     let (size, align) = bx.cx().size_and_align_of(ty);
-    let size = bx.cx().const_usize(size.bytes());
+    let size = bx.mul(bx.cx().const_usize(size.bytes()), count);
     let flags = if volatile {
         MemFlags::VOLATILE
     } else {
         MemFlags::empty()
     };
-    bx.memset(dst, val, bx.mul(size, count), align, flags);
+    bx.memset(dst, val, size, align, flags);
 }
 
 fn try_intrinsic(
-    bx: &Builder<'a, 'll, 'tcx>,
-    cx: &CodegenCx<'ll, 'tcx>,
+    bx: &mut Builder<'a, 'll, 'tcx>,
     func: &'ll Value,
     data: &'ll Value,
     local_ptr: &'ll Value,
@@ -804,11 +816,11 @@ fn try_intrinsic(
     if bx.cx().sess().no_landing_pads() {
         bx.call(func, &[data], None);
         let ptr_align = bx.tcx().data_layout.pointer_align;
-        bx.store(cx.const_null(cx.type_i8p()), dest, ptr_align);
+        bx.store(bx.cx().const_null(bx.cx().type_i8p()), dest, ptr_align);
     } else if wants_msvc_seh(bx.cx().sess()) {
-        codegen_msvc_try(bx, cx, func, data, local_ptr, dest);
+        codegen_msvc_try(bx, func, data, local_ptr, dest);
     } else {
-        codegen_gnu_try(bx, cx, func, data, local_ptr, dest);
+        codegen_gnu_try(bx, func, data, local_ptr, dest);
     }
 }
 
@@ -820,21 +832,18 @@ fn try_intrinsic(
 // writing, however, LLVM does not recommend the usage of these new instructions
 // as the old ones are still more optimized.
 fn codegen_msvc_try(
-    bx: &Builder<'a, 'll, 'tcx>,
-    cx: &CodegenCx<'ll, 'tcx>,
+    bx: &mut Builder<'a, 'll, 'tcx>,
     func: &'ll Value,
     data: &'ll Value,
     local_ptr: &'ll Value,
     dest: &'ll Value,
 ) {
-    let llfn = get_rust_try_fn(cx, &mut |bx| {
-        let cx = bx.cx();
-
+    let llfn = get_rust_try_fn(bx.cx(), &mut |mut bx| {
         bx.set_personality_fn(bx.cx().eh_personality());
 
         let mut normal = bx.build_sibling_block("normal");
-        let catchswitch = bx.build_sibling_block("catchswitch");
-        let catchpad = bx.build_sibling_block("catchpad");
+        let mut catchswitch = bx.build_sibling_block("catchswitch");
+        let mut catchpad = bx.build_sibling_block("catchpad");
         let mut caught = bx.build_sibling_block("caught");
 
         let func = llvm::get_param(bx.llfn(), 0);
@@ -880,34 +889,35 @@ fn codegen_msvc_try(
         //      }
         //
         // More information can be found in libstd's seh.rs implementation.
-        let i64p = cx.type_ptr_to(cx.type_i64());
+        let i64p = bx.cx().type_ptr_to(bx.cx().type_i64());
         let ptr_align = bx.tcx().data_layout.pointer_align;
         let slot = bx.alloca(i64p, "slot", ptr_align);
         bx.invoke(func, &[data], normal.llbb(), catchswitch.llbb(), None);
 
-        normal.ret(cx.const_i32(0));
+        normal.ret(bx.cx().const_i32(0));
 
         let cs = catchswitch.catch_switch(None, None, 1);
         catchswitch.add_handler(cs, catchpad.llbb());
 
-        let tcx = cx.tcx;
-        let tydesc = match tcx.lang_items().msvc_try_filter() {
-            Some(did) => cx.get_static(did),
+        let tydesc = match bx.tcx().lang_items().msvc_try_filter() {
+            Some(did) => bx.cx().get_static(did),
             None => bug!("msvc_try_filter not defined"),
         };
-        let funclet = catchpad.catch_pad(cs, &[tydesc, cx.const_i32(0), slot]);
+        let funclet = catchpad.catch_pad(cs, &[tydesc, bx.cx().const_i32(0), slot]);
         let addr = catchpad.load(slot, ptr_align);
 
         let i64_align = bx.tcx().data_layout.i64_align;
         let arg1 = catchpad.load(addr, i64_align);
-        let val1 = cx.const_i32(1);
-        let arg2 = catchpad.load(catchpad.inbounds_gep(addr, &[val1]), i64_align);
+        let val1 = bx.cx().const_i32(1);
+        let gep1 = catchpad.inbounds_gep(addr, &[val1]);
+        let arg2 = catchpad.load(gep1, i64_align);
         let local_ptr = catchpad.bitcast(local_ptr, i64p);
+        let gep2 = catchpad.inbounds_gep(local_ptr, &[val1]);
         catchpad.store(arg1, local_ptr, i64_align);
-        catchpad.store(arg2, catchpad.inbounds_gep(local_ptr, &[val1]), i64_align);
+        catchpad.store(arg2, gep2, i64_align);
         catchpad.catch_ret(&funclet, caught.llbb());
 
-        caught.ret(cx.const_i32(1));
+        caught.ret(bx.cx().const_i32(1));
     });
 
     // Note that no invoke is used here because by definition this function
@@ -929,16 +939,13 @@ fn codegen_msvc_try(
 // functions in play. By calling a shim we're guaranteed that our shim will have
 // the right personality function.
 fn codegen_gnu_try(
-    bx: &Builder<'a, 'll, 'tcx>,
-    cx: &CodegenCx<'ll, 'tcx>,
+    bx: &mut Builder<'a, 'll, 'tcx>,
     func: &'ll Value,
     data: &'ll Value,
     local_ptr: &'ll Value,
     dest: &'ll Value,
 ) {
-    let llfn = get_rust_try_fn(cx, &mut |bx| {
-        let cx = bx.cx();
-
+    let llfn = get_rust_try_fn(bx.cx(), &mut |mut bx| {
         // Codegens the shims described above:
         //
         //   bx:
@@ -963,7 +970,7 @@ fn codegen_gnu_try(
         let data = llvm::get_param(bx.llfn(), 1);
         let local_ptr = llvm::get_param(bx.llfn(), 2);
         bx.invoke(func, &[data], then.llbb(), catch.llbb(), None);
-        then.ret(cx.const_i32(0));
+        then.ret(bx.cx().const_i32(0));
 
         // Type indicator for the exception being thrown.
         //
@@ -971,13 +978,14 @@ fn codegen_gnu_try(
         // being thrown.  The second value is a "selector" indicating which of
         // the landing pad clauses the exception's type had been matched to.
         // rust_try ignores the selector.
-        let lpad_ty = cx.type_struct(&[cx.type_i8p(), cx.type_i32()], false);
+        let lpad_ty = bx.cx().type_struct(&[bx.cx().type_i8p(), bx.cx().type_i32()], false);
         let vals = catch.landing_pad(lpad_ty, bx.cx().eh_personality(), 1);
-        catch.add_clause(vals, bx.cx().const_null(cx.type_i8p()));
+        catch.add_clause(vals, bx.cx().const_null(bx.cx().type_i8p()));
         let ptr = catch.extract_value(vals, 0);
         let ptr_align = bx.tcx().data_layout.pointer_align;
-        catch.store(ptr, catch.bitcast(local_ptr, cx.type_ptr_to(cx.type_i8p())), ptr_align);
-        catch.ret(cx.const_i32(1));
+        let bitcast = catch.bitcast(local_ptr, bx.cx().type_ptr_to(bx.cx().type_i8p()));
+        catch.store(ptr, bitcast, ptr_align);
+        catch.ret(bx.cx().const_i32(1));
     });
 
     // Note that no invoke is used here because by definition this function
@@ -1043,7 +1051,7 @@ fn span_invalid_monomorphization_error(a: &Session, b: Span, c: &str) {
 }
 
 fn generic_simd_intrinsic(
-    bx: &Builder<'a, 'll, 'tcx>,
+    bx: &mut Builder<'a, 'll, 'tcx>,
     name: &str,
     callee_ty: Ty<'tcx>,
     args: &[OperandRef<'tcx, &'ll Value>],
@@ -1219,7 +1227,7 @@ fn generic_simd_intrinsic(
         in_elem: &::rustc::ty::TyS,
         in_ty: &::rustc::ty::TyS,
         in_len: usize,
-        bx: &Builder<'a, 'll, 'tcx>,
+        bx: &mut Builder<'a, 'll, 'tcx>,
         span: Span,
         args: &[OperandRef<'tcx, &'ll Value>],
     ) -> Result<&'ll Value, ()> {