@@ -2361,15 +2361,13 @@ remapIndices(Function &Caller, BasicBlock *StartBB,
2361
2361
// Updating the contextual profile after an inlining means, at a high level,
2362
2362
// copying over the data of the callee, **intentionally without any value
2363
2363
// scaling**, and copying over the callees of the inlined callee.
2364
- llvm::InlineResult llvm::InlineFunction (CallBase &CB, InlineFunctionInfo &IFI,
2365
- PGOContextualProfile &CtxProf,
2366
- bool MergeAttributes,
2367
- AAResults *CalleeAAR,
2368
- bool InsertLifetime,
2369
- Function *ForwardVarArgsTo) {
2364
+ llvm::InlineResult llvm::InlineFunction (
2365
+ CallBase &CB, InlineFunctionInfo &IFI, PGOContextualProfile &CtxProf,
2366
+ bool MergeAttributes, AAResults *CalleeAAR, bool InsertLifetime,
2367
+ Function *ForwardVarArgsTo, OptimizationRemarkEmitter *ORE) {
2370
2368
if (!CtxProf.isInSpecializedModule ())
2371
2369
return InlineFunction (CB, IFI, MergeAttributes, CalleeAAR, InsertLifetime,
2372
- ForwardVarArgsTo);
2370
+ ForwardVarArgsTo, ORE );
2373
2371
2374
2372
auto &Caller = *CB.getCaller ();
2375
2373
auto &Callee = *CB.getCalledFunction ();
@@ -2387,7 +2385,7 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
2387
2385
const auto NumCalleeCallsites = CtxProf.getNumCallsites (Callee);
2388
2386
2389
2387
auto Ret = InlineFunction (CB, IFI, MergeAttributes, CalleeAAR, InsertLifetime,
2390
- ForwardVarArgsTo);
2388
+ ForwardVarArgsTo, ORE );
2391
2389
if (!Ret.isSuccess ())
2392
2390
return Ret;
2393
2391
@@ -2457,20 +2455,8 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
2457
2455
return Ret;
2458
2456
}
2459
2457
2460
- // / This function inlines the called function into the basic block of the
2461
- // / caller. This returns false if it is not possible to inline this call.
2462
- // / The program is still in a well defined state if this occurs though.
2463
- // /
2464
- // / Note that this only does one level of inlining. For example, if the
2465
- // / instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
2466
- // / exists in the instruction stream. Similarly this will inline a recursive
2467
- // / function by one level.
2468
- llvm::InlineResult llvm::InlineFunction (CallBase &CB, InlineFunctionInfo &IFI,
2469
- bool MergeAttributes,
2470
- AAResults *CalleeAAR,
2471
- bool InsertLifetime,
2472
- Function *ForwardVarArgsTo,
2473
- OptimizationRemarkEmitter *ORE) {
2458
+ llvm::InlineResult llvm::CanInlineCallSite (const CallBase &CB,
2459
+ InlineFunctionInfo &IFI) {
2474
2460
assert (CB.getParent () && CB.getFunction () && " Instruction not in function!" );
2475
2461
2476
2462
// FIXME: we don't inline callbr yet.
@@ -2487,7 +2473,6 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
2487
2473
2488
2474
// The inliner does not know how to inline through calls with operand bundles
2489
2475
// in general ...
2490
- Value *ConvergenceControlToken = nullptr ;
2491
2476
if (CB.hasOperandBundles ()) {
2492
2477
for (int i = 0 , e = CB.getNumOperandBundles (); i != e; ++i) {
2493
2478
auto OBUse = CB.getOperandBundleAt (i);
@@ -2503,7 +2488,7 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
2503
2488
if (Tag == LLVMContext::OB_kcfi)
2504
2489
continue ;
2505
2490
if (Tag == LLVMContext::OB_convergencectrl) {
2506
- ConvergenceControlToken = OBUse.Inputs [0 ].get ();
2491
+ IFI. ConvergenceControlToken = OBUse.Inputs [0 ].get ();
2507
2492
continue ;
2508
2493
}
2509
2494
@@ -2521,28 +2506,22 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
2521
2506
// fully implements convergence control tokens, there is no mixing of
2522
2507
// controlled and uncontrolled convergent operations in the whole program.
2523
2508
if (CB.isConvergent ()) {
2524
- if (!ConvergenceControlToken &&
2509
+ if (!IFI. ConvergenceControlToken &&
2525
2510
getConvergenceEntry (CalledFunc->getEntryBlock ())) {
2526
2511
return InlineResult::failure (
2527
2512
" convergent call needs convergencectrl operand" );
2528
2513
}
2529
2514
}
2530
2515
2531
- // If the call to the callee cannot throw, set the 'nounwind' flag on any
2532
- // calls that we inline.
2533
- bool MarkNoUnwind = CB.doesNotThrow ();
2534
-
2535
- BasicBlock *OrigBB = CB.getParent ();
2536
- Function *Caller = OrigBB->getParent ();
2516
+ const BasicBlock *OrigBB = CB.getParent ();
2517
+ const Function *Caller = OrigBB->getParent ();
2537
2518
2538
2519
// GC poses two hazards to inlining, which only occur when the callee has GC:
2539
2520
// 1. If the caller has no GC, then the callee's GC must be propagated to the
2540
2521
// caller.
2541
2522
// 2. If the caller has a differing GC, it is invalid to inline.
2542
2523
if (CalledFunc->hasGC ()) {
2543
- if (!Caller->hasGC ())
2544
- Caller->setGC (CalledFunc->getGC ());
2545
- else if (CalledFunc->getGC () != Caller->getGC ())
2524
+ if (Caller->hasGC () && CalledFunc->getGC () != Caller->getGC ())
2546
2525
return InlineResult::failure (" incompatible GC" );
2547
2526
}
2548
2527
@@ -2560,34 +2539,31 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
2560
2539
? Caller->getPersonalityFn ()->stripPointerCasts ()
2561
2540
: nullptr ;
2562
2541
if (CalledPersonality) {
2563
- if (!CallerPersonality)
2564
- Caller->setPersonalityFn (CalledPersonality);
2565
2542
// If the personality functions match, then we can perform the
2566
2543
// inlining. Otherwise, we can't inline.
2567
2544
// TODO: This isn't 100% true. Some personality functions are proper
2568
2545
// supersets of others and can be used in place of the other.
2569
- else if (CalledPersonality != CallerPersonality)
2546
+ if (CallerPersonality && CalledPersonality != CallerPersonality)
2570
2547
return InlineResult::failure (" incompatible personality" );
2571
2548
}
2572
2549
2573
2550
// We need to figure out which funclet the callsite was in so that we may
2574
2551
// properly nest the callee.
2575
- Instruction *CallSiteEHPad = nullptr ;
2576
2552
if (CallerPersonality) {
2577
2553
EHPersonality Personality = classifyEHPersonality (CallerPersonality);
2578
2554
if (isScopedEHPersonality (Personality)) {
2579
2555
std::optional<OperandBundleUse> ParentFunclet =
2580
2556
CB.getOperandBundle (LLVMContext::OB_funclet);
2581
2557
if (ParentFunclet)
2582
- CallSiteEHPad = cast<FuncletPadInst>(ParentFunclet->Inputs .front ());
2558
+ IFI. CallSiteEHPad = cast<FuncletPadInst>(ParentFunclet->Inputs .front ());
2583
2559
2584
2560
// OK, the inlining site is legal. What about the target function?
2585
2561
2586
- if (CallSiteEHPad) {
2562
+ if (IFI. CallSiteEHPad ) {
2587
2563
if (Personality == EHPersonality::MSVC_CXX) {
2588
2564
// The MSVC personality cannot tolerate catches getting inlined into
2589
2565
// cleanup funclets.
2590
- if (isa<CleanupPadInst>(CallSiteEHPad)) {
2566
+ if (isa<CleanupPadInst>(IFI. CallSiteEHPad )) {
2591
2567
// Ok, the call site is within a cleanuppad. Let's check the callee
2592
2568
// for catchpads.
2593
2569
for (const BasicBlock &CalledBB : *CalledFunc) {
@@ -2607,13 +2583,34 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
2607
2583
}
2608
2584
}
2609
2585
2586
+ return InlineResult::success ();
2587
+ }
2588
+
2589
+ // / This function inlines the called function into the basic block of the
2590
+ // / caller. This returns false if it is not possible to inline this call.
2591
+ // / The program is still in a well defined state if this occurs though.
2592
+ // /
2593
+ // / Note that this only does one level of inlining. For example, if the
2594
+ // / instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
2595
+ // / exists in the instruction stream. Similarly this will inline a recursive
2596
+ // / function by one level.
2597
+ void llvm::InlineFunctionImpl (CallBase &CB, InlineFunctionInfo &IFI,
2598
+ bool MergeAttributes, AAResults *CalleeAAR,
2599
+ bool InsertLifetime, Function *ForwardVarArgsTo,
2600
+ OptimizationRemarkEmitter *ORE) {
2601
+ BasicBlock *OrigBB = CB.getParent ();
2602
+ Function *Caller = OrigBB->getParent ();
2603
+ Function *CalledFunc = CB.getCalledFunction ();
2604
+ assert (CalledFunc && !CalledFunc->isDeclaration () &&
2605
+ " CanInlineCallSite should have verified direct call to definition" );
2606
+
2610
2607
// Determine if we are dealing with a call in an EHPad which does not unwind
2611
2608
// to caller.
2612
2609
bool EHPadForCallUnwindsLocally = false ;
2613
- if (CallSiteEHPad && isa<CallInst>(CB)) {
2610
+ if (IFI. CallSiteEHPad && isa<CallInst>(CB)) {
2614
2611
UnwindDestMemoTy FuncletUnwindMap;
2615
2612
Value *CallSiteUnwindDestToken =
2616
- getUnwindDestToken (CallSiteEHPad, FuncletUnwindMap);
2613
+ getUnwindDestToken (IFI. CallSiteEHPad , FuncletUnwindMap);
2617
2614
2618
2615
EHPadForCallUnwindsLocally =
2619
2616
CallSiteUnwindDestToken &&
@@ -2630,6 +2627,30 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
2630
2627
ClonedCodeInfo InlinedFunctionInfo;
2631
2628
Function::iterator FirstNewBlock;
2632
2629
2630
+ // GC poses two hazards to inlining, which only occur when the callee has GC:
2631
+ // 1. If the caller has no GC, then the callee's GC must be propagated to the
2632
+ // caller.
2633
+ // 2. If the caller has a differing GC, it is invalid to inline.
2634
+ if (CalledFunc->hasGC ()) {
2635
+ if (!Caller->hasGC ())
2636
+ Caller->setGC (CalledFunc->getGC ());
2637
+ else {
2638
+ assert (CalledFunc->getGC () == Caller->getGC () &&
2639
+ " CanInlineCallSite should have verified compatible GCs" );
2640
+ }
2641
+ }
2642
+
2643
+ if (CalledFunc->hasPersonalityFn ()) {
2644
+ Constant *CalledPersonality =
2645
+ CalledFunc->getPersonalityFn ()->stripPointerCasts ();
2646
+ if (!Caller->hasPersonalityFn ()) {
2647
+ Caller->setPersonalityFn (CalledPersonality);
2648
+ } else
2649
+ assert (Caller->getPersonalityFn ()->stripPointerCasts () ==
2650
+ CalledPersonality &&
2651
+ " CanInlineCallSite should have verified compatible personality" );
2652
+ }
2653
+
2633
2654
{ // Scope to destroy VMap after cloning.
2634
2655
ValueToValueMapTy VMap;
2635
2656
struct ByValInit {
@@ -2819,10 +2840,10 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
2819
2840
IFI.GetAssumptionCache (*Caller).registerAssumption (II);
2820
2841
}
2821
2842
2822
- if (ConvergenceControlToken) {
2843
+ if (IFI. ConvergenceControlToken ) {
2823
2844
IntrinsicInst *IntrinsicCall = getConvergenceEntry (*FirstNewBlock);
2824
2845
if (IntrinsicCall) {
2825
- IntrinsicCall->replaceAllUsesWith (ConvergenceControlToken);
2846
+ IntrinsicCall->replaceAllUsesWith (IFI. ConvergenceControlToken );
2826
2847
IntrinsicCall->eraseFromParent ();
2827
2848
}
2828
2849
}
@@ -2869,6 +2890,10 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
2869
2890
}
2870
2891
}
2871
2892
2893
+ // If the call to the callee cannot throw, set the 'nounwind' flag on any
2894
+ // calls that we inline.
2895
+ bool MarkNoUnwind = CB.doesNotThrow ();
2896
+
2872
2897
SmallVector<Value*,4 > VarArgsToForward;
2873
2898
SmallVector<AttributeSet, 4 > VarArgsAttrs;
2874
2899
for (unsigned i = CalledFunc->getFunctionType ()->getNumParams ();
@@ -3055,12 +3080,12 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
3055
3080
// Update the lexical scopes of the new funclets and callsites.
3056
3081
// Anything that had 'none' as its parent is now nested inside the callsite's
3057
3082
// EHPad.
3058
- if (CallSiteEHPad) {
3083
+ if (IFI. CallSiteEHPad ) {
3059
3084
for (Function::iterator BB = FirstNewBlock->getIterator (),
3060
3085
E = Caller->end ();
3061
3086
BB != E; ++BB) {
3062
3087
// Add bundle operands to inlined call sites.
3063
- PropagateOperandBundles (BB, CallSiteEHPad);
3088
+ PropagateOperandBundles (BB, IFI. CallSiteEHPad );
3064
3089
3065
3090
// It is problematic if the inlinee has a cleanupret which unwinds to
3066
3091
// caller and we inline it into a call site which doesn't unwind but into
@@ -3076,11 +3101,11 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
3076
3101
3077
3102
if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(I)) {
3078
3103
if (isa<ConstantTokenNone>(CatchSwitch->getParentPad ()))
3079
- CatchSwitch->setParentPad (CallSiteEHPad);
3104
+ CatchSwitch->setParentPad (IFI. CallSiteEHPad );
3080
3105
} else {
3081
3106
auto *FPI = cast<FuncletPadInst>(I);
3082
3107
if (isa<ConstantTokenNone>(FPI->getParentPad ()))
3083
- FPI->setParentPad (CallSiteEHPad);
3108
+ FPI->setParentPad (IFI. CallSiteEHPad );
3084
3109
}
3085
3110
}
3086
3111
}
@@ -3236,7 +3261,7 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
3236
3261
AttributeFuncs::mergeAttributesForInlining (*Caller, *CalledFunc);
3237
3262
3238
3263
// We are now done with the inlining.
3239
- return InlineResult::success () ;
3264
+ return ;
3240
3265
}
3241
3266
3242
3267
// Otherwise, we have the normal case, of more than one block to inline or
@@ -3404,6 +3429,19 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
3404
3429
3405
3430
if (MergeAttributes)
3406
3431
AttributeFuncs::mergeAttributesForInlining (*Caller, *CalledFunc);
3432
+ }
3407
3433
3408
- return InlineResult::success ();
3434
+ llvm::InlineResult llvm::InlineFunction (CallBase &CB, InlineFunctionInfo &IFI,
3435
+ bool MergeAttributes,
3436
+ AAResults *CalleeAAR,
3437
+ bool InsertLifetime,
3438
+ Function *ForwardVarArgsTo,
3439
+ OptimizationRemarkEmitter *ORE) {
3440
+ llvm::InlineResult Result = CanInlineCallSite (CB, IFI);
3441
+ if (Result.isSuccess ()) {
3442
+ InlineFunctionImpl (CB, IFI, MergeAttributes, CalleeAAR, InsertLifetime,
3443
+ ForwardVarArgsTo, ORE);
3444
+ }
3445
+
3446
+ return Result;
3409
3447
}
0 commit comments