Skip to content

Commit 25d6834

Browse files
committed
rustc_codegen_llvm: replace fn noname() with const UNNAMED.
1 parent 268e646 commit 25d6834

File tree

2 files changed

+52
-47
lines changed

2 files changed

+52
-47
lines changed

src/librustc_codegen_llvm/builder.rs

+51-47
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rustc_codegen_ssa::mir::operand::{OperandValue, OperandRef};
2020
use rustc_codegen_ssa::mir::place::PlaceRef;
2121
use rustc_target::spec::{HasTargetSpec, Target};
2222
use std::borrow::Cow;
23+
use std::ffi::CStr;
2324
use std::ops::{Deref, Range};
2425
use std::ptr;
2526
use std::iter::TrustedLen;
@@ -39,12 +40,15 @@ impl Drop for Builder<'a, 'll, 'tcx> {
3940
}
4041
}
4142

42-
// This is a really awful way to get a zero-length c-string, but better (and a
43-
// lot more efficient) than doing str::as_c_str("", ...) every time.
44-
fn noname() -> *const c_char {
45-
static CNULL: c_char = 0;
46-
&CNULL
47-
}
43+
// FIXME(eddyb) use a checked constructor when they become `const fn`.
44+
const EMPTY_C_STR: &CStr = unsafe {
45+
CStr::from_bytes_with_nul_unchecked(b"\0")
46+
};
47+
48+
/// Empty string, to be used where LLVM expects an instruction name, indicating
49+
/// that the instruction is to be left unnamed (i.e. numbered, in textual IR).
50+
// FIXME(eddyb) pass `&CStr` directly to FFI once it's a thin pointer.
51+
const UNNAMED: *const c_char = EMPTY_C_STR.as_ptr();
4852

4953
impl BackendTypes for Builder<'_, 'll, 'tcx> {
5054
type Value = <CodegenCx<'ll, 'tcx> as BackendTypes>::Value;
@@ -104,7 +108,7 @@ macro_rules! builder_methods_for_value_instructions {
104108
($($name:ident($($arg:ident),*) => $llvm_capi:ident),+ $(,)?) => {
105109
$(fn $name(&mut self, $($arg: &'ll Value),*) -> &'ll Value {
106110
unsafe {
107-
llvm::$llvm_capi(self.llbuilder, $($arg,)* noname())
111+
llvm::$llvm_capi(self.llbuilder, $($arg,)* UNNAMED)
108112
}
109113
})*
110114
}
@@ -227,7 +231,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
227231
then,
228232
catch,
229233
bundle,
230-
noname())
234+
UNNAMED)
231235
}
232236
}
233237

@@ -265,39 +269,39 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
265269

266270
fn fadd_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
267271
unsafe {
268-
let instr = llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, noname());
272+
let instr = llvm::LLVMBuildFAdd(self.llbuilder, lhs, rhs, UNNAMED);
269273
llvm::LLVMRustSetHasUnsafeAlgebra(instr);
270274
instr
271275
}
272276
}
273277

274278
fn fsub_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
275279
unsafe {
276-
let instr = llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, noname());
280+
let instr = llvm::LLVMBuildFSub(self.llbuilder, lhs, rhs, UNNAMED);
277281
llvm::LLVMRustSetHasUnsafeAlgebra(instr);
278282
instr
279283
}
280284
}
281285

282286
fn fmul_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
283287
unsafe {
284-
let instr = llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, noname());
288+
let instr = llvm::LLVMBuildFMul(self.llbuilder, lhs, rhs, UNNAMED);
285289
llvm::LLVMRustSetHasUnsafeAlgebra(instr);
286290
instr
287291
}
288292
}
289293

290294
fn fdiv_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
291295
unsafe {
292-
let instr = llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, noname());
296+
let instr = llvm::LLVMBuildFDiv(self.llbuilder, lhs, rhs, UNNAMED);
293297
llvm::LLVMRustSetHasUnsafeAlgebra(instr);
294298
instr
295299
}
296300
}
297301

