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.
13 use lib::llvm::{CallConv, AtomicBinOp, AtomicOrdering, AsmDialect};
14 use lib::llvm::{Opcode, IntPredicate, RealPredicate, False};
15 use lib::llvm::{ValueRef, BasicBlockRef, BuilderRef, ModuleRef};
16 use middle::trans::base;
17 use middle::trans::common::*;
18 use middle::trans::machine::llalign_of_pref;
19 use middle::trans::type_::Type;
21 use collections::HashMap;
22 use std::libc::{c_uint, c_ulonglong, c_char};
23 use syntax::codemap::Span;
25 pub struct Builder<'a> {
26 llbuilder: BuilderRef,
27 ccx: &'a CrateContext,
30 // This is a really awful way to get a zero-length c-string, but better (and a
31 // lot more efficient) than doing str::as_c_str("", ...) every time.
32 pub fn noname() -> *c_char {
33 static cnull: c_char = 0;
37 impl<'a> Builder<'a> {
38 pub fn new(ccx: &'a CrateContext) -> Builder<'a> {
40 llbuilder: ccx.builder.b,
45 pub fn count_insn(&self, category: &str) {
46 if self.ccx.sess().trans_stats() {
47 self.ccx.stats.n_llvm_insns.set(self.ccx
52 if self.ccx.sess().count_llvm_insns() {
53 base::with_insn_ctxt(|v| {
54 let mut h = self.ccx.stats.llvm_insns.borrow_mut();
56 // Build version of path with cycles removed.
58 // Pass 1: scan table mapping str -> rightmost pos.
59 let mut mm = HashMap::new();
67 // Pass 2: concat strings for each elt, skipping
68 // forwards over any cycles by advancing to rightmost
69 // occurrence of each element in path.
82 let n = match h.get().find(&s) {
86 h.get().insert(s, n+1u);
91 pub fn position_before(&self, insn: ValueRef) {
93 llvm::LLVMPositionBuilderBefore(self.llbuilder, insn);
97 pub fn position_at_end(&self, llbb: BasicBlockRef) {
99 llvm::LLVMPositionBuilderAtEnd(self.llbuilder, llbb);
103 pub fn ret_void(&self) {
104 self.count_insn("retvoid");
106 llvm::LLVMBuildRetVoid(self.llbuilder);
110 pub fn ret(&self, v: ValueRef) {
111 self.count_insn("ret");
113 llvm::LLVMBuildRet(self.llbuilder, v);
117 pub fn aggregate_ret(&self, ret_vals: &[ValueRef]) {
119 llvm::LLVMBuildAggregateRet(self.llbuilder,
121 ret_vals.len() as c_uint);
125 pub fn br(&self, dest: BasicBlockRef) {
126 self.count_insn("br");
128 llvm::LLVMBuildBr(self.llbuilder, dest);
132 pub fn cond_br(&self, cond: ValueRef, then_llbb: BasicBlockRef, else_llbb: BasicBlockRef) {
133 self.count_insn("condbr");
135 llvm::LLVMBuildCondBr(self.llbuilder, cond, then_llbb, else_llbb);
139 pub fn switch(&self, v: ValueRef, else_llbb: BasicBlockRef, num_cases: uint) -> ValueRef {
141 llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, num_cases as c_uint)
145 pub fn indirect_br(&self, addr: ValueRef, num_dests: uint) {
146 self.count_insn("indirectbr");
148 llvm::LLVMBuildIndirectBr(self.llbuilder, addr, num_dests as c_uint);
156 catch: BasicBlockRef,
157 attributes: &[(uint, lib::llvm::Attribute)])
159 self.count_insn("invoke");
161 let v = llvm::LLVMBuildInvoke(self.llbuilder,
164 args.len() as c_uint,
168 for &(idx, attr) in attributes.iter() {
169 llvm::LLVMAddInstrAttribute(v, idx as c_uint, attr as c_uint);
175 pub fn unreachable(&self) {
176 self.count_insn("unreachable");
178 llvm::LLVMBuildUnreachable(self.llbuilder);
183 pub fn add(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
184 self.count_insn("add");
186 llvm::LLVMBuildAdd(self.llbuilder, lhs, rhs, noname())
190 pub fn nswadd(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
191 self.count_insn("nswadd");
193 llvm::LLVMBuildNSWAdd(self.llbuilder, lhs, rhs, noname())
197 pub fn nuwadd(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
198 self.count_insn("nuwadd");
200 llvm::LLVMBuildNUWAdd(self.llbuilder, lhs, rhs, noname())
204 pub fn fadd(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
205 self.count_insn("fadd");
207 llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, noname())
211 pub fn sub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
212 self.count_insn("sub");
214 llvm::LLVMBuildSub(self.llbuilder, lhs, rhs, noname())
218 pub fn nswsub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
219 self.count_insn("nwsub");
221 llvm::LLVMBuildNSWSub(self.llbuilder, lhs, rhs, noname())
225 pub fn nuwsub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
226 self.count_insn("nuwsub");
228 llvm::LLVMBuildNUWSub(self.llbuilder, lhs, rhs, noname())
232 pub fn fsub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
233 self.count_insn("sub");
235 llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, noname())
239 pub fn mul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
240 self.count_insn("mul");
242 llvm::LLVMBuildMul(self.llbuilder, lhs, rhs, noname())
246 pub fn nswmul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
247 self.count_insn("nswmul");
249 llvm::LLVMBuildNSWMul(self.llbuilder, lhs, rhs, noname())
253 pub fn nuwmul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
254 self.count_insn("nuwmul");
256 llvm::LLVMBuildNUWMul(self.llbuilder, lhs, rhs, noname())
260 pub fn fmul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
261 self.count_insn("fmul");
263 llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, noname())
267 pub fn udiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
268 self.count_insn("udiv");
270 llvm::LLVMBuildUDiv(self.llbuilder, lhs, rhs, noname())
274 pub fn sdiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
275 self.count_insn("sdiv");
277 llvm::LLVMBuildSDiv(self.llbuilder, lhs, rhs, noname())
281 pub fn exactsdiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
282 self.count_insn("exactsdiv");
284 llvm::LLVMBuildExactSDiv(self.llbuilder, lhs, rhs, noname())
288 pub fn fdiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
289 self.count_insn("fdiv");
291 llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, noname())
295 pub fn urem(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
296 self.count_insn("urem");
298 llvm::LLVMBuildURem(self.llbuilder, lhs, rhs, noname())
302 pub fn srem(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
303 self.count_insn("srem");
305 llvm::LLVMBuildSRem(self.llbuilder, lhs, rhs, noname())
309 pub fn frem(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
310 self.count_insn("frem");
312 llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, noname())
316 pub fn shl(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
317 self.count_insn("shl");
319 llvm::LLVMBuildShl(self.llbuilder, lhs, rhs, noname())
323 pub fn lshr(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
324 self.count_insn("lshr");
326 llvm::LLVMBuildLShr(self.llbuilder, lhs, rhs, noname())
330 pub fn ashr(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
331 self.count_insn("ashr");
333 llvm::LLVMBuildAShr(self.llbuilder, lhs, rhs, noname())
337 pub fn and(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
338 self.count_insn("and");
340 llvm::LLVMBuildAnd(self.llbuilder, lhs, rhs, noname())
344 pub fn or(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
345 self.count_insn("or");
347 llvm::LLVMBuildOr(self.llbuilder, lhs, rhs, noname())
351 pub fn xor(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
352 self.count_insn("xor");
354 llvm::LLVMBuildXor(self.llbuilder, lhs, rhs, noname())
358 pub fn binop(&self, op: Opcode, lhs: ValueRef, rhs: ValueRef)
360 self.count_insn("binop");
362 llvm::LLVMBuildBinOp(self.llbuilder, op, lhs, rhs, noname())
366 pub fn neg(&self, v: ValueRef) -> ValueRef {
367 self.count_insn("neg");
369 llvm::LLVMBuildNeg(self.llbuilder, v, noname())
373 pub fn nswneg(&self, v: ValueRef) -> ValueRef {
374 self.count_insn("nswneg");
376 llvm::LLVMBuildNSWNeg(self.llbuilder, v, noname())
380 pub fn nuwneg(&self, v: ValueRef) -> ValueRef {
381 self.count_insn("nuwneg");
383 llvm::LLVMBuildNUWNeg(self.llbuilder, v, noname())
386 pub fn fneg(&self, v: ValueRef) -> ValueRef {
387 self.count_insn("fneg");
389 llvm::LLVMBuildFNeg(self.llbuilder, v, noname())
393 pub fn not(&self, v: ValueRef) -> ValueRef {
394 self.count_insn("not");
396 llvm::LLVMBuildNot(self.llbuilder, v, noname())
401 pub fn malloc(&self, ty: Type) -> ValueRef {
402 self.count_insn("malloc");
404 llvm::LLVMBuildMalloc(self.llbuilder, ty.to_ref(), noname())
408 pub fn array_malloc(&self, ty: Type, val: ValueRef) -> ValueRef {
409 self.count_insn("arraymalloc");
411 llvm::LLVMBuildArrayMalloc(self.llbuilder, ty.to_ref(), val, noname())
415 pub fn alloca(&self, ty: Type, name: &str) -> ValueRef {
416 self.count_insn("alloca");
419 llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(), noname())
421 name.with_c_str(|c| {
422 llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(), c)
428 pub fn array_alloca(&self, ty: Type, val: ValueRef) -> ValueRef {
429 self.count_insn("arrayalloca");
431 llvm::LLVMBuildArrayAlloca(self.llbuilder, ty.to_ref(), val, noname())
435 pub fn free(&self, ptr: ValueRef) {
436 self.count_insn("free");
438 llvm::LLVMBuildFree(self.llbuilder, ptr);
442 pub fn load(&self, ptr: ValueRef) -> ValueRef {
443 self.count_insn("load");
445 llvm::LLVMBuildLoad(self.llbuilder, ptr, noname())
449 pub fn volatile_load(&self, ptr: ValueRef) -> ValueRef {
450 self.count_insn("load.volatile");
452 let insn = llvm::LLVMBuildLoad(self.llbuilder, ptr, noname());
453 llvm::LLVMSetVolatile(insn, lib::llvm::True);
458 pub fn atomic_load(&self, ptr: ValueRef, order: AtomicOrdering) -> ValueRef {
459 self.count_insn("load.atomic");
461 let ty = Type::from_ref(llvm::LLVMTypeOf(ptr));
462 let align = llalign_of_pref(self.ccx, ty.element_type());
463 llvm::LLVMBuildAtomicLoad(self.llbuilder, ptr, noname(), order,
469 pub fn load_range_assert(&self, ptr: ValueRef, lo: c_ulonglong,
470 hi: c_ulonglong, signed: lib::llvm::Bool) -> ValueRef {
471 let value = self.load(ptr);
474 let t = llvm::LLVMGetElementType(llvm::LLVMTypeOf(ptr));
475 let min = llvm::LLVMConstInt(t, lo, signed);
476 let max = llvm::LLVMConstInt(t, hi, signed);
480 llvm::LLVMSetMetadata(value, lib::llvm::MD_range as c_uint,
481 llvm::LLVMMDNodeInContext(self.ccx.llcx,
482 v.as_ptr(), v.len() as c_uint));
488 pub fn store(&self, val: ValueRef, ptr: ValueRef) {
489 debug!("Store {} -> {}",
490 self.ccx.tn.val_to_str(val),
491 self.ccx.tn.val_to_str(ptr));
492 assert!(self.llbuilder.is_not_null());
493 self.count_insn("store");
495 llvm::LLVMBuildStore(self.llbuilder, val, ptr);
499 pub fn volatile_store(&self, val: ValueRef, ptr: ValueRef) {
500 debug!("Store {} -> {}",
501 self.ccx.tn.val_to_str(val),
502 self.ccx.tn.val_to_str(ptr));
503 assert!(self.llbuilder.is_not_null());
504 self.count_insn("store.volatile");
506 let insn = llvm::LLVMBuildStore(self.llbuilder, val, ptr);
507 llvm::LLVMSetVolatile(insn, lib::llvm::True);
511 pub fn atomic_store(&self, val: ValueRef, ptr: ValueRef, order: AtomicOrdering) {
512 debug!("Store {} -> {}",
513 self.ccx.tn.val_to_str(val),
514 self.ccx.tn.val_to_str(ptr));
515 self.count_insn("store.atomic");
517 let ty = Type::from_ref(llvm::LLVMTypeOf(ptr));
518 let align = llalign_of_pref(self.ccx, ty.element_type());
519 llvm::LLVMBuildAtomicStore(self.llbuilder, val, ptr, order, align as c_uint);
523 pub fn gep(&self, ptr: ValueRef, indices: &[ValueRef]) -> ValueRef {
524 self.count_insn("gep");
526 llvm::LLVMBuildGEP(self.llbuilder, ptr, indices.as_ptr(),
527 indices.len() as c_uint, noname())
531 // Simple wrapper around GEP that takes an array of ints and wraps them
534 pub fn gepi(&self, base: ValueRef, ixs: &[uint]) -> ValueRef {
535 // Small vector optimization. This should catch 100% of the cases that
538 let mut small_vec = [ C_i32(self.ccx, 0), ..16 ];
539 for (small_vec_e, &ix) in small_vec.mut_iter().zip(ixs.iter()) {
540 *small_vec_e = C_i32(self.ccx, ix as i32);
542 self.inbounds_gep(base, small_vec.slice(0, ixs.len()))
544 let v = ixs.iter().map(|i| C_i32(self.ccx, *i as i32)).collect::<Vec<ValueRef>>();
545 self.count_insn("gepi");
546 self.inbounds_gep(base, v.as_slice())
550 pub fn inbounds_gep(&self, ptr: ValueRef, indices: &[ValueRef]) -> ValueRef {
551 self.count_insn("inboundsgep");
553 llvm::LLVMBuildInBoundsGEP(
554 self.llbuilder, ptr, indices.as_ptr(), indices.len() as c_uint, noname())
558 pub fn struct_gep(&self, ptr: ValueRef, idx: uint) -> ValueRef {
559 self.count_insn("structgep");
561 llvm::LLVMBuildStructGEP(self.llbuilder, ptr, idx as c_uint, noname())
565 pub fn global_string(&self, _str: *c_char) -> ValueRef {
566 self.count_insn("globalstring");
568 llvm::LLVMBuildGlobalString(self.llbuilder, _str, noname())
572 pub fn global_string_ptr(&self, _str: *c_char) -> ValueRef {
573 self.count_insn("globalstringptr");
575 llvm::LLVMBuildGlobalStringPtr(self.llbuilder, _str, noname())
580 pub fn trunc(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
581 self.count_insn("trunc");
583 llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty.to_ref(), noname())
587 pub fn zext(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
588 self.count_insn("zext");
590 llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty.to_ref(), noname())
594 pub fn sext(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
595 self.count_insn("sext");
597 llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty.to_ref(), noname())
601 pub fn fptoui(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
602 self.count_insn("fptoui");
604 llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty.to_ref(), noname())
608 pub fn fptosi(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
609 self.count_insn("fptosi");
611 llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty.to_ref(),noname())
615 pub fn uitofp(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
616 self.count_insn("uitofp");
618 llvm::LLVMBuildUIToFP(self.llbuilder, val, dest_ty.to_ref(), noname())
622 pub fn sitofp(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
623 self.count_insn("sitofp");
625 llvm::LLVMBuildSIToFP(self.llbuilder, val, dest_ty.to_ref(), noname())
629 pub fn fptrunc(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
630 self.count_insn("fptrunc");
632 llvm::LLVMBuildFPTrunc(self.llbuilder, val, dest_ty.to_ref(), noname())
636 pub fn fpext(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
637 self.count_insn("fpext");
639 llvm::LLVMBuildFPExt(self.llbuilder, val, dest_ty.to_ref(), noname())
643 pub fn ptrtoint(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
644 self.count_insn("ptrtoint");
646 llvm::LLVMBuildPtrToInt(self.llbuilder, val, dest_ty.to_ref(), noname())
650 pub fn inttoptr(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
651 self.count_insn("inttoptr");
653 llvm::LLVMBuildIntToPtr(self.llbuilder, val, dest_ty.to_ref(), noname())
657 pub fn bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
658 self.count_insn("bitcast");
660 llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
664 pub fn zext_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
665 self.count_insn("zextorbitcast");
667 llvm::LLVMBuildZExtOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
671 pub fn sext_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
672 self.count_insn("sextorbitcast");
674 llvm::LLVMBuildSExtOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
678 pub fn trunc_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
679 self.count_insn("truncorbitcast");
681 llvm::LLVMBuildTruncOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
685 pub fn cast(&self, op: Opcode, val: ValueRef, dest_ty: Type) -> ValueRef {
686 self.count_insn("cast");
688 llvm::LLVMBuildCast(self.llbuilder, op, val, dest_ty.to_ref(), noname())
692 pub fn pointercast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
693 self.count_insn("pointercast");
695 llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty.to_ref(), noname())
699 pub fn intcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
700 self.count_insn("intcast");
702 llvm::LLVMBuildIntCast(self.llbuilder, val, dest_ty.to_ref(), noname())
706 pub fn fpcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
707 self.count_insn("fpcast");
709 llvm::LLVMBuildFPCast(self.llbuilder, val, dest_ty.to_ref(), noname())
715 pub fn icmp(&self, op: IntPredicate, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
716 self.count_insn("icmp");
718 llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, noname())
722 pub fn fcmp(&self, op: RealPredicate, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
723 self.count_insn("fcmp");
725 llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, noname())
729 /* Miscellaneous instructions */
730 pub fn empty_phi(&self, ty: Type) -> ValueRef {
731 self.count_insn("emptyphi");
733 llvm::LLVMBuildPhi(self.llbuilder, ty.to_ref(), noname())
737 pub fn phi(&self, ty: Type, vals: &[ValueRef], bbs: &[BasicBlockRef]) -> ValueRef {
738 assert_eq!(vals.len(), bbs.len());
739 let phi = self.empty_phi(ty);
740 self.count_insn("addincoming");
742 llvm::LLVMAddIncoming(phi, vals.as_ptr(),
744 vals.len() as c_uint);
749 pub fn add_span_comment(&self, sp: Span, text: &str) {
750 if self.ccx.sess().asm_comments() {
751 let s = format!("{} ({})", text, self.ccx.sess().codemap().span_to_str(sp));
757 pub fn add_comment(&self, text: &str) {
758 if self.ccx.sess().asm_comments() {
759 let sanitized = text.replace("$", "");
760 let comment_text = format!("\\# {}", sanitized.replace("\n", "\n\t# "));
761 self.count_insn("inlineasm");
762 let asm = comment_text.with_c_str(|c| {
764 llvm::LLVMConstInlineAsm(Type::func([], &Type::void(self.ccx)).to_ref(),
765 c, noname(), False, False)
768 self.call(asm, [], []);
772 pub fn inline_asm_call(&self, asm: *c_char, cons: *c_char,
773 inputs: &[ValueRef], output: Type,
774 volatile: bool, alignstack: bool,
775 dia: AsmDialect) -> ValueRef {
776 self.count_insn("inlineasm");
778 let volatile = if volatile { lib::llvm::True }
779 else { lib::llvm::False };
780 let alignstack = if alignstack { lib::llvm::True }
781 else { lib::llvm::False };
783 let argtys = inputs.map(|v| {
784 debug!("Asm Input Type: {:?}", self.ccx.tn.val_to_str(*v));
788 debug!("Asm Output Type: {:?}", self.ccx.tn.type_to_str(output));
789 let fty = Type::func(argtys, &output);
791 let v = llvm::LLVMInlineAsm(
792 fty.to_ref(), asm, cons, volatile, alignstack, dia as c_uint);
793 self.call(v, inputs, [])
797 pub fn call(&self, llfn: ValueRef, args: &[ValueRef],
798 attributes: &[(uint, lib::llvm::Attribute)]) -> ValueRef {
799 self.count_insn("call");
801 debug!("Call {} with args ({})",
802 self.ccx.tn.val_to_str(llfn),
803 args.map(|&v| self.ccx.tn.val_to_str(v)).connect(", "));
806 let v = llvm::LLVMBuildCall(self.llbuilder, llfn, args.as_ptr(),
807 args.len() as c_uint, noname());
808 for &(idx, attr) in attributes.iter() {
809 llvm::LLVMAddInstrAttribute(v, idx as c_uint, attr as c_uint);
815 pub fn call_with_conv(&self, llfn: ValueRef, args: &[ValueRef],
816 conv: CallConv, attributes: &[(uint, lib::llvm::Attribute)]) -> ValueRef {
817 self.count_insn("callwithconv");
818 let v = self.call(llfn, args, attributes);
819 lib::llvm::SetInstructionCallConv(v, conv);
823 pub fn select(&self, cond: ValueRef, then_val: ValueRef, else_val: ValueRef) -> ValueRef {
824 self.count_insn("select");
826 llvm::LLVMBuildSelect(self.llbuilder, cond, then_val, else_val, noname())
830 pub fn va_arg(&self, list: ValueRef, ty: Type) -> ValueRef {
831 self.count_insn("vaarg");
833 llvm::LLVMBuildVAArg(self.llbuilder, list, ty.to_ref(), noname())
837 pub fn extract_element(&self, vec: ValueRef, idx: ValueRef) -> ValueRef {
838 self.count_insn("extractelement");
840 llvm::LLVMBuildExtractElement(self.llbuilder, vec, idx, noname())
844 pub fn insert_element(&self, vec: ValueRef, elt: ValueRef, idx: ValueRef) -> ValueRef {
845 self.count_insn("insertelement");
847 llvm::LLVMBuildInsertElement(self.llbuilder, vec, elt, idx, noname())
851 pub fn shuffle_vector(&self, v1: ValueRef, v2: ValueRef, mask: ValueRef) -> ValueRef {
852 self.count_insn("shufflevector");
854 llvm::LLVMBuildShuffleVector(self.llbuilder, v1, v2, mask, noname())
858 pub fn vector_splat(&self, num_elts: uint, elt: ValueRef) -> ValueRef {
860 let elt_ty = val_ty(elt);
861 let undef = llvm::LLVMGetUndef(Type::vector(&elt_ty, num_elts as u64).to_ref());
862 let vec = self.insert_element(undef, elt, C_i32(self.ccx, 0));
863 let vec_i32_ty = Type::vector(&Type::i32(self.ccx), num_elts as u64);
864 self.shuffle_vector(vec, undef, C_null(vec_i32_ty))
868 pub fn extract_value(&self, agg_val: ValueRef, idx: uint) -> ValueRef {
869 self.count_insn("extractvalue");
871 llvm::LLVMBuildExtractValue(self.llbuilder, agg_val, idx as c_uint, noname())
875 pub fn insert_value(&self, agg_val: ValueRef, elt: ValueRef,
876 idx: uint) -> ValueRef {
877 self.count_insn("insertvalue");
879 llvm::LLVMBuildInsertValue(self.llbuilder, agg_val, elt, idx as c_uint,
884 pub fn is_null(&self, val: ValueRef) -> ValueRef {
885 self.count_insn("isnull");
887 llvm::LLVMBuildIsNull(self.llbuilder, val, noname())
891 pub fn is_not_null(&self, val: ValueRef) -> ValueRef {
892 self.count_insn("isnotnull");
894 llvm::LLVMBuildIsNotNull(self.llbuilder, val, noname())
898 pub fn ptrdiff(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
899 self.count_insn("ptrdiff");
901 llvm::LLVMBuildPtrDiff(self.llbuilder, lhs, rhs, noname())
907 let bb: BasicBlockRef = llvm::LLVMGetInsertBlock(self.llbuilder);
908 let fn_: ValueRef = llvm::LLVMGetBasicBlockParent(bb);
909 let m: ModuleRef = llvm::LLVMGetGlobalParent(fn_);
910 let t: ValueRef = "llvm.trap".with_c_str(|buf| {
911 llvm::LLVMGetNamedFunction(m, buf)
913 assert!((t as int != 0));
914 let args: &[ValueRef] = [];
915 self.count_insn("trap");
917 self.llbuilder, t, args.as_ptr(), args.len() as c_uint, noname());
921 pub fn landing_pad(&self, ty: Type, pers_fn: ValueRef, num_clauses: uint) -> ValueRef {
922 self.count_insn("landingpad");
924 llvm::LLVMBuildLandingPad(
925 self.llbuilder, ty.to_ref(), pers_fn, num_clauses as c_uint, noname())
929 pub fn set_cleanup(&self, landing_pad: ValueRef) {
930 self.count_insn("setcleanup");
932 llvm::LLVMSetCleanup(landing_pad, lib::llvm::True);
936 pub fn resume(&self, exn: ValueRef) -> ValueRef {
937 self.count_insn("resume");
939 llvm::LLVMBuildResume(self.llbuilder, exn)
944 pub fn atomic_cmpxchg(&self, dst: ValueRef,
945 cmp: ValueRef, src: ValueRef,
946 order: AtomicOrdering) -> ValueRef {
948 llvm::LLVMBuildAtomicCmpXchg(self.llbuilder, dst, cmp, src, order)
951 pub fn atomic_rmw(&self, op: AtomicBinOp,
952 dst: ValueRef, src: ValueRef,
953 order: AtomicOrdering) -> ValueRef {
955 llvm::LLVMBuildAtomicRMW(self.llbuilder, op, dst, src, order, False)
959 pub fn atomic_fence(&self, order: AtomicOrdering) {
961 llvm::LLVMBuildAtomicFence(self.llbuilder, order);