@@ -118,14 +118,15 @@ static FullApplySite CloneApply(FullApplySite AI, SILBuilder &Builder) {
118
118
// / Insert monomorphic inline caches for a specific class or metatype
119
119
// / type \p SubClassTy.
120
120
static FullApplySite speculateMonomorphicTarget (FullApplySite AI,
121
- SILType SubType,
121
+ CanType SubType,
122
+ ClassDecl *CD,
122
123
CheckedCastBranchInst *&CCBI) {
123
- CCBI = nullptr ;
124
- // Bail if this class_method cannot be devirtualized.
125
- if (!canDevirtualizeClassMethod (AI, SubType))
124
+ if (SubType->hasDynamicSelfType ())
126
125
return FullApplySite ();
127
126
128
- if (SubType.getASTType ()->hasDynamicSelfType ())
127
+ CCBI = nullptr ;
128
+ // Bail if this class_method cannot be devirtualized.
129
+ if (!canDevirtualizeClassMethod (AI, CD))
129
130
return FullApplySite ();
130
131
131
132
// Can't speculate begin_apply yet.
@@ -144,7 +145,9 @@ static FullApplySite speculateMonomorphicTarget(FullApplySite AI,
144
145
SILBasicBlock *Iden = F->createBasicBlock ();
145
146
// Virt is the block containing the slow virtual call.
146
147
SILBasicBlock *Virt = F->createBasicBlock ();
147
- Iden->createPhiArgument (SubType, ValueOwnershipKind::Owned);
148
+ Iden->createPhiArgument (
149
+ SILType::getPrimitiveObjectType (SubType),
150
+ ValueOwnershipKind::Owned);
148
151
149
152
SILBasicBlock *Continue = Entry->split (It);
150
153
@@ -155,8 +158,10 @@ static FullApplySite speculateMonomorphicTarget(FullApplySite AI,
155
158
ClassMethodInst *CMI = cast<ClassMethodInst>(AI.getCallee ());
156
159
157
160
CCBI = Builder.createCheckedCastBranch (AI.getLoc (), /* exact*/ true ,
158
- CMI->getOperand (), SubType, Iden,
159
- Virt);
161
+ CMI->getOperand (),
162
+ SILType::getPrimitiveObjectType (SubType),
163
+ Iden,
164
+ Virt);
160
165
It = CCBI->getIterator ();
161
166
162
167
SILBuilderWithScope VirtBuilder (Virt, AI.getInstruction ());
@@ -219,7 +224,7 @@ static FullApplySite speculateMonomorphicTarget(FullApplySite AI,
219
224
220
225
// Devirtualize the apply instruction on the identical path.
221
226
auto NewInst =
222
- devirtualizeClassMethod (IdenAI, DownCastedClassInstance, nullptr );
227
+ devirtualizeClassMethod (IdenAI, DownCastedClassInstance, CD, nullptr );
223
228
assert (NewInst && " Expected to be able to devirtualize apply!" );
224
229
(void )NewInst;
225
230
@@ -381,21 +386,20 @@ static bool tryToSpeculateTarget(FullApplySite AI, ClassHierarchyAnalysis *CHA,
381
386
// with a value whose type is closer (in the class hierarchy) to the
382
387
// actual dynamic type.
383
388
auto SubTypeValue = stripUpCasts (CMI->getOperand ());
384
- SILType SubType = SubTypeValue->getType ();
389
+ CanType SubType = SubTypeValue->getType (). getASTType ();
385
390
386
391
// Bail if any generic types parameters of the class instance type are
387
392
// unbound.
388
393
// We cannot devirtualize unbound generic calls yet.
389
- if (SubType. hasArchetype ())
394
+ if (SubType-> hasArchetype ())
390
395
return false ;
391
396
392
- auto &M = CMI->getModule ();
393
- auto ClassType = SubType;
394
- if (SubType.is <MetatypeType>())
395
- ClassType = SubType.getMetatypeInstanceType (M);
397
+ auto *F = CMI->getFunction ();
398
+ auto &M = F->getModule ();
396
399
397
400
CheckedCastBranchInst *LastCCBI = nullptr ;
398
401
402
+ auto ClassType = getSelfInstanceType (SubType);
399
403
ClassDecl *CD = ClassType.getClassOrBoundGenericClass ();
400
404
assert (CD && " Expected decl for class type!" );
401
405
@@ -404,15 +408,15 @@ static bool tryToSpeculateTarget(FullApplySite AI, ClassHierarchyAnalysis *CHA,
404
408
// try to devirtualize it completely.
405
409
ClassHierarchyAnalysis::ClassList Subs;
406
410
if (isDefaultCaseKnown (CHA, AI, CD, Subs)) {
407
- auto NewInst = tryDevirtualizeClassMethod (AI, SubTypeValue, &ORE);
411
+ auto NewInst = tryDevirtualizeClassMethod (AI, SubTypeValue, CD, &ORE);
408
412
if (NewInst)
409
413
deleteDevirtualizedApply (AI);
410
414
return bool (NewInst);
411
415
}
412
416
413
417
LLVM_DEBUG (llvm::dbgs () << " Inserting monomorphic speculative call for "
414
418
" class " << CD->getName () << " \n " );
415
- return !!speculateMonomorphicTarget (AI, SubType, LastCCBI);
419
+ return !!speculateMonomorphicTarget (AI, SubType, CD, LastCCBI);
416
420
}
417
421
418
422
// True if any instructions were changed or generated.
@@ -438,7 +442,7 @@ static bool tryToSpeculateTarget(FullApplySite AI, ClassHierarchyAnalysis *CHA,
438
442
439
443
// Try to devirtualize the static class of instance
440
444
// if it is possible.
441
- if (auto F = getTargetClassMethod (M, SubType , CMI)) {
445
+ if (auto F = getTargetClassMethod (M, CD , CMI)) {
442
446
// Do not devirtualize if a method in the base class is marked
443
447
// as non-optimizable. This way it is easy to disable the
444
448
// devirtualization of this method in the base class and
@@ -447,7 +451,7 @@ static bool tryToSpeculateTarget(FullApplySite AI, ClassHierarchyAnalysis *CHA,
447
451
return false ;
448
452
}
449
453
450
- auto FirstAI = speculateMonomorphicTarget (AI, SubType, LastCCBI);
454
+ auto FirstAI = speculateMonomorphicTarget (AI, SubType, CD, LastCCBI);
451
455
if (FirstAI) {
452
456
Changed = true ;
453
457
AI = FirstAI;
@@ -501,18 +505,15 @@ static bool tryToSpeculateTarget(FullApplySite AI, ClassHierarchyAnalysis *CHA,
501
505
}
502
506
503
507
CanType CanClassType = S->getDeclaredInterfaceType ()->getCanonicalType ();
504
- SILType ClassType = SILType::getPrimitiveObjectType (CanClassType);
505
-
506
- auto ClassOrMetatypeType = ClassType;
507
- if (auto EMT = SubType.getAs <AnyMetatypeType>()) {
508
- auto InstTy = ClassType.getASTType ();
509
- auto *MetaTy = MetatypeType::get (InstTy, EMT->getRepresentation ());
510
- auto CanMetaTy = CanMetatypeType (MetaTy);
511
- ClassOrMetatypeType = SILType::getPrimitiveObjectType (CanMetaTy);
508
+
509
+ auto ClassOrMetatypeType = CanClassType;
510
+ if (auto MT = dyn_cast<MetatypeType>(SubType)) {
511
+ ClassOrMetatypeType = CanMetatypeType::get (CanClassType,
512
+ MT->getRepresentation ());
512
513
}
513
514
514
515
// Pass the metatype of the subclass.
515
- auto NewAI = speculateMonomorphicTarget (AI, ClassOrMetatypeType, LastCCBI);
516
+ auto NewAI = speculateMonomorphicTarget (AI, ClassOrMetatypeType, S, LastCCBI);
516
517
if (!NewAI) {
517
518
NotHandledSubsNum++;
518
519
continue ;
@@ -567,7 +568,7 @@ static bool tryToSpeculateTarget(FullApplySite AI, ClassHierarchyAnalysis *CHA,
567
568
ORE.emit (RB);
568
569
return true ;
569
570
}
570
- auto NewInst = tryDevirtualizeClassMethod (AI, SubTypeValue, nullptr );
571
+ auto NewInst = tryDevirtualizeClassMethod (AI, SubTypeValue, CD, nullptr );
571
572
if (NewInst) {
572
573
ORE.emit (RB);
573
574
deleteDevirtualizedApply (AI);
0 commit comments