Skip to content

[FuncSpec] Improve handling of BinaryOperator instructions #114534

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
Nov 4, 2024
Merged
Show file tree
Hide file tree
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
17 changes: 9 additions & 8 deletions llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,16 +489,17 @@ Constant *InstCostVisitor::visitUnaryOperator(UnaryOperator &I) {
Constant *InstCostVisitor::visitBinaryOperator(BinaryOperator &I) {
assert(LastVisited != KnownConstants.end() && "Invalid iterator!");

bool Swap = I.getOperand(1) == LastVisited->first;
Value *V = Swap ? I.getOperand(0) : I.getOperand(1);
bool ConstOnRHS = I.getOperand(1) == LastVisited->first;
Value *V = ConstOnRHS ? I.getOperand(0) : I.getOperand(1);
Constant *Other = findConstantFor(V, KnownConstants);
if (!Other)
return nullptr;
Value *OtherVal = Other ? Other : V;
Value *ConstVal = LastVisited->second;

Constant *Const = LastVisited->second;
return dyn_cast_or_null<Constant>(Swap ?
simplifyBinOp(I.getOpcode(), Other, Const, SimplifyQuery(DL))
: simplifyBinOp(I.getOpcode(), Const, Other, SimplifyQuery(DL)));
if (ConstOnRHS)
std::swap(ConstVal, OtherVal);

return dyn_cast_or_null<Constant>(
simplifyBinOp(I.getOpcode(), ConstVal, OtherVal, SimplifyQuery(DL)));
}

Constant *FunctionSpecializer::getPromotableAlloca(AllocaInst *Alloca,
Expand Down
35 changes: 35 additions & 0 deletions llvm/unittests/Transforms/IPO/FunctionSpecializationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,3 +469,38 @@ TEST_F(FunctionSpecializationTest, PhiNode) {
EXPECT_TRUE(Test > 0);
}

TEST_F(FunctionSpecializationTest, BinOp) {
// Verify that we can handle binary operators even when only one operand is
// constant.
const char *ModuleString = R"(
define i32 @foo(i1 %a, i1 %b) {
%and1 = and i1 %a, %b
%and2 = and i1 %b, %and1
%sel = select i1 %and2, i32 1, i32 0
ret i32 %sel
}
)";

Module &M = parseModule(ModuleString);
Function *F = M.getFunction("foo");
FunctionSpecializer Specializer = getSpecializerFor(F);
InstCostVisitor Visitor = Specializer.getInstCostVisitorFor(F);

Constant *False = ConstantInt::getFalse(M.getContext());
BasicBlock &BB = F->front();
Instruction &And1 = BB.front();
Instruction &And2 = *++BB.begin();
Instruction &Select = *++BB.begin();

Cost RefCodeSize = getCodeSizeSavings(And1) + getCodeSizeSavings(And2) +
getCodeSizeSavings(Select);
Cost RefLatency = getLatencySavings(F);

Cost TestCodeSize = Visitor.getCodeSizeSavingsForArg(F->getArg(0), False);
Cost TestLatency = Visitor.getLatencySavingsForKnownConstants();

EXPECT_EQ(TestCodeSize, RefCodeSize);
EXPECT_TRUE(TestCodeSize > 0);
EXPECT_EQ(TestLatency, RefLatency);
EXPECT_TRUE(TestLatency > 0);
}
Loading