[llvm] [InstCombine] x mod(c1*c2)/c2 --> (x /c2) mod(c1) (PR #140197)
via llvm-commits
llvm-commits at lists.llvm.org
Thu May 15 23:22:06 PDT 2025
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff HEAD~1 HEAD --extensions cpp,h -- llvm/include/llvm/ObjCopy/CommonConfig.h llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp llvm/lib/ObjCopy/ELF/ELFObject.cpp llvm/lib/ObjCopy/ELF/ELFObject.h llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
``````````
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp b/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
index 6eec21216..ad31f7ed7 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
+++ b/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
@@ -547,7 +547,8 @@ static Error replaceAndRemoveSections(const CommonConfig &Config,
};
}
- if (Error E = Obj.removeSections(ELFConfig.AllowBrokenLinks, RemovePred, Config.Verbose))
+ if (Error E = Obj.removeSections(ELFConfig.AllowBrokenLinks, RemovePred,
+ Config.Verbose))
return E;
if (Error E = Obj.compressOrDecompressSections(Config))
@@ -792,7 +793,8 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
if (!Config.SplitDWO.empty() && Config.ExtractDWO) {
return Obj.removeSections(
ELFConfig.AllowBrokenLinks,
- [&Obj](const SectionBase &Sec) { return onlyKeepDWOPred(Obj, Sec); }, Config.Verbose);
+ [&Obj](const SectionBase &Sec) { return onlyKeepDWOPred(Obj, Sec); },
+ Config.Verbose);
}
// Dump sections before add/remove for compatibility with GNU objcopy.
diff --git a/llvm/lib/ObjCopy/ELF/ELFObject.cpp b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
index fece7a88b..cc80dc365 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObject.cpp
+++ b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
@@ -764,16 +764,15 @@ void SymbolTableSection::updateSymbols(function_ref<void(Symbol &)> Callable) {
Error SymbolTableSection::removeSymbols(
function_ref<bool(const Symbol &)> ToRemove) {
- Symbols.erase(
- std::remove_if(std::begin(Symbols) + 1, std::end(Symbols),
- [&](const SymPtr &Sym) {
- if (ToRemove(*Sym)) {
- if(VerboseOutput)
- outs() << "Symbols Removed:" << Sym->Name<< "\n";
- return true;
- }
- return false;
- }));
+ Symbols.erase(std::remove_if(
+ std::begin(Symbols) + 1, std::end(Symbols), [&](const SymPtr &Sym) {
+ if (ToRemove(*Sym)) {
+ if (VerboseOutput)
+ outs() << "Symbols Removed:" << Sym->Name << "\n";
+ return true;
+ }
+ return false;
+ }));
auto PrevSize = Size;
Size = Symbols.size() * EntrySize;
@@ -2200,8 +2199,9 @@ Error Object::updateSectionData(SectionBase &S, ArrayRef<uint8_t> Data) {
return updateSectionData(*It, Data);
}
-Error Object::removeSections(
- bool AllowBrokenLinks, std::function<bool(const SectionBase &)> ToRemove, bool VerboseOutput) {
+Error Object::removeSections(bool AllowBrokenLinks,
+ std::function<bool(const SectionBase &)> ToRemove,
+ bool VerboseOutput) {
auto Iter = std::stable_partition(
std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) {
@@ -2282,15 +2282,16 @@ Error Object::replaceSections(
if (Error E = removeSections(
/*AllowBrokenLinks=*/false,
- [=](const SectionBase &Sec) { return FromTo.count(&Sec) > 0; }, false))
+ [=](const SectionBase &Sec) { return FromTo.count(&Sec) > 0; },
+ false))
return E;
llvm::sort(Sections, SectionIndexLess);
return Error::success();
}
Error Object::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
- if (SymbolTable){
- for (const SecPtr &Sec : Sections){
+ if (SymbolTable) {
+ for (const SecPtr &Sec : Sections) {
if (Error E = Sec->removeSymbols(ToRemove))
return E;
outs() << "removed symbols:" << Sec->Name;
@@ -2580,9 +2581,12 @@ static Error removeUnneededSections(Object &Obj) {
auto *StrTab = Obj.SymbolTable->getStrTab() == Obj.SectionNames
? nullptr
: Obj.SymbolTable->getStrTab();
- return Obj.removeSections(false, [&](const SectionBase &Sec) {
- return &Sec == Obj.SymbolTable || &Sec == StrTab;
- }, false);
+ return Obj.removeSections(
+ false,
+ [&](const SectionBase &Sec) {
+ return &Sec == Obj.SymbolTable || &Sec == StrTab;
+ },
+ false);
}
template <class ELFT> Error ELFWriter<ELFT>::finalize() {
@@ -2633,10 +2637,12 @@ template <class ELFT> Error ELFWriter<ELFT>::finalize() {
// references to it.
if (Obj.SectionIndexTable != nullptr) {
// We do not support sections referring to the section index table.
- if (Error E = Obj.removeSections(false /*AllowBrokenLinks*/,
- [this](const SectionBase &Sec) {
- return &Sec == Obj.SectionIndexTable;
- }, false))
+ if (Error E = Obj.removeSections(
+ false /*AllowBrokenLinks*/,
+ [this](const SectionBase &Sec) {
+ return &Sec == Obj.SectionIndexTable;
+ },
+ false))
return E;
}
}
diff --git a/llvm/lib/ObjCopy/ELF/ELFObject.h b/llvm/lib/ObjCopy/ELF/ELFObject.h
index bcda10034..42a6cab11 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObject.h
+++ b/llvm/lib/ObjCopy/ELF/ELFObject.h
@@ -816,6 +816,7 @@ class SymbolTableSection : public SectionBase {
private:
bool VerboseOutput;
+
protected:
std::vector<std::unique_ptr<Symbol>> Symbols;
StringTableSection *SymbolNames = nullptr;
@@ -1228,7 +1229,8 @@ public:
ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Error removeSections(bool AllowBrokenLinks,
- std::function<bool(const SectionBase &)> ToRemove, bool VerboseOutput);
+ std::function<bool(const SectionBase &)> ToRemove,
+ bool VerboseOutput);
Error compressOrDecompressSections(const CommonConfig &Config);
Error replaceSections(const DenseMap<SectionBase *, SectionBase *> &FromTo);
Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 3e245532d..b5a3482c4 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -2872,32 +2872,33 @@ Instruction *InstCombinerImpl::visitAnd(BinaryOperator &I) {
/*SimplifyOnly*/ false, *this))
return BinaryOperator::CreateAnd(Op0, V);
// I is the 'and' instruction
-if (auto *Add = dyn_cast<BinaryOperator>(I.getOperand(0))) {
- if (Add->getOpcode() == Instruction::Add) {
+ if (auto *Add = dyn_cast<BinaryOperator>(I.getOperand(0))) {
+ if (Add->getOpcode() == Instruction::Add) {
Value *LHS = Add->getOperand(0); // should be shl
ConstantInt *AddConst = dyn_cast<ConstantInt>(Add->getOperand(1));
ConstantInt *AndConst = dyn_cast<ConstantInt>(I.getOperand(1));
if (AddConst && AndConst) {
- // check if AddConst is 47 and AndConst is -32
- if (AddConst->equalsInt(47) && AndConst->getSExtValue() == -32) {
- // Check if LHS is shl i64 %a, 5
- if (auto *Shl = dyn_cast<BinaryOperator>(LHS)) {
- if (Shl->getOpcode() == Instruction::Shl) {
- ConstantInt *ShlConst = dyn_cast<ConstantInt>(Shl->getOperand(1));
- if (ShlConst && ShlConst->equalsInt(5)) {
- // You've matched the pattern!
- // Replace with: shl i64 (add i64 %a, 1), 5
- IRBuilder<> Builder(&I);
- Value *NewAdd = Builder.CreateAdd(Shl->getOperand(0), ConstantInt::get(I.getType(), 1));
- Value *NewShl = Builder.CreateShl(NewAdd, ShlConst);
- I.replaceAllUsesWith(NewShl);
- }
- }
+ // check if AddConst is 47 and AndConst is -32
+ if (AddConst->equalsInt(47) && AndConst->getSExtValue() == -32) {
+ // Check if LHS is shl i64 %a, 5
+ if (auto *Shl = dyn_cast<BinaryOperator>(LHS)) {
+ if (Shl->getOpcode() == Instruction::Shl) {
+ ConstantInt *ShlConst = dyn_cast<ConstantInt>(Shl->getOperand(1));
+ if (ShlConst && ShlConst->equalsInt(5)) {
+ // You've matched the pattern!
+ // Replace with: shl i64 (add i64 %a, 1), 5
+ IRBuilder<> Builder(&I);
+ Value *NewAdd = Builder.CreateAdd(
+ Shl->getOperand(0), ConstantInt::get(I.getType(), 1));
+ Value *NewShl = Builder.CreateShl(NewAdd, ShlConst);
+ I.replaceAllUsesWith(NewShl);
}
+ }
}
+ }
}
+ }
}
-}
return nullptr;
}
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index c488f0c16..a44ba6dc4 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1271,14 +1271,16 @@ Instruction *InstCombinerImpl::commonIDivRemTransforms(BinaryOperator &I) {
return R;
}
- if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::smul_fix>(m_APInt(C1), m_APInt(C2)))) &&
- match(Op1, m_OneUse(m_URem(m_Value(X), Op0))) &&
- match(Op2, m_OneUse(m_UDiv(Op1, m_APInt(C2))))) {
-
+ if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::smul_fix>(m_APInt(C1),
+ m_APInt(C2)))) &&
+ match(Op1, m_OneUse(m_URem(m_Value(X), Op0))) &&
+ match(Op2, m_OneUse(m_UDiv(Op1, m_APInt(C2))))) {
+
Value *XDivC2 = Builder.CreateUDiv(X, ConstantInt::get(X->getType(), *C2));
- Value *Result = Builder.CreateURem(XDivC2, ConstantInt::get(X->getType(), *C1));
+ Value *Result =
+ Builder.CreateURem(XDivC2, ConstantInt::get(X->getType(), *C1));
return replaceInstUsesWith(I, Result);
-}
+ }
return nullptr;
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/140197
More information about the llvm-commits
mailing list