@@ -422,6 +422,14 @@ class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
422
422
return It->second ;
423
423
}
424
424
425
+ // / Use a value in its given form directly if possible, otherwise try looking
426
+ // / for it in SimplifiedValues.
427
+ template <typename T> T *getDirectOrSimplifiedValue (Value *V) const {
428
+ if (auto *Direct = dyn_cast<T>(V))
429
+ return Direct;
430
+ return dyn_cast_if_present<T>(SimplifiedValues.lookup (V));
431
+ }
432
+
425
433
// Custom simplification helper routines.
426
434
bool isAllocaDerivedArg (Value *V);
427
435
void disableSROAForArg (AllocaInst *SROAArg);
@@ -1435,10 +1443,8 @@ bool CallAnalyzer::accumulateGEPOffset(GEPOperator &GEP, APInt &Offset) {
1435
1443
1436
1444
for (gep_type_iterator GTI = gep_type_begin (GEP), GTE = gep_type_end (GEP);
1437
1445
GTI != GTE; ++GTI) {
1438
- ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand ());
1439
- if (!OpC)
1440
- if (Constant *SimpleOp = SimplifiedValues.lookup (GTI.getOperand ()))
1441
- OpC = dyn_cast<ConstantInt>(SimpleOp);
1446
+ ConstantInt *OpC =
1447
+ getDirectOrSimplifiedValue<ConstantInt>(GTI.getOperand ());
1442
1448
if (!OpC)
1443
1449
return false ;
1444
1450
if (OpC->isZero ())
@@ -1552,9 +1558,7 @@ bool CallAnalyzer::visitPHI(PHINode &I) {
1552
1558
if (&I == V)
1553
1559
continue ;
1554
1560
1555
- Constant *C = dyn_cast<Constant>(V);
1556
- if (!C)
1557
- C = SimplifiedValues.lookup (V);
1561
+ Constant *C = getDirectOrSimplifiedValue<Constant>(V);
1558
1562
1559
1563
std::pair<Value *, APInt> BaseAndOffset = {nullptr , ZeroOffset};
1560
1564
if (!C && CheckSROA)
@@ -1639,7 +1643,7 @@ bool CallAnalyzer::visitGetElementPtr(GetElementPtrInst &I) {
1639
1643
// Lambda to check whether a GEP's indices are all constant.
1640
1644
auto IsGEPOffsetConstant = [&](GetElementPtrInst &GEP) {
1641
1645
for (const Use &Op : GEP.indices ())
1642
- if (!isa <Constant>(Op) && !SimplifiedValues. lookup (Op))
1646
+ if (!getDirectOrSimplifiedValue <Constant>(Op))
1643
1647
return false ;
1644
1648
return true ;
1645
1649
};
@@ -1666,9 +1670,7 @@ bool CallAnalyzer::visitGetElementPtr(GetElementPtrInst &I) {
1666
1670
bool CallAnalyzer::simplifyInstruction (Instruction &I) {
1667
1671
SmallVector<Constant *> COps;
1668
1672
for (Value *Op : I.operands ()) {
1669
- Constant *COp = dyn_cast<Constant>(Op);
1670
- if (!COp)
1671
- COp = SimplifiedValues.lookup (Op);
1673
+ Constant *COp = getDirectOrSimplifiedValue<Constant>(Op);
1672
1674
if (!COp)
1673
1675
return false ;
1674
1676
COps.push_back (COp);
@@ -1691,10 +1693,7 @@ bool CallAnalyzer::simplifyInstruction(Instruction &I) {
1691
1693
// / llvm.is.constant would evaluate.
1692
1694
bool CallAnalyzer::simplifyIntrinsicCallIsConstant (CallBase &CB) {
1693
1695
Value *Arg = CB.getArgOperand (0 );
1694
- auto *C = dyn_cast<Constant>(Arg);
1695
-
1696
- if (!C)
1697
- C = dyn_cast_or_null<Constant>(SimplifiedValues.lookup (Arg));
1696
+ auto *C = getDirectOrSimplifiedValue<Constant>(Arg);
1698
1697
1699
1698
Type *RT = CB.getFunctionType ()->getReturnType ();
1700
1699
SimplifiedValues[&CB] = ConstantInt::get (RT, C ? 1 : 0 );
@@ -2126,12 +2125,8 @@ bool CallAnalyzer::visitSub(BinaryOperator &I) {
2126
2125
2127
2126
bool CallAnalyzer::visitBinaryOperator (BinaryOperator &I) {
2128
2127
Value *LHS = I.getOperand (0 ), *RHS = I.getOperand (1 );
2129
- Constant *CLHS = dyn_cast<Constant>(LHS);
2130
- if (!CLHS)
2131
- CLHS = SimplifiedValues.lookup (LHS);
2132
- Constant *CRHS = dyn_cast<Constant>(RHS);
2133
- if (!CRHS)
2134
- CRHS = SimplifiedValues.lookup (RHS);
2128
+ Constant *CLHS = getDirectOrSimplifiedValue<Constant>(LHS);
2129
+ Constant *CRHS = getDirectOrSimplifiedValue<Constant>(RHS);
2135
2130
2136
2131
Value *SimpleV = nullptr ;
2137
2132
if (auto FI = dyn_cast<FPMathOperator>(&I))
@@ -2165,9 +2160,7 @@ bool CallAnalyzer::visitBinaryOperator(BinaryOperator &I) {
2165
2160
2166
2161
bool CallAnalyzer::visitFNeg (UnaryOperator &I) {
2167
2162
Value *Op = I.getOperand (0 );
2168
- Constant *COp = dyn_cast<Constant>(Op);
2169
- if (!COp)
2170
- COp = SimplifiedValues.lookup (Op);
2163
+ Constant *COp = getDirectOrSimplifiedValue<Constant>(Op);
2171
2164
2172
2165
Value *SimpleV = simplifyFNegInst (
2173
2166
COp ? COp : Op, cast<FPMathOperator>(I).getFastMathFlags (), DL);
@@ -2255,9 +2248,7 @@ bool CallAnalyzer::simplifyCallSite(Function *F, CallBase &Call) {
2255
2248
SmallVector<Constant *, 4 > ConstantArgs;
2256
2249
ConstantArgs.reserve (Call.arg_size ());
2257
2250
for (Value *I : Call.args ()) {
2258
- Constant *C = dyn_cast<Constant>(I);
2259
- if (!C)
2260
- C = dyn_cast_or_null<Constant>(SimplifiedValues.lookup (I));
2251
+ Constant *C = getDirectOrSimplifiedValue<Constant>(I);
2261
2252
if (!C)
2262
2253
return false ; // This argument doesn't map to a constant.
2263
2254
@@ -2288,14 +2279,9 @@ bool CallAnalyzer::isLoweredToCall(Function *F, CallBase &Call) {
2288
2279
// platforms whose headers redirect memcpy to __memcpy_chk (e.g. Darwin), as
2289
2280
// other platforms use memcpy intrinsics, which are already exempt from the
2290
2281
// call penalty.
2291
- auto *LenOp = dyn_cast<ConstantInt>(Call.getOperand (2 ));
2292
- if (!LenOp)
2293
- LenOp = dyn_cast_or_null<ConstantInt>(
2294
- SimplifiedValues.lookup (Call.getOperand (2 )));
2295
- auto *ObjSizeOp = dyn_cast<ConstantInt>(Call.getOperand (3 ));
2296
- if (!ObjSizeOp)
2297
- ObjSizeOp = dyn_cast_or_null<ConstantInt>(
2298
- SimplifiedValues.lookup (Call.getOperand (3 )));
2282
+ auto *LenOp = getDirectOrSimplifiedValue<ConstantInt>(Call.getOperand (2 ));
2283
+ auto *ObjSizeOp =
2284
+ getDirectOrSimplifiedValue<ConstantInt>(Call.getOperand (3 ));
2299
2285
if (LenOp && ObjSizeOp &&
2300
2286
LenOp->getLimitedValue () <= ObjSizeOp->getLimitedValue ()) {
2301
2287
return false ;
@@ -2411,23 +2397,18 @@ bool CallAnalyzer::visitBranchInst(BranchInst &BI) {
2411
2397
// shouldn't exist at all, but handling them makes the behavior of the
2412
2398
// inliner more regular and predictable. Interestingly, conditional branches
2413
2399
// which will fold away are also free.
2414
- return BI.isUnconditional () || isa<ConstantInt>(BI.getCondition ()) ||
2415
- BI.getMetadata (LLVMContext::MD_make_implicit) ||
2416
- isa_and_nonnull<ConstantInt>(
2417
- SimplifiedValues.lookup (BI.getCondition ()));
2400
+ return BI.isUnconditional () ||
2401
+ getDirectOrSimplifiedValue<ConstantInt>(BI.getCondition ()) ||
2402
+ BI.getMetadata (LLVMContext::MD_make_implicit);
2418
2403
}
2419
2404
2420
2405
bool CallAnalyzer::visitSelectInst (SelectInst &SI) {
2421
2406
bool CheckSROA = SI.getType ()->isPointerTy ();
2422
2407
Value *TrueVal = SI.getTrueValue ();
2423
2408
Value *FalseVal = SI.getFalseValue ();
2424
2409
2425
- Constant *TrueC = dyn_cast<Constant>(TrueVal);
2426
- if (!TrueC)
2427
- TrueC = SimplifiedValues.lookup (TrueVal);
2428
- Constant *FalseC = dyn_cast<Constant>(FalseVal);
2429
- if (!FalseC)
2430
- FalseC = SimplifiedValues.lookup (FalseVal);
2410
+ Constant *TrueC = getDirectOrSimplifiedValue<Constant>(TrueVal);
2411
+ Constant *FalseC = getDirectOrSimplifiedValue<Constant>(FalseVal);
2431
2412
Constant *CondC =
2432
2413
dyn_cast_or_null<Constant>(SimplifiedValues.lookup (SI.getCondition ()));
2433
2414
@@ -2497,11 +2478,8 @@ bool CallAnalyzer::visitSelectInst(SelectInst &SI) {
2497
2478
bool CallAnalyzer::visitSwitchInst (SwitchInst &SI) {
2498
2479
// We model unconditional switches as free, see the comments on handling
2499
2480
// branches.
2500
- if (isa <ConstantInt>(SI.getCondition ()))
2481
+ if (getDirectOrSimplifiedValue <ConstantInt>(SI.getCondition ()))
2501
2482
return true ;
2502
- if (Value *V = SimplifiedValues.lookup (SI.getCondition ()))
2503
- if (isa<ConstantInt>(V))
2504
- return true ;
2505
2483
2506
2484
// Assume the most general case where the switch is lowered into
2507
2485
// either a jump table, bit test, or a balanced binary tree consisting of
0 commit comments