298302
fn frem_fast(&mut self, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
299303
unsafe {
300-
let instr = llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, noname());
304+
let instr = llvm::LLVMBuildFRem(self.llbuilder, lhs, rhs, UNNAMED);
301305
llvm::LLVMRustSetHasUnsafeAlgebra(instr);
302306
instr
303307
}
@@ -388,7 +392,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
388392
fn dynamic_alloca(&mut self, ty: &'ll Type, name: &str, align: Align) -> &'ll Value {
389393
unsafe {
390394
let alloca = if name.is_empty() {
391-
llvm::LLVMBuildAlloca(self.llbuilder, ty, noname())
395+
llvm::LLVMBuildAlloca(self.llbuilder, ty, UNNAMED)
392396
} else {
393397
let name = SmallCStr::new(name);
394398
llvm::LLVMBuildAlloca(self.llbuilder, ty,
@@ -406,7 +410,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
406410
align: Align) -> &'ll Value {
407411
unsafe {
408412
let alloca = if name.is_empty() {
409-
llvm::LLVMBuildArrayAlloca(self.llbuilder, ty, len, noname())
413+
llvm::LLVMBuildArrayAlloca(self.llbuilder, ty, len, UNNAMED)
410414
} else {
411415
let name = SmallCStr::new(name);
412416
llvm::LLVMBuildArrayAlloca(self.llbuilder, ty, len,
@@ -419,15 +423,15 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
419423

420424
fn load(&mut self, ptr: &'ll Value, align: Align) -> &'ll Value {
421425
unsafe {
422-
let load = llvm::LLVMBuildLoad(self.llbuilder, ptr, noname());
426+
let load = llvm::LLVMBuildLoad(self.llbuilder, ptr, UNNAMED);
423427
llvm::LLVMSetAlignment(load, align.bytes() as c_uint);
424428
load
425429
}
426430
}
427431

428432
fn volatile_load(&mut self, ptr: &'ll Value) -> &'ll Value {
429433
unsafe {
430-
let load = llvm::LLVMBuildLoad(self.llbuilder, ptr, noname());
434+
let load = llvm::LLVMBuildLoad(self.llbuilder, ptr, UNNAMED);
431435
llvm::LLVMSetVolatile(load, llvm::True);
432436
load
433437
}
@@ -443,7 +447,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
443447
let load = llvm::LLVMRustBuildAtomicLoad(
444448
self.llbuilder,
445449
ptr,
446-
noname(),
450+
UNNAMED,
447451
AtomicOrdering::from_generic(order),
448452
);
449453
// LLVM requires the alignment of atomic loads to be at least the size of the type.
@@ -646,88 +650,88 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
646650
fn gep(&mut self, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
647651
unsafe {
648652
llvm::LLVMBuildGEP(self.llbuilder, ptr, indices.as_ptr(),
649-
indices.len() as c_uint, noname())
653+
indices.len() as c_uint, UNNAMED)
650654
}
651655
}
652656

653657
fn inbounds_gep(&mut self, ptr: &'ll Value, indices: &[&'ll Value]) -> &'ll Value {
654658
unsafe {
655659
llvm::LLVMBuildInBoundsGEP(
656-
self.llbuilder, ptr, indices.as_ptr(), indices.len() as c_uint, noname())
660+
self.llbuilder, ptr, indices.as_ptr(), indices.len() as c_uint, UNNAMED)
657661
}
658662
}
659663

660664
fn struct_gep(&mut self, ptr: &'ll Value, idx: u64) -> &'ll Value {
661665
assert_eq!(idx as c_uint as u64, idx);
662666
unsafe {
663-
llvm::LLVMBuildStructGEP(self.llbuilder, ptr, idx as c_uint, noname())
667+
llvm::LLVMBuildStructGEP(self.llbuilder, ptr, idx as c_uint, UNNAMED)
664668
}
665669
}
666670

667671
/* Casts */
668672
fn trunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
669673
unsafe {
670-
llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty, noname())
674+
llvm::LLVMBuildTrunc(self.llbuilder, val, dest_ty, UNNAMED)
671675
}
672676
}
673677

674678
fn sext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
675679
unsafe {
676-
llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty, noname())
680+
llvm::LLVMBuildSExt(self.llbuilder, val, dest_ty, UNNAMED)
677681
}
678682
}
679683

680684
fn fptoui(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
681685
unsafe {
682-
llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty, noname())
686+
llvm::LLVMBuildFPToUI(self.llbuilder, val, dest_ty, UNNAMED)
683687
}
684688
}
685689

686690
fn fptosi(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
687691
unsafe {
688-
llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty,noname())
692+
llvm::LLVMBuildFPToSI(self.llbuilder, val, dest_ty,UNNAMED)
689693
}
690694
}
691695

692696
fn uitofp(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
693697
unsafe {
694-
llvm::LLVMBuildUIToFP(self.llbuilder, val, dest_ty, noname())
698+
llvm::LLVMBuildUIToFP(self.llbuilder, val, dest_ty, UNNAMED)
695699
}
696700
}
697701

698702
fn sitofp(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
699703
unsafe {
700-
llvm::LLVMBuildSIToFP(self.llbuilder, val, dest_ty, noname())
704+
llvm::LLVMBuildSIToFP(self.llbuilder, val, dest_ty, UNNAMED)
701705
}
702706
}
703707

704708
fn fptrunc(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
705709
unsafe {
706-
llvm::LLVMBuildFPTrunc(self.llbuilder, val, dest_ty, noname())
710+
llvm::LLVMBuildFPTrunc(self.llbuilder, val, dest_ty, UNNAMED)
707711
}
708712
}
709713

710714
fn fpext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
711715
unsafe {
712-
llvm::LLVMBuildFPExt(self.llbuilder, val, dest_ty, noname())
716+
llvm::LLVMBuildFPExt(self.llbuilder, val, dest_ty, UNNAMED)
713717
}
714718
}
715719

