@@ -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
@@ -2287,9 +2316,18 @@ bool CallAnalyzer::visitStore(StoreInst &I) {
2287
2316
}
2288
2317
2289
2318
bool CallAnalyzer::visitExtractValue (ExtractValueInst &I) {
2290
- // Constant folding for extract value is trivial.
2291
- if (simplifyInstruction (I))
2292
- return true ;
2319
+ Value *Op = I.getAggregateOperand ();
2320
+
2321
+ // Special handling, because we want to simplify extractvalue with a
2322
+ // potential insertvalue from the caller.
2323
+ if (Value *SimpleOp = getSimplifiedValueUnchecked (Op)) {
2324
+ SimplifyQuery SQ (DL);
2325
+ Value *SimpleV = simplifyExtractValueInst (SimpleOp, I.getIndices (), SQ);
2326
+ if (SimpleV) {
2327
+ SimplifiedValues[&I] = SimpleV;
2328
+ return true ;
2329
+ }
2330
+ }
2293
2331
2294
2332
// SROA can't look through these, but they may be free.
2295
2333
return Base::visitExtractValue (I);
@@ -2388,7 +2426,7 @@ bool CallAnalyzer::visitCallBase(CallBase &Call) {
2388
2426
// Check if this happens to be an indirect function call to a known function
2389
2427
// in this inline context. If not, we've done all we can.
2390
2428
Value *Callee = Call.getCalledOperand ();
2391
- F = dyn_cast_or_null <Function>(SimplifiedValues. lookup ( Callee) );
2429
+ F = getSimplifiedValue <Function>(Callee);
2392
2430
if (!F || F->getFunctionType () != Call.getFunctionType ()) {
2393
2431
onCallArgumentSetup (Call);
2394
2432
@@ -2483,8 +2521,7 @@ bool CallAnalyzer::visitSelectInst(SelectInst &SI) {
2483
2521
2484
2522
Constant *TrueC = getDirectOrSimplifiedValue<Constant>(TrueVal);
2485
2523
Constant *FalseC = getDirectOrSimplifiedValue<Constant>(FalseVal);
2486
- Constant *CondC =
2487
- dyn_cast_or_null<Constant>(SimplifiedValues.lookup (SI.getCondition ()));
2524
+ Constant *CondC = getSimplifiedValue<Constant>(SI.getCondition ());
2488
2525
2489
2526
if (!CondC) {
2490
2527
// Select C, X, X => X
@@ -2833,8 +2870,9 @@ InlineResult CallAnalyzer::analyze() {
2833
2870
auto CAI = CandidateCall.arg_begin ();
2834
2871
for (Argument &FAI : F.args ()) {
2835
2872
assert (CAI != CandidateCall.arg_end ());
2836
- if (Constant *C = dyn_cast<Constant>(CAI))
2837
- SimplifiedValues[&FAI] = C;
2873
+ SimplifiedValues[&FAI] = *CAI;
2874
+ if (isa<Constant>(*CAI))
2875
+ ++NumConstantArgs;
2838
2876
2839
2877
Value *PtrArg = *CAI;
2840
2878
if (ConstantInt *C = stripAndComputeInBoundsConstantOffsets (PtrArg)) {
@@ -2849,7 +2887,6 @@ InlineResult CallAnalyzer::analyze() {
2849
2887
}
2850
2888
++CAI;
2851
2889
}
2852
- NumConstantArgs = SimplifiedValues.size ();
2853
2890
NumConstantOffsetPtrArgs = ConstantOffsetPtrs.size ();
2854
2891
NumAllocaArgs = SROAArgValues.size ();
2855
2892
@@ -2911,8 +2948,7 @@ InlineResult CallAnalyzer::analyze() {
2911
2948
if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
2912
2949
if (BI->isConditional ()) {
2913
2950
Value *Cond = BI->getCondition ();
2914
- if (ConstantInt *SimpleCond =
2915
- dyn_cast_or_null<ConstantInt>(SimplifiedValues.lookup (Cond))) {
2951
+ if (ConstantInt *SimpleCond = getSimplifiedValue<ConstantInt>(Cond)) {
2916
2952
BasicBlock *NextBB = BI->getSuccessor (SimpleCond->isZero () ? 1 : 0 );
2917
2953
BBWorklist.insert (NextBB);
2918
2954
KnownSuccessors[BB] = NextBB;
@@ -2922,8 +2958,7 @@ InlineResult CallAnalyzer::analyze() {
2922
2958
}
2923
2959
} else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
2924
2960
Value *Cond = SI->getCondition ();
2925
- if (ConstantInt *SimpleCond =
2926
- dyn_cast_or_null<ConstantInt>(SimplifiedValues.lookup (Cond))) {
2961
+ if (ConstantInt *SimpleCond = getSimplifiedValue<ConstantInt>(Cond)) {
2927
2962
BasicBlock *NextBB = SI->findCaseValue (SimpleCond)->getCaseSuccessor ();
2928
2963
BBWorklist.insert (NextBB);
2929
2964
KnownSuccessors[BB] = NextBB;
0 commit comments