Skip to content

Commit 6a288c1

Browse files
committed
Replace hardcoded intrinsic list with speculatable attribute.
No change in which intrinsics should be speculated. llvm-svn: 301995
1 parent 8955efc commit 6a288c1

File tree

12 files changed

+44
-89
lines changed

12 files changed

+44
-89
lines changed

llvm/include/llvm/IR/Intrinsics.td

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ def int_memset : Intrinsic<[],
404404
// FIXME: Add version of these floating point intrinsics which allow non-default
405405
// rounding modes and FP exception handling.
406406

407-
let IntrProperties = [IntrNoMem] in {
407+
let IntrProperties = [IntrNoMem, IntrSpeculatable] in {
408408
def int_fma : Intrinsic<[llvm_anyfloat_ty],
409409
[LLVMMatchType<0>, LLVMMatchType<0>,
410410
LLVMMatchType<0>]>;
@@ -440,10 +440,12 @@ let IntrProperties = [IntrNoMem] in {
440440
}
441441

442442
def int_minnum : Intrinsic<[llvm_anyfloat_ty],
443-
[LLVMMatchType<0>, LLVMMatchType<0>], [IntrNoMem, Commutative]
443+
[LLVMMatchType<0>, LLVMMatchType<0>],
444+
[IntrNoMem, IntrSpeculatable, Commutative]
444445
>;
445446
def int_maxnum : Intrinsic<[llvm_anyfloat_ty],
446-
[LLVMMatchType<0>, LLVMMatchType<0>], [IntrNoMem, Commutative]
447+
[LLVMMatchType<0>, LLVMMatchType<0>],
448+
[IntrNoMem, IntrSpeculatable, Commutative]
447449
>;
448450

449451
// NOTE: these are internal interfaces.
@@ -455,7 +457,7 @@ def int_siglongjmp : Intrinsic<[], [llvm_ptr_ty, llvm_i32_ty], [IntrNoReturn]>;
455457
// Internal interface for object size checking
456458
def int_objectsize : Intrinsic<[llvm_anyint_ty],
457459
[llvm_anyptr_ty, llvm_i1_ty, llvm_i1_ty],
458-
[IntrNoMem]>,
460+
[IntrNoMem, IntrSpeculatable]>,
459461
GCCBuiltin<"__builtin_object_size">;
460462

461463
//===--------------- Constrained Floating Point Intrinsics ----------------===//
@@ -500,7 +502,7 @@ def int_expect : Intrinsic<[llvm_anyint_ty], [LLVMMatchType<0>,
500502
//
501503

502504
// None of these intrinsics accesses memory at all.
503-
let IntrProperties = [IntrNoMem] in {
505+
let IntrProperties = [IntrNoMem, IntrSpeculatable] in {
504506
def int_bswap: Intrinsic<[llvm_anyint_ty], [LLVMMatchType<0>]>;
505507
def int_ctpop: Intrinsic<[llvm_anyint_ty], [LLVMMatchType<0>]>;
506508
def int_ctlz : Intrinsic<[llvm_anyint_ty], [LLVMMatchType<0>, llvm_i1_ty]>;
@@ -511,10 +513,11 @@ let IntrProperties = [IntrNoMem] in {
511513
//===------------------------ Debugger Intrinsics -------------------------===//
512514
//
513515

514-
// None of these intrinsics accesses memory at all...but that doesn't mean the
515-
// optimizers can change them aggressively. Special handling needed in a few
516-
// places.
517-
let IntrProperties = [IntrNoMem] in {
516+
// None of these intrinsics accesses memory at all...but that doesn't
517+
// mean the optimizers can change them aggressively. Special handling
518+
// needed in a few places. These synthetic intrinsics have no
519+
// side-effects and just mark information about their operands.
520+
let IntrProperties = [IntrNoMem, IntrSpeculatable] in {
518521
def int_dbg_declare : Intrinsic<[],
519522
[llvm_metadata_ty,
520523
llvm_metadata_ty,
@@ -592,24 +595,24 @@ def int_adjust_trampoline : Intrinsic<[llvm_ptr_ty], [llvm_ptr_ty],
592595
// Expose the carry flag from add operations on two integrals.
593596
def int_sadd_with_overflow : Intrinsic<[llvm_anyint_ty, llvm_i1_ty],
594597
[LLVMMatchType<0>, LLVMMatchType<0>],
595-
[IntrNoMem]>;
598+
[IntrNoMem, IntrSpeculatable]>;
596599
def int_uadd_with_overflow : Intrinsic<[llvm_anyint_ty, llvm_i1_ty],
597600
[LLVMMatchType<0>, LLVMMatchType<0>],
598-
[IntrNoMem]>;
601+
[IntrNoMem, IntrSpeculatable]>;
599602

600603
def int_ssub_with_overflow : Intrinsic<[llvm_anyint_ty, llvm_i1_ty],
601604
[LLVMMatchType<0>, LLVMMatchType<0>],
602-
[IntrNoMem]>;
605+
[IntrNoMem, IntrSpeculatable]>;
603606
def int_usub_with_overflow : Intrinsic<[llvm_anyint_ty, llvm_i1_ty],
604607
[LLVMMatchType<0>, LLVMMatchType<0>],
605-
[IntrNoMem]>;
608+
[IntrNoMem, IntrSpeculatable]>;
606609

607610
def int_smul_with_overflow : Intrinsic<[llvm_anyint_ty, llvm_i1_ty],
608611
[LLVMMatchType<0>, LLVMMatchType<0>],
609-
[IntrNoMem]>;
612+
[IntrNoMem, IntrSpeculatable]>;
610613
def int_umul_with_overflow : Intrinsic<[llvm_anyint_ty, llvm_i1_ty],
611614
[LLVMMatchType<0>, LLVMMatchType<0>],
612-
[IntrNoMem]>;
615+
[IntrNoMem, IntrSpeculatable]>;
613616

614617
//===------------------------- Memory Use Markers -------------------------===//
615618
//
@@ -633,7 +636,7 @@ def int_invariant_end : Intrinsic<[],
633636
// it can be CSE only if memory didn't change between 2 barriers call,
634637
// which is valid.
635638
// The argument also can't be marked with 'returned' attribute, because
636-
// it would remove barrier.
639+
// it would remove barrier.
637640
def int_invariant_group_barrier : Intrinsic<[llvm_ptr_ty],
638641
[llvm_ptr_ty],
639642
[IntrReadMem, IntrArgMemOnly]>;

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3320,67 +3320,10 @@ bool llvm::isSafeToSpeculativelyExecute(const Value *V,
33203320
case Instruction::Call: {
33213321
auto *CI = cast<const CallInst>(Inst);
33223322
const Function *Callee = CI->getCalledFunction();
3323-
if (Callee && Callee->isSpeculatable())
3324-
return true;
3325-
if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
3326-
switch (II->getIntrinsicID()) {
3327-
// These synthetic intrinsics have no side-effects and just mark
3328-
// information about their operands.
3329-
// FIXME: There are other no-op synthetic instructions that potentially
3330-
// should be considered at least *safe* to speculate...
3331-
// FIXME: The speculatable attribute should be added to all these
3332-
// intrinsics and this case statement should be removed.
3333-
case Intrinsic::dbg_declare:
3334-
case Intrinsic::dbg_value:
3335-
return true;
33363323

3337-
case Intrinsic::bitreverse:
3338-
case Intrinsic::bswap:
3339-
case Intrinsic::ctlz:
3340-
case Intrinsic::ctpop:
3341-
case Intrinsic::cttz:
3342-
case Intrinsic::objectsize:
3343-
case Intrinsic::sadd_with_overflow:
3344-
case Intrinsic::smul_with_overflow:
3345-
case Intrinsic::ssub_with_overflow:
3346-
case Intrinsic::uadd_with_overflow:
3347-
case Intrinsic::umul_with_overflow:
3348-
case Intrinsic::usub_with_overflow:
3349-
return true;
3350-
// These intrinsics are defined to have the same behavior as libm
3351-
// functions except for setting errno.
3352-
case Intrinsic::sqrt:
3353-
case Intrinsic::fma:
3354-
case Intrinsic::fmuladd:
3355-
return true;
3356-
// These intrinsics are defined to have the same behavior as libm
3357-
// functions, and the corresponding libm functions never set errno.
3358-
case Intrinsic::trunc:
3359-
case Intrinsic::copysign:
3360-
case Intrinsic::fabs:
3361-
case Intrinsic::minnum:
3362-
case Intrinsic::maxnum:
3363-
return true;
3364-
// These intrinsics are defined to have the same behavior as libm
3365-
// functions, which never overflow when operating on the IEEE754 types
3366-
// that we support, and never set errno otherwise.
3367-
case Intrinsic::ceil:
3368-
case Intrinsic::floor:
3369-
case Intrinsic::nearbyint:
3370-
case Intrinsic::rint:
3371-
case Intrinsic::round:
3372-
return true;
3373-
// These intrinsics do not correspond to any libm function, and
3374-
// do not set errno.
3375-
case Intrinsic::powi:
3376-
return true;
3377-
// TODO: are convert_{from,to}_fp16 safe?
3378-
// TODO: can we list target-specific intrinsics here?
3379-
default: break;
3380-
}
3381-
}
3382-
return false; // The called function could have undefined behavior or
3383-
// side-effects, even if marked readnone nounwind.
3324+
// The called function could have undefined behavior or side-effects, even
3325+
// if marked readnone nounwind.
3326+
return Callee && Callee->isSpeculatable();
33843327
}
33853328
case Instruction::VAArg:
33863329
case Instruction::Alloca:

llvm/lib/Transforms/Utils/BuildLibCalls.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,13 @@ Value *llvm::emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
889889
Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
890890
Op->getType());
891891
CallInst *CI = B.CreateCall(Callee, Op, Name);
892-
CI->setAttributes(Attrs);
892+
893+
// The incoming attribute set may have come from a speculatable intrinsic, but
894+
// is being replaced with a library call which is not allowed to be
895+
// speculatable.
896+
CI->setAttributes(Attrs.removeAttribute(B.getContext(),
897+
AttributeList::FunctionIndex,
898+
Attribute::Speculatable));
893899
if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
894900
CI->setCallingConv(F->getCallingConv());
895901

llvm/test/Feature/intrinsics.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,5 @@ define void @trap() {
6969
ret void
7070
}
7171

72-
; CHECK: attributes #0 = { nounwind readnone }
72+
; CHECK: attributes #0 = { nounwind readnone speculatable }
7373
; CHECK: attributes #1 = { noreturn nounwind }

llvm/test/Transforms/BBVectorize/simple-int.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,4 +503,4 @@ define i64 @testcttzneg(i64 %A1, i64 %A2, i64 %B1, i64 %B2) {
503503
; CHECK: declare <2 x i64> @llvm.ctpop.v2i64(<2 x i64>) #0
504504
; CHECK: declare <2 x i64> @llvm.ctlz.v2i64(<2 x i64>, i1) #0
505505
; CHECK: declare <2 x i64> @llvm.cttz.v2i64(<2 x i64>, i1) #0
506-
; CHECK: attributes #0 = { nounwind readnone }
506+
; CHECK: attributes #0 = { nounwind readnone speculatable }

llvm/test/Transforms/DeadArgElim/2010-04-30-DbgInfo.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ bb2: ; preds = %bb1, %bb
3939
declare void @llvm.dbg.value(metadata, i64, metadata, metadata) nounwind readnone
4040

4141
; CHECK: attributes #0 = { nounwind ssp }
42-
; CHECK: attributes #1 = { nounwind readnone }
42+
; CHECK: attributes #1 = { nounwind readnone speculatable }
4343
; CHECK: attributes #2 = { noinline nounwind ssp }
4444
; CHECK: attributes [[NUW]] = { nounwind }
4545

llvm/test/Transforms/InstCombine/intrinsics.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ define i32 @cttz(i32 %a) {
284284
define i1 @cttz_knownbits(i32 %arg) {
285285
; CHECK-LABEL: @cttz_knownbits(
286286
; CHECK-NEXT: [[OR:%.*]] = or i32 [[ARG:%.*]], 4
287-
; CHECK-NEXT: [[CNT:%.*]] = call i32 @llvm.cttz.i32(i32 [[OR]], i1 true) #0
287+
; CHECK-NEXT: [[CNT:%.*]] = call i32 @llvm.cttz.i32(i32 [[OR]], i1 true)
288288
; CHECK-NEXT: [[RES:%.*]] = icmp eq i32 [[CNT]], 4
289289
; CHECK-NEXT: ret i1 [[RES]]
290290
;
@@ -307,7 +307,7 @@ define i8 @ctlz(i8 %a) {
307307
define i1 @ctlz_knownbits(i8 %arg) {
308308
; CHECK-LABEL: @ctlz_knownbits(
309309
; CHECK-NEXT: [[OR:%.*]] = or i8 [[ARG:%.*]], 32
310-
; CHECK-NEXT: [[CNT:%.*]] = call i8 @llvm.ctlz.i8(i8 [[OR]], i1 true) #0
310+
; CHECK-NEXT: [[CNT:%.*]] = call i8 @llvm.ctlz.i8(i8 [[OR]], i1 true)
311311
; CHECK-NEXT: [[RES:%.*]] = icmp eq i8 [[CNT]], 4
312312
; CHECK-NEXT: ret i1 [[RES]]
313313
;

llvm/test/Transforms/InstCombine/pow-sqrt.ll

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ define double @pow_half(double %x) {
66
}
77

88
; CHECK-LABEL: define double @pow_half(
9-
; CHECK-NEXT: %sqrt = call fast double @sqrt(double %x)
9+
; CHECK-NEXT: %sqrt = call fast double @sqrt(double %x) #1
1010
; CHECK-NEXT: ret double %sqrt
1111

1212
define double @pow_neghalf(double %x) {
@@ -15,8 +15,11 @@ define double @pow_neghalf(double %x) {
1515
}
1616

1717
; CHECK-LABEL: define double @pow_neghalf(
18-
; CHECK-NEXT: %sqrt = call fast double @sqrt(double %x) #0
18+
; CHECK-NEXT: %sqrt = call fast double @sqrt(double %x) #1
1919
; CHECK-NEXT: %sqrtrecip = fdiv fast double 1.000000e+00, %sqrt
2020
; CHECK-NEXT: ret double %sqrtrecip
2121

22-
declare double @llvm.pow.f64(double, double)
22+
declare double @llvm.pow.f64(double, double) #0
23+
24+
attributes #0 = { nounwind readnone speculatable }
25+
attributes #1 = { nounwind readnone }

llvm/test/Transforms/InstCombine/sub-xor.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ declare i32 @llvm.ctlz.i32(i32, i1) nounwind readnone
2727

2828
define i32 @test2(i32 %x) nounwind {
2929
; CHECK-LABEL: @test2(
30-
; CHECK-NEXT: [[COUNT:%.*]] = tail call i32 @llvm.ctlz.i32(i32 %x, i1 true) #0
30+
; CHECK-NEXT: [[COUNT:%.*]] = tail call i32 @llvm.ctlz.i32(i32 %x, i1 true)
3131
; CHECK-NEXT: [[SUB:%.*]] = xor i32 [[COUNT]], 31
3232
; CHECK-NEXT: ret i32 [[SUB]]
3333
;

llvm/test/Transforms/ObjCARC/basic.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3049,6 +3049,6 @@ define void @test67(i8* %x) {
30493049
!4 = !DIFile(filename: "path/to/file", directory: "/path/to/dir")
30503050
!5 = !{i32 2, !"Debug Info Version", i32 3}
30513051

3052-
; CHECK: attributes #0 = { nounwind readnone }
3052+
; CHECK: attributes #0 = { nounwind readnone speculatable }
30533053
; CHECK: attributes [[NUW]] = { nounwind }
30543054
; CHECK: ![[RELEASE]] = !{}

llvm/test/Transforms/ObjCARC/ensure-that-exception-unwind-path-is-visited.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ declare void @NSLog(i8*, ...)
105105
declare void @llvm.dbg.value(metadata, i64, metadata, metadata) nounwind readnone
106106

107107
; CHECK: attributes #0 = { ssp uwtable }
108-
; CHECK: attributes #1 = { nounwind readnone }
108+
; CHECK: attributes #1 = { nounwind readnone speculatable }
109109
; CHECK: attributes #2 = { nonlazybind }
110110
; CHECK: attributes #3 = { noinline ssp uwtable }
111111
; CHECK: attributes [[NUW]] = { nounwind }

llvm/test/Transforms/SLPVectorizer/X86/call.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,5 @@ entry:
147147
; CHECK: declare <2 x double> @llvm.pow.v2f64(<2 x double>, <2 x double>) [[ATTR0]]
148148
; CHECK: declare <2 x double> @llvm.exp2.v2f64(<2 x double>) [[ATTR0]]
149149

150-
; CHECK: attributes [[ATTR0]] = { nounwind readnone }
150+
; CHECK: attributes [[ATTR0]] = { nounwind readnone speculatable }
151151

0 commit comments

Comments
 (0)