716720
fn ptrtoint(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
717721
unsafe {
718-
llvm::LLVMBuildPtrToInt(self.llbuilder, val, dest_ty, noname())
722+
llvm::LLVMBuildPtrToInt(self.llbuilder, val, dest_ty, UNNAMED)
719723
}
720724
}
721725

722726
fn inttoptr(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
723727
unsafe {
724-
llvm::LLVMBuildIntToPtr(self.llbuilder, val, dest_ty, noname())
728+
llvm::LLVMBuildIntToPtr(self.llbuilder, val, dest_ty, UNNAMED)
725729
}
726730
}
727731

728732
fn bitcast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
729733
unsafe {
730-
llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty, noname())
734+
llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty, UNNAMED)
731735
}
732736
}
733737

@@ -740,21 +744,21 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
740744

741745
fn pointercast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
742746
unsafe {
743-
llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty, noname())
747+
llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty, UNNAMED)
744748
}
745749
}
746750

747751
/* Comparisons */
748752
fn icmp(&mut self, op: IntPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
749753
let op = llvm::IntPredicate::from_generic(op);
750754
unsafe {
751-
llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, noname())
755+
llvm::LLVMBuildICmp(self.llbuilder, op as c_uint, lhs, rhs, UNNAMED)
752756
}
753757
}
754758

755759
fn fcmp(&mut self, op: RealPredicate, lhs: &'ll Value, rhs: &'ll Value) -> &'ll Value {
756760
unsafe {
757-
llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, noname())
761+
llvm::LLVMBuildFCmp(self.llbuilder, op as c_uint, lhs, rhs, UNNAMED)
758762
}
759763
}
760764

@@ -822,20 +826,20 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
822826
else_val: &'ll Value,
823827
) -> &'ll Value {
824828
unsafe {
825-
llvm::LLVMBuildSelect(self.llbuilder, cond, then_val, else_val, noname())
829+
llvm::LLVMBuildSelect(self.llbuilder, cond, then_val, else_val, UNNAMED)
826830
}
827831
}
828832

829833
#[allow(dead_code)]
830834
fn va_arg(&mut self, list: &'ll Value, ty: &'ll Type) -> &'ll Value {
831835
unsafe {
832-
llvm::LLVMBuildVAArg(self.llbuilder, list, ty, noname())
836+
llvm::LLVMBuildVAArg(self.llbuilder, list, ty, UNNAMED)
833837
}
834838
}
835839

836840
fn extract_element(&mut self, vec: &'ll Value, idx: &'ll Value) -> &'ll Value {
837841
unsafe {
838-
llvm::LLVMBuildExtractElement(self.llbuilder, vec, idx, noname())
842+
llvm::LLVMBuildExtractElement(self.llbuilder, vec, idx, UNNAMED)
839843
}
840844
}
841845

