@@ -391,7 +391,8 @@ class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
391
391
// / likely simplifications post-inlining. The most important aspect we track
392
392
// / is CFG altering simplifications -- when we prove a basic block dead, that
393
393
// / can cause dramatic shifts in the cost of inlining a function.
394
- DenseMap<Value *, Constant *> SimplifiedValues;
394
+ // / Note: The simplified Value may be owned by the caller function.
395
+ DenseMap<Value *, Value *> SimplifiedValues;
395
396
396
397
// / Keep track of the values which map back (through function arguments) to
397
398
// / allocas on the caller stack which could be simplified through SROA.
@@ -432,7 +433,7 @@ class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
432
433
template <typename T> T *getDirectOrSimplifiedValue (Value *V) const {
433
434
if (auto *Direct = dyn_cast<T>(V))
434
435
return Direct;
435
- return dyn_cast_if_present <T>(SimplifiedValues. lookup (V) );
436
+ return getSimplifiedValue <T>(V );
436
437
}
437
438
438
439
// Custom simplification helper routines.
@@ -525,11 +526,33 @@ class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
525
526
526
527
InlineResult analyze ();
527
528
528
- std::optional<Constant *> getSimplifiedValue (Instruction *I) {
529
- auto It = SimplifiedValues.find (I);
530
- if (It != SimplifiedValues.end ())
531
- return It->second ;
532
- return std::nullopt;
529
+ // / Lookup simplified Value. May return a value owned by the caller.
530
+ Value *getSimplifiedValueUnchecked (Value *V) const {
531
+ return SimplifiedValues.lookup (V);
532
+ }
533
+
534
+ // / Lookup simplified Value, but return nullptr if the simplified value is
535
+ // / owned by the caller.
536
+ template <typename T> T *getSimplifiedValue (Value *V) const {
537
+ Value *SimpleV = SimplifiedValues.lookup (V);
538
+ if (!SimpleV)
539
+ return nullptr ;
540
+
541
+ // Skip checks if we know T is a global. This has a small, but measurable
542
+ // impact on compile-time.
543
+ if constexpr (std::is_base_of_v<Constant, T>)
544
+ return dyn_cast<T>(SimpleV);
545
+
546
+ // Make sure the simplified Value is owned by this function
547
+ if (auto *I = dyn_cast<Instruction>(SimpleV)) {
548
+ if (I->getFunction () != &F)
549
+ return nullptr ;
550
+ } else if (auto *Arg = dyn_cast<Argument>(SimpleV)) {
551
+ if (Arg->getParent () != &F)
552
+ return nullptr ;
553
+ } else if (!isa<Constant>(SimpleV))
554
+ return nullptr ;
555
+ return dyn_cast<T>(SimpleV);
533
556
}
534
557
535
558
// Keep a bunch of stats about the cost savings found so we can print them
@@ -921,12 +944,11 @@ class InlineCostCallAnalyzer final : public CallAnalyzer {
921
944
if (BranchInst *BI = dyn_cast<BranchInst>(&I)) {
922
945
// Count a conditional branch as savings if it becomes unconditional.
923
946
if (BI->isConditional () &&
924
- isa_and_nonnull<ConstantInt>(
925
- SimplifiedValues.lookup (BI->getCondition ()))) {
947
+ getSimplifiedValue<ConstantInt>(BI->getCondition ())) {
926
948
CurrentSavings += InstrCost;
927
949
}
928
950
} else if (SwitchInst *SI = dyn_cast<SwitchInst>(&I)) {
929
- if (isa_and_present <ConstantInt>(SimplifiedValues. lookup ( SI->getCondition () )))
951
+ if (getSimplifiedValue <ConstantInt>(SI->getCondition ()))
930
952
CurrentSavings += InstrCost;
931
953
} else if (Value *V = dyn_cast<Value>(&I)) {
932
954
// Count an instruction as savings if we can fold it.
@@ -1423,10 +1445,17 @@ void InlineCostAnnotationWriter::emitInstructionAnnot(
1423
1445
if (Record->hasThresholdChanged ())
1424
1446
OS << " , threshold delta = " << Record->getThresholdDelta ();
1425
1447
}
1426
- auto C = ICCA->getSimplifiedValue (const_cast <Instruction *>(I));
1427
- if (C ) {
1448
+ auto *V = ICCA->getSimplifiedValueUnchecked (const_cast <Instruction *>(I));
1449
+ if (V ) {
1428
1450
OS << " , simplified to " ;
1429
- (*C)->print (OS, true );
1451
+ V->print (OS, true );
1452
+ if (auto *VI = dyn_cast<Instruction>(V)) {
1453
+ if (VI->getFunction () != I->getFunction ())
1454
+ OS << " (caller instruction)" ;
1455
+ } else if (auto *VArg = dyn_cast<Argument>(V)) {
1456
+ if (VArg->getParent () != I->getFunction ())
1457
+ OS << " (caller argument)" ;
1458
+ }
1430
1459
}
1431
1460
OS << " \n " ;
1432
1461
}
@@ -1483,7 +1512,7 @@ bool CallAnalyzer::isGEPFree(GetElementPtrInst &GEP) {
1483
1512
SmallVector<Value *, 4 > Operands;
1484
1513
Operands.push_back (GEP.getOperand (0 ));
1485
1514
for (const Use &Op : GEP.indices ())
1486
- if (Constant *SimpleOp = SimplifiedValues. lookup (Op))
1515
+ if (Constant *SimpleOp = getSimplifiedValue<Constant> (Op))
1487
1516
Operands.push_back (SimpleOp);
1488
1517
else
1489
1518
Operands.push_back (Op);
@@ -1498,7 +1527,7 @@ bool CallAnalyzer::visitAlloca(AllocaInst &I) {
1498
1527
// Check whether inlining will turn a dynamic alloca into a static
1499
1528
// alloca and handle that case.
1500
1529
if (I.isArrayAllocation ()) {
1501
- Constant *Size = SimplifiedValues. lookup (I.getArraySize ());
1530
+ Constant *Size = getSimplifiedValue<Constant> (I.getArraySize ());
1502
1531
if (auto *AllocSize = dyn_cast_or_null<ConstantInt>(Size)) {
1503
1532
// Sometimes a dynamic alloca could be converted into a static alloca
1504
1533
// after this constant prop, and become a huge static alloca on an
@@ -2388,7 +2417,7 @@ bool CallAnalyzer::visitCallBase(CallBase &Call) {
2388
2417
// Check if this happens to be an indirect function call to a known function
2389
2418
// in this inline context. If not, we've done all we can.
2390
2419
Value *Callee = Call.getCalledOperand ();
2391
- F = dyn_cast_or_null <Function>(SimplifiedValues. lookup ( Callee) );
2420
+ F = getSimplifiedValue <Function>(Callee);
2392
2421
if (!F || F->getFunctionType () != Call.getFunctionType ()) {
2393
2422
onCallArgumentSetup (Call);
2394
2423
@@ -2483,8 +2512,7 @@ bool CallAnalyzer::visitSelectInst(SelectInst &SI) {
2483
2512
2484
2513
Constant *TrueC = getDirectOrSimplifiedValue<Constant>(TrueVal);
2485
2514
Constant *FalseC = getDirectOrSimplifiedValue<Constant>(FalseVal);
2486
- Constant *CondC =
2487
- dyn_cast_or_null<Constant>(SimplifiedValues.lookup (SI.getCondition ()));
2515
+ Constant *CondC = getSimplifiedValue<Constant>(SI.getCondition ());
2488
2516
2489
2517
if (!CondC) {
2490
2518
// Select C, X, X => X
@@ -2833,8 +2861,9 @@ InlineResult CallAnalyzer::analyze() {
2833
2861
auto CAI = CandidateCall.arg_begin ();
2834
2862
for (Argument &FAI : F.args ()) {
2835
2863
assert (CAI != CandidateCall.arg_end ());
2836
- if (Constant *C = dyn_cast<Constant>(CAI))
2837
- SimplifiedValues[&FAI] = C;
2864
+ SimplifiedValues[&FAI] = *CAI;
2865
+ if (isa<Constant>(*CAI))
2866
+ ++NumConstantArgs;
2838
2867
2839
2868
Value *PtrArg = *CAI;
2840
2869
if (ConstantInt *C = stripAndComputeInBoundsConstantOffsets (PtrArg)) {
@@ -2849,7 +2878,6 @@ InlineResult CallAnalyzer::analyze() {
2849
2878
}
2850
2879
++CAI;
2851
2880
}
2852
- NumConstantArgs = SimplifiedValues.size ();
2853
2881
NumConstantOffsetPtrArgs = ConstantOffsetPtrs.size ();
2854
2882
NumAllocaArgs = SROAArgValues.size ();
2855
2883
@@ -2911,8 +2939,7 @@ InlineResult CallAnalyzer::analyze() {
2911
2939
if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
2912
2940
if (BI->isConditional ()) {
2913
2941
Value *Cond = BI->getCondition ();
2914
- if (ConstantInt *SimpleCond =
2915
- dyn_cast_or_null<ConstantInt>(SimplifiedValues.lookup (Cond))) {
2942
+ if (ConstantInt *SimpleCond = getSimplifiedValue<ConstantInt>(Cond)) {
2916
2943
BasicBlock *NextBB = BI->getSuccessor (SimpleCond->isZero () ? 1 : 0 );
2917
2944
BBWorklist.insert (NextBB);
2918
2945
KnownSuccessors[BB] = NextBB;
@@ -2922,8 +2949,7 @@ InlineResult CallAnalyzer::analyze() {
2922
2949
}
2923
2950
} else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
2924
2951
Value *Cond = SI->getCondition ();
2925
- if (ConstantInt *SimpleCond =
2926
- dyn_cast_or_null<ConstantInt>(SimplifiedValues.lookup (Cond))) {
2952
+ if (ConstantInt *SimpleCond = getSimplifiedValue<ConstantInt>(Cond)) {
2927
2953
BasicBlock *NextBB = SI->findCaseValue (SimpleCond)->getCaseSuccessor ();
2928
2954
BBWorklist.insert (NextBB);
2929
2955
KnownSuccessors[BB] = NextBB;
0 commit comments