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_min;
19 use middle::trans::type_::Type;
21 use std::hashmap::HashMap;
22 use std::libc::{c_uint, c_ulonglong, c_char};
25 use syntax::codemap::span;
28 llbuilder: BuilderRef,
29 ccx: @mut CrateContext,
32 // This is a really awful way to get a zero-length c-string, but better (and a
33 // lot more efficient) than doing str::as_c_str("", ...) every time.
34 pub fn noname() -> *c_char {
36 static cnull: uint = 0u;
37 cast::transmute(&cnull)
42 pub fn new(ccx: @mut CrateContext) -> Builder {
44 llbuilder: ccx.builder.B,
49 pub fn count_insn(&self, category: &str) {
50 if self.ccx.sess.trans_stats() {
51 self.ccx.stats.n_llvm_insns += 1;
53 if self.ccx.sess.count_llvm_insns() {
54 do base::with_insn_ctxt |v| {
55 let h = &mut self.ccx.stats.llvm_insns;
57 // Build version of path with cycles removed.
59 // Pass 1: scan table mapping str -> rightmost pos.
60 let mut mm = HashMap::new();
68 // Pass 2: concat strings for each elt, skipping
69 // forwards over any cycles by advancing to rightmost
70 // occurrence of each element in path.
83 let n = match h.find(&s) {
92 pub fn position_before(&self, insn: ValueRef) {
94 llvm::LLVMPositionBuilderBefore(self.llbuilder, insn);
98 pub fn position_at_end(&self, llbb: BasicBlockRef) {
100 llvm::LLVMPositionBuilderAtEnd(self.llbuilder, llbb);
104 pub fn ret_void(&self) {
105 self.count_insn("retvoid");
107 llvm::LLVMBuildRetVoid(self.llbuilder);
111 pub fn ret(&self, v: ValueRef) {
112 self.count_insn("ret");
114 llvm::LLVMBuildRet(self.llbuilder, v);
118 pub fn aggregate_ret(&self, ret_vals: &[ValueRef]) {
120 llvm::LLVMBuildAggregateRet(self.llbuilder,
121 vec::raw::to_ptr(ret_vals),
122 ret_vals.len() as c_uint);
126 pub fn br(&self, dest: BasicBlockRef) {
127 self.count_insn("br");
129 llvm::LLVMBuildBr(self.llbuilder, dest);
133 pub fn cond_br(&self, cond: ValueRef, then_llbb: BasicBlockRef, else_llbb: BasicBlockRef) {
134 self.count_insn("condbr");
136 llvm::LLVMBuildCondBr(self.llbuilder, cond, then_llbb, else_llbb);
140 pub fn switch(&self, v: ValueRef, else_llbb: BasicBlockRef, num_cases: uint) -> ValueRef {
142 llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, num_cases as c_uint)
146 pub fn indirect_br(&self, addr: ValueRef, num_dests: uint) {
147 self.count_insn("indirectbr");
149 llvm::LLVMBuildIndirectBr(self.llbuilder, addr, num_dests as c_uint);
157 catch: BasicBlockRef)
159 self.count_insn("invoke");
161 llvm::LLVMBuildInvoke(self.llbuilder,
163 vec::raw::to_ptr(args),
164 args.len() as c_uint,
171 pub fn fast_invoke(&self,
175 catch: BasicBlockRef) {
176 self.count_insn("fastinvoke");
177 let v = self.invoke(llfn, args, then, catch);
178 lib::llvm::SetInstructionCallConv(v, lib::llvm::FastCallConv);
181 pub fn unreachable(&self) {
182 self.count_insn("unreachable");
184 llvm::LLVMBuildUnreachable(self.llbuilder);
189 pub fn add(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
190 self.count_insn("add");
192 llvm::LLVMBuildAdd(self.llbuilder, lhs, rhs, noname())
196 pub fn nswadd(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
197 self.count_insn("nswadd");
199 llvm::LLVMBuildNSWAdd(self.llbuilder, lhs, rhs, noname())
203 pub fn nuwadd(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
204 self.count_insn("nuwadd");
206 llvm::LLVMBuildNUWAdd(self.llbuilder, lhs, rhs, noname())
210 pub fn fadd(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
211 self.count_insn("fadd");
213 llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, noname())
217 pub fn sub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
218 self.count_insn("sub");
220 llvm::LLVMBuildSub(self.llbuilder, lhs, rhs, noname())
224 pub fn nswsub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
225 self.count_insn("nwsub");
227 llvm::LLVMBuildNSWSub(self.llbuilder, lhs, rhs, noname())
231 pub fn nuwsub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
232 self.count_insn("nuwsub");
234 llvm::LLVMBuildNUWSub(self.llbuilder, lhs, rhs, noname())
238 pub fn fsub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
239 self.count_insn("sub");
241 llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, noname())
245 pub fn mul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
246 self.count_insn("mul");
248 llvm::LLVMBuildMul(self.llbuilder, lhs, rhs, noname())
252 pub fn nswmul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
253 self.count_insn("nswmul");
255 llvm::LLVMBuildNSWMul(self.llbuilder, lhs, rhs, noname())
259 pub fn nuwmul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
260 self.count_insn("nuwmul");
262 llvm::LLVMBuildNUWMul(self.llbuilder, lhs, rhs, noname())
266 pub fn fmul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
267 self.count_insn("fmul");
269 llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, noname())
273 pub fn udiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
274 self.count_insn("udiv");
276 llvm::LLVMBuildUDiv(self.llbuilder, lhs, rhs, noname())
280 pub fn sdiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
281 self.count_insn("sdiv");
283 llvm::LLVMBuildSDiv(self.llbuilder, lhs, rhs, noname())
287 pub fn exactsdiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
288 self.count_insn("exactsdiv");
290 llvm::LLVMBuildExactSDiv(self.llbuilder, lhs, rhs, noname())
294 pub fn fdiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
295 self.count_insn("fdiv");
297 llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, noname())
301 pub fn urem(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
302 self.count_insn("urem");
304 llvm::LLVMBuildURem(self.llbuilder, lhs, rhs, noname())
308 pub fn srem(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
309 self.count_insn("srem");
311 llvm::LLVMBuildSRem(self.llbuilder, lhs, rhs, noname())
315 pub fn frem(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
316 self.count_insn("frem");
318 llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, noname())
322 pub fn shl(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
323 self.count_insn("shl");
325 llvm::LLVMBuildShl(self.llbuilder, lhs, rhs, noname())
329 pub fn lshr(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
330 self.count_insn("lshr");
332 llvm::LLVMBuildLShr(self.llbuilder, lhs, rhs, noname())
336 pub fn ashr(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
337 self.count_insn("ashr");
339 llvm::LLVMBuildAShr(self.llbuilder, lhs, rhs, noname())
343 pub fn and(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
344 self.count_insn("and");
346 llvm::LLVMBuildAnd(self.llbuilder, lhs, rhs, noname())
350 pub fn or(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
351 self.count_insn("or");
353 llvm::LLVMBuildOr(self.llbuilder, lhs, rhs, noname())
357 pub fn xor(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
358 self.count_insn("xor");
360 llvm::LLVMBuildXor(self.llbuilder, lhs, rhs, noname())
364 pub fn binop(&self, op: Opcode, lhs: ValueRef, rhs: ValueRef)
366 self.count_insn("binop");
368 llvm::LLVMBuildBinOp(self.llbuilder, op, lhs, rhs, noname())
372 pub fn neg(&self, V: ValueRef) -> ValueRef {
373 self.count_insn("neg");
375 llvm::LLVMBuildNeg(self.llbuilder, V, noname())
379 pub fn nswneg(&self, V: ValueRef) -> ValueRef {
380 self.count_insn("nswneg");
382 llvm::LLVMBuildNSWNeg(self.llbuilder, V, noname())
386 pub fn nuwneg(&self, V: ValueRef) -> ValueRef {
387 self.count_insn("nuwneg");
389 llvm::LLVMBuildNUWNeg(self.llbuilder, V, noname())
392 pub fn fneg(&self, V: ValueRef) -> ValueRef {
393 self.count_insn("fneg");
395 llvm::LLVMBuildFNeg(self.llbuilder, V, noname())
399 pub fn not(&self, V: ValueRef) -> ValueRef {
400 self.count_insn("not");
402 llvm::LLVMBuildNot(self.llbuilder, V, noname())
407 pub fn malloc(&self, ty: Type) -> ValueRef {
408 self.count_insn("malloc");
410 llvm::LLVMBuildMalloc(self.llbuilder, ty.to_ref(), noname())
414 pub fn array_malloc(&self, ty: Type, val: ValueRef) -> ValueRef {
415 self.count_insn("arraymalloc");
417 llvm::LLVMBuildArrayMalloc(self.llbuilder, ty.to_ref(), val, noname())
421 pub fn alloca(&self, ty: Type, name: &str) -> ValueRef {
422 self.count_insn("alloca");
425 llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(), noname())
429 |c| llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(), c))
434 pub fn array_alloca(&self, ty: Type, val: ValueRef) -> ValueRef {
435 self.count_insn("arrayalloca");
437 llvm::LLVMBuildArrayAlloca(self.llbuilder, ty.to_ref(), val, noname())
441 pub fn free(&self, ptr: ValueRef) {
442 self.count_insn("free");
444 llvm::LLVMBuildFree(self.llbuilder, ptr);
448 pub fn load(&self, ptr: ValueRef) -> ValueRef {
449 self.count_insn("load");
451 llvm::LLVMBuildLoad(self.llbuilder, ptr, noname())
455 pub fn atomic_load(&self, ptr: ValueRef, order: AtomicOrdering) -> ValueRef {
456 self.count_insn("load.atomic");
458 let align = llalign_of_min(self.ccx, self.ccx.int_type);
459 llvm::LLVMBuildAtomicLoad(self.llbuilder, ptr, noname(), order, align as c_uint)
464 pub fn load_range_assert(&self, ptr: ValueRef, lo: c_ulonglong,
465 hi: c_ulonglong, signed: lib::llvm::Bool) -> ValueRef {
466 let value = self.load(ptr);
469 let t = llvm::LLVMGetElementType(llvm::LLVMTypeOf(ptr));
470 let min = llvm::LLVMConstInt(t, lo, signed);
471 let max = llvm::LLVMConstInt(t, hi, signed);
473 do [min, max].as_imm_buf |ptr, len| {
474 llvm::LLVMSetMetadata(value, lib::llvm::MD_range as c_uint,
475 llvm::LLVMMDNodeInContext(self.ccx.llcx,
476 ptr, len as c_uint));
483 pub fn store(&self, val: ValueRef, ptr: ValueRef) {
484 debug!("Store %s -> %s",
485 self.ccx.tn.val_to_str(val),
486 self.ccx.tn.val_to_str(ptr));
487 self.count_insn("store");
489 llvm::LLVMBuildStore(self.llbuilder, val, ptr);
493 pub fn atomic_store(&self, val: ValueRef, ptr: ValueRef, order: AtomicOrdering) {
494 debug!("Store %s -> %s",
495 self.ccx.tn.val_to_str(val),
496 self.ccx.tn.val_to_str(ptr));
497 self.count_insn("store.atomic");
498 let align = llalign_of_min(self.ccx, self.ccx.int_type);
500 llvm::LLVMBuildAtomicStore(self.llbuilder, val, ptr, order, align as c_uint);
504 pub fn gep(&self, ptr: ValueRef, indices: &[ValueRef]) -> ValueRef {
505 self.count_insn("gep");
507 llvm::LLVMBuildGEP(self.llbuilder, ptr, vec::raw::to_ptr(indices),
508 indices.len() as c_uint, noname())
512 // Simple wrapper around GEP that takes an array of ints and wraps them
515 pub fn gepi(&self, base: ValueRef, ixs: &[uint]) -> ValueRef {
516 // Small vector optimization. This should catch 100% of the cases that
519 let mut small_vec = [ C_i32(0), ..16 ];
520 for small_vec.mut_iter().zip(ixs.iter()).advance |(small_vec_e, &ix)| {
521 *small_vec_e = C_i32(ix as i32);
523 self.inbounds_gep(base, small_vec.slice(0, ixs.len()))
525 let v = do ixs.iter().transform |i| { C_i32(*i as i32) }.collect::<~[ValueRef]>();
526 self.count_insn("gepi");
527 self.inbounds_gep(base, v)
531 pub fn inbounds_gep(&self, ptr: ValueRef, indices: &[ValueRef]) -> ValueRef {
532 self.count_insn("inboundsgep");
534 llvm::LLVMBuildInBoundsGEP(
535 self.llbuilder, ptr, vec::raw::to_ptr(indices), indices.len() as c_uint, noname())
539 pub fn struct_gep(&self, ptr: ValueRef, idx: uint) -> ValueRef {
540 self.count_insn("structgep");
542 llvm::LLVMBuildStructGEP(self.llbuilder, ptr, idx as c_uint, noname())
546 pub fn global_string(&self, _Str: *c_char) -> ValueRef {
547 self.count_insn("globalstring");
549 llvm::LLVMBuildGlobalString(self.llbuilder, _Str, noname())
553 pub fn global_string_ptr(&self, _Str: *c_char) -> ValueRef {
554 self.count_insn("globalstringptr");
556 llvm::LLVMBuildGlobalStringPtr(self.llbuilder, _Str, noname())
561 pub fn trunc(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
562 self.count_insn("trunc");
564 llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty.to_ref(), noname())
568 pub fn zext(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
569 self.count_insn("zext");
571 llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty.to_ref(), noname())
575 pub fn sext(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
576 self.count_insn("sext");
578 llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty.to_ref(), noname())
582 pub fn fptoui(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
583 self.count_insn("fptoui");
585 llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty.to_ref(), noname())
589 pub fn fptosi(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
590 self.count_insn("fptosi");
592 llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty.to_ref(),noname())
596 pub fn uitofp(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
597 self.count_insn("uitofp");
599 llvm::LLVMBuildUIToFP(self.llbuilder, val, dest_ty.to_ref(), noname())
603 pub fn sitofp(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
604 self.count_insn("sitofp");
606 llvm::LLVMBuildSIToFP(self.llbuilder, val, dest_ty.to_ref(), noname())
610 pub fn fptrunc(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
611 self.count_insn("fptrunc");
613 llvm::LLVMBuildFPTrunc(self.llbuilder, val, dest_ty.to_ref(), noname())
617 pub fn fpext(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
618 self.count_insn("fpext");
620 llvm::LLVMBuildFPExt(self.llbuilder, val, dest_ty.to_ref(), noname())
624 pub fn ptrtoint(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
625 self.count_insn("ptrtoint");
627 llvm::LLVMBuildPtrToInt(self.llbuilder, val, dest_ty.to_ref(), noname())
631 pub fn inttoptr(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
632 self.count_insn("inttoptr");
634 llvm::LLVMBuildIntToPtr(self.llbuilder, val, dest_ty.to_ref(), noname())
638 pub fn bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
639 self.count_insn("bitcast");
641 llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
645 pub fn zext_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
646 self.count_insn("zextorbitcast");
648 llvm::LLVMBuildZExtOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
652 pub fn sext_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
653 self.count_insn("sextorbitcast");
655 llvm::LLVMBuildSExtOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
659 pub fn trunc_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
660 self.count_insn("truncorbitcast");
662 llvm::LLVMBuildTruncOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname())
666 pub fn cast(&self, op: Opcode, val: ValueRef, dest_ty: Type) -> ValueRef {
667 self.count_insn("cast");
669 llvm::LLVMBuildCast(self.llbuilder, op, val, dest_ty.to_ref(), noname())
673 pub fn pointercast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
674 self.count_insn("pointercast");
676 llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty.to_ref(), noname())
680 pub fn intcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
681 self.count_insn("intcast");
683 llvm::LLVMBuildIntCast(self.llbuilder, val, dest_ty.to_ref(), noname())
687 pub fn fpcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef {
688 self.count_insn("fpcast");
690 llvm::LLVMBuildFPCast(self.llbuilder, val, dest_ty.to_ref(), noname())
696 pub fn icmp(&self, op: IntPredicate, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
697 self.count_insn("icmp");
699 llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, noname())
703 pub fn fcmp(&self, op: RealPredicate, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
704 self.count_insn("fcmp");
706 llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, noname())
710 /* Miscellaneous instructions */
711 pub fn empty_phi(&self, ty: Type) -> ValueRef {
712 self.count_insn("emptyphi");
714 llvm::LLVMBuildPhi(self.llbuilder, ty.to_ref(), noname())
718 pub fn phi(&self, ty: Type, vals: &[ValueRef], bbs: &[BasicBlockRef]) -> ValueRef {
719 assert_eq!(vals.len(), bbs.len());
720 let phi = self.empty_phi(ty);
721 self.count_insn("addincoming");
723 llvm::LLVMAddIncoming(phi, vec::raw::to_ptr(vals),
724 vec::raw::to_ptr(bbs),
725 vals.len() as c_uint);
730 pub fn add_span_comment(&self, sp: span, text: &str) {
731 if self.ccx.sess.asm_comments() {
732 let s = fmt!("%s (%s)", text, self.ccx.sess.codemap.span_to_str(sp));
738 pub fn add_comment(&self, text: &str) {
739 if self.ccx.sess.asm_comments() {
740 let sanitized = text.replace("$", "");
741 let comment_text = fmt!("# %s", sanitized.replace("\n", "\n\t# "));
742 self.count_insn("inlineasm");
743 let asm = do comment_text.as_c_str |c| {
745 llvm::LLVMConstInlineAsm(Type::func([], &Type::void()).to_ref(),
746 c, noname(), False, False)
753 pub fn inline_asm_call(&self, asm: *c_char, cons: *c_char,
754 inputs: &[ValueRef], output: Type,
755 volatile: bool, alignstack: bool,
756 dia: AsmDialect) -> ValueRef {
757 self.count_insn("inlineasm");
759 let volatile = if volatile { lib::llvm::True }
760 else { lib::llvm::False };
761 let alignstack = if alignstack { lib::llvm::True }
762 else { lib::llvm::False };
764 let argtys = do inputs.map |v| {
765 debug!("Asm Input Type: %?", self.ccx.tn.val_to_str(*v));
769 debug!("Asm Output Type: %?", self.ccx.tn.type_to_str(output));
770 let fty = Type::func(argtys, &output);
772 let v = llvm::LLVMInlineAsm(
773 fty.to_ref(), asm, cons, volatile, alignstack, dia as c_uint);
778 pub fn call(&self, llfn: ValueRef, args: &[ValueRef]) -> ValueRef {
779 self.count_insn("call");
781 debug!("Call(llfn=%s, args=%?)",
782 self.ccx.tn.val_to_str(llfn),
783 args.map(|arg| self.ccx.tn.val_to_str(*arg)));
785 do args.as_imm_buf |ptr, len| {
787 llvm::LLVMBuildCall(self.llbuilder, llfn, ptr, len as c_uint, noname())
792 pub fn fastcall(&self, llfn: ValueRef, args: &[ValueRef]) -> ValueRef {
793 self.count_insn("fastcall");
795 let v = llvm::LLVMBuildCall(self.llbuilder, llfn, vec::raw::to_ptr(args),
796 args.len() as c_uint, noname());
797 lib::llvm::SetInstructionCallConv(v, lib::llvm::FastCallConv);
802 pub fn call_with_conv(&self, llfn: ValueRef, args: &[ValueRef],
803 conv: CallConv) -> ValueRef {
804 self.count_insn("callwithconv");
806 let v = llvm::LLVMBuildCall(self.llbuilder, llfn, vec::raw::to_ptr(args),
807 args.len() as c_uint, noname());
808 lib::llvm::SetInstructionCallConv(v, conv);
813 pub fn select(&self, cond: ValueRef, then_val: ValueRef, else_val: ValueRef) -> ValueRef {
814 self.count_insn("select");
816 llvm::LLVMBuildSelect(self.llbuilder, cond, then_val, else_val, noname())
820 pub fn va_arg(&self, list: ValueRef, ty: Type) -> ValueRef {
821 self.count_insn("vaarg");
823 llvm::LLVMBuildVAArg(self.llbuilder, list, ty.to_ref(), noname())
827 pub fn extract_element(&self, vec: ValueRef, idx: ValueRef) -> ValueRef {
828 self.count_insn("extractelement");
830 llvm::LLVMBuildExtractElement(self.llbuilder, vec, idx, noname())
834 pub fn insert_element(&self, vec: ValueRef, elt: ValueRef, idx: ValueRef) -> ValueRef {
835 self.count_insn("insertelement");
837 llvm::LLVMBuildInsertElement(self.llbuilder, vec, elt, idx, noname())
841 pub fn shuffle_vector(&self, v1: ValueRef, v2: ValueRef, mask: ValueRef) -> ValueRef {
842 self.count_insn("shufflevector");
844 llvm::LLVMBuildShuffleVector(self.llbuilder, v1, v2, mask, noname())
848 pub fn vector_splat(&self, num_elts: uint, elt: ValueRef) -> ValueRef {
850 let elt_ty = val_ty(elt);
851 let Undef = llvm::LLVMGetUndef(Type::vector(&elt_ty, num_elts as u64).to_ref());
852 let vec = self.insert_element(Undef, elt, C_i32(0));
853 self.shuffle_vector(vec, Undef, C_null(Type::vector(&Type::i32(), num_elts as u64)))
857 pub fn extract_value(&self, agg_val: ValueRef, idx: uint) -> ValueRef {
858 self.count_insn("extractvalue");
860 llvm::LLVMBuildExtractValue(self.llbuilder, agg_val, idx as c_uint, noname())
864 pub fn insert_value(&self, agg_val: ValueRef, elt: ValueRef,
866 self.count_insn("insertvalue");
868 llvm::LLVMBuildInsertValue(self.llbuilder, agg_val, elt, idx as c_uint,
873 pub fn is_null(&self, val: ValueRef) -> ValueRef {
874 self.count_insn("isnull");
876 llvm::LLVMBuildIsNull(self.llbuilder, val, noname())
880 pub fn is_not_null(&self, val: ValueRef) -> ValueRef {
881 self.count_insn("isnotnull");
883 llvm::LLVMBuildIsNotNull(self.llbuilder, val, noname())
887 pub fn ptrdiff(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
888 self.count_insn("ptrdiff");
890 llvm::LLVMBuildPtrDiff(self.llbuilder, lhs, rhs, noname())
896 let BB: BasicBlockRef = llvm::LLVMGetInsertBlock(self.llbuilder);
897 let FN: ValueRef = llvm::LLVMGetBasicBlockParent(BB);
898 let M: ModuleRef = llvm::LLVMGetGlobalParent(FN);
899 let T: ValueRef = str::as_c_str("llvm.trap", |buf| {
900 llvm::LLVMGetNamedFunction(M, buf)
902 assert!((T as int != 0));
903 let args: &[ValueRef] = [];
904 self.count_insn("trap");
906 self.llbuilder, T, vec::raw::to_ptr(args), args.len() as c_uint, noname());
910 pub fn landing_pad(&self, ty: Type, pers_fn: ValueRef, num_clauses: uint) -> ValueRef {
911 self.count_insn("landingpad");
913 llvm::LLVMBuildLandingPad(
914 self.llbuilder, ty.to_ref(), pers_fn, num_clauses as c_uint, noname())
918 pub fn set_cleanup(&self, landing_pad: ValueRef) {
919 self.count_insn("setcleanup");
921 llvm::LLVMSetCleanup(landing_pad, lib::llvm::True);
925 pub fn resume(&self, exn: ValueRef) -> ValueRef {
926 self.count_insn("resume");
928 llvm::LLVMBuildResume(self.llbuilder, exn)
933 pub fn atomic_cmpxchg(&self, dst: ValueRef,
934 cmp: ValueRef, src: ValueRef,
935 order: AtomicOrdering) -> ValueRef {
937 llvm::LLVMBuildAtomicCmpXchg(self.llbuilder, dst, cmp, src, order)
940 pub fn atomic_rmw(&self, op: AtomicBinOp,
941 dst: ValueRef, src: ValueRef,
942 order: AtomicOrdering) -> ValueRef {
944 llvm::LLVMBuildAtomicRMW(self.llbuilder, op, dst, src, order)