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 #![allow(dead_code)] // FFI wrappers
14 use llvm::{AtomicRmwBinOp, AtomicOrdering, SynchronizationScope, AsmDialect};
15 use llvm::{Opcode, IntPredicate, RealPredicate, False, OperandBundleDef};
16 use llvm::{ValueRef, BasicBlockRef, BuilderRef, ModuleRef};
18 use machine::llalign_of_pref;
21 use libc::{c_uint, c_char};
22 use rustc::ty::TyCtxt;
23 use rustc::session::Session;
26 use std::ffi::CString;
30 // All Builders must have an llfn associated with them
32 pub struct Builder<'a, 'tcx: 'a> {
33 pub llbuilder: BuilderRef,
34 pub ccx: &'a CrateContext<'a, 'tcx>,
37 impl<'a, 'tcx> Drop for Builder<'a, 'tcx> {
40 llvm::LLVMDisposeBuilder(self.llbuilder);
45 // This is a really awful way to get a zero-length c-string, but better (and a
46 // lot more efficient) than doing str::as_c_str("", ...) every time.
47 fn noname() -> *const c_char {
48 static CNULL: c_char = 0;
52 impl<'a, 'tcx> Builder<'a, 'tcx> {
53 pub fn new_block<'b>(ccx: &'a CrateContext<'a, 'tcx>, llfn: ValueRef, name: &'b str) -> Self {
54 let builder = Builder::with_ccx(ccx);
56 let name = CString::new(name).unwrap();
57 llvm::LLVMAppendBasicBlockInContext(
63 builder.position_at_end(llbb);
67 pub fn with_ccx(ccx: &'a CrateContext<'a, 'tcx>) -> Self {
68 // Create a fresh builder from the crate context.
69 let llbuilder = unsafe {
70 llvm::LLVMCreateBuilderInContext(ccx.llcx())
78 pub fn build_sibling_block<'b>(&self, name: &'b str) -> Builder<'a, 'tcx> {
79 Builder::new_block(self.ccx, self.llfn(), name)
82 pub fn sess(&self) -> &Session {
86 pub fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx> {
90 pub fn llfn(&self) -> ValueRef {
92 llvm::LLVMGetBasicBlockParent(self.llbb())
96 pub fn llbb(&self) -> BasicBlockRef {
98 llvm::LLVMGetInsertBlock(self.llbuilder)
102 fn count_insn(&self, category: &str) {
103 if self.ccx.sess().trans_stats() {
104 self.ccx.stats().n_llvm_insns.set(self.ccx.stats().n_llvm_insns.get() + 1);
106 if self.ccx.sess().count_llvm_insns() {
107 let mut h = self.ccx.stats().llvm_insns.borrow_mut();
108 *h.entry(category.to_string()).or_insert(0) += 1;
112 pub fn position_before(&self, insn: ValueRef) {
114 llvm::LLVMPositionBuilderBefore(self.llbuilder, insn);
118 pub fn position_at_end(&self, llbb: BasicBlockRef) {
120 llvm::LLVMPositionBuilderAtEnd(self.llbuilder, llbb);
124 pub fn position_at_start(&self, llbb: BasicBlockRef) {
126 llvm::LLVMRustPositionBuilderAtStart(self.llbuilder, llbb);
130 pub fn ret_void(&self) {
131 self.count_insn("retvoid");
133 llvm::LLVMBuildRetVoid(self.llbuilder);
137 pub fn ret(&self, v: ValueRef) {
138 self.count_insn("ret");
140 llvm::LLVMBuildRet(self.llbuilder, v);
144 pub fn aggregate_ret(&self, ret_vals: &[ValueRef]) {
146 llvm::LLVMBuildAggregateRet(self.llbuilder,
148 ret_vals.len() as c_uint);
152 pub fn br(&self, dest: BasicBlockRef) {
153 self.count_insn("br");
155 llvm::LLVMBuildBr(self.llbuilder, dest);
159 pub fn cond_br(&self, cond: ValueRef, then_llbb: BasicBlockRef, else_llbb: BasicBlockRef) {
160 self.count_insn("condbr");
162 llvm::LLVMBuildCondBr(self.llbuilder, cond, then_llbb, else_llbb);
166 pub fn switch(&self, v: ValueRef, else_llbb: BasicBlockRef, num_cases: usize) -> ValueRef {
168 llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, num_cases as c_uint)
172 pub fn indirect_br(&self, addr: ValueRef, num_dests: usize) {
173 self.count_insn("indirectbr");
175 llvm::LLVMBuildIndirectBr(self.llbuilder, addr, num_dests as c_uint);
183 catch: BasicBlockRef,
184 bundle: Option<&OperandBundleDef>) -> ValueRef {
185 self.count_insn("invoke");
187 debug!("Invoke {:?} with args ({})",
190 .map(|&v| format!("{:?}", Value(v)))
191 .collect::<Vec<String>>()
194 let args = self.check_call("invoke", llfn, args);
195 let bundle = bundle.as_ref().map(|b| b.raw()).unwrap_or(ptr::null_mut());
198 llvm::LLVMRustBuildInvoke(self.llbuilder,
201 args.len() as c_uint,
209 pub fn unreachable(&self) {
210 self.count_insn("unreachable");
212 llvm::LLVMBuildUnreachable(self.llbuilder);
217 pub fn add(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
218 self.count_insn("add");
220 llvm::LLVMBuildAdd(self.llbuilder, lhs, rhs, noname())
224 pub fn nswadd(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
225 self.count_insn("nswadd");
227 llvm::LLVMBuildNSWAdd(self.llbuilder, lhs, rhs, noname())
231 pub fn nuwadd(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
232 self.count_insn("nuwadd");
234 llvm::LLVMBuildNUWAdd(self.llbuilder, lhs, rhs, noname())
238 pub fn fadd(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
239 self.count_insn("fadd");
241 llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, noname())
245 pub fn fadd_fast(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
246 self.count_insn("fadd");
248 let instr = llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, noname());
249 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
254 pub fn sub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
255 self.count_insn("sub");
257 llvm::LLVMBuildSub(self.llbuilder, lhs, rhs, noname())
261 pub fn nswsub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
262 self.count_insn("nwsub");
264 llvm::LLVMBuildNSWSub(self.llbuilder, lhs, rhs, noname())
268 pub fn nuwsub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
269 self.count_insn("nuwsub");
271 llvm::LLVMBuildNUWSub(self.llbuilder, lhs, rhs, noname())
275 pub fn fsub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
276 self.count_insn("sub");
278 llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, noname())
282 pub fn fsub_fast(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
283 self.count_insn("sub");
285 let instr = llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, noname());
286 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
291 pub fn mul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
292 self.count_insn("mul");
294 llvm::LLVMBuildMul(self.llbuilder, lhs, rhs, noname())
298 pub fn nswmul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
299 self.count_insn("nswmul");
301 llvm::LLVMBuildNSWMul(self.llbuilder, lhs, rhs, noname())
305 pub fn nuwmul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
306 self.count_insn("nuwmul");
308 llvm::LLVMBuildNUWMul(self.llbuilder, lhs, rhs, noname())
312 pub fn fmul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
313 self.count_insn("fmul");
315 llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, noname())
319 pub fn fmul_fast(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
320 self.count_insn("fmul");
322 let instr = llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, noname());
323 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
329 pub fn udiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
330 self.count_insn("udiv");
332 llvm::LLVMBuildUDiv(self.llbuilder, lhs, rhs, noname())
336 pub fn sdiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
337 self.count_insn("sdiv");
339 llvm::LLVMBuildSDiv(self.llbuilder, lhs, rhs, noname())
343 pub fn exactsdiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
344 self.count_insn("exactsdiv");
346 llvm::LLVMBuildExactSDiv(self.llbuilder, lhs, rhs, noname())
350 pub fn fdiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
351 self.count_insn("fdiv");
353 llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, noname())
357 pub fn fdiv_fast(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
358 self.count_insn("fdiv");
360 let instr = llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, noname());
361 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
366 pub fn urem(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
367 self.count_insn("urem");
369 llvm::LLVMBuildURem(self.llbuilder, lhs, rhs, noname())
373 pub fn srem(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
374 self.count_insn("srem");
376 llvm::LLVMBuildSRem(self.llbuilder, lhs, rhs, noname())
380 pub fn frem(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
381 self.count_insn("frem");
383 llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, noname())
387 pub fn frem_fast(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
388 self.count_insn("frem");
390 let instr = llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, noname());
391 llvm::LLVMRustSetHasUnsafeAlgebra(instr);
396 pub fn shl(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
397 self.count_insn("shl");
399 llvm::LLVMBuildShl(self.llbuilder, lhs, rhs, noname())
403 pub fn lshr(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
404 self.count_insn("lshr");
406 llvm::LLVMBuildLShr(self.llbuilder, lhs, rhs, noname())
410 pub fn ashr(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
411 self.count_insn("ashr");
413 llvm::LLVMBuildAShr(self.llbuilder, lhs, rhs, noname())
417 pub fn and(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
418 self.count_insn("and");
420 llvm::LLVMBuildAnd(self.llbuilder, lhs, rhs, noname())
424 pub fn or(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
425 self.count_insn("or");
427 llvm::LLVMBuildOr(self.llbuilder, lhs, rhs, noname())
431 pub fn xor(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
432 self.count_insn("xor");
434 llvm::LLVMBuildXor(self.llbuilder, lhs, rhs, noname())
438 pub fn binop(&self, op: Opcode, lhs: ValueRef, rhs: ValueRef)
440 self.count_insn("binop");
442 llvm::LLVMBuildBinOp(self.llbuilder, op, lhs, rhs, noname())
446 pub fn neg(&self, v: ValueRef) -> ValueRef {
447 self.count_insn("neg");
449 llvm::LLVMBuildNeg(self.llbuilder, v, noname())
453 pub fn nswneg(&self, v: ValueRef) -> ValueRef {
454 self.count_insn("nswneg");
456 llvm::LLVMBuildNSWNeg(self.llbuilder, v, noname())
460 pub fn nuwneg(&self, v: ValueRef) -> ValueRef {
461 self.count_insn("nuwneg");
463 llvm::LLVMBuildNUWNeg(self.llbuilder, v, noname())
466 pub fn fneg(&self, v: ValueRef) -> ValueRef {
467 self.count_insn("fneg");
469 llvm::LLVMBuildFNeg(self.llbuilder, v, noname())
473 pub fn not(&self, v: ValueRef) -> ValueRef {
474 self.count_insn("not");
476 llvm::LLVMBuildNot(self.llbuilder, v, noname())
480 pub fn alloca(&self, ty: Type, name: &str, align: Option<u32>) -> ValueRef {
481 let builder = Builder::with_ccx(self.ccx);
482 builder.position_at_start(unsafe {
483 llvm::LLVMGetFirstBasicBlock(self.llfn())
485 builder.dynamic_alloca(ty, name, align)
488 pub fn dynamic_alloca(&self, ty: Type, name: &str, align: Option<u32>) -> ValueRef {
489 self.count_insn("alloca");
491 let alloca = if name.is_empty() {
492 llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(), noname())
494 let name = CString::new(name).unwrap();
495 llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(),
498 if let Some(align) = align {
499 llvm::LLVMSetAlignment(alloca, align as c_uint);
505 pub fn free(&self, ptr: ValueRef) {
506 self.count_insn("free");
508 llvm::LLVMBuildFree(self.llbuilder, ptr);
512 pub fn load(&self, ptr: ValueRef, align: Option<u32>) -> ValueRef {
513 self.count_insn("load");
515 let load = llvm::LLVMBuildLoad(self.llbuilder, ptr, noname());
516 if let Some(align) = align {
517 llvm::LLVMSetAlignment(load, align as c_uint);
523 pub fn volatile_load(&self, ptr: ValueRef) -> ValueRef {
524 self.count_insn("load.volatile");
526 let insn = llvm::LLVMBuildLoad(self.llbuilder, ptr, noname());
527 llvm::LLVMSetVolatile(insn, llvm::True);
532 pub fn atomic_load(&self, ptr: ValueRef, order: AtomicOrdering) -> ValueRef {
533 self.count_insn("load.atomic");
535 let ty = Type::from_ref(llvm::LLVMTypeOf(ptr));
536 let align = llalign_of_pref(self.ccx, ty.element_type());
537 llvm::LLVMRustBuildAtomicLoad(self.llbuilder, ptr, noname(), order,
543 pub fn load_range_assert(&self, ptr: ValueRef, lo: u64,
544 hi: u64, signed: llvm::Bool,
545 align: Option<u32>) -> ValueRef {
546 let value = self.load(ptr, align);
549 let t = llvm::LLVMGetElementType(llvm::LLVMTypeOf(ptr));
550 let min = llvm::LLVMConstInt(t, lo, signed);
551 let max = llvm::LLVMConstInt(t, hi, signed);
555 llvm::LLVMSetMetadata(value, llvm::MD_range as c_uint,
556 llvm::LLVMMDNodeInContext(self.ccx.llcx(),
564 pub fn load_nonnull(&self, ptr: ValueRef, align: Option<u32>) -> ValueRef {
565 let value = self.load(ptr, align);
567 llvm::LLVMSetMetadata(value, llvm::MD_nonnull as c_uint,
568 llvm::LLVMMDNodeInContext(self.ccx.llcx(), ptr::null(), 0));
574 pub fn store(&self, val: ValueRef, ptr: ValueRef, align: Option<u32>) -> ValueRef {
575 debug!("Store {:?} -> {:?}", Value(val), Value(ptr));
576 assert!(!self.llbuilder.is_null());
577 self.count_insn("store");
578 let ptr = self.check_store(val, ptr);
580 let store = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
581 if let Some(align) = align {
582 llvm::LLVMSetAlignment(store, align as c_uint);
588 pub fn volatile_store(&self, val: ValueRef, ptr: ValueRef) -> ValueRef {
589 debug!("Store {:?} -> {:?}", Value(val), Value(ptr));
590 assert!(!self.llbuilder.is_null());
591 self.count_insn("store.volatile");
592 let ptr = self.check_store(val, ptr);
594 let insn = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
595 llvm::LLVMSetVolatile(insn, llvm::True);
600 pub fn atomic_store(&self, val: ValueRef, ptr: ValueRef, order: AtomicOrdering) {
601 debug!("Store {:?} -> {:?}", Value(val), Value(ptr));
602 self.count_insn("store.atomic");
603 let ptr = self.check_store(val, ptr);
605 let ty = Type::from_ref(llvm::LLVMTypeOf(ptr));
606 let align = llalign_of_pref(self.ccx, ty.element_type());
607 llvm::LLVMRustBuildAtomicStore(self.llbuilder, val, ptr, order, align as c_uint);
611 pub fn gep(&self, ptr: ValueRef, indices: &[ValueRef]) -> ValueRef {
612 self.count_insn("gep");
614 llvm::LLVMBuildGEP(self.llbuilder, ptr, indices.as_ptr(),
615 indices.len() as c_uint, noname())
619 // Simple wrapper around GEP that takes an array of ints and wraps them
622 pub fn gepi(&self, base: ValueRef, ixs: &[usize]) -> ValueRef {
623 // Small vector optimization. This should catch 100% of the cases that
626 let mut small_vec = [ C_i32(self.ccx, 0); 16 ];
627 for (small_vec_e, &ix) in small_vec.iter_mut().zip(ixs) {
628 *small_vec_e = C_i32(self.ccx, ix as i32);
630 self.inbounds_gep(base, &small_vec[..ixs.len()])
632 let v = ixs.iter().map(|i| C_i32(self.ccx, *i as i32)).collect::<Vec<ValueRef>>();
633 self.count_insn("gepi");
634 self.inbounds_gep(base, &v)
638 pub fn inbounds_gep(&self, ptr: ValueRef, indices: &[ValueRef]) -> ValueRef {
639 self.count_insn("inboundsgep");
641 llvm::LLVMBuildInBoundsGEP(
642 self.llbuilder, ptr, indices.as_ptr(), indices.len() as c_uint, noname())
646 pub fn struct_gep(&self, ptr: ValueRef, idx: usize) -> ValueRef {
647 self.count_insn("structgep");
649 llvm::LLVMBuildStructGEP(self.llbuilder, ptr, idx as c_uint, noname())
653 pub fn global_string(&self, _str: *const c_char) -> ValueRef {
654 self.count_insn("globalstring");
656 llvm::LLVMBuildGlobalString(self.llbuilder, _str, noname())
660 pub fn global_string_ptr(&self, _str: *const c_char) -> ValueRef {
661 self.count_insn("globalstringptr");
663 llvm::LLVMBuildGlobalStringPtr(self.llbuilder, _str, noname())
668 pub fn trunc(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
669 self.count_insn("trunc");
671 llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty.to_ref(), noname())
675 pub fn zext(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
676 self.count_insn("zext");
678 llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty.to_ref(), noname())
682 pub fn sext(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
683 self.count_insn("sext");
685 llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty.to_ref(), noname())
689 pub fn fptoui(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
690 self.count_insn("fptoui");
692 llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty.to_ref(), noname())
696 pub fn fptosi(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
697 self.count_insn("fptosi");
699 llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty.to_ref(),noname())
703 pub fn uitofp(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
704 self.count_insn("uitofp");
706 llvm::LLVMBuildUIToFP(self.llbuilder, val, dest_ty.to_ref(), noname())
710 pub fn sitofp(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
711 self.count_insn("sitofp");
713 llvm::LLVMBuildSIToFP(self.llbuilder, val, dest_ty.to_ref(), noname())
717 pub fn fptrunc(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
718 self.count_insn("fptrunc");
720 llvm::LLVMBuildFPTrunc(self.llbuilder, val, dest_ty.to_ref(), noname())
724 pub fn fpext(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
725 self.count_insn("fpext");
727 llvm::LLVMBuildFPExt(self.llbuilder, val, dest_ty.to_ref(), noname())
731 pub fn ptrtoint(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
732 self.count_insn("ptrtoint");
734 llvm::LLVMBuildPtrToInt(self.llbuilder, val, dest_ty.to_ref(), noname())
738 pub fn inttoptr(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
739 self.count_insn("inttoptr");
741 llvm::LLVMBuildIntToPtr(self.llbuilder, val, dest_ty.to_ref(), noname())
745 pub fn bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
746 self.count_insn("bitcast");
748 llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
752 pub fn zext_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
753 self.count_insn("zextorbitcast");
755 llvm::LLVMBuildZExtOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
759 pub fn sext_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
760 self.count_insn("sextorbitcast");
762 llvm::LLVMBuildSExtOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
766 pub fn trunc_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
767 self.count_insn("truncorbitcast");
769 llvm::LLVMBuildTruncOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
773 pub fn cast(&self, op: Opcode, val: ValueRef, dest_ty: Type) -> ValueRef {
774 self.count_insn("cast");
776 llvm::LLVMBuildCast(self.llbuilder, op, val, dest_ty.to_ref(), noname())
780 pub fn pointercast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
781 self.count_insn("pointercast");
783 llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty.to_ref(), noname())
787 pub fn intcast(&self, val: ValueRef, dest_ty: Type, is_signed: bool) -> ValueRef {
788 self.count_insn("intcast");
790 llvm::LLVMRustBuildIntCast(self.llbuilder, val, dest_ty.to_ref(), is_signed)
794 pub fn fpcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
795 self.count_insn("fpcast");
797 llvm::LLVMBuildFPCast(self.llbuilder, val, dest_ty.to_ref(), noname())
803 pub fn icmp(&self, op: IntPredicate, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
804 self.count_insn("icmp");
806 llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, noname())
810 pub fn fcmp(&self, op: RealPredicate, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
811 self.count_insn("fcmp");
813 llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, noname())
817 /* Miscellaneous instructions */
818 pub fn empty_phi(&self, ty: Type) -> ValueRef {
819 self.count_insn("emptyphi");
821 llvm::LLVMBuildPhi(self.llbuilder, ty.to_ref(), noname())
825 pub fn phi(&self, ty: Type, vals: &[ValueRef], bbs: &[BasicBlockRef]) -> ValueRef {
826 assert_eq!(vals.len(), bbs.len());
827 let phi = self.empty_phi(ty);
828 self.count_insn("addincoming");
830 llvm::LLVMAddIncoming(phi, vals.as_ptr(),
832 vals.len() as c_uint);
837 pub fn add_span_comment(&self, sp: Span, text: &str) {
838 if self.ccx.sess().asm_comments() {
839 let s = format!("{} ({})",
841 self.ccx.sess().codemap().span_to_string(sp));
843 self.add_comment(&s);
847 pub fn add_comment(&self, text: &str) {
848 if self.ccx.sess().asm_comments() {
849 let sanitized = text.replace("$", "");
850 let comment_text = format!("{} {}", "#",
851 sanitized.replace("\n", "\n\t# "));
852 self.count_insn("inlineasm");
853 let comment_text = CString::new(comment_text).unwrap();
855 llvm::LLVMConstInlineAsm(Type::func(&[], &Type::void(self.ccx)).to_ref(),
856 comment_text.as_ptr(), noname(), False,
859 self.call(asm, &[], None);
863 pub fn inline_asm_call(&self, asm: *const c_char, cons: *const c_char,
864 inputs: &[ValueRef], output: Type,
865 volatile: bool, alignstack: bool,
866 dia: AsmDialect) -> ValueRef {
867 self.count_insn("inlineasm");
869 let volatile = if volatile { llvm::True }
870 else { llvm::False };
871 let alignstack = if alignstack { llvm::True }
872 else { llvm::False };
874 let argtys = inputs.iter().map(|v| {
875 debug!("Asm Input Type: {:?}", Value(*v));
877 }).collect::<Vec<_>>();
879 debug!("Asm Output Type: {:?}", output);
880 let fty = Type::func(&argtys[..], &output);
882 let v = llvm::LLVMRustInlineAsm(
883 fty.to_ref(), asm, cons, volatile, alignstack, dia);
884 self.call(v, inputs, None)
888 pub fn call(&self, llfn: ValueRef, args: &[ValueRef],
889 bundle: Option<&OperandBundleDef>) -> ValueRef {
890 self.count_insn("call");
892 debug!("Call {:?} with args ({})",
895 .map(|&v| format!("{:?}", Value(v)))
896 .collect::<Vec<String>>()
899 let args = self.check_call("call", llfn, args);
900 let bundle = bundle.as_ref().map(|b| b.raw()).unwrap_or(ptr::null_mut());
903 llvm::LLVMRustBuildCall(self.llbuilder, llfn, args.as_ptr(),
904 args.len() as c_uint, bundle, noname())
908 pub fn select(&self, cond: ValueRef, then_val: ValueRef, else_val: ValueRef) -> ValueRef {
909 self.count_insn("select");
911 llvm::LLVMBuildSelect(self.llbuilder, cond, then_val, else_val, noname())
915 pub fn va_arg(&self, list: ValueRef, ty: Type) -> ValueRef {
916 self.count_insn("vaarg");
918 llvm::LLVMBuildVAArg(self.llbuilder, list, ty.to_ref(), noname())
922 pub fn extract_element(&self, vec: ValueRef, idx: ValueRef) -> ValueRef {
923 self.count_insn("extractelement");
925 llvm::LLVMBuildExtractElement(self.llbuilder, vec, idx, noname())
929 pub fn insert_element(&self, vec: ValueRef, elt: ValueRef, idx: ValueRef) -> ValueRef {
930 self.count_insn("insertelement");
932 llvm::LLVMBuildInsertElement(self.llbuilder, vec, elt, idx, noname())
936 pub fn shuffle_vector(&self, v1: ValueRef, v2: ValueRef, mask: ValueRef) -> ValueRef {
937 self.count_insn("shufflevector");
939 llvm::LLVMBuildShuffleVector(self.llbuilder, v1, v2, mask, noname())
943 pub fn vector_splat(&self, num_elts: usize, elt: ValueRef) -> ValueRef {
945 let elt_ty = val_ty(elt);
946 let undef = llvm::LLVMGetUndef(Type::vector(&elt_ty, num_elts as u64).to_ref());
947 let vec = self.insert_element(undef, elt, C_i32(self.ccx, 0));
948 let vec_i32_ty = Type::vector(&Type::i32(self.ccx), num_elts as u64);
949 self.shuffle_vector(vec, undef, C_null(vec_i32_ty))
953 pub fn extract_value(&self, agg_val: ValueRef, idx: usize) -> ValueRef {
954 self.count_insn("extractvalue");
956 llvm::LLVMBuildExtractValue(self.llbuilder, agg_val, idx as c_uint, noname())
960 pub fn insert_value(&self, agg_val: ValueRef, elt: ValueRef,
961 idx: usize) -> ValueRef {
962 self.count_insn("insertvalue");
964 llvm::LLVMBuildInsertValue(self.llbuilder, agg_val, elt, idx as c_uint,
969 pub fn is_null(&self, val: ValueRef) -> ValueRef {
970 self.count_insn("isnull");
972 llvm::LLVMBuildIsNull(self.llbuilder, val, noname())
976 pub fn is_not_null(&self, val: ValueRef) -> ValueRef {
977 self.count_insn("isnotnull");
979 llvm::LLVMBuildIsNotNull(self.llbuilder, val, noname())
983 pub fn ptrdiff(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
984 self.count_insn("ptrdiff");
986 llvm::LLVMBuildPtrDiff(self.llbuilder, lhs, rhs, noname())
992 let bb: BasicBlockRef = llvm::LLVMGetInsertBlock(self.llbuilder);
993 let fn_: ValueRef = llvm::LLVMGetBasicBlockParent(bb);
994 let m: ModuleRef = llvm::LLVMGetGlobalParent(fn_);
995 let p = "llvm.trap\0".as_ptr();
996 let t: ValueRef = llvm::LLVMGetNamedFunction(m, p as *const _);
997 assert!((t as isize != 0));
998 let args: &[ValueRef] = &[];
999 self.count_insn("trap");
1000 llvm::LLVMRustBuildCall(self.llbuilder, t,
1001 args.as_ptr(), args.len() as c_uint,
1007 pub fn landing_pad(&self, ty: Type, pers_fn: ValueRef,
1009 llfn: ValueRef) -> ValueRef {
1010 self.count_insn("landingpad");
1012 llvm::LLVMRustBuildLandingPad(self.llbuilder, ty.to_ref(), pers_fn,
1013 num_clauses as c_uint, noname(), llfn)
1017 pub fn add_clause(&self, landing_pad: ValueRef, clause: ValueRef) {
1019 llvm::LLVMAddClause(landing_pad, clause);
1023 pub fn set_cleanup(&self, landing_pad: ValueRef) {
1024 self.count_insn("setcleanup");
1026 llvm::LLVMSetCleanup(landing_pad, llvm::True);
1030 pub fn resume(&self, exn: ValueRef) -> ValueRef {
1031 self.count_insn("resume");
1033 llvm::LLVMBuildResume(self.llbuilder, exn)
1037 pub fn cleanup_pad(&self,
1038 parent: Option<ValueRef>,
1039 args: &[ValueRef]) -> ValueRef {
1040 self.count_insn("cleanuppad");
1041 let parent = parent.unwrap_or(ptr::null_mut());
1042 let name = CString::new("cleanuppad").unwrap();
1044 llvm::LLVMRustBuildCleanupPad(self.llbuilder,
1046 args.len() as c_uint,
1050 assert!(!ret.is_null(), "LLVM does not have support for cleanuppad");
1054 pub fn cleanup_ret(&self, cleanup: ValueRef,
1055 unwind: Option<BasicBlockRef>) -> ValueRef {
1056 self.count_insn("cleanupret");
1057 let unwind = unwind.unwrap_or(ptr::null_mut());
1059 llvm::LLVMRustBuildCleanupRet(self.llbuilder, cleanup, unwind)
1061 assert!(!ret.is_null(), "LLVM does not have support for cleanupret");
1065 pub fn catch_pad(&self,
1067 args: &[ValueRef]) -> ValueRef {
1068 self.count_insn("catchpad");
1069 let name = CString::new("catchpad").unwrap();
1071 llvm::LLVMRustBuildCatchPad(self.llbuilder, parent,
1072 args.len() as c_uint, args.as_ptr(),
1075 assert!(!ret.is_null(), "LLVM does not have support for catchpad");
1079 pub fn catch_ret(&self, pad: ValueRef, unwind: BasicBlockRef) -> ValueRef {
1080 self.count_insn("catchret");
1082 llvm::LLVMRustBuildCatchRet(self.llbuilder, pad, unwind)
1084 assert!(!ret.is_null(), "LLVM does not have support for catchret");
1088 pub fn catch_switch(&self,
1089 parent: Option<ValueRef>,
1090 unwind: Option<BasicBlockRef>,
1091 num_handlers: usize) -> ValueRef {
1092 self.count_insn("catchswitch");
1093 let parent = parent.unwrap_or(ptr::null_mut());
1094 let unwind = unwind.unwrap_or(ptr::null_mut());
1095 let name = CString::new("catchswitch").unwrap();
1097 llvm::LLVMRustBuildCatchSwitch(self.llbuilder, parent, unwind,
1098 num_handlers as c_uint,
1101 assert!(!ret.is_null(), "LLVM does not have support for catchswitch");
1105 pub fn add_handler(&self, catch_switch: ValueRef, handler: BasicBlockRef) {
1107 llvm::LLVMRustAddHandler(catch_switch, handler);
1111 pub fn set_personality_fn(&self, personality: ValueRef) {
1113 llvm::LLVMSetPersonalityFn(self.llfn(), personality);
1117 // Atomic Operations
1118 pub fn atomic_cmpxchg(&self, dst: ValueRef,
1119 cmp: ValueRef, src: ValueRef,
1120 order: AtomicOrdering,
1121 failure_order: AtomicOrdering,
1122 weak: llvm::Bool) -> ValueRef {
1124 llvm::LLVMRustBuildAtomicCmpXchg(self.llbuilder, dst, cmp, src,
1125 order, failure_order, weak)
1128 pub fn atomic_rmw(&self, op: AtomicRmwBinOp,
1129 dst: ValueRef, src: ValueRef,
1130 order: AtomicOrdering) -> ValueRef {
1132 llvm::LLVMBuildAtomicRMW(self.llbuilder, op, dst, src, order, False)
1136 pub fn atomic_fence(&self, order: AtomicOrdering, scope: SynchronizationScope) {
1138 llvm::LLVMRustBuildAtomicFence(self.llbuilder, order, scope);
1142 pub fn add_case(&self, s: ValueRef, on_val: ValueRef, dest: BasicBlockRef) {
1144 if llvm::LLVMIsUndef(s) == llvm::True { return; }
1145 llvm::LLVMAddCase(s, on_val, dest)
1149 pub fn add_incoming_to_phi(&self, phi: ValueRef, val: ValueRef, bb: BasicBlockRef) {
1151 if llvm::LLVMIsUndef(phi) == llvm::True { return; }
1152 llvm::LLVMAddIncoming(phi, &val, &bb, 1 as c_uint);
1156 pub fn set_invariant_load(&self, load: ValueRef) {
1158 llvm::LLVMSetMetadata(load, llvm::MD_invariant_load as c_uint,
1159 llvm::LLVMMDNodeInContext(self.ccx.llcx(), ptr::null(), 0));
1163 /// Returns the ptr value that should be used for storing `val`.
1164 fn check_store<'b>(&self,
1166 ptr: ValueRef) -> ValueRef {
1167 let dest_ptr_ty = val_ty(ptr);
1168 let stored_ty = val_ty(val);
1169 let stored_ptr_ty = stored_ty.ptr_to();
1171 assert_eq!(dest_ptr_ty.kind(), llvm::TypeKind::Pointer);
1173 if dest_ptr_ty == stored_ptr_ty {
1176 debug!("Type mismatch in store. \
1177 Expected {:?}, got {:?}; inserting bitcast",
1178 dest_ptr_ty, stored_ptr_ty);
1179 self.bitcast(ptr, stored_ptr_ty)
1183 /// Returns the args that should be used for a call to `llfn`.
1184 fn check_call<'b>(&self,
1187 args: &'b [ValueRef]) -> Cow<'b, [ValueRef]> {
1188 let mut fn_ty = val_ty(llfn);
1189 // Strip off pointers
1190 while fn_ty.kind() == llvm::TypeKind::Pointer {
1191 fn_ty = fn_ty.element_type();
1194 assert!(fn_ty.kind() == llvm::TypeKind::Function,
1195 "builder::{} not passed a function, but {:?}", typ, fn_ty);
1197 let param_tys = fn_ty.func_params();
1199 let all_args_match = param_tys.iter()
1200 .zip(args.iter().map(|&v| val_ty(v)))
1201 .all(|(expected_ty, actual_ty)| *expected_ty == actual_ty);
1204 return Cow::Borrowed(args);
1207 let casted_args: Vec<_> = param_tys.into_iter()
1210 .map(|(i, (expected_ty, &actual_val))| {
1211 let actual_ty = val_ty(actual_val);
1212 if expected_ty != actual_ty {
1213 debug!("Type mismatch in function call of {:?}. \
1214 Expected {:?} for param {}, got {:?}; injecting bitcast",
1216 expected_ty, i, actual_ty);
1217 self.bitcast(actual_val, expected_ty)
1224 return Cow::Owned(casted_args);