Skip to content

[Inliner] Add a helper around SimplifiedValues.lookup. NFCI #118646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 27 additions & 49 deletions llvm/lib/Analysis/InlineCost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,14 @@ class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
return It->second;
}

/// Use a value in its given form directly if possible, otherwise try looking
/// for it in SimplifiedValues.
template <typename T> T *getDirectOrSimplifiedValue(Value *V) const {
if (auto *Direct = dyn_cast<T>(V))
return Direct;
return dyn_cast_if_present<T>(SimplifiedValues.lookup(V));
}

// Custom simplification helper routines.
bool isAllocaDerivedArg(Value *V);
void disableSROAForArg(AllocaInst *SROAArg);
Expand Down Expand Up @@ -1435,10 +1443,8 @@ bool CallAnalyzer::accumulateGEPOffset(GEPOperator &GEP, APInt &Offset) {

for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP);
GTI != GTE; ++GTI) {
ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
if (!OpC)
if (Constant *SimpleOp = SimplifiedValues.lookup(GTI.getOperand()))
OpC = dyn_cast<ConstantInt>(SimpleOp);
ConstantInt *OpC =
getDirectOrSimplifiedValue<ConstantInt>(GTI.getOperand());
if (!OpC)
return false;
if (OpC->isZero())
Expand Down Expand Up @@ -1552,9 +1558,7 @@ bool CallAnalyzer::visitPHI(PHINode &I) {
if (&I == V)
continue;

Constant *C = dyn_cast<Constant>(V);
if (!C)
C = SimplifiedValues.lookup(V);
Constant *C = getDirectOrSimplifiedValue<Constant>(V);

std::pair<Value *, APInt> BaseAndOffset = {nullptr, ZeroOffset};
if (!C && CheckSROA)
Expand Down Expand Up @@ -1639,7 +1643,7 @@ bool CallAnalyzer::visitGetElementPtr(GetElementPtrInst &I) {
// Lambda to check whether a GEP's indices are all constant.
auto IsGEPOffsetConstant = [&](GetElementPtrInst &GEP) {
for (const Use &Op : GEP.indices())
if (!isa<Constant>(Op) && !SimplifiedValues.lookup(Op))
if (!getDirectOrSimplifiedValue<Constant>(Op))
return false;
return true;
};
Expand All @@ -1666,9 +1670,7 @@ bool CallAnalyzer::visitGetElementPtr(GetElementPtrInst &I) {
bool CallAnalyzer::simplifyInstruction(Instruction &I) {
SmallVector<Constant *> COps;
for (Value *Op : I.operands()) {
Constant *COp = dyn_cast<Constant>(Op);
if (!COp)
COp = SimplifiedValues.lookup(Op);
Constant *COp = getDirectOrSimplifiedValue<Constant>(Op);
if (!COp)
return false;
COps.push_back(COp);
Expand All @@ -1691,10 +1693,7 @@ bool CallAnalyzer::simplifyInstruction(Instruction &I) {
/// llvm.is.constant would evaluate.
bool CallAnalyzer::simplifyIntrinsicCallIsConstant(CallBase &CB) {
Value *Arg = CB.getArgOperand(0);
auto *C = dyn_cast<Constant>(Arg);

if (!C)
C = dyn_cast_or_null<Constant>(SimplifiedValues.lookup(Arg));
auto *C = getDirectOrSimplifiedValue<Constant>(Arg);

Type *RT = CB.getFunctionType()->getReturnType();
SimplifiedValues[&CB] = ConstantInt::get(RT, C ? 1 : 0);
Expand Down Expand Up @@ -2126,12 +2125,8 @@ bool CallAnalyzer::visitSub(BinaryOperator &I) {

bool CallAnalyzer::visitBinaryOperator(BinaryOperator &I) {
Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
Constant *CLHS = dyn_cast<Constant>(LHS);
if (!CLHS)
CLHS = SimplifiedValues.lookup(LHS);
Constant *CRHS = dyn_cast<Constant>(RHS);
if (!CRHS)
CRHS = SimplifiedValues.lookup(RHS);
Constant *CLHS = getDirectOrSimplifiedValue<Constant>(LHS);
Constant *CRHS = getDirectOrSimplifiedValue<Constant>(RHS);

Value *SimpleV = nullptr;
if (auto FI = dyn_cast<FPMathOperator>(&I))
Expand Down Expand Up @@ -2165,9 +2160,7 @@ bool CallAnalyzer::visitBinaryOperator(BinaryOperator &I) {

bool CallAnalyzer::visitFNeg(UnaryOperator &I) {
Value *Op = I.getOperand(0);
Constant *COp = dyn_cast<Constant>(Op);
if (!COp)
COp = SimplifiedValues.lookup(Op);
Constant *COp = getDirectOrSimplifiedValue<Constant>(Op);

Value *SimpleV = simplifyFNegInst(
COp ? COp : Op, cast<FPMathOperator>(I).getFastMathFlags(), DL);
Expand Down Expand Up @@ -2255,9 +2248,7 @@ bool CallAnalyzer::simplifyCallSite(Function *F, CallBase &Call) {
SmallVector<Constant *, 4> ConstantArgs;
ConstantArgs.reserve(Call.arg_size());
for (Value *I : Call.args()) {
Constant *C = dyn_cast<Constant>(I);
if (!C)
C = dyn_cast_or_null<Constant>(SimplifiedValues.lookup(I));
Constant *C = getDirectOrSimplifiedValue<Constant>(I);
if (!C)
return false; // This argument doesn't map to a constant.

Expand Down Expand Up @@ -2288,14 +2279,9 @@ bool CallAnalyzer::isLoweredToCall(Function *F, CallBase &Call) {
// platforms whose headers redirect memcpy to __memcpy_chk (e.g. Darwin), as
// other platforms use memcpy intrinsics, which are already exempt from the
// call penalty.
auto *LenOp = dyn_cast<ConstantInt>(Call.getOperand(2));
if (!LenOp)
LenOp = dyn_cast_or_null<ConstantInt>(
SimplifiedValues.lookup(Call.getOperand(2)));
auto *ObjSizeOp = dyn_cast<ConstantInt>(Call.getOperand(3));
if (!ObjSizeOp)
ObjSizeOp = dyn_cast_or_null<ConstantInt>(
SimplifiedValues.lookup(Call.getOperand(3)));
auto *LenOp = getDirectOrSimplifiedValue<ConstantInt>(Call.getOperand(2));
auto *ObjSizeOp =
getDirectOrSimplifiedValue<ConstantInt>(Call.getOperand(3));
if (LenOp && ObjSizeOp &&
LenOp->getLimitedValue() <= ObjSizeOp->getLimitedValue()) {
return false;
Expand Down Expand Up @@ -2411,23 +2397,18 @@ bool CallAnalyzer::visitBranchInst(BranchInst &BI) {
// shouldn't exist at all, but handling them makes the behavior of the
// inliner more regular and predictable. Interestingly, conditional branches
// which will fold away are also free.
return BI.isUnconditional() || isa<ConstantInt>(BI.getCondition()) ||
BI.getMetadata(LLVMContext::MD_make_implicit) ||
isa_and_nonnull<ConstantInt>(
SimplifiedValues.lookup(BI.getCondition()));
return BI.isUnconditional() ||
getDirectOrSimplifiedValue<ConstantInt>(BI.getCondition()) ||
BI.getMetadata(LLVMContext::MD_make_implicit);
}

bool CallAnalyzer::visitSelectInst(SelectInst &SI) {
bool CheckSROA = SI.getType()->isPointerTy();
Value *TrueVal = SI.getTrueValue();
Value *FalseVal = SI.getFalseValue();

Constant *TrueC = dyn_cast<Constant>(TrueVal);
if (!TrueC)
TrueC = SimplifiedValues.lookup(TrueVal);
Constant *FalseC = dyn_cast<Constant>(FalseVal);
if (!FalseC)
FalseC = SimplifiedValues.lookup(FalseVal);
Constant *TrueC = getDirectOrSimplifiedValue<Constant>(TrueVal);
Constant *FalseC = getDirectOrSimplifiedValue<Constant>(FalseVal);
Constant *CondC =
dyn_cast_or_null<Constant>(SimplifiedValues.lookup(SI.getCondition()));

Expand Down Expand Up @@ -2497,11 +2478,8 @@ bool CallAnalyzer::visitSelectInst(SelectInst &SI) {
bool CallAnalyzer::visitSwitchInst(SwitchInst &SI) {
// We model unconditional switches as free, see the comments on handling
// branches.
if (isa<ConstantInt>(SI.getCondition()))
if (getDirectOrSimplifiedValue<ConstantInt>(SI.getCondition()))
return true;
if (Value *V = SimplifiedValues.lookup(SI.getCondition()))
if (isa<ConstantInt>(V))
return true;

// Assume the most general case where the switch is lowered into
// either a jump table, bit test, or a balanced binary tree consisting of
Expand Down
Loading