@@ -422,14 +422,6 @@ 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-
433425 // Custom simplification helper routines.
434426 bool isAllocaDerivedArg (Value *V);
435427 void disableSROAForArg (AllocaInst *SROAArg);
@@ -1443,8 +1435,10 @@ bool CallAnalyzer::accumulateGEPOffset(GEPOperator &GEP, APInt &Offset) {
14431435
14441436 for (gep_type_iterator GTI = gep_type_begin (GEP), GTE = gep_type_end (GEP);
14451437 GTI != GTE; ++GTI) {
1446- ConstantInt *OpC =
1447- getDirectOrSimplifiedValue<ConstantInt>(GTI.getOperand ());
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);
14481442 if (!OpC)
14491443 return false ;
14501444 if (OpC->isZero ())
@@ -1558,7 +1552,9 @@ bool CallAnalyzer::visitPHI(PHINode &I) {
15581552 if (&I == V)
15591553 continue ;
15601554
1561- Constant *C = getDirectOrSimplifiedValue<Constant>(V);
1555+ Constant *C = dyn_cast<Constant>(V);
1556+ if (!C)
1557+ C = SimplifiedValues.lookup (V);
15621558
15631559 std::pair<Value *, APInt> BaseAndOffset = {nullptr , ZeroOffset};
15641560 if (!C && CheckSROA)
@@ -1643,7 +1639,7 @@ bool CallAnalyzer::visitGetElementPtr(GetElementPtrInst &I) {
16431639 // Lambda to check whether a GEP's indices are all constant.
16441640 auto IsGEPOffsetConstant = [&](GetElementPtrInst &GEP) {
16451641 for (const Use &Op : GEP.indices ())
1646- if (!getDirectOrSimplifiedValue <Constant>(Op))
1642+ if (!isa <Constant>(Op) && !SimplifiedValues. lookup (Op))
16471643 return false ;
16481644 return true ;
16491645 };
@@ -1670,7 +1666,9 @@ bool CallAnalyzer::visitGetElementPtr(GetElementPtrInst &I) {
16701666bool CallAnalyzer::simplifyInstruction (Instruction &I) {
16711667 SmallVector<Constant *> COps;
16721668 for (Value *Op : I.operands ()) {
1673- Constant *COp = getDirectOrSimplifiedValue<Constant>(Op);
1669+ Constant *COp = dyn_cast<Constant>(Op);
1670+ if (!COp)
1671+ COp = SimplifiedValues.lookup (Op);
16741672 if (!COp)
16751673 return false ;
16761674 COps.push_back (COp);
@@ -1693,7 +1691,10 @@ bool CallAnalyzer::simplifyInstruction(Instruction &I) {
16931691// / llvm.is.constant would evaluate.
16941692bool CallAnalyzer::simplifyIntrinsicCallIsConstant (CallBase &CB) {
16951693 Value *Arg = CB.getArgOperand (0 );
1696- auto *C = getDirectOrSimplifiedValue<Constant>(Arg);
1694+ auto *C = dyn_cast<Constant>(Arg);
1695+
1696+ if (!C)
1697+ C = dyn_cast_or_null<Constant>(SimplifiedValues.lookup (Arg));
16971698
16981699 Type *RT = CB.getFunctionType ()->getReturnType ();
16991700 SimplifiedValues[&CB] = ConstantInt::get (RT, C ? 1 : 0 );
@@ -2125,8 +2126,12 @@ bool CallAnalyzer::visitSub(BinaryOperator &I) {
21252126
21262127bool CallAnalyzer::visitBinaryOperator (BinaryOperator &I) {
21272128 Value *LHS = I.getOperand (0 ), *RHS = I.getOperand (1 );
2128- Constant *CLHS = getDirectOrSimplifiedValue<Constant>(LHS);
2129- Constant *CRHS = getDirectOrSimplifiedValue<Constant>(RHS);
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);
21302135
21312136 Value *SimpleV = nullptr ;
21322137 if (auto FI = dyn_cast<FPMathOperator>(&I))
@@ -2160,7 +2165,9 @@ bool CallAnalyzer::visitBinaryOperator(BinaryOperator &I) {
21602165
21612166bool CallAnalyzer::visitFNeg (UnaryOperator &I) {
21622167 Value *Op = I.getOperand (0 );
2163- Constant *COp = getDirectOrSimplifiedValue<Constant>(Op);
2168+ Constant *COp = dyn_cast<Constant>(Op);
2169+ if (!COp)
2170+ COp = SimplifiedValues.lookup (Op);
21642171
21652172 Value *SimpleV = simplifyFNegInst (
21662173 COp ? COp : Op, cast<FPMathOperator>(I).getFastMathFlags (), DL);
@@ -2248,7 +2255,9 @@ bool CallAnalyzer::simplifyCallSite(Function *F, CallBase &Call) {
22482255 SmallVector<Constant *, 4 > ConstantArgs;
22492256 ConstantArgs.reserve (Call.arg_size ());
22502257 for (Value *I : Call.args ()) {
2251- Constant *C = getDirectOrSimplifiedValue<Constant>(I);
2258+ Constant *C = dyn_cast<Constant>(I);
2259+ if (!C)
2260+ C = dyn_cast_or_null<Constant>(SimplifiedValues.lookup (I));
22522261 if (!C)
22532262 return false ; // This argument doesn't map to a constant.
22542263
@@ -2279,9 +2288,14 @@ bool CallAnalyzer::isLoweredToCall(Function *F, CallBase &Call) {
22792288 // platforms whose headers redirect memcpy to __memcpy_chk (e.g. Darwin), as
22802289 // other platforms use memcpy intrinsics, which are already exempt from the
22812290 // call penalty.
2282- auto *LenOp = getDirectOrSimplifiedValue<ConstantInt>(Call.getOperand (2 ));
2283- auto *ObjSizeOp =
2284- getDirectOrSimplifiedValue<ConstantInt>(Call.getOperand (3 ));
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 )));
22852299 if (LenOp && ObjSizeOp &&
22862300 LenOp->getLimitedValue () <= ObjSizeOp->getLimitedValue ()) {
22872301 return false ;
@@ -2397,18 +2411,23 @@ bool CallAnalyzer::visitBranchInst(BranchInst &BI) {
23972411 // shouldn't exist at all, but handling them makes the behavior of the
23982412 // inliner more regular and predictable. Interestingly, conditional branches
23992413 // which will fold away are also free.
2400- return BI.isUnconditional () ||
2401- getDirectOrSimplifiedValue<ConstantInt>(BI.getCondition ()) ||
2402- BI.getMetadata (LLVMContext::MD_make_implicit);
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 ()));
24032418}
24042419
24052420bool CallAnalyzer::visitSelectInst (SelectInst &SI) {
24062421 bool CheckSROA = SI.getType ()->isPointerTy ();
24072422 Value *TrueVal = SI.getTrueValue ();
24082423 Value *FalseVal = SI.getFalseValue ();
24092424
2410- Constant *TrueC = getDirectOrSimplifiedValue<Constant>(TrueVal);
2411- Constant *FalseC = getDirectOrSimplifiedValue<Constant>(FalseVal);
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);
24122431 Constant *CondC =
24132432 dyn_cast_or_null<Constant>(SimplifiedValues.lookup (SI.getCondition ()));
24142433
@@ -2478,8 +2497,11 @@ bool CallAnalyzer::visitSelectInst(SelectInst &SI) {
24782497bool CallAnalyzer::visitSwitchInst (SwitchInst &SI) {
24792498 // We model unconditional switches as free, see the comments on handling
24802499 // branches.
2481- if (getDirectOrSimplifiedValue <ConstantInt>(SI.getCondition ()))
2500+ if (isa <ConstantInt>(SI.getCondition ()))
24822501 return true ;
2502+ if (Value *V = SimplifiedValues.lookup (SI.getCondition ()))
2503+ if (isa<ConstantInt>(V))
2504+ return true ;
24832505
24842506 // Assume the most general case where the switch is lowered into
24852507 // either a jump table, bit test, or a balanced binary tree consisting of
0 commit comments