1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use llvm::{AtomicRmwBinOp, AtomicOrdering, SynchronizationScope, AsmDialect};
12 use llvm::{IntPredicate, RealPredicate, False, OperandBundleDef};
13 use llvm::{self, BasicBlock};
17 use libc::{c_uint, c_char};
18 use rustc::ty::TyCtxt;
19 use rustc::ty::layout::{Align, Size};
20 use rustc::session::{config, Session};
21 use rustc_data_structures::small_c_str::SmallCStr;
27 // All Builders must have an llfn associated with them
29 pub struct Builder<'a, 'll: 'a, 'tcx: 'll> {
30 pub llbuilder: &'ll mut llvm::Builder<'ll>,
31 pub cx: &'a CodegenCx<'ll, 'tcx>,
34 impl Drop for Builder<'a, 'll, 'tcx> {
37 llvm::LLVMDisposeBuilder(&mut *(self.llbuilder as *mut _));
42 // This is a really awful way to get a zero-length c-string, but better (and a
43 // lot more efficient) than doing str::as_c_str("", ...) every time.
44 fn noname() -> *const c_char {
45 static CNULL: c_char = 0;
50 pub struct MemFlags: u8 {
51 const VOLATILE = 1 << 0;
52 const NONTEMPORAL = 1 << 1;
53 const UNALIGNED = 1 << 2;
57 impl Builder<'a, 'll, 'tcx> {
58 pub fn new_block<'b>(cx: &'a CodegenCx<'ll, 'tcx>, llfn: &'ll Value, name: &'b str) -> Self {
59 let bx = Builder::with_cx(cx);
61 let name = SmallCStr::new(name);
62 llvm::LLVMAppendBasicBlockInContext(
68 bx.position_at_end(llbb);
72 pub fn with_cx(cx: &'a CodegenCx<'ll, 'tcx>) -> Self {
73 // Create a fresh builder from the crate context.
74 let llbuilder = unsafe {
75 llvm::LLVMCreateBuilderInContext(cx.llcx)
83 pub fn build_sibling_block<'b>(&self, name: &'b str) -> Builder<'a, 'll, 'tcx> {
84 Builder::new_block(self.cx, self.llfn(), name)
87 pub fn sess(&self) -> &Session {
91 pub fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx> {
95 pub fn llfn(&self) -> &'ll Value {
97 llvm::LLVMGetBasicBlockParent(self.llbb())
101 pub fn llbb(&self) -> &'ll BasicBlock {
103 llvm::LLVMGetInsertBlock(self.llbuilder)
107 fn count_insn(&self, category: &str) {
108 if self.cx.sess().codegen_stats() {
109 self.cx.stats.borrow_mut().n_llvm_insns += 1;
111 if self.cx.sess().count_llvm_insns() {
115 .entry(category.to_string())
120 pub fn set_value_name(&self, value: &'ll Value, name: &str) {
121 let cname = SmallCStr::new(name);
123 llvm::LLVMSetValueName(value, cname.as_ptr());
127 pub fn position_at_end(&self, llbb: &'ll BasicBlock) {
129 llvm::LLVMPositionBuilderAtEnd(self.llbuilder, llbb);
133 pub fn position_at_start(&self, llbb: &'ll BasicBlock) {
135 llvm::LLVMRustPositionBuilderAtStart(self.llbuilder, llbb);
139 pub fn ret_void(&self) {
140 self.count_insn("retvoid");
142 llvm::LLVMBuildRetVoid(self.llbuilder);
146 pub fn ret(&self, v: &'ll Value) {
147 self.count_insn("ret");
149 llvm::LLVMBuildRet(self.llbuilder, v);
153 pub fn br(&self, dest: &'ll BasicBlock) {
154 self.count_insn("br");
156 llvm::LLVMBuildBr(self.llbuilder, dest);
163 then_llbb: &'ll BasicBlock,
164 else_llbb: &'ll BasicBlock,
166 self.count_insn("condbr");
168 llvm::LLVMBuildCondBr(self.llbuilder, cond, then_llbb, else_llbb);
175 else_llbb: &'ll BasicBlock,
179 llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, num_cases as c_uint)
186 then: &'ll BasicBlock,
187 catch: &'ll BasicBlock,
188 bundle: Option<&OperandBundleDef<'ll>>) -> &'ll Value {
189 self.count_insn("invoke");
191 debug!("Invoke {:?} with args ({:?})",
195 let args = self.check_call("invoke", llfn, args);
196 let bundle = bundle.map(|b| &*b.raw);
199 llvm::LLVMRustBuildInvoke(self.llbuilder,
202 args.len() as c_uint,
210 pub fn unreachable(&self) {
211 self.count_insn("unreachable");
213 llvm::LLVMBuildUnreachable(self.llbuilder);
218 pub fn add(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
219 self.count_insn("add");
221 llvm::LLVMBuildAdd(self.llbuilder, lhs, rhs, noname())
225 pub fn fadd(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
226 self.count_insn("fadd");
228 llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, noname())
232 pub fn fadd_fast(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
233 self.count_insn("fadd");
235 let instr = llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, noname());
236 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
241 pub fn sub(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
242 self.count_insn("sub");
244 llvm::LLVMBuildSub(self.llbuilder, lhs, rhs, noname())
248 pub fn fsub(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
249 self.count_insn("fsub");
251 llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, noname())
255 pub fn fsub_fast(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
256 self.count_insn("fsub");
258 let instr = llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, noname());
259 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
264 pub fn mul(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
265 self.count_insn("mul");
267 llvm::LLVMBuildMul(self.llbuilder, lhs, rhs, noname())
271 pub fn fmul(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
272 self.count_insn("fmul");
274 llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, noname())
278 pub fn fmul_fast(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
279 self.count_insn("fmul");
281 let instr = llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, noname());
282 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
288 pub fn udiv(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
289 self.count_insn("udiv");
291 llvm::LLVMBuildUDiv(self.llbuilder, lhs, rhs, noname())
295 pub fn exactudiv(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
296 self.count_insn("exactudiv");
298 llvm::LLVMBuildExactUDiv(self.llbuilder, lhs, rhs, noname())
302 pub fn sdiv(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
303 self.count_insn("sdiv");
305 llvm::LLVMBuildSDiv(self.llbuilder, lhs, rhs, noname())
309 pub fn exactsdiv(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
310 self.count_insn("exactsdiv");
312 llvm::LLVMBuildExactSDiv(self.llbuilder, lhs, rhs, noname())
316 pub fn fdiv(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
317 self.count_insn("fdiv");
319 llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, noname())
323 pub fn fdiv_fast(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
324 self.count_insn("fdiv");
326 let instr = llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, noname());
327 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
332 pub fn urem(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
333 self.count_insn("urem");
335 llvm::LLVMBuildURem(self.llbuilder, lhs, rhs, noname())
339 pub fn srem(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
340 self.count_insn("srem");
342 llvm::LLVMBuildSRem(self.llbuilder, lhs, rhs, noname())
346 pub fn frem(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
347 self.count_insn("frem");
349 llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, noname())
353 pub fn frem_fast(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
354 self.count_insn("frem");
356 let instr = llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, noname());
357 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
362 pub fn shl(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
363 self.count_insn("shl");
365 llvm::LLVMBuildShl(self.llbuilder, lhs, rhs, noname())
369 pub fn lshr(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
370 self.count_insn("lshr");
372 llvm::LLVMBuildLShr(self.llbuilder, lhs, rhs, noname())
376 pub fn ashr(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
377 self.count_insn("ashr");
379 llvm::LLVMBuildAShr(self.llbuilder, lhs, rhs, noname())
383 pub fn and(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
384 self.count_insn("and");
386 llvm::LLVMBuildAnd(self.llbuilder, lhs, rhs, noname())
390 pub fn or(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
391 self.count_insn("or");
393 llvm::LLVMBuildOr(self.llbuilder, lhs, rhs, noname())
397 pub fn xor(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
398 self.count_insn("xor");
400 llvm::LLVMBuildXor(self.llbuilder, lhs, rhs, noname())
404 pub fn neg(&self, v: &'ll Value) -> &'ll Value {
405 self.count_insn("neg");
407 llvm::LLVMBuildNeg(self.llbuilder, v, noname())
411 pub fn fneg(&self, v: &'ll Value) -> &'ll Value {
412 self.count_insn("fneg");
414 llvm::LLVMBuildFNeg(self.llbuilder, v, noname())
418 pub fn not(&self, v: &'ll Value) -> &'ll Value {
419 self.count_insn("not");
421 llvm::LLVMBuildNot(self.llbuilder, v, noname())
425 pub fn alloca(&self, ty: &'ll Type, name: &str, align: Align) -> &'ll Value {
426 let bx = Builder::with_cx(self.cx);
427 bx.position_at_start(unsafe {
428 llvm::LLVMGetFirstBasicBlock(self.llfn())
430 bx.dynamic_alloca(ty, name, align)
433 pub fn dynamic_alloca(&self, ty: &'ll Type, name: &str, align: Align) -> &'ll Value {
434 self.count_insn("alloca");
436 let alloca = if name.is_empty() {
437 llvm::LLVMBuildAlloca(self.llbuilder, ty, noname())
439 let name = SmallCStr::new(name);
440 llvm::LLVMBuildAlloca(self.llbuilder, ty,
443 llvm::LLVMSetAlignment(alloca, align.abi() as c_uint);
448 pub fn array_alloca(&self,
452 align: Align) -> &'ll Value {
453 self.count_insn("alloca");
455 let alloca = if name.is_empty() {
456 llvm::LLVMBuildArrayAlloca(self.llbuilder, ty, len, noname())
458 let name = SmallCStr::new(name);
459 llvm::LLVMBuildArrayAlloca(self.llbuilder, ty, len,
462 llvm::LLVMSetAlignment(alloca, align.abi() as c_uint);
467 pub fn load(&self, ptr: &'ll Value, align: Align) -> &'ll Value {
468 self.count_insn("load");
470 let load = llvm::LLVMBuildLoad(self.llbuilder, ptr, noname());
471 llvm::LLVMSetAlignment(load, align.abi() as c_uint);
476 pub fn volatile_load(&self, ptr: &'ll Value) -> &'ll Value {
477 self.count_insn("load.volatile");
479 let insn = llvm::LLVMBuildLoad(self.llbuilder, ptr, noname());
480 llvm::LLVMSetVolatile(insn, llvm::True);
485 pub fn atomic_load(&self, ptr: &'ll Value, order: AtomicOrdering, align: Align) -> &'ll Value {
486 self.count_insn("load.atomic");
488 let load = llvm::LLVMRustBuildAtomicLoad(self.llbuilder, ptr, noname(), order);
489 // FIXME(eddyb) Isn't it UB to use `pref` instead of `abi` here?
490 // However, 64-bit atomic loads on `i686-apple-darwin` appear to
491 // require `___atomic_load` with ABI-alignment, so it's staying.
492 llvm::LLVMSetAlignment(load, align.pref() as c_uint);
498 pub fn range_metadata(&self, load: &'ll Value, range: Range<u128>) {
499 if self.sess().target.target.arch == "amdgpu" {
500 // amdgpu/LLVM does something weird and thinks a i64 value is
501 // split into a v2i32, halving the bitwidth LLVM expects,
502 // tripping an assertion. So, for now, just disable this
508 let llty = val_ty(load);
510 C_uint_big(llty, range.start),
511 C_uint_big(llty, range.end)
514 llvm::LLVMSetMetadata(load, llvm::MD_range as c_uint,
515 llvm::LLVMMDNodeInContext(self.cx.llcx,
521 pub fn nonnull_metadata(&self, load: &'ll Value) {
523 llvm::LLVMSetMetadata(load, llvm::MD_nonnull as c_uint,
524 llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0));
528 pub fn store(&self, val: &'ll Value, ptr: &'ll Value, align: Align) -> &'ll Value {
529 self.store_with_flags(val, ptr, align, MemFlags::empty())
532 pub fn store_with_flags(
539 debug!("Store {:?} -> {:?} ({:?})", val, ptr, flags);
540 self.count_insn("store");
541 let ptr = self.check_store(val, ptr);
543 let store = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
544 let align = if flags.contains(MemFlags::UNALIGNED) {
547 align.abi() as c_uint
549 llvm::LLVMSetAlignment(store, align);
550 if flags.contains(MemFlags::VOLATILE) {
551 llvm::LLVMSetVolatile(store, llvm::True);
553 if flags.contains(MemFlags::NONTEMPORAL) {
554 // According to LLVM [1] building a nontemporal store must
555 // *always* point to a metadata value of the integer 1.
557 // [1]: http://llvm.org/docs/LangRef.html#store-instruction
558 let one = C_i32(self.cx, 1);
559 let node = llvm::LLVMMDNodeInContext(self.cx.llcx, &one, 1);
560 llvm::LLVMSetMetadata(store, llvm::MD_nontemporal as c_uint, node);
566 pub fn atomic_store(&self, val: &'ll Value, ptr: &'ll Value,
567 order: AtomicOrdering, align: Align) {
568 debug!("Store {:?} -> {:?}", val, ptr);
569 self.count_insn("store.atomic");
570 let ptr = self.check_store(val, ptr);
572 let store = llvm::LLVMRustBuildAtomicStore(self.llbuilder, val, ptr, order);
573 // FIXME(eddyb) Isn't it UB to use `pref` instead of `abi` here?
574 // Also see `atomic_load` for more context.
575 llvm::LLVMSetAlignment(store, align.pref() as c_uint);
579 pub fn gep(&self, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
580 self.count_insn("gep");
582 llvm::LLVMBuildGEP(self.llbuilder, ptr, indices.as_ptr(),
583 indices.len() as c_uint, noname())
587 pub fn inbounds_gep(&self, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
588 self.count_insn("inboundsgep");
590 llvm::LLVMBuildInBoundsGEP(
591 self.llbuilder, ptr, indices.as_ptr(), indices.len() as c_uint, noname())
595 pub fn struct_gep(&self, ptr: &'ll Value, idx: u64) -> &'ll Value {
596 self.count_insn("structgep");
597 assert_eq!(idx as c_uint as u64, idx);
599 llvm::LLVMBuildStructGEP(self.llbuilder, ptr, idx as c_uint, noname())
604 pub fn trunc(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
605 self.count_insn("trunc");
607 llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty, noname())
611 pub fn zext(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
612 self.count_insn("zext");
614 llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty, noname())
618 pub fn sext(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
619 self.count_insn("sext");
621 llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty, noname())
625 pub fn fptoui(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
626 self.count_insn("fptoui");
628 llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty, noname())
632 pub fn fptosi(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
633 self.count_insn("fptosi");
635 llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty,noname())
639 pub fn uitofp(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
640 self.count_insn("uitofp");
642 llvm::LLVMBuildUIToFP(self.llbuilder, val, dest_ty, noname())
646 pub fn sitofp(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
647 self.count_insn("sitofp");
649 llvm::LLVMBuildSIToFP(self.llbuilder, val, dest_ty, noname())
653 pub fn fptrunc(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
654 self.count_insn("fptrunc");
656 llvm::LLVMBuildFPTrunc(self.llbuilder, val, dest_ty, noname())
660 pub fn fpext(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
661 self.count_insn("fpext");
663 llvm::LLVMBuildFPExt(self.llbuilder, val, dest_ty, noname())
667 pub fn ptrtoint(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
668 self.count_insn("ptrtoint");
670 llvm::LLVMBuildPtrToInt(self.llbuilder, val, dest_ty, noname())
674 pub fn inttoptr(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
675 self.count_insn("inttoptr");
677 llvm::LLVMBuildIntToPtr(self.llbuilder, val, dest_ty, noname())
681 pub fn bitcast(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
682 self.count_insn("bitcast");
684 llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty, noname())
688 pub fn pointercast(&self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
689 self.count_insn("pointercast");
691 llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty, noname())
695 pub fn intcast(&self, val: &'ll Value, dest_ty: &'ll Type, is_signed: bool) -> &'ll Value {
696 self.count_insn("intcast");
698 llvm::LLVMRustBuildIntCast(self.llbuilder, val, dest_ty, is_signed)
703 pub fn icmp(&self, op: IntPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
704 self.count_insn("icmp");
706 llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, noname())
710 pub fn fcmp(&self, op: RealPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
711 self.count_insn("fcmp");
713 llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, noname())
717 /* Miscellaneous instructions */
718 pub fn empty_phi(&self, ty: &'ll Type) -> &'ll Value {
719 self.count_insn("emptyphi");
721 llvm::LLVMBuildPhi(self.llbuilder, ty, noname())
725 pub fn phi(&self, ty: &'ll Type, vals: &[&'ll Value], bbs: &[&'ll BasicBlock]) -> &'ll Value {
726 assert_eq!(vals.len(), bbs.len());
727 let phi = self.empty_phi(ty);
728 self.count_insn("addincoming");
730 llvm::LLVMAddIncoming(phi, vals.as_ptr(),
732 vals.len() as c_uint);
737 pub fn inline_asm_call(&self, asm: *const c_char, cons: *const c_char,
738 inputs: &[&'ll Value], output: &'ll Type,
739 volatile: bool, alignstack: bool,
740 dia: AsmDialect) -> Option<&'ll Value> {
741 self.count_insn("inlineasm");
743 let volatile = if volatile { llvm::True }
744 else { llvm::False };
745 let alignstack = if alignstack { llvm::True }
746 else { llvm::False };
748 let argtys = inputs.iter().map(|v| {
749 debug!("Asm Input Type: {:?}", *v);
751 }).collect::<Vec<_>>();
753 debug!("Asm Output Type: {:?}", output);
754 let fty = Type::func(&argtys[..], output);
756 // Ask LLVM to verify that the constraints are well-formed.
757 let constraints_ok = llvm::LLVMRustInlineAsmVerify(fty, cons);
758 debug!("Constraint verification result: {:?}", constraints_ok);
760 let v = llvm::LLVMRustInlineAsm(
761 fty, asm, cons, volatile, alignstack, dia);
762 Some(self.call(v, inputs, None))
764 // LLVM has detected an issue with our constraints, bail out
770 pub fn call(&self, llfn: &'ll Value, args: &[&'ll Value],
771 bundle: Option<&OperandBundleDef<'ll>>) -> &'ll Value {
772 self.count_insn("call");
774 debug!("Call {:?} with args ({:?})",
778 let args = self.check_call("call", llfn, args);
779 let bundle = bundle.map(|b| &*b.raw);
782 llvm::LLVMRustBuildCall(self.llbuilder, llfn, args.as_ptr(),
783 args.len() as c_uint, bundle, noname())
787 pub fn minnum(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
788 self.count_insn("minnum");
790 let instr = llvm::LLVMRustBuildMinNum(self.llbuilder, lhs, rhs);
791 instr.expect("LLVMRustBuildMinNum is not available in LLVM version < 6.0")
794 pub fn maxnum(&self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
795 self.count_insn("maxnum");
797 let instr = llvm::LLVMRustBuildMaxNum(self.llbuilder, lhs, rhs);
798 instr.expect("LLVMRustBuildMaxNum is not available in LLVM version < 6.0")
803 &self, cond: &'ll Value,
804 then_val: &'ll Value,
805 else_val: &'ll Value,
807 self.count_insn("select");
809 llvm::LLVMBuildSelect(self.llbuilder, cond, then_val, else_val, noname())
814 pub fn va_arg(&self, list: &'ll Value, ty: &'ll Type) -> &'ll Value {
815 self.count_insn("vaarg");
817 llvm::LLVMBuildVAArg(self.llbuilder, list, ty, noname())
821 pub fn extract_element(&self, vec: &'ll Value, idx: &'ll Value) -> &'ll Value {
822 self.count_insn("extractelement");
824 llvm::LLVMBuildExtractElement(self.llbuilder, vec, idx, noname())
828 pub fn insert_element(
829 &self, vec: &'ll Value,
833 self.count_insn("insertelement");
835 llvm::LLVMBuildInsertElement(self.llbuilder, vec, elt, idx, noname())
839 pub fn shuffle_vector(&self, v1: &'ll Value, v2: &'ll Value, mask: &'ll Value) -> &'ll Value {
840 self.count_insn("shufflevector");
842 llvm::LLVMBuildShuffleVector(self.llbuilder, v1, v2, mask, noname())
846 pub fn vector_splat(&self, num_elts: usize, elt: &'ll Value) -> &'ll Value {
848 let elt_ty = val_ty(elt);
849 let undef = llvm::LLVMGetUndef(Type::vector(elt_ty, num_elts as u64));
850 let vec = self.insert_element(undef, elt, C_i32(self.cx, 0));
851 let vec_i32_ty = Type::vector(Type::i32(self.cx), num_elts as u64);
852 self.shuffle_vector(vec, undef, C_null(vec_i32_ty))
856 pub fn vector_reduce_fadd_fast(&self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
857 self.count_insn("vector.reduce.fadd_fast");
859 // FIXME: add a non-fast math version once
860 // https://bugs.llvm.org/show_bug.cgi?id=36732
862 let instr = llvm::LLVMRustBuildVectorReduceFAdd(self.llbuilder, acc, src)
863 .expect("LLVMRustBuildVectorReduceFAdd is not available in LLVM version < 5.0");
864 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
868 pub fn vector_reduce_fmul_fast(&self, acc: &'ll Value, src: &'ll Value) -> &'ll Value {
869 self.count_insn("vector.reduce.fmul_fast");
871 // FIXME: add a non-fast math version once
872 // https://bugs.llvm.org/show_bug.cgi?id=36732
874 let instr = llvm::LLVMRustBuildVectorReduceFMul(self.llbuilder, acc, src)
875 .expect("LLVMRustBuildVectorReduceFMul is not available in LLVM version < 5.0");
876 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
880 pub fn vector_reduce_add(&self, src: &'ll Value) -> &'ll Value {
881 self.count_insn("vector.reduce.add");
883 let instr = llvm::LLVMRustBuildVectorReduceAdd(self.llbuilder, src);
884 instr.expect("LLVMRustBuildVectorReduceAdd is not available in LLVM version < 5.0")
887 pub fn vector_reduce_mul(&self, src: &'ll Value) -> &'ll Value {
888 self.count_insn("vector.reduce.mul");
890 let instr = llvm::LLVMRustBuildVectorReduceMul(self.llbuilder, src);
891 instr.expect("LLVMRustBuildVectorReduceMul is not available in LLVM version < 5.0")
894 pub fn vector_reduce_and(&self, src: &'ll Value) -> &'ll Value {
895 self.count_insn("vector.reduce.and");
897 let instr = llvm::LLVMRustBuildVectorReduceAnd(self.llbuilder, src);
898 instr.expect("LLVMRustBuildVectorReduceAnd is not available in LLVM version < 5.0")
901 pub fn vector_reduce_or(&self, src: &'ll Value) -> &'ll Value {
902 self.count_insn("vector.reduce.or");
904 let instr = llvm::LLVMRustBuildVectorReduceOr(self.llbuilder, src);
905 instr.expect("LLVMRustBuildVectorReduceOr is not available in LLVM version < 5.0")
908 pub fn vector_reduce_xor(&self, src: &'ll Value) -> &'ll Value {
909 self.count_insn("vector.reduce.xor");
911 let instr = llvm::LLVMRustBuildVectorReduceXor(self.llbuilder, src);
912 instr.expect("LLVMRustBuildVectorReduceXor is not available in LLVM version < 5.0")
915 pub fn vector_reduce_fmin(&self, src: &'ll Value) -> &'ll Value {
916 self.count_insn("vector.reduce.fmin");
918 let instr = llvm::LLVMRustBuildVectorReduceFMin(self.llbuilder, src, /*NoNaNs:*/ false);
919 instr.expect("LLVMRustBuildVectorReduceFMin is not available in LLVM version < 5.0")
922 pub fn vector_reduce_fmax(&self, src: &'ll Value) -> &'ll Value {
923 self.count_insn("vector.reduce.fmax");
925 let instr = llvm::LLVMRustBuildVectorReduceFMax(self.llbuilder, src, /*NoNaNs:*/ false);
926 instr.expect("LLVMRustBuildVectorReduceFMax is not available in LLVM version < 5.0")
929 pub fn vector_reduce_fmin_fast(&self, src: &'ll Value) -> &'ll Value {
930 self.count_insn("vector.reduce.fmin_fast");
932 let instr = llvm::LLVMRustBuildVectorReduceFMin(self.llbuilder, src, /*NoNaNs:*/ true)
933 .expect("LLVMRustBuildVectorReduceFMin is not available in LLVM version < 5.0");
934 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
938 pub fn vector_reduce_fmax_fast(&self, src: &'ll Value) -> &'ll Value {
939 self.count_insn("vector.reduce.fmax_fast");
941 let instr = llvm::LLVMRustBuildVectorReduceFMax(self.llbuilder, src, /*NoNaNs:*/ true)
942 .expect("LLVMRustBuildVectorReduceFMax is not available in LLVM version < 5.0");
943 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
947 pub fn vector_reduce_min(&self, src: &'ll Value, is_signed: bool) -> &'ll Value {
948 self.count_insn("vector.reduce.min");
950 let instr = llvm::LLVMRustBuildVectorReduceMin(self.llbuilder, src, is_signed);
951 instr.expect("LLVMRustBuildVectorReduceMin is not available in LLVM version < 5.0")
954 pub fn vector_reduce_max(&self, src: &'ll Value, is_signed: bool) -> &'ll Value {
955 self.count_insn("vector.reduce.max");
957 let instr = llvm::LLVMRustBuildVectorReduceMax(self.llbuilder, src, is_signed);
958 instr.expect("LLVMRustBuildVectorReduceMax is not available in LLVM version < 5.0")
962 pub fn extract_value(&self, agg_val: &'ll Value, idx: u64) -> &'ll Value {
963 self.count_insn("extractvalue");
964 assert_eq!(idx as c_uint as u64, idx);
966 llvm::LLVMBuildExtractValue(self.llbuilder, agg_val, idx as c_uint, noname())
970 pub fn insert_value(&self, agg_val: &'ll Value, elt: &'ll Value,
971 idx: u64) -> &'ll Value {
972 self.count_insn("insertvalue");
973 assert_eq!(idx as c_uint as u64, idx);
975 llvm::LLVMBuildInsertValue(self.llbuilder, agg_val, elt, idx as c_uint,
980 pub fn landing_pad(&self, ty: &'ll Type, pers_fn: &'ll Value,
981 num_clauses: usize) -> &'ll Value {
982 self.count_insn("landingpad");
984 llvm::LLVMBuildLandingPad(self.llbuilder, ty, pers_fn,
985 num_clauses as c_uint, noname())
989 pub fn add_clause(&self, landing_pad: &'ll Value, clause: &'ll Value) {
991 llvm::LLVMAddClause(landing_pad, clause);
995 pub fn set_cleanup(&self, landing_pad: &'ll Value) {
996 self.count_insn("setcleanup");
998 llvm::LLVMSetCleanup(landing_pad, llvm::True);
1002 pub fn resume(&self, exn: &'ll Value) -> &'ll Value {
1003 self.count_insn("resume");
1005 llvm::LLVMBuildResume(self.llbuilder, exn)
1009 pub fn cleanup_pad(&self,
1010 parent: Option<&'ll Value>,
1011 args: &[&'ll Value]) -> &'ll Value {
1012 self.count_insn("cleanuppad");
1013 let name = const_cstr!("cleanuppad");
1015 llvm::LLVMRustBuildCleanupPad(self.llbuilder,
1017 args.len() as c_uint,
1021 ret.expect("LLVM does not have support for cleanuppad")
1025 &self, cleanup: &'ll Value,
1026 unwind: Option<&'ll BasicBlock>,
1028 self.count_insn("cleanupret");
1030 llvm::LLVMRustBuildCleanupRet(self.llbuilder, cleanup, unwind)
1032 ret.expect("LLVM does not have support for cleanupret")
1035 pub fn catch_pad(&self,
1037 args: &[&'ll Value]) -> &'ll Value {
1038 self.count_insn("catchpad");
1039 let name = const_cstr!("catchpad");
1041 llvm::LLVMRustBuildCatchPad(self.llbuilder, parent,
1042 args.len() as c_uint, args.as_ptr(),
1045 ret.expect("LLVM does not have support for catchpad")
1048 pub fn catch_ret(&self, pad: &'ll Value, unwind: &'ll BasicBlock) -> &'ll Value {
1049 self.count_insn("catchret");
1051 llvm::LLVMRustBuildCatchRet(self.llbuilder, pad, unwind)
1053 ret.expect("LLVM does not have support for catchret")
1056 pub fn catch_switch(
1058 parent: Option<&'ll Value>,
1059 unwind: Option<&'ll BasicBlock>,
1060 num_handlers: usize,
1062 self.count_insn("catchswitch");
1063 let name = const_cstr!("catchswitch");
1065 llvm::LLVMRustBuildCatchSwitch(self.llbuilder, parent, unwind,
1066 num_handlers as c_uint,
1069 ret.expect("LLVM does not have support for catchswitch")
1072 pub fn add_handler(&self, catch_switch: &'ll Value, handler: &'ll BasicBlock) {
1074 llvm::LLVMRustAddHandler(catch_switch, handler);
1078 pub fn set_personality_fn(&self, personality: &'ll Value) {
1080 llvm::LLVMSetPersonalityFn(self.llfn(), personality);
1084 // Atomic Operations
1085 pub fn atomic_cmpxchg(
1090 order: AtomicOrdering,
1091 failure_order: AtomicOrdering,
1095 llvm::LLVMRustBuildAtomicCmpXchg(self.llbuilder, dst, cmp, src,
1096 order, failure_order, weak)
1104 order: AtomicOrdering,
1107 llvm::LLVMBuildAtomicRMW(self.llbuilder, op, dst, src, order, False)
1111 pub fn atomic_fence(&self, order: AtomicOrdering, scope: SynchronizationScope) {
1113 llvm::LLVMRustBuildAtomicFence(self.llbuilder, order, scope);
1117 pub fn add_case(&self, s: &'ll Value, on_val: &'ll Value, dest: &'ll BasicBlock) {
1119 llvm::LLVMAddCase(s, on_val, dest)
1123 pub fn add_incoming_to_phi(&self, phi: &'ll Value, val: &'ll Value, bb: &'ll BasicBlock) {
1124 self.count_insn("addincoming");
1126 llvm::LLVMAddIncoming(phi, &val, &bb, 1 as c_uint);
1130 pub fn set_invariant_load(&self, load: &'ll Value) {
1132 llvm::LLVMSetMetadata(load, llvm::MD_invariant_load as c_uint,
1133 llvm::LLVMMDNodeInContext(self.cx.llcx, ptr::null(), 0));
1137 /// Returns the ptr value that should be used for storing `val`.
1138 fn check_store<'b>(&self,
1140 ptr: &'ll Value) -> &'ll Value {
1141 let dest_ptr_ty = val_ty(ptr);
1142 let stored_ty = val_ty(val);
1143 let stored_ptr_ty = stored_ty.ptr_to();
1145 assert_eq!(dest_ptr_ty.kind(), llvm::TypeKind::Pointer);
1147 if dest_ptr_ty == stored_ptr_ty {
1150 debug!("Type mismatch in store. \
1151 Expected {:?}, got {:?}; inserting bitcast",
1152 dest_ptr_ty, stored_ptr_ty);
1153 self.bitcast(ptr, stored_ptr_ty)
1157 /// Returns the args that should be used for a call to `llfn`.
1158 fn check_call<'b>(&self,
1161 args: &'b [&'ll Value]) -> Cow<'b, [&'ll Value]> {
1162 let mut fn_ty = val_ty(llfn);
1163 // Strip off pointers
1164 while fn_ty.kind() == llvm::TypeKind::Pointer {
1165 fn_ty = fn_ty.element_type();
1168 assert!(fn_ty.kind() == llvm::TypeKind::Function,
1169 "builder::{} not passed a function, but {:?}", typ, fn_ty);
1171 let param_tys = fn_ty.func_params();
1173 let all_args_match = param_tys.iter()
1174 .zip(args.iter().map(|&v| val_ty(v)))
1175 .all(|(expected_ty, actual_ty)| *expected_ty == actual_ty);
1178 return Cow::Borrowed(args);
1181 let casted_args: Vec<_> = param_tys.into_iter()
1184 .map(|(i, (expected_ty, &actual_val))| {
1185 let actual_ty = val_ty(actual_val);
1186 if expected_ty != actual_ty {
1187 debug!("Type mismatch in function call of {:?}. \
1188 Expected {:?} for param {}, got {:?}; injecting bitcast",
1189 llfn, expected_ty, i, actual_ty);
1190 self.bitcast(actual_val, expected_ty)
1197 Cow::Owned(casted_args)
1200 pub fn lifetime_start(&self, ptr: &'ll Value, size: Size) {
1201 self.call_lifetime_intrinsic("llvm.lifetime.start", ptr, size);
1204 pub fn lifetime_end(&self, ptr: &'ll Value, size: Size) {
1205 self.call_lifetime_intrinsic("llvm.lifetime.end", ptr, size);
1208 /// If LLVM lifetime intrinsic support is enabled (i.e. optimizations
1209 /// on), and `ptr` is nonzero-sized, then extracts the size of `ptr`
1210 /// and the intrinsic for `lt` and passes them to `emit`, which is in
1211 /// charge of generating code to call the passed intrinsic on whatever
1212 /// block of generated code is targeted for the intrinsic.
1214 /// If LLVM lifetime intrinsic support is disabled (i.e. optimizations
1215 /// off) or `ptr` is zero-sized, then no-op (does not call `emit`).
1216 fn call_lifetime_intrinsic(&self, intrinsic: &str, ptr: &'ll Value, size: Size) {
1217 if self.cx.sess().opts.optimize == config::OptLevel::No {
1221 let size = size.bytes();
1226 let lifetime_intrinsic = self.cx.get_intrinsic(intrinsic);
1228 let ptr = self.pointercast(ptr, Type::i8p(self.cx));
1229 self.call(lifetime_intrinsic, &[C_u64(self.cx, size), ptr], None);