@@ -852,7 +856,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
852856
fn extract_value(&mut self, agg_val: &'ll Value, idx: u64) -> &'ll Value {
853857
assert_eq!(idx as c_uint as u64, idx);
854858
unsafe {
855-
llvm::LLVMBuildExtractValue(self.llbuilder, agg_val, idx as c_uint, noname())
859+
llvm::LLVMBuildExtractValue(self.llbuilder, agg_val, idx as c_uint, UNNAMED)
856860
}
857861
}
858862

@@ -861,15 +865,15 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
861865
assert_eq!(idx as c_uint as u64, idx);
862866
unsafe {
863867
llvm::LLVMBuildInsertValue(self.llbuilder, agg_val, elt, idx as c_uint,
864-
noname())
868+
UNNAMED)
865869
}
866870
}
867871

868872
fn landing_pad(&mut self, ty: &'ll Type, pers_fn: &'ll Value,
869873
num_clauses: usize) -> &'ll Value {
870874
unsafe {
871875
llvm::LLVMBuildLandingPad(self.llbuilder, ty, pers_fn,
872-
num_clauses as c_uint, noname())
876+
num_clauses as c_uint, UNNAMED)
873877
}
874878
}
875879

@@ -1039,14 +1043,14 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
10391043
llfn,
10401044
args.as_ptr() as *const &llvm::Value,
10411045
args.len() as c_uint,
1042-
bundle, noname()
1046+
bundle, UNNAMED
10431047
)
10441048
}
10451049
}
10461050

10471051
fn zext(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
10481052
unsafe {
1049-
llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty, noname())
1053+
llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty, UNNAMED)
10501054
}
10511055
}
10521056

@@ -1128,7 +1132,7 @@ impl Builder<'a, 'll, 'tcx> {
11281132
idx: &'ll Value,
11291133
) -> &'ll Value {
11301134
unsafe {
1131-
llvm::LLVMBuildInsertElement(self.llbuilder, vec, elt, idx, noname())
1135+
llvm::LLVMBuildInsertElement(self.llbuilder, vec, elt, idx, UNNAMED)
11321136
}
11331137
}
11341138

@@ -1139,7 +1143,7 @@ impl Builder<'a, 'll, 'tcx> {
11391143
mask: &'ll Value,
11401144
) -> &'ll Value {
11411145
unsafe {
1142-
llvm::LLVMBuildShuffleVector(self.llbuilder, v1, v2, mask, noname())
1146+
llvm::LLVMBuildShuffleVector(self.llbuilder, v1, v2, mask, UNNAMED)
11431147
}
11441148
}
11451149

@@ -1281,7 +1285,7 @@ impl Builder<'a, 'll, 'tcx> {
12811285

12821286
pub fn va_arg(&mut self, list: &'ll Value, ty: &'ll Type) -> &'ll Value {
12831287
unsafe {
1284-
llvm::LLVMBuildVAArg(self.llbuilder, list, ty, noname())
1288+
llvm::LLVMBuildVAArg(self.llbuilder, list, ty, UNNAMED)
12851289
}
12861290
}
12871291

@@ -1304,7 +1308,7 @@ impl Builder<'a, 'll, 'tcx> {
13041308
fn phi(&mut self, ty: &'ll Type, vals: &[&'ll Value], bbs: &[&'ll BasicBlock]) -> &'ll Value {
13051309
assert_eq!(vals.len(), bbs.len());
13061310
let phi = unsafe {
1307-
llvm::LLVMBuildPhi(self.llbuilder, ty, noname())
1311+
llvm::LLVMBuildPhi(self.llbuilder, ty, UNNAMED)
13081312
};
13091313
unsafe {
13101314
llvm::LLVMAddIncoming(phi, vals.as_ptr(),

src/librustc_codegen_llvm/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#![feature(box_patterns)]
1010
#![feature(box_syntax)]
11+
#![feature(const_cstr_unchecked)]
1112
#![feature(crate_visibility_modifier)]
1213
#![feature(custom_attribute)]
1314
#![feature(extern_types)]

0 commit comments

Comments
 (0)