@@ -422,6 +422,14 @@ class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
422422 return It->second ;
423423 }
424424
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+
425433 // Custom simplification helper routines.
426434 bool isAllocaDerivedArg (Value *V);
427435 void disableSROAForArg (AllocaInst *SROAArg);
@@ -1435,10 +1443,8 @@ bool CallAnalyzer::accumulateGEPOffset(GEPOperator &GEP, APInt &Offset) {
14351443
14361444 for (gep_type_iterator GTI = gep_type_begin (GEP), GTE = gep_type_end (GEP);
14371445 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 ());
14421448 if (!OpC)
14431449 return false ;
14441450 if (OpC->isZero ())
@@ -1552,9 +1558,7 @@ bool CallAnalyzer::visitPHI(PHINode &I) {
15521558 if (&I == V)
15531559 continue ;
15541560
1555- Constant *C = dyn_cast<Constant>(V);
1556- if (!C)
1557- C = SimplifiedValues.lookup (V);
1561+ Constant *C = getDirectOrSimplifiedValue<Constant>(V);
15581562
15591563 std::pair<Value *, APInt> BaseAndOffset = {nullptr , ZeroOffset};
15601564 if (!C && CheckSROA)
@@ -1639,7 +1643,7 @@ bool CallAnalyzer::visitGetElementPtr(GetElementPtrInst &I) {
16391643 // Lambda to check whether a GEP's indices are all constant.
16401644 auto IsGEPOffsetConstant = [&](GetElementPtrInst &GEP) {
16411645 for (const Use &Op : GEP.indices ())
1642- if (!isa <Constant>(Op) && !SimplifiedValues. lookup (Op))
1646+ if (!getDirectOrSimplifiedValue <Constant>(Op))
16431647 return false ;
16441648 return true ;
16451649 };
@@ -1666,9 +1670,7 @@ bool CallAnalyzer::visitGetElementPtr(GetElementPtrInst &I) {
16661670bool CallAnalyzer::simplifyInstruction (Instruction &I) {
16671671 SmallVector<Constant *> COps;
16681672 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);
16721674 if (!COp)
16731675 return false ;
16741676 COps.push_back (COp);
@@ -1691,10 +1693,7 @@ bool CallAnalyzer::simplifyInstruction(Instruction &I) {
16911693// / llvm.is.constant would evaluate.
16921694bool CallAnalyzer::simplifyIntrinsicCallIsConstant (CallBase &CB) {
16931695 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);
16981697
16991698 Type *RT = CB.getFunctionType ()->getReturnType ();
17001699 SimplifiedValues[&CB] = ConstantInt::get (RT, C ? 1 : 0 );
@@ -2126,12 +2125,8 @@ bool CallAnalyzer::visitSub(BinaryOperator &I) {
21262125
21272126bool CallAnalyzer::visitBinaryOperator (BinaryOperator &I) {
21282127 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);
21352130
21362131 Value *SimpleV = nullptr ;
21372132 if (auto FI = dyn_cast<FPMathOperator>(&I))
@@ -2165,9 +2160,7 @@ bool CallAnalyzer::visitBinaryOperator(BinaryOperator &I) {
21652160
21662161bool CallAnalyzer::visitFNeg (UnaryOperator &I) {
21672162 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);
21712164
21722165 Value *SimpleV = simplifyFNegInst (
21732166 COp ? COp : Op, cast<FPMathOperator>(I).getFastMathFlags (), DL);
@@ -2255,9 +2248,7 @@ bool CallAnalyzer::simplifyCallSite(Function *F, CallBase &Call) {
22552248 SmallVector<Constant *, 4 > ConstantArgs;
22562249 ConstantArgs.reserve (Call.arg_size ());
22572250 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);
22612252 if (!C)
22622253 return false ; // This argument doesn't map to a constant.
22632254
@@ -2288,14 +2279,9 @@ bool CallAnalyzer::isLoweredToCall(Function *F, CallBase &Call) {
22882279 // platforms whose headers redirect memcpy to __memcpy_chk (e.g. Darwin), as
22892280 // other platforms use memcpy intrinsics, which are already exempt from the
22902281 // 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 ));
22992285 if (LenOp && ObjSizeOp &&
23002286 LenOp->getLimitedValue () <= ObjSizeOp->getLimitedValue ()) {
23012287 return false ;
@@ -2411,23 +2397,18 @@ bool CallAnalyzer::visitBranchInst(BranchInst &BI) {
24112397 // shouldn't exist at all, but handling them makes the behavior of the
24122398 // inliner more regular and predictable. Interestingly, conditional branches
24132399 // 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);
24182403}
24192404
24202405bool CallAnalyzer::visitSelectInst (SelectInst &SI) {
24212406 bool CheckSROA = SI.getType ()->isPointerTy ();
24222407 Value *TrueVal = SI.getTrueValue ();
24232408 Value *FalseVal = SI.getFalseValue ();
24242409
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);
24312412 Constant *CondC =
24322413 dyn_cast_or_null<Constant>(SimplifiedValues.lookup (SI.getCondition ()));
24332414
@@ -2497,11 +2478,8 @@ bool CallAnalyzer::visitSelectInst(SelectInst &SI) {
24972478bool CallAnalyzer::visitSwitchInst (SwitchInst &SI) {
24982479 // We model unconditional switches as free, see the comments on handling
24992480 // branches.
2500- if (isa <ConstantInt>(SI.getCondition ()))
2481+ if (getDirectOrSimplifiedValue <ConstantInt>(SI.getCondition ()))
25012482 return true ;
2502- if (Value *V = SimplifiedValues.lookup (SI.getCondition ()))
2503- if (isa<ConstantInt>(V))
2504- return true ;
25052483
25062484 // Assume the most general case where the switch is lowered into
25072485 // either a jump table, bit test, or a balanced binary tree consisting of
0 commit comments