Skip to content

Conversation

@kshitijvp
Copy link
Contributor

Addeed dbgs() in removeSections() function to print the removed
sections and symbols.

Fixes: #123041

ilogb libcall was not being constant folded correctly.
It was due to LibCallShrinkWrap pass not handling
ilogbx, as a result SimplifyCFG pass did not remove
redundant libcall. This patch adds ilogb case in
performCallErrors().

Fixes llvm#101873
ilogb libcall was not being constant folded correctly.
This patch adds ilogb with its correct domain in isMathLibCallNoop().

Fixes llvm#101873
I have added the dbgs() function  inside removeSections() function
to print the removed sections and symbols.

Fixes: llvm#123041
@llvmbot llvmbot added llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:analysis Includes value tracking, cost tables and constant folding llvm:transforms llvm:binary-utilities labels Jan 27, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 27, 2025

@llvm/pr-subscribers-llvm-binary-utilities

@llvm/pr-subscribers-llvm-analysis

Author: Kshitij Paranjape (kshitijvp)

Changes

Addeed dbgs() in removeSections() function to print the removed
sections and symbols.

Fixes: #123041


Full diff: https://github.com/llvm/llvm-project/pull/124582.diff

4 Files Affected:

  • (modified) llvm/lib/Analysis/ConstantFolding.cpp (+3)
  • (modified) llvm/lib/ObjCopy/ELF/ELFObject.cpp (+20-6)
  • (added) llvm/test/Transforms/InstSimplify/pr122582.ll (+213)
  • (modified) llvm/tools/llvm-objcopy/CommonOpts.td (+2)
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 031d675c330ec4..6814a1e4d8394c 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3807,6 +3807,9 @@ bool llvm::isMathLibCallNoop(const CallBase *Call,
       case LibFunc_log10f:
         return Op.isNaN() || (!Op.isZero() && !Op.isNegative());
 
+      case LibFunc_ilogb:
+        return !Op.isNaN() && !Op.isZero() && !Op.isInfinity();
+
       case LibFunc_expl:
       case LibFunc_exp:
       case LibFunc_expf:
diff --git a/llvm/lib/ObjCopy/ELF/ELFObject.cpp b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
index 01c2f24629077a..340edebbc67902 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObject.cpp
+++ b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
@@ -766,8 +766,14 @@ Error SymbolTableSection::removeSymbols(
     function_ref<bool(const Symbol &)> ToRemove) {
   Symbols.erase(
       std::remove_if(std::begin(Symbols) + 1, std::end(Symbols),
-                     [ToRemove](const SymPtr &Sym) { return ToRemove(*Sym); }),
-      std::end(Symbols));
+               [ToRemove](const SymPtr &Sym) {
+                   if (ToRemove(*Sym)) {
+                       dbgs()<<"Symbols Removed:"<<Sym->Name<< "\n";
+                       return true;
+                   }
+                   return false;
+               }));
+
   auto PrevSize = Size;
   Size = Symbols.size() * EntrySize;
   if (Size < PrevSize)
@@ -2240,10 +2246,18 @@ Error Object::removeSections(
 
   // Transfer removed sections into the Object RemovedSections container for use
   // later.
-  std::move(Iter, Sections.end(), std::back_inserter(RemovedSections));
-  // Now finally get rid of them all together.
-  Sections.erase(Iter, std::end(Sections));
-  return Error::success();
+  for(auto &KeepSec : make_range(std::begin(Sections) , Iter))
+  {
+    
+    if (Error E = KeepSec->removeSectionReferences(
+            AllowBrokenLinks, [&RemoveSections](const SectionBase *Sec) {
+              return RemoveSections.find(Sec) != RemoveSections.end();
+            }))
+      std::move(Iter, Sections.end(), std::back_inserter(RemovedSections));
+      dbgs()<<"Sections Removed:"<<KeepSec->Name<<'\n';
+      Sections.erase(Iter, std::end(Sections));
+      return Error::success();
+  }
 }
 
 Error Object::replaceSections(
diff --git a/llvm/test/Transforms/InstSimplify/pr122582.ll b/llvm/test/Transforms/InstSimplify/pr122582.ll
new file mode 100644
index 00000000000000..fd507e39eccdda
--- /dev/null
+++ b/llvm/test/Transforms/InstSimplify/pr122582.ll
@@ -0,0 +1,213 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
+
+define i32 @ilogbf_const1() {
+; CHECK-LABEL: define i32 @ilogbf_const1() {
+; CHECK-NEXT:    [[R:%.*]] = call i32 @ilogbf(float 7.000000e+00)
+; CHECK-NEXT:    ret i32 2
+;
+  %r = call i32 @ilogbf(float 7.000000e+00)
+  ret i32 %r
+}
+
+define i32 @ilogb_const1() {
+; CHECK-LABEL: define i32 @ilogb_const1() {
+; CHECK-NEXT:    [[R:%.*]] = call i32 @ilogb(double -7.000000e+00)
+; CHECK-NEXT:    ret i32 2
+;
+  %r = call i32 @ilogb(double -7.000000e+00)
+  ret i32 %r
+}
+
+define i32 @ilogbf_const2() {
+; CHECK-LABEL: define i32 @ilogbf_const2() {
+; CHECK-NEXT:    [[R:%.*]] = call i32 @ilogbf(float 5.000000e-01)
+; CHECK-NEXT:    ret i32 -1
+;
+  %r = call i32 @ilogbf(float 5.000000e-01)
+  ret i32 %r
+}
+
+define i32 @ilogb_const2() {
+; CHECK-LABEL: define i32 @ilogb_const2() {
+; CHECK-NEXT:    [[R:%.*]] = call i32 @ilogb(double -5.000000e-01)
+; CHECK-NEXT:    ret i32 -1
+;
+  %r = call i32 @ilogb(double -5.000000e-01)
+  ret i32 %r
+}
+
+define i32 @ilogbf_zero() {
+; CHECK-LABEL: define i32 @ilogbf_zero() {
+; CHECK-NEXT:    [[R:%.*]] = call i32 @ilogbf(float 0.000000e+00)
+; CHECK-NEXT:    ret i32 [[R]]
+;
+  %r = call i32 @ilogbf(float 0.000000e+00)
+  ret i32 %r
+}
+
+define i32 @ilogb_zero() {
+; CHECK-LABEL: define i32 @ilogb_zero() {
+; CHECK-NEXT:    [[R:%.*]] = call i32 @ilogb(double 0.000000e+00)
+; CHECK-NEXT:    ret i32 [[R]]
+;
+  %r = call i32 @ilogb(double 0.000000e+00)
+  ret i32 %r
+}
+
+define i32 @ilogbf_neg_zero() {
+; CHECK-LABEL: define i32 @ilogbf_neg_zero() {
+; CHECK-NEXT:    [[R:%.*]] = call i32 @ilogbf(float -0.000000e+00)
+; CHECK-NEXT:    ret i32 [[R]]
+;
+  %r = call i32 @ilogbf(float -0.000000e+00)
+  ret i32 %r
+}
+
+define i32 @ilogb_neg_zero() {
+; CHECK-LABEL: define i32 @ilogb_neg_zero() {
+; CHECK-NEXT:    [[R:%.*]] = call i32 @ilogb(double -0.000000e+00)
+; CHECK-NEXT:    ret i32 [[R]]
+;
+  %r = call i32 @ilogb(double -0.000000e+00)
+  ret i32 %r
+}
+
+define i32 @ilogbf_inf() {
+; CHECK-LABEL: define i32 @ilogbf_inf() {
+; CHECK-NEXT:    [[R:%.*]] = call i32 @ilogbf(float 0x7FF0000000000000)
+; CHECK-NEXT:    ret i32 [[R]]
+;
+  %r = call i32 @ilogbf(float 0x7FF0000000000000)
+  ret i32 %r
+}
+
+define i32 @ilogb_inf() {
+; CHECK-LABEL: define i32 @ilogb_inf() {
+; CHECK-NEXT:    [[R:%.*]] = call i32 @ilogb(double 0x7FF0000000000000)
+; CHECK-NEXT:    ret i32 [[R]]
+;
+  %r = call i32 @ilogb(double 0x7FF0000000000000)
+  ret i32 %r
+}
+
+define i32 @ilogbf_nan() {
+; CHECK-LABEL: define i32 @ilogbf_nan() {
+; CHECK-NEXT:    [[R:%.*]] = call i32 @ilogbf(float 0x7FF8000000000000)
+; CHECK-NEXT:    ret i32 [[R]]
+;
+  %r = call i32 @ilogbf(float 0x7FF8000000000000)
+  ret i32 %r
+}
+
+define i32 @ilogb_nan() {
+; CHECK-LABEL: define i32 @ilogb_nan() {
+; CHECK-NEXT:    [[R:%.*]] = call i32 @ilogb(double 0x7FF8000000000000)
+; CHECK-NEXT:    ret i32 [[R]]
+;
+  %r = call i32 @ilogb(double 0x7FF8000000000000)
+  ret i32 %r
+}
+
+define i32 @ilogbf_zero_readnone() {
+; CHECK-LABEL: define i32 @ilogbf_zero_readnone() {
+; CHECK-NEXT:    [[R:%.*]] = call i32 @ilogbf(float 0.000000e+00) #[[ATTR1:[0-9]+]]
+; CHECK-NEXT:    ret i32 [[R]]
+;
+  %r = call i32 @ilogbf(float 0.000000e+00) readnone
+  ret i32 %r
+}
+
+define i32 @ilogb_zero_readnone() {
+; CHECK-LABEL: define i32 @ilogb_zero_readnone() {
+; CHECK-NEXT:    [[R:%.*]] = call i32 @ilogb(double 0.000000e+00) #[[ATTR1]]
+; CHECK-NEXT:    ret i32 [[R]]
+;
+  %r = call i32 @ilogb(double 0.000000e+00) readnone
+  ret i32 %r
+}
+
+define i32 @ilogbf_neg_zero_readnone() {
+; CHECK-LABEL: define i32 @ilogbf_neg_zero_readnone() {
+; CHECK-NEXT:    [[R:%.*]] = call i32 @ilogbf(float -0.000000e+00) #[[ATTR1]]
+; CHECK-NEXT:    ret i32 [[R]]
+;
+  %r = call i32 @ilogbf(float -0.000000e+00) readnone
+  ret i32 %r
+}
+
+define i32 @ilogb_neg_zero_readnone() {
+; CHECK-LABEL: define i32 @ilogb_neg_zero_readnone() {
+; CHECK-NEXT:    [[R:%.*]] = call i32 @ilogb(double -0.000000e+00) #[[ATTR1]]
+; CHECK-NEXT:    ret i32 [[R]]
+;
+  %r = call i32 @ilogb(double -0.000000e+00) readnone
+  ret i32 %r
+}
+
+define i32 @ilogbf_inf_readnone() {
+; CHECK-LABEL: define i32 @ilogbf_inf_readnone() {
+; CHECK-NEXT:    [[R:%.*]] = call i32 @ilogbf(float 0x7FF0000000000000) #[[ATTR1]]
+; CHECK-NEXT:    ret i32 [[R]]
+;
+  %r = call i32 @ilogbf(float 0x7FF0000000000000) readnone
+  ret i32 %r
+}
+
+define i32 @ilogb_inf_readnone() {
+; CHECK-LABEL: define i32 @ilogb_inf_readnone() {
+; CHECK-NEXT:    [[R:%.*]] = call i32 @ilogb(double 0x7FF0000000000000) #[[ATTR1]]
+; CHECK-NEXT:    ret i32 [[R]]
+;
+  %r = call i32 @ilogb(double 0x7FF0000000000000) readnone
+  ret i32 %r
+}
+
+define i32 @ilogbf_nan_readnone() {
+; CHECK-LABEL: define i32 @ilogbf_nan_readnone() {
+; CHECK-NEXT:    [[R:%.*]] = call i32 @ilogbf(float 0x7FF8000000000000) #[[ATTR1]]
+; CHECK-NEXT:    ret i32 [[R]]
+;
+  %r = call i32 @ilogbf(float 0x7FF8000000000000) readnone
+  ret i32 %r
+}
+
+define i32 @ilogb_nan_readnone() {
+; CHECK-LABEL: define i32 @ilogb_nan_readnone() {
+; CHECK-NEXT:    [[R:%.*]] = call i32 @ilogb(double 0x7FF8000000000000) #[[ATTR1]]
+; CHECK-NEXT:    ret i32 [[R]]
+;
+  %r = call i32 @ilogb(double 0x7FF8000000000000) readnone
+  ret i32 %r
+}
+
+define i32 @ilogbf_poison() {
+; CHECK-LABEL: define i32 @ilogbf_poison() {
+; CHECK-NEXT:    [[R:%.*]] = call i32 @ilogbf(float poison)
+; CHECK-NEXT:    ret i32 [[R]]
+;
+  %r = call i32 @ilogbf(float poison)
+  ret i32 %r
+}
+
+define i32 @ilogb_poison() {
+; CHECK-LABEL: define i32 @ilogb_poison() {
+; CHECK-NEXT:    [[R:%.*]] = call i32 @ilogb(double poison)
+; CHECK-NEXT:    ret i32 [[R]]
+;
+  %r = call i32 @ilogb(double poison)
+  ret i32 %r
+}
+
+define i32 @ilogb_const1_1() strictfp {
+; CHECK-LABEL: define i32 @ilogb_const1_1(
+; CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT:    [[R:%.*]] = call i32 @ilogb(double -7.000000e+00)
+; CHECK-NEXT:    ret i32 2
+;
+  %r = call i32 @ilogb(double -7.000000e+00)
+  ret i32 %r      
+}
+
+declare i32 @ilogbf(float)
+declare i32 @ilogb(double)
diff --git a/llvm/tools/llvm-objcopy/CommonOpts.td b/llvm/tools/llvm-objcopy/CommonOpts.td
index c247c93f6e0f27..5b15191f546051 100644
--- a/llvm/tools/llvm-objcopy/CommonOpts.td
+++ b/llvm/tools/llvm-objcopy/CommonOpts.td
@@ -117,6 +117,8 @@ def regex
 
 def version : Flag<["--"], "version">,
               HelpText<"Print the version and exit.">;
+def verbose : Flag<["--"], "verbose">,
+              HelpText<"Prints the removed symbols and sections">;
 def V : Flag<["-"], "V">,
         Alias<version>,
         HelpText<"Alias for --version">;

@kshitijvp kshitijvp closed this Jan 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:analysis Includes value tracking, cost tables and constant folding llvm:binary-utilities llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a --verbose option for llvm-strip

2 participants