Skip to content

Commit 5e64ea6

Browse files
committed
[irgen] Refactor llvm intrinsic emission out of emitBuiltinCall.
This simplifies the function and will make it easier for me to convert the function into an exhaustive switch with an NFC commit.
1 parent 66eef68 commit 5e64ea6

File tree

1 file changed

+58
-49
lines changed

1 file changed

+58
-49
lines changed

lib/IRGen/GenBuiltin.cpp

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,71 @@ getMaximallyAbstractedLoweredTypeAndTypeInfo(IRGenModule &IGM, Type unloweredTyp
129129
return {lowered, IGM.getTypeInfo(lowered)};
130130
}
131131

132+
static bool emitLLVMIRIntrinsicCall(IRGenFunction &IGF, Identifier FnID,
133+
Explosion &args, Explosion &out) {
134+
const IntrinsicInfo &IInfo = IGF.getSILModule().getIntrinsicInfo(FnID);
135+
llvm::Intrinsic::ID IID = IInfo.ID;
136+
137+
if (IID == llvm::Intrinsic::not_intrinsic)
138+
return false;
139+
140+
// Emit non-mergeable traps only.
141+
if (IGF.Builder.isTrapIntrinsic(IID)) {
142+
IGF.Builder.CreateNonMergeableTrap(IGF.IGM, StringRef());
143+
return true;
144+
}
145+
146+
// Implement the ptrauth builtins as no-ops when the Clang
147+
// intrinsics are disabled.
148+
if ((IID == llvm::Intrinsic::ptrauth_sign ||
149+
IID == llvm::Intrinsic::ptrauth_auth ||
150+
IID == llvm::Intrinsic::ptrauth_resign ||
151+
IID == llvm::Intrinsic::ptrauth_strip) &&
152+
!IGF.IGM.getClangASTContext().getLangOpts().PointerAuthIntrinsics) {
153+
out.add(args.claimNext()); // Return the input pointer.
154+
(void)args.claimNext(); // Ignore the key.
155+
if (IID != llvm::Intrinsic::ptrauth_strip) {
156+
(void)args.claimNext(); // Ignore the discriminator.
157+
}
158+
if (IID == llvm::Intrinsic::ptrauth_resign) {
159+
(void)args.claimNext(); // Ignore the new key.
160+
(void)args.claimNext(); // Ignore the new discriminator.
161+
}
162+
return true;
163+
}
164+
165+
SmallVector<llvm::Type *, 4> ArgTys;
166+
for (auto T : IInfo.Types)
167+
ArgTys.push_back(IGF.IGM.getStorageTypeForLowered(T->getCanonicalType()));
168+
169+
auto F = llvm::Intrinsic::getOrInsertDeclaration(
170+
&IGF.IGM.Module, (llvm::Intrinsic::ID)IID, ArgTys);
171+
llvm::FunctionType *FT = F->getFunctionType();
172+
SmallVector<llvm::Value *, 8> IRArgs;
173+
for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
174+
IRArgs.push_back(args.claimNext());
175+
llvm::Value *TheCall =
176+
IGF.Builder.CreateIntrinsicCall((llvm::Intrinsic::ID)IID, ArgTys, IRArgs);
177+
178+
if (!TheCall->getType()->isVoidTy())
179+
extractScalarResults(IGF, TheCall->getType(), TheCall, out);
180+
181+
return true;
182+
}
183+
132184
/// emitBuiltinCall - Emit a call to a builtin function.
133185
void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin,
134186
BuiltinInst *Inst, ArrayRef<SILType> argTypes,
135187
Explosion &args, Explosion &out) {
136188
auto &IGM = IGF.IGM;
137189

138190
Identifier FnId = Inst->getName();
191+
192+
// Before we do anything, lets see if we have an LLVM IR Intrinsic Call. If we
193+
// did, we can return early.
194+
if (emitLLVMIRIntrinsicCall(IGF, FnId, args, out))
195+
return;
196+
139197
SILType resultType = Inst->getType();
140198
SubstitutionMap substitutions = Inst->getSubstitutions();
141199

@@ -413,55 +471,6 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin,
413471
return;
414472
}
415473

416-
// If this is an LLVM IR intrinsic, lower it to an intrinsic call.
417-
const IntrinsicInfo &IInfo = IGF.getSILModule().getIntrinsicInfo(FnId);
418-
llvm::Intrinsic::ID IID = IInfo.ID;
419-
420-
// Emit non-mergeable traps only.
421-
if (IGF.Builder.isTrapIntrinsic(IID)) {
422-
IGF.Builder.CreateNonMergeableTrap(IGF.IGM, StringRef());
423-
return;
424-
}
425-
426-
// Implement the ptrauth builtins as no-ops when the Clang
427-
// intrinsics are disabled.
428-
if ((IID == llvm::Intrinsic::ptrauth_sign ||
429-
IID == llvm::Intrinsic::ptrauth_auth ||
430-
IID == llvm::Intrinsic::ptrauth_resign ||
431-
IID == llvm::Intrinsic::ptrauth_strip) &&
432-
!IGF.IGM.getClangASTContext().getLangOpts().PointerAuthIntrinsics) {
433-
out.add(args.claimNext()); // Return the input pointer.
434-
(void) args.claimNext(); // Ignore the key.
435-
if (IID != llvm::Intrinsic::ptrauth_strip) {
436-
(void) args.claimNext(); // Ignore the discriminator.
437-
}
438-
if (IID == llvm::Intrinsic::ptrauth_resign) {
439-
(void) args.claimNext(); // Ignore the new key.
440-
(void) args.claimNext(); // Ignore the new discriminator.
441-
}
442-
return;
443-
}
444-
445-
if (IID != llvm::Intrinsic::not_intrinsic) {
446-
SmallVector<llvm::Type*, 4> ArgTys;
447-
for (auto T : IInfo.Types)
448-
ArgTys.push_back(IGF.IGM.getStorageTypeForLowered(T->getCanonicalType()));
449-
450-
auto F = llvm::Intrinsic::getOrInsertDeclaration(
451-
&IGF.IGM.Module, (llvm::Intrinsic::ID)IID, ArgTys);
452-
llvm::FunctionType *FT = F->getFunctionType();
453-
SmallVector<llvm::Value*, 8> IRArgs;
454-
for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
455-
IRArgs.push_back(args.claimNext());
456-
llvm::Value *TheCall = IGF.Builder.CreateIntrinsicCall(
457-
(llvm::Intrinsic::ID)IID, ArgTys, IRArgs);
458-
459-
if (!TheCall->getType()->isVoidTy())
460-
extractScalarResults(IGF, TheCall->getType(), TheCall, out);
461-
462-
return;
463-
}
464-
465474
if (Builtin.ID == BuiltinValueKind::StringObjectOr) {
466475
llvm::Value *lhs = args.claimNext();
467476
llvm::Value *rhs = args.claimNext();

0 commit comments

Comments
 (0)