]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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. | |
4 | // | |
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. | |
10 | ||
11 | #![allow(dead_code)] // FFI wrappers | |
12 | ||
13 | use llvm; | |
d9579d0f | 14 | use llvm::{CallConv, AtomicBinOp, AtomicOrdering, SynchronizationScope, AsmDialect, AttrBuilder}; |
1a4d82fc JJ |
15 | use llvm::{Opcode, IntPredicate, RealPredicate, False}; |
16 | use llvm::{ValueRef, BasicBlockRef, BuilderRef, ModuleRef}; | |
17 | use trans::base; | |
18 | use trans::common::*; | |
19 | use trans::machine::llalign_of_pref; | |
20 | use trans::type_::Type; | |
21 | use util::nodemap::FnvHashMap; | |
22 | use libc::{c_uint, c_char}; | |
23 | ||
24 | use std::ffi::CString; | |
85aaf69f | 25 | use std::ptr; |
1a4d82fc JJ |
26 | use syntax::codemap::Span; |
27 | ||
28 | pub struct Builder<'a, 'tcx: 'a> { | |
29 | pub llbuilder: BuilderRef, | |
30 | pub ccx: &'a CrateContext<'a, 'tcx>, | |
31 | } | |
32 | ||
33 | // This is a really awful way to get a zero-length c-string, but better (and a | |
34 | // lot more efficient) than doing str::as_c_str("", ...) every time. | |
35 | pub fn noname() -> *const c_char { | |
36 | static CNULL: c_char = 0; | |
85aaf69f | 37 | &CNULL |
1a4d82fc JJ |
38 | } |
39 | ||
40 | impl<'a, 'tcx> Builder<'a, 'tcx> { | |
41 | pub fn new(ccx: &'a CrateContext<'a, 'tcx>) -> Builder<'a, 'tcx> { | |
42 | Builder { | |
43 | llbuilder: ccx.raw_builder(), | |
44 | ccx: ccx, | |
45 | } | |
46 | } | |
47 | ||
48 | pub fn count_insn(&self, category: &str) { | |
49 | if self.ccx.sess().trans_stats() { | |
50 | self.ccx.stats().n_llvm_insns.set(self.ccx | |
51 | .stats() | |
52 | .n_llvm_insns | |
53 | .get() + 1); | |
54 | } | |
55 | self.ccx.count_llvm_insn(); | |
56 | if self.ccx.sess().count_llvm_insns() { | |
57 | base::with_insn_ctxt(|v| { | |
58 | let mut h = self.ccx.stats().llvm_insns.borrow_mut(); | |
59 | ||
60 | // Build version of path with cycles removed. | |
61 | ||
62 | // Pass 1: scan table mapping str -> rightmost pos. | |
85aaf69f | 63 | let mut mm = FnvHashMap(); |
1a4d82fc | 64 | let len = v.len(); |
85aaf69f | 65 | let mut i = 0; |
1a4d82fc JJ |
66 | while i < len { |
67 | mm.insert(v[i], i); | |
85aaf69f | 68 | i += 1; |
1a4d82fc JJ |
69 | } |
70 | ||
71 | // Pass 2: concat strings for each elt, skipping | |
72 | // forwards over any cycles by advancing to rightmost | |
73 | // occurrence of each element in path. | |
62682a34 | 74 | let mut s = String::from("."); |
85aaf69f | 75 | i = 0; |
1a4d82fc JJ |
76 | while i < len { |
77 | i = mm[v[i]]; | |
78 | s.push('/'); | |
79 | s.push_str(v[i]); | |
85aaf69f | 80 | i += 1; |
1a4d82fc JJ |
81 | } |
82 | ||
83 | s.push('/'); | |
84 | s.push_str(category); | |
85 | ||
86 | let n = match h.get(&s) { | |
87 | Some(&n) => n, | |
85aaf69f | 88 | _ => 0 |
1a4d82fc | 89 | }; |
85aaf69f | 90 | h.insert(s, n+1); |
1a4d82fc JJ |
91 | }) |
92 | } | |
93 | } | |
94 | ||
95 | pub fn position_before(&self, insn: ValueRef) { | |
96 | unsafe { | |
97 | llvm::LLVMPositionBuilderBefore(self.llbuilder, insn); | |
98 | } | |
99 | } | |
100 | ||
101 | pub fn position_at_end(&self, llbb: BasicBlockRef) { | |
102 | unsafe { | |
103 | llvm::LLVMPositionBuilderAtEnd(self.llbuilder, llbb); | |
104 | } | |
105 | } | |
106 | ||
107 | pub fn ret_void(&self) { | |
108 | self.count_insn("retvoid"); | |
109 | unsafe { | |
110 | llvm::LLVMBuildRetVoid(self.llbuilder); | |
111 | } | |
112 | } | |
113 | ||
114 | pub fn ret(&self, v: ValueRef) { | |
115 | self.count_insn("ret"); | |
116 | unsafe { | |
117 | llvm::LLVMBuildRet(self.llbuilder, v); | |
118 | } | |
119 | } | |
120 | ||
121 | pub fn aggregate_ret(&self, ret_vals: &[ValueRef]) { | |
122 | unsafe { | |
123 | llvm::LLVMBuildAggregateRet(self.llbuilder, | |
124 | ret_vals.as_ptr(), | |
125 | ret_vals.len() as c_uint); | |
126 | } | |
127 | } | |
128 | ||
129 | pub fn br(&self, dest: BasicBlockRef) { | |
130 | self.count_insn("br"); | |
131 | unsafe { | |
132 | llvm::LLVMBuildBr(self.llbuilder, dest); | |
133 | } | |
134 | } | |
135 | ||
136 | pub fn cond_br(&self, cond: ValueRef, then_llbb: BasicBlockRef, else_llbb: BasicBlockRef) { | |
137 | self.count_insn("condbr"); | |
138 | unsafe { | |
139 | llvm::LLVMBuildCondBr(self.llbuilder, cond, then_llbb, else_llbb); | |
140 | } | |
141 | } | |
142 | ||
c34b1796 | 143 | pub fn switch(&self, v: ValueRef, else_llbb: BasicBlockRef, num_cases: usize) -> ValueRef { |
1a4d82fc JJ |
144 | unsafe { |
145 | llvm::LLVMBuildSwitch(self.llbuilder, v, else_llbb, num_cases as c_uint) | |
146 | } | |
147 | } | |
148 | ||
c34b1796 | 149 | pub fn indirect_br(&self, addr: ValueRef, num_dests: usize) { |
1a4d82fc JJ |
150 | self.count_insn("indirectbr"); |
151 | unsafe { | |
152 | llvm::LLVMBuildIndirectBr(self.llbuilder, addr, num_dests as c_uint); | |
153 | } | |
154 | } | |
155 | ||
156 | pub fn invoke(&self, | |
157 | llfn: ValueRef, | |
158 | args: &[ValueRef], | |
159 | then: BasicBlockRef, | |
160 | catch: BasicBlockRef, | |
161 | attributes: Option<AttrBuilder>) | |
162 | -> ValueRef { | |
163 | self.count_insn("invoke"); | |
164 | ||
165 | debug!("Invoke {} with args ({})", | |
166 | self.ccx.tn().val_to_string(llfn), | |
167 | args.iter() | |
168 | .map(|&v| self.ccx.tn().val_to_string(v)) | |
169 | .collect::<Vec<String>>() | |
170 | .connect(", ")); | |
171 | ||
172 | unsafe { | |
173 | let v = llvm::LLVMBuildInvoke(self.llbuilder, | |
174 | llfn, | |
175 | args.as_ptr(), | |
176 | args.len() as c_uint, | |
177 | then, | |
178 | catch, | |
179 | noname()); | |
180 | match attributes { | |
181 | Some(a) => a.apply_callsite(v), | |
182 | None => {} | |
183 | } | |
184 | v | |
185 | } | |
186 | } | |
187 | ||
188 | pub fn unreachable(&self) { | |
189 | self.count_insn("unreachable"); | |
190 | unsafe { | |
191 | llvm::LLVMBuildUnreachable(self.llbuilder); | |
192 | } | |
193 | } | |
194 | ||
195 | /* Arithmetic */ | |
196 | pub fn add(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
197 | self.count_insn("add"); | |
198 | unsafe { | |
199 | llvm::LLVMBuildAdd(self.llbuilder, lhs, rhs, noname()) | |
200 | } | |
201 | } | |
202 | ||
203 | pub fn nswadd(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
204 | self.count_insn("nswadd"); | |
205 | unsafe { | |
206 | llvm::LLVMBuildNSWAdd(self.llbuilder, lhs, rhs, noname()) | |
207 | } | |
208 | } | |
209 | ||
210 | pub fn nuwadd(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
211 | self.count_insn("nuwadd"); | |
212 | unsafe { | |
213 | llvm::LLVMBuildNUWAdd(self.llbuilder, lhs, rhs, noname()) | |
214 | } | |
215 | } | |
216 | ||
217 | pub fn fadd(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
218 | self.count_insn("fadd"); | |
219 | unsafe { | |
220 | llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, noname()) | |
221 | } | |
222 | } | |
223 | ||
224 | pub fn sub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
225 | self.count_insn("sub"); | |
226 | unsafe { | |
227 | llvm::LLVMBuildSub(self.llbuilder, lhs, rhs, noname()) | |
228 | } | |
229 | } | |
230 | ||
231 | pub fn nswsub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
232 | self.count_insn("nwsub"); | |
233 | unsafe { | |
234 | llvm::LLVMBuildNSWSub(self.llbuilder, lhs, rhs, noname()) | |
235 | } | |
236 | } | |
237 | ||
238 | pub fn nuwsub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
239 | self.count_insn("nuwsub"); | |
240 | unsafe { | |
241 | llvm::LLVMBuildNUWSub(self.llbuilder, lhs, rhs, noname()) | |
242 | } | |
243 | } | |
244 | ||
245 | pub fn fsub(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
246 | self.count_insn("sub"); | |
247 | unsafe { | |
248 | llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, noname()) | |
249 | } | |
250 | } | |
251 | ||
252 | pub fn mul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
253 | self.count_insn("mul"); | |
254 | unsafe { | |
255 | llvm::LLVMBuildMul(self.llbuilder, lhs, rhs, noname()) | |
256 | } | |
257 | } | |
258 | ||
259 | pub fn nswmul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
260 | self.count_insn("nswmul"); | |
261 | unsafe { | |
262 | llvm::LLVMBuildNSWMul(self.llbuilder, lhs, rhs, noname()) | |
263 | } | |
264 | } | |
265 | ||
266 | pub fn nuwmul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
267 | self.count_insn("nuwmul"); | |
268 | unsafe { | |
269 | llvm::LLVMBuildNUWMul(self.llbuilder, lhs, rhs, noname()) | |
270 | } | |
271 | } | |
272 | ||
273 | pub fn fmul(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
274 | self.count_insn("fmul"); | |
275 | unsafe { | |
276 | llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, noname()) | |
277 | } | |
278 | } | |
279 | ||
280 | pub fn udiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
281 | self.count_insn("udiv"); | |
282 | unsafe { | |
283 | llvm::LLVMBuildUDiv(self.llbuilder, lhs, rhs, noname()) | |
284 | } | |
285 | } | |
286 | ||
287 | pub fn sdiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
288 | self.count_insn("sdiv"); | |
289 | unsafe { | |
290 | llvm::LLVMBuildSDiv(self.llbuilder, lhs, rhs, noname()) | |
291 | } | |
292 | } | |
293 | ||
294 | pub fn exactsdiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
295 | self.count_insn("exactsdiv"); | |
296 | unsafe { | |
297 | llvm::LLVMBuildExactSDiv(self.llbuilder, lhs, rhs, noname()) | |
298 | } | |
299 | } | |
300 | ||
301 | pub fn fdiv(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
302 | self.count_insn("fdiv"); | |
303 | unsafe { | |
304 | llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, noname()) | |
305 | } | |
306 | } | |
307 | ||
308 | pub fn urem(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
309 | self.count_insn("urem"); | |
310 | unsafe { | |
311 | llvm::LLVMBuildURem(self.llbuilder, lhs, rhs, noname()) | |
312 | } | |
313 | } | |
314 | ||
315 | pub fn srem(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
316 | self.count_insn("srem"); | |
317 | unsafe { | |
318 | llvm::LLVMBuildSRem(self.llbuilder, lhs, rhs, noname()) | |
319 | } | |
320 | } | |
321 | ||
322 | pub fn frem(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
323 | self.count_insn("frem"); | |
324 | unsafe { | |
325 | llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, noname()) | |
326 | } | |
327 | } | |
328 | ||
329 | pub fn shl(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
330 | self.count_insn("shl"); | |
331 | unsafe { | |
332 | llvm::LLVMBuildShl(self.llbuilder, lhs, rhs, noname()) | |
333 | } | |
334 | } | |
335 | ||
336 | pub fn lshr(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
337 | self.count_insn("lshr"); | |
338 | unsafe { | |
339 | llvm::LLVMBuildLShr(self.llbuilder, lhs, rhs, noname()) | |
340 | } | |
341 | } | |
342 | ||
343 | pub fn ashr(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
344 | self.count_insn("ashr"); | |
345 | unsafe { | |
346 | llvm::LLVMBuildAShr(self.llbuilder, lhs, rhs, noname()) | |
347 | } | |
348 | } | |
349 | ||
350 | pub fn and(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
351 | self.count_insn("and"); | |
352 | unsafe { | |
353 | llvm::LLVMBuildAnd(self.llbuilder, lhs, rhs, noname()) | |
354 | } | |
355 | } | |
356 | ||
357 | pub fn or(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
358 | self.count_insn("or"); | |
359 | unsafe { | |
360 | llvm::LLVMBuildOr(self.llbuilder, lhs, rhs, noname()) | |
361 | } | |
362 | } | |
363 | ||
364 | pub fn xor(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
365 | self.count_insn("xor"); | |
366 | unsafe { | |
367 | llvm::LLVMBuildXor(self.llbuilder, lhs, rhs, noname()) | |
368 | } | |
369 | } | |
370 | ||
371 | pub fn binop(&self, op: Opcode, lhs: ValueRef, rhs: ValueRef) | |
372 | -> ValueRef { | |
373 | self.count_insn("binop"); | |
374 | unsafe { | |
375 | llvm::LLVMBuildBinOp(self.llbuilder, op, lhs, rhs, noname()) | |
376 | } | |
377 | } | |
378 | ||
379 | pub fn neg(&self, v: ValueRef) -> ValueRef { | |
380 | self.count_insn("neg"); | |
381 | unsafe { | |
382 | llvm::LLVMBuildNeg(self.llbuilder, v, noname()) | |
383 | } | |
384 | } | |
385 | ||
386 | pub fn nswneg(&self, v: ValueRef) -> ValueRef { | |
387 | self.count_insn("nswneg"); | |
388 | unsafe { | |
389 | llvm::LLVMBuildNSWNeg(self.llbuilder, v, noname()) | |
390 | } | |
391 | } | |
392 | ||
393 | pub fn nuwneg(&self, v: ValueRef) -> ValueRef { | |
394 | self.count_insn("nuwneg"); | |
395 | unsafe { | |
396 | llvm::LLVMBuildNUWNeg(self.llbuilder, v, noname()) | |
397 | } | |
398 | } | |
399 | pub fn fneg(&self, v: ValueRef) -> ValueRef { | |
400 | self.count_insn("fneg"); | |
401 | unsafe { | |
402 | llvm::LLVMBuildFNeg(self.llbuilder, v, noname()) | |
403 | } | |
404 | } | |
405 | ||
406 | pub fn not(&self, v: ValueRef) -> ValueRef { | |
407 | self.count_insn("not"); | |
408 | unsafe { | |
409 | llvm::LLVMBuildNot(self.llbuilder, v, noname()) | |
410 | } | |
411 | } | |
412 | ||
1a4d82fc JJ |
413 | pub fn alloca(&self, ty: Type, name: &str) -> ValueRef { |
414 | self.count_insn("alloca"); | |
415 | unsafe { | |
416 | if name.is_empty() { | |
417 | llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(), noname()) | |
418 | } else { | |
85aaf69f | 419 | let name = CString::new(name).unwrap(); |
1a4d82fc JJ |
420 | llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(), |
421 | name.as_ptr()) | |
422 | } | |
423 | } | |
424 | } | |
425 | ||
1a4d82fc JJ |
426 | pub fn free(&self, ptr: ValueRef) { |
427 | self.count_insn("free"); | |
428 | unsafe { | |
429 | llvm::LLVMBuildFree(self.llbuilder, ptr); | |
430 | } | |
431 | } | |
432 | ||
433 | pub fn load(&self, ptr: ValueRef) -> ValueRef { | |
434 | self.count_insn("load"); | |
435 | unsafe { | |
436 | llvm::LLVMBuildLoad(self.llbuilder, ptr, noname()) | |
437 | } | |
438 | } | |
439 | ||
440 | pub fn volatile_load(&self, ptr: ValueRef) -> ValueRef { | |
441 | self.count_insn("load.volatile"); | |
442 | unsafe { | |
443 | let insn = llvm::LLVMBuildLoad(self.llbuilder, ptr, noname()); | |
444 | llvm::LLVMSetVolatile(insn, llvm::True); | |
445 | insn | |
446 | } | |
447 | } | |
448 | ||
449 | pub fn atomic_load(&self, ptr: ValueRef, order: AtomicOrdering) -> ValueRef { | |
450 | self.count_insn("load.atomic"); | |
451 | unsafe { | |
452 | let ty = Type::from_ref(llvm::LLVMTypeOf(ptr)); | |
453 | let align = llalign_of_pref(self.ccx, ty.element_type()); | |
454 | llvm::LLVMBuildAtomicLoad(self.llbuilder, ptr, noname(), order, | |
455 | align as c_uint) | |
456 | } | |
457 | } | |
458 | ||
459 | ||
460 | pub fn load_range_assert(&self, ptr: ValueRef, lo: u64, | |
461 | hi: u64, signed: llvm::Bool) -> ValueRef { | |
462 | let value = self.load(ptr); | |
463 | ||
464 | unsafe { | |
465 | let t = llvm::LLVMGetElementType(llvm::LLVMTypeOf(ptr)); | |
466 | let min = llvm::LLVMConstInt(t, lo, signed); | |
467 | let max = llvm::LLVMConstInt(t, hi, signed); | |
468 | ||
469 | let v = [min, max]; | |
470 | ||
471 | llvm::LLVMSetMetadata(value, llvm::MD_range as c_uint, | |
472 | llvm::LLVMMDNodeInContext(self.ccx.llcx(), | |
473 | v.as_ptr(), | |
474 | v.len() as c_uint)); | |
475 | } | |
476 | ||
477 | value | |
478 | } | |
479 | ||
85aaf69f SL |
480 | pub fn load_nonnull(&self, ptr: ValueRef) -> ValueRef { |
481 | let value = self.load(ptr); | |
482 | unsafe { | |
483 | llvm::LLVMSetMetadata(value, llvm::MD_nonnull as c_uint, | |
484 | llvm::LLVMMDNodeInContext(self.ccx.llcx(), ptr::null(), 0)); | |
485 | } | |
486 | ||
487 | value | |
488 | } | |
489 | ||
d9579d0f | 490 | pub fn store(&self, val: ValueRef, ptr: ValueRef) -> ValueRef { |
1a4d82fc JJ |
491 | debug!("Store {} -> {}", |
492 | self.ccx.tn().val_to_string(val), | |
493 | self.ccx.tn().val_to_string(ptr)); | |
494 | assert!(!self.llbuilder.is_null()); | |
495 | self.count_insn("store"); | |
496 | unsafe { | |
d9579d0f | 497 | llvm::LLVMBuildStore(self.llbuilder, val, ptr) |
1a4d82fc JJ |
498 | } |
499 | } | |
500 | ||
d9579d0f | 501 | pub fn volatile_store(&self, val: ValueRef, ptr: ValueRef) -> ValueRef { |
1a4d82fc JJ |
502 | debug!("Store {} -> {}", |
503 | self.ccx.tn().val_to_string(val), | |
504 | self.ccx.tn().val_to_string(ptr)); | |
505 | assert!(!self.llbuilder.is_null()); | |
506 | self.count_insn("store.volatile"); | |
507 | unsafe { | |
508 | let insn = llvm::LLVMBuildStore(self.llbuilder, val, ptr); | |
509 | llvm::LLVMSetVolatile(insn, llvm::True); | |
d9579d0f | 510 | insn |
1a4d82fc JJ |
511 | } |
512 | } | |
513 | ||
514 | pub fn atomic_store(&self, val: ValueRef, ptr: ValueRef, order: AtomicOrdering) { | |
515 | debug!("Store {} -> {}", | |
516 | self.ccx.tn().val_to_string(val), | |
517 | self.ccx.tn().val_to_string(ptr)); | |
518 | self.count_insn("store.atomic"); | |
519 | unsafe { | |
520 | let ty = Type::from_ref(llvm::LLVMTypeOf(ptr)); | |
521 | let align = llalign_of_pref(self.ccx, ty.element_type()); | |
522 | llvm::LLVMBuildAtomicStore(self.llbuilder, val, ptr, order, align as c_uint); | |
523 | } | |
524 | } | |
525 | ||
526 | pub fn gep(&self, ptr: ValueRef, indices: &[ValueRef]) -> ValueRef { | |
527 | self.count_insn("gep"); | |
528 | unsafe { | |
529 | llvm::LLVMBuildGEP(self.llbuilder, ptr, indices.as_ptr(), | |
530 | indices.len() as c_uint, noname()) | |
531 | } | |
532 | } | |
533 | ||
534 | // Simple wrapper around GEP that takes an array of ints and wraps them | |
535 | // in C_i32() | |
536 | #[inline] | |
c34b1796 | 537 | pub fn gepi(&self, base: ValueRef, ixs: &[usize]) -> ValueRef { |
1a4d82fc JJ |
538 | // Small vector optimization. This should catch 100% of the cases that |
539 | // we care about. | |
540 | if ixs.len() < 16 { | |
541 | let mut small_vec = [ C_i32(self.ccx, 0); 16 ]; | |
62682a34 | 542 | for (small_vec_e, &ix) in small_vec.iter_mut().zip(ixs) { |
1a4d82fc JJ |
543 | *small_vec_e = C_i32(self.ccx, ix as i32); |
544 | } | |
85aaf69f | 545 | self.inbounds_gep(base, &small_vec[..ixs.len()]) |
1a4d82fc JJ |
546 | } else { |
547 | let v = ixs.iter().map(|i| C_i32(self.ccx, *i as i32)).collect::<Vec<ValueRef>>(); | |
548 | self.count_insn("gepi"); | |
85aaf69f | 549 | self.inbounds_gep(base, &v[..]) |
1a4d82fc JJ |
550 | } |
551 | } | |
552 | ||
553 | pub fn inbounds_gep(&self, ptr: ValueRef, indices: &[ValueRef]) -> ValueRef { | |
554 | self.count_insn("inboundsgep"); | |
555 | unsafe { | |
556 | llvm::LLVMBuildInBoundsGEP( | |
557 | self.llbuilder, ptr, indices.as_ptr(), indices.len() as c_uint, noname()) | |
558 | } | |
559 | } | |
560 | ||
c34b1796 | 561 | pub fn struct_gep(&self, ptr: ValueRef, idx: usize) -> ValueRef { |
1a4d82fc JJ |
562 | self.count_insn("structgep"); |
563 | unsafe { | |
564 | llvm::LLVMBuildStructGEP(self.llbuilder, ptr, idx as c_uint, noname()) | |
565 | } | |
566 | } | |
567 | ||
568 | pub fn global_string(&self, _str: *const c_char) -> ValueRef { | |
569 | self.count_insn("globalstring"); | |
570 | unsafe { | |
571 | llvm::LLVMBuildGlobalString(self.llbuilder, _str, noname()) | |
572 | } | |
573 | } | |
574 | ||
575 | pub fn global_string_ptr(&self, _str: *const c_char) -> ValueRef { | |
576 | self.count_insn("globalstringptr"); | |
577 | unsafe { | |
578 | llvm::LLVMBuildGlobalStringPtr(self.llbuilder, _str, noname()) | |
579 | } | |
580 | } | |
581 | ||
582 | /* Casts */ | |
583 | pub fn trunc(&self, val: ValueRef, dest_ty: Type) -> ValueRef { | |
584 | self.count_insn("trunc"); | |
585 | unsafe { | |
586 | llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty.to_ref(), noname()) | |
587 | } | |
588 | } | |
589 | ||
590 | pub fn zext(&self, val: ValueRef, dest_ty: Type) -> ValueRef { | |
591 | self.count_insn("zext"); | |
592 | unsafe { | |
593 | llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty.to_ref(), noname()) | |
594 | } | |
595 | } | |
596 | ||
597 | pub fn sext(&self, val: ValueRef, dest_ty: Type) -> ValueRef { | |
598 | self.count_insn("sext"); | |
599 | unsafe { | |
600 | llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty.to_ref(), noname()) | |
601 | } | |
602 | } | |
603 | ||
604 | pub fn fptoui(&self, val: ValueRef, dest_ty: Type) -> ValueRef { | |
605 | self.count_insn("fptoui"); | |
606 | unsafe { | |
607 | llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty.to_ref(), noname()) | |
608 | } | |
609 | } | |
610 | ||
611 | pub fn fptosi(&self, val: ValueRef, dest_ty: Type) -> ValueRef { | |
612 | self.count_insn("fptosi"); | |
613 | unsafe { | |
614 | llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty.to_ref(),noname()) | |
615 | } | |
616 | } | |
617 | ||
618 | pub fn uitofp(&self, val: ValueRef, dest_ty: Type) -> ValueRef { | |
619 | self.count_insn("uitofp"); | |
620 | unsafe { | |
621 | llvm::LLVMBuildUIToFP(self.llbuilder, val, dest_ty.to_ref(), noname()) | |
622 | } | |
623 | } | |
624 | ||
625 | pub fn sitofp(&self, val: ValueRef, dest_ty: Type) -> ValueRef { | |
626 | self.count_insn("sitofp"); | |
627 | unsafe { | |
628 | llvm::LLVMBuildSIToFP(self.llbuilder, val, dest_ty.to_ref(), noname()) | |
629 | } | |
630 | } | |
631 | ||
632 | pub fn fptrunc(&self, val: ValueRef, dest_ty: Type) -> ValueRef { | |
633 | self.count_insn("fptrunc"); | |
634 | unsafe { | |
635 | llvm::LLVMBuildFPTrunc(self.llbuilder, val, dest_ty.to_ref(), noname()) | |
636 | } | |
637 | } | |
638 | ||
639 | pub fn fpext(&self, val: ValueRef, dest_ty: Type) -> ValueRef { | |
640 | self.count_insn("fpext"); | |
641 | unsafe { | |
642 | llvm::LLVMBuildFPExt(self.llbuilder, val, dest_ty.to_ref(), noname()) | |
643 | } | |
644 | } | |
645 | ||
646 | pub fn ptrtoint(&self, val: ValueRef, dest_ty: Type) -> ValueRef { | |
647 | self.count_insn("ptrtoint"); | |
648 | unsafe { | |
649 | llvm::LLVMBuildPtrToInt(self.llbuilder, val, dest_ty.to_ref(), noname()) | |
650 | } | |
651 | } | |
652 | ||
653 | pub fn inttoptr(&self, val: ValueRef, dest_ty: Type) -> ValueRef { | |
654 | self.count_insn("inttoptr"); | |
655 | unsafe { | |
656 | llvm::LLVMBuildIntToPtr(self.llbuilder, val, dest_ty.to_ref(), noname()) | |
657 | } | |
658 | } | |
659 | ||
660 | pub fn bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef { | |
661 | self.count_insn("bitcast"); | |
662 | unsafe { | |
663 | llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty.to_ref(), noname()) | |
664 | } | |
665 | } | |
666 | ||
667 | pub fn zext_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef { | |
668 | self.count_insn("zextorbitcast"); | |
669 | unsafe { | |
670 | llvm::LLVMBuildZExtOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname()) | |
671 | } | |
672 | } | |
673 | ||
674 | pub fn sext_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef { | |
675 | self.count_insn("sextorbitcast"); | |
676 | unsafe { | |
677 | llvm::LLVMBuildSExtOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname()) | |
678 | } | |
679 | } | |
680 | ||
681 | pub fn trunc_or_bitcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef { | |
682 | self.count_insn("truncorbitcast"); | |
683 | unsafe { | |
684 | llvm::LLVMBuildTruncOrBitCast(self.llbuilder, val, dest_ty.to_ref(), noname()) | |
685 | } | |
686 | } | |
687 | ||
688 | pub fn cast(&self, op: Opcode, val: ValueRef, dest_ty: Type) -> ValueRef { | |
689 | self.count_insn("cast"); | |
690 | unsafe { | |
691 | llvm::LLVMBuildCast(self.llbuilder, op, val, dest_ty.to_ref(), noname()) | |
692 | } | |
693 | } | |
694 | ||
695 | pub fn pointercast(&self, val: ValueRef, dest_ty: Type) -> ValueRef { | |
696 | self.count_insn("pointercast"); | |
697 | unsafe { | |
698 | llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty.to_ref(), noname()) | |
699 | } | |
700 | } | |
701 | ||
702 | pub fn intcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef { | |
703 | self.count_insn("intcast"); | |
704 | unsafe { | |
705 | llvm::LLVMBuildIntCast(self.llbuilder, val, dest_ty.to_ref(), noname()) | |
706 | } | |
707 | } | |
708 | ||
709 | pub fn fpcast(&self, val: ValueRef, dest_ty: Type) -> ValueRef { | |
710 | self.count_insn("fpcast"); | |
711 | unsafe { | |
712 | llvm::LLVMBuildFPCast(self.llbuilder, val, dest_ty.to_ref(), noname()) | |
713 | } | |
714 | } | |
715 | ||
716 | ||
717 | /* Comparisons */ | |
718 | pub fn icmp(&self, op: IntPredicate, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
719 | self.count_insn("icmp"); | |
720 | unsafe { | |
721 | llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, noname()) | |
722 | } | |
723 | } | |
724 | ||
725 | pub fn fcmp(&self, op: RealPredicate, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
726 | self.count_insn("fcmp"); | |
727 | unsafe { | |
728 | llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, noname()) | |
729 | } | |
730 | } | |
731 | ||
732 | /* Miscellaneous instructions */ | |
733 | pub fn empty_phi(&self, ty: Type) -> ValueRef { | |
734 | self.count_insn("emptyphi"); | |
735 | unsafe { | |
736 | llvm::LLVMBuildPhi(self.llbuilder, ty.to_ref(), noname()) | |
737 | } | |
738 | } | |
739 | ||
740 | pub fn phi(&self, ty: Type, vals: &[ValueRef], bbs: &[BasicBlockRef]) -> ValueRef { | |
741 | assert_eq!(vals.len(), bbs.len()); | |
742 | let phi = self.empty_phi(ty); | |
743 | self.count_insn("addincoming"); | |
744 | unsafe { | |
745 | llvm::LLVMAddIncoming(phi, vals.as_ptr(), | |
746 | bbs.as_ptr(), | |
747 | vals.len() as c_uint); | |
748 | phi | |
749 | } | |
750 | } | |
751 | ||
752 | pub fn add_span_comment(&self, sp: Span, text: &str) { | |
753 | if self.ccx.sess().asm_comments() { | |
754 | let s = format!("{} ({})", | |
755 | text, | |
756 | self.ccx.sess().codemap().span_to_string(sp)); | |
85aaf69f SL |
757 | debug!("{}", &s[..]); |
758 | self.add_comment(&s[..]); | |
1a4d82fc JJ |
759 | } |
760 | } | |
761 | ||
762 | pub fn add_comment(&self, text: &str) { | |
763 | if self.ccx.sess().asm_comments() { | |
764 | let sanitized = text.replace("$", ""); | |
765 | let comment_text = format!("{} {}", "#", | |
766 | sanitized.replace("\n", "\n\t# ")); | |
767 | self.count_insn("inlineasm"); | |
85aaf69f | 768 | let comment_text = CString::new(comment_text).unwrap(); |
1a4d82fc JJ |
769 | let asm = unsafe { |
770 | llvm::LLVMConstInlineAsm(Type::func(&[], &Type::void(self.ccx)).to_ref(), | |
771 | comment_text.as_ptr(), noname(), False, | |
772 | False) | |
773 | }; | |
774 | self.call(asm, &[], None); | |
775 | } | |
776 | } | |
777 | ||
778 | pub fn inline_asm_call(&self, asm: *const c_char, cons: *const c_char, | |
779 | inputs: &[ValueRef], output: Type, | |
780 | volatile: bool, alignstack: bool, | |
781 | dia: AsmDialect) -> ValueRef { | |
782 | self.count_insn("inlineasm"); | |
783 | ||
784 | let volatile = if volatile { llvm::True } | |
785 | else { llvm::False }; | |
786 | let alignstack = if alignstack { llvm::True } | |
787 | else { llvm::False }; | |
788 | ||
789 | let argtys = inputs.iter().map(|v| { | |
790 | debug!("Asm Input Type: {}", self.ccx.tn().val_to_string(*v)); | |
791 | val_ty(*v) | |
792 | }).collect::<Vec<_>>(); | |
793 | ||
794 | debug!("Asm Output Type: {}", self.ccx.tn().type_to_string(output)); | |
85aaf69f | 795 | let fty = Type::func(&argtys[..], &output); |
1a4d82fc JJ |
796 | unsafe { |
797 | let v = llvm::LLVMInlineAsm( | |
798 | fty.to_ref(), asm, cons, volatile, alignstack, dia as c_uint); | |
799 | self.call(v, inputs, None) | |
800 | } | |
801 | } | |
802 | ||
803 | pub fn call(&self, llfn: ValueRef, args: &[ValueRef], | |
804 | attributes: Option<AttrBuilder>) -> ValueRef { | |
805 | self.count_insn("call"); | |
806 | ||
807 | debug!("Call {} with args ({})", | |
808 | self.ccx.tn().val_to_string(llfn), | |
809 | args.iter() | |
810 | .map(|&v| self.ccx.tn().val_to_string(v)) | |
811 | .collect::<Vec<String>>() | |
812 | .connect(", ")); | |
813 | ||
814 | unsafe { | |
815 | let v = llvm::LLVMBuildCall(self.llbuilder, llfn, args.as_ptr(), | |
816 | args.len() as c_uint, noname()); | |
817 | match attributes { | |
818 | Some(a) => a.apply_callsite(v), | |
819 | None => {} | |
820 | } | |
821 | v | |
822 | } | |
823 | } | |
824 | ||
825 | pub fn call_with_conv(&self, llfn: ValueRef, args: &[ValueRef], | |
826 | conv: CallConv, attributes: Option<AttrBuilder>) -> ValueRef { | |
827 | self.count_insn("callwithconv"); | |
828 | let v = self.call(llfn, args, attributes); | |
829 | llvm::SetInstructionCallConv(v, conv); | |
830 | v | |
831 | } | |
832 | ||
833 | pub fn select(&self, cond: ValueRef, then_val: ValueRef, else_val: ValueRef) -> ValueRef { | |
834 | self.count_insn("select"); | |
835 | unsafe { | |
836 | llvm::LLVMBuildSelect(self.llbuilder, cond, then_val, else_val, noname()) | |
837 | } | |
838 | } | |
839 | ||
840 | pub fn va_arg(&self, list: ValueRef, ty: Type) -> ValueRef { | |
841 | self.count_insn("vaarg"); | |
842 | unsafe { | |
843 | llvm::LLVMBuildVAArg(self.llbuilder, list, ty.to_ref(), noname()) | |
844 | } | |
845 | } | |
846 | ||
847 | pub fn extract_element(&self, vec: ValueRef, idx: ValueRef) -> ValueRef { | |
848 | self.count_insn("extractelement"); | |
849 | unsafe { | |
850 | llvm::LLVMBuildExtractElement(self.llbuilder, vec, idx, noname()) | |
851 | } | |
852 | } | |
853 | ||
854 | pub fn insert_element(&self, vec: ValueRef, elt: ValueRef, idx: ValueRef) -> ValueRef { | |
855 | self.count_insn("insertelement"); | |
856 | unsafe { | |
857 | llvm::LLVMBuildInsertElement(self.llbuilder, vec, elt, idx, noname()) | |
858 | } | |
859 | } | |
860 | ||
861 | pub fn shuffle_vector(&self, v1: ValueRef, v2: ValueRef, mask: ValueRef) -> ValueRef { | |
862 | self.count_insn("shufflevector"); | |
863 | unsafe { | |
864 | llvm::LLVMBuildShuffleVector(self.llbuilder, v1, v2, mask, noname()) | |
865 | } | |
866 | } | |
867 | ||
c34b1796 | 868 | pub fn vector_splat(&self, num_elts: usize, elt: ValueRef) -> ValueRef { |
1a4d82fc JJ |
869 | unsafe { |
870 | let elt_ty = val_ty(elt); | |
871 | let undef = llvm::LLVMGetUndef(Type::vector(&elt_ty, num_elts as u64).to_ref()); | |
872 | let vec = self.insert_element(undef, elt, C_i32(self.ccx, 0)); | |
873 | let vec_i32_ty = Type::vector(&Type::i32(self.ccx), num_elts as u64); | |
874 | self.shuffle_vector(vec, undef, C_null(vec_i32_ty)) | |
875 | } | |
876 | } | |
877 | ||
c34b1796 | 878 | pub fn extract_value(&self, agg_val: ValueRef, idx: usize) -> ValueRef { |
1a4d82fc JJ |
879 | self.count_insn("extractvalue"); |
880 | unsafe { | |
881 | llvm::LLVMBuildExtractValue(self.llbuilder, agg_val, idx as c_uint, noname()) | |
882 | } | |
883 | } | |
884 | ||
885 | pub fn insert_value(&self, agg_val: ValueRef, elt: ValueRef, | |
c34b1796 | 886 | idx: usize) -> ValueRef { |
1a4d82fc JJ |
887 | self.count_insn("insertvalue"); |
888 | unsafe { | |
889 | llvm::LLVMBuildInsertValue(self.llbuilder, agg_val, elt, idx as c_uint, | |
890 | noname()) | |
891 | } | |
892 | } | |
893 | ||
894 | pub fn is_null(&self, val: ValueRef) -> ValueRef { | |
895 | self.count_insn("isnull"); | |
896 | unsafe { | |
897 | llvm::LLVMBuildIsNull(self.llbuilder, val, noname()) | |
898 | } | |
899 | } | |
900 | ||
901 | pub fn is_not_null(&self, val: ValueRef) -> ValueRef { | |
902 | self.count_insn("isnotnull"); | |
903 | unsafe { | |
904 | llvm::LLVMBuildIsNotNull(self.llbuilder, val, noname()) | |
905 | } | |
906 | } | |
907 | ||
908 | pub fn ptrdiff(&self, lhs: ValueRef, rhs: ValueRef) -> ValueRef { | |
909 | self.count_insn("ptrdiff"); | |
910 | unsafe { | |
911 | llvm::LLVMBuildPtrDiff(self.llbuilder, lhs, rhs, noname()) | |
912 | } | |
913 | } | |
914 | ||
915 | pub fn trap(&self) { | |
916 | unsafe { | |
917 | let bb: BasicBlockRef = llvm::LLVMGetInsertBlock(self.llbuilder); | |
918 | let fn_: ValueRef = llvm::LLVMGetBasicBlockParent(bb); | |
919 | let m: ModuleRef = llvm::LLVMGetGlobalParent(fn_); | |
920 | let p = "llvm.trap\0".as_ptr(); | |
921 | let t: ValueRef = llvm::LLVMGetNamedFunction(m, p as *const _); | |
c34b1796 | 922 | assert!((t as isize != 0)); |
1a4d82fc JJ |
923 | let args: &[ValueRef] = &[]; |
924 | self.count_insn("trap"); | |
925 | llvm::LLVMBuildCall( | |
926 | self.llbuilder, t, args.as_ptr(), args.len() as c_uint, noname()); | |
927 | } | |
928 | } | |
929 | ||
c34b1796 | 930 | pub fn landing_pad(&self, ty: Type, pers_fn: ValueRef, num_clauses: usize) -> ValueRef { |
1a4d82fc JJ |
931 | self.count_insn("landingpad"); |
932 | unsafe { | |
933 | llvm::LLVMBuildLandingPad( | |
934 | self.llbuilder, ty.to_ref(), pers_fn, num_clauses as c_uint, noname()) | |
935 | } | |
936 | } | |
937 | ||
938 | pub fn set_cleanup(&self, landing_pad: ValueRef) { | |
939 | self.count_insn("setcleanup"); | |
940 | unsafe { | |
941 | llvm::LLVMSetCleanup(landing_pad, llvm::True); | |
942 | } | |
943 | } | |
944 | ||
945 | pub fn resume(&self, exn: ValueRef) -> ValueRef { | |
946 | self.count_insn("resume"); | |
947 | unsafe { | |
948 | llvm::LLVMBuildResume(self.llbuilder, exn) | |
949 | } | |
950 | } | |
951 | ||
952 | // Atomic Operations | |
953 | pub fn atomic_cmpxchg(&self, dst: ValueRef, | |
954 | cmp: ValueRef, src: ValueRef, | |
955 | order: AtomicOrdering, | |
956 | failure_order: AtomicOrdering) -> ValueRef { | |
957 | unsafe { | |
958 | llvm::LLVMBuildAtomicCmpXchg(self.llbuilder, dst, cmp, src, | |
959 | order, failure_order) | |
960 | } | |
961 | } | |
962 | pub fn atomic_rmw(&self, op: AtomicBinOp, | |
963 | dst: ValueRef, src: ValueRef, | |
964 | order: AtomicOrdering) -> ValueRef { | |
965 | unsafe { | |
966 | llvm::LLVMBuildAtomicRMW(self.llbuilder, op, dst, src, order, False) | |
967 | } | |
968 | } | |
969 | ||
d9579d0f | 970 | pub fn atomic_fence(&self, order: AtomicOrdering, scope: SynchronizationScope) { |
1a4d82fc | 971 | unsafe { |
d9579d0f | 972 | llvm::LLVMBuildAtomicFence(self.llbuilder, order, scope); |
1a4d82fc JJ |
973 | } |
974 | } | |
975 | } |