Skip to content

Commit 05048af

Browse files
author
git apple-llvm automerger
committed
Merge commit '3eb3b067919b' from swift/release/5.7 into stable/20211026
2 parents bd78ff3 + 3eb3b06 commit 05048af

File tree

2 files changed

+106
-28
lines changed

2 files changed

+106
-28
lines changed

llvm/lib/Target/X86/X86FastISel.cpp

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ class X86FastISel final : public FastISel {
165165

166166
bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
167167

168+
bool isPointerSwiftError(const Value *PtrV);
169+
168170
bool IsMemcpySmall(uint64_t Len);
169171

170172
bool TryEmitSmallMemcpy(X86AddressMode DestAM,
@@ -1123,6 +1125,22 @@ bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
11231125
return false;
11241126
}
11251127

1128+
// Swifterror values can come from either a function parameter with
1129+
// swifterror attribute or an alloca with swifterror attribute.
1130+
bool X86FastISel::isPointerSwiftError(const Value *PtrV) {
1131+
if (!TLI.supportSwiftError())
1132+
return false;
1133+
1134+
if (const Argument *Arg = dyn_cast<Argument>(PtrV))
1135+
if (Arg->hasSwiftErrorAttr())
1136+
return true;;
1137+
1138+
if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV))
1139+
if (Alloca->isSwiftError())
1140+
return true;
1141+
1142+
return false;
1143+
}
11261144

11271145
/// X86SelectStore - Select and emit code to implement store instructions.
11281146
bool X86FastISel::X86SelectStore(const Instruction *I) {
@@ -1132,20 +1150,8 @@ bool X86FastISel::X86SelectStore(const Instruction *I) {
11321150
if (S->isAtomic())
11331151
return false;
11341152

1135-
const Value *PtrV = I->getOperand(1);
1136-
if (TLI.supportSwiftError()) {
1137-
// Swifterror values can come from either a function parameter with
1138-
// swifterror attribute or an alloca with swifterror attribute.
1139-
if (const Argument *Arg = dyn_cast<Argument>(PtrV)) {
1140-
if (Arg->hasSwiftErrorAttr())
1141-
return false;
1142-
}
1143-
1144-
if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) {
1145-
if (Alloca->isSwiftError())
1146-
return false;
1147-
}
1148-
}
1153+
if (isPointerSwiftError(I->getOperand(1)))
1154+
return false;
11491155

11501156
const Value *Val = S->getValueOperand();
11511157
const Value *Ptr = S->getPointerOperand();
@@ -1324,20 +1330,8 @@ bool X86FastISel::X86SelectLoad(const Instruction *I) {
13241330
if (LI->isAtomic())
13251331
return false;
13261332

1327-
const Value *SV = I->getOperand(0);
1328-
if (TLI.supportSwiftError()) {
1329-
// Swifterror values can come from either a function parameter with
1330-
// swifterror attribute or an alloca with swifterror attribute.
1331-
if (const Argument *Arg = dyn_cast<Argument>(SV)) {
1332-
if (Arg->hasSwiftErrorAttr())
1333-
return false;
1334-
}
1335-
1336-
if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) {
1337-
if (Alloca->isSwiftError())
1338-
return false;
1339-
}
1340-
}
1333+
if (isPointerSwiftError(I->getOperand(0)))
1334+
return false;
13411335

13421336
MVT VT;
13431337
if (!isTypeLegal(LI->getType(), VT, /*AllowI1=*/true))
@@ -3935,6 +3929,10 @@ unsigned X86FastISel::fastMaterializeFloatZero(const ConstantFP *CF) {
39353929
bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
39363930
const LoadInst *LI) {
39373931
const Value *Ptr = LI->getPointerOperand();
3932+
3933+
if (isPointerSwiftError(Ptr))
3934+
return false;
3935+
39383936
X86AddressMode AM;
39393937
if (!X86SelectAddress(Ptr, AM))
39403938
return false;

llvm/test/CodeGen/X86/swifterror.ll

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,3 +807,83 @@ a:
807807
%error = load %swift_error*, %swift_error** %err
808808
ret %swift_error* %error
809809
}
810+
811+
define i32 @nofold_swifterror() {
812+
; CHECK-LABEL: nofold_swifterror:
813+
; CHECK: cmpq $0, %r12
814+
; CHECK-APPLE-LABEL: nofold_swifterror:
815+
; CHECK-APPLE: ## %bb.0:
816+
; CHECK-APPLE-NEXT: pushq %r12
817+
; CHECK-APPLE-NEXT: .cfi_def_cfa_offset 16
818+
; CHECK-APPLE-NEXT: subq $16, %rsp
819+
; CHECK-APPLE-NEXT: .cfi_def_cfa_offset 32
820+
; CHECK-APPLE-NEXT: .cfi_offset %r12, -16
821+
; CHECK-APPLE-NEXT: callq _bar
822+
; CHECK-APPLE-NEXT: testq %r12, %r12
823+
; CHECK-APPLE-NEXT: leaq 16(%rsp), %rsp
824+
; CHECK-APPLE-NEXT: popq %r12
825+
; CHECK-APPLE-NEXT: jne LBB26_2
826+
; CHECK-APPLE-NEXT: ## %bb.1: ## %good
827+
; CHECK-APPLE-NEXT: xorl %eax, %eax
828+
; CHECK-APPLE-NEXT: retq
829+
; CHECK-APPLE-NEXT: LBB26_2: ## %bad
830+
; CHECK-APPLE-NEXT: movl $42, %eax
831+
; CHECK-APPLE-NEXT: retq
832+
;
833+
; CHECK-O0-LABEL: nofold_swifterror:
834+
; CHECK-O0: ## %bb.0:
835+
; CHECK-O0-NEXT: pushq %r12
836+
; CHECK-O0-NEXT: .cfi_def_cfa_offset 16
837+
; CHECK-O0-NEXT: subq $16, %rsp
838+
; CHECK-O0-NEXT: .cfi_def_cfa_offset 32
839+
; CHECK-O0-NEXT: .cfi_offset %r12, -16
840+
; CHECK-O0-NEXT: ## implicit-def: $rax
841+
; CHECK-O0-NEXT: ## %bb.1: ## %next
842+
; CHECK-O0-NEXT: movq (%rsp), %r12 ## 8-byte Reload
843+
; CHECK-O0-NEXT: callq _bar
844+
; CHECK-O0-NEXT: cmpq $0, %r12
845+
; CHECK-O0-NEXT: jne LBB26_3
846+
; CHECK-O0-NEXT: ## %bb.2: ## %good
847+
; CHECK-O0-NEXT: xorl %eax, %eax
848+
; CHECK-O0-NEXT: addq $16, %rsp
849+
; CHECK-O0-NEXT: popq %r12
850+
; CHECK-O0-NEXT: retq
851+
; CHECK-O0-NEXT: LBB26_3: ## %bad
852+
; CHECK-O0-NEXT: movl $42, %eax
853+
; CHECK-O0-NEXT: addq $16, %rsp
854+
; CHECK-O0-NEXT: popq %r12
855+
; CHECK-O0-NEXT: retq
856+
;
857+
; CHECK-i386-LABEL: nofold_swifterror:
858+
; CHECK-i386: ## %bb.0:
859+
; CHECK-i386-NEXT: subl $12, %esp
860+
; CHECK-i386-NEXT: .cfi_def_cfa_offset 16
861+
; CHECK-i386-NEXT: leal 8(%esp), %eax
862+
; CHECK-i386-NEXT: movl %eax, (%esp)
863+
; CHECK-i386-NEXT: calll _bar
864+
; CHECK-i386-NEXT: cmpl $0, 8(%esp)
865+
; CHECK-i386-NEXT: leal 12(%esp), %esp
866+
; CHECK-i386-NEXT: jne LBB26_2
867+
; CHECK-i386-NEXT: ## %bb.1: ## %good
868+
; CHECK-i386-NEXT: xorl %eax, %eax
869+
; CHECK-i386-NEXT: retl
870+
; CHECK-i386-NEXT: LBB26_2: ## %bad
871+
; CHECK-i386-NEXT: movl $42, %eax
872+
; CHECK-i386-NEXT: retl
873+
%se = alloca swifterror i8*, align 8
874+
br label %next
875+
876+
next:
877+
call void @bar(i8** swifterror %se)
878+
%err = load i8*, i8** %se, align 8
879+
%tst = icmp eq i8* %err, null
880+
br i1 %tst, label %good, label %bad
881+
882+
good:
883+
ret i32 0
884+
885+
bad:
886+
ret i32 42
887+
}
888+
889+
declare void @bar(i8** swifterror)

0 commit comments

Comments
 (0)