Skip to content

[ConstantFolding] Constify ConstantFoldInstOperands and ConstantFoldInstruction argument. NFC #138108

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
May 1, 2025

Conversation

lukel97
Copy link
Contributor

@lukel97 lukel97 commented May 1, 2025

I tried to use these with a const reference in a separate patch, but the pointers weren't marked as const. It turns out that these don't mutate the instruction.

…nstruction. NFC

I tried to use these with a const reference in a separate patch, but the pointers weren't marked as const. It turns out that these don't mutate the instruction.
@lukel97 lukel97 requested review from nikic and dtcxzyw May 1, 2025 10:36
@lukel97 lukel97 changed the title [ConstantFolding] Constify ConstantFoldInstOperands and ConstantFoldInstruction. NFC [ConstantFolding] Constify ConstantFoldInstOperands and ConstantFoldInstruction argument. NFC May 1, 2025
@llvmbot
Copy link
Member

llvmbot commented May 1, 2025

@llvm/pr-subscribers-llvm-analysis

Author: Luke Lau (lukel97)

Changes

I tried to use these with a const reference in a separate patch, but the pointers weren't marked as const. It turns out that these don't mutate the instruction.


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

2 Files Affected:

  • (modified) llvm/include/llvm/Analysis/ConstantFolding.h (+3-2)
  • (modified) llvm/lib/Analysis/ConstantFolding.cpp (+4-3)
diff --git a/llvm/include/llvm/Analysis/ConstantFolding.h b/llvm/include/llvm/Analysis/ConstantFolding.h
index 58b38fb8b0367..706ba0d835cb1 100644
--- a/llvm/include/llvm/Analysis/ConstantFolding.h
+++ b/llvm/include/llvm/Analysis/ConstantFolding.h
@@ -53,7 +53,7 @@ bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset,
 /// Note that this fails if not all of the operands are constant.  Otherwise,
 /// this function can only fail when attempting to fold instructions like loads
 /// and stores, which have no constant expression form.
-Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
+Constant *ConstantFoldInstruction(const Instruction *I, const DataLayout &DL,
                                   const TargetLibraryInfo *TLI = nullptr);
 
 /// ConstantFoldConstant - Fold the constant using the specified DataLayout.
@@ -74,7 +74,8 @@ Constant *ConstantFoldConstant(const Constant *C, const DataLayout &DL,
 /// all uses of the original operation are replaced by the constant-folded
 /// result. The \p AllowNonDeterministic parameter controls whether this is
 /// allowed.
-Constant *ConstantFoldInstOperands(Instruction *I, ArrayRef<Constant *> Ops,
+Constant *ConstantFoldInstOperands(const Instruction *I,
+                                   ArrayRef<Constant *> Ops,
                                    const DataLayout &DL,
                                    const TargetLibraryInfo *TLI = nullptr,
                                    bool AllowNonDeterministic = true);
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index dc905ab03e861..5b329e2f898f3 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -1123,7 +1123,8 @@ ConstantFoldConstantImpl(const Constant *C, const DataLayout &DL,
 
 } // end anonymous namespace
 
-Constant *llvm::ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
+Constant *llvm::ConstantFoldInstruction(const Instruction *I,
+                                        const DataLayout &DL,
                                         const TargetLibraryInfo *TLI) {
   // Handle PHI nodes quickly here...
   if (auto *PN = dyn_cast<PHINode>(I)) {
@@ -1156,7 +1157,7 @@ Constant *llvm::ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
 
   // Scan the operand list, checking to see if they are all constants, if so,
   // hand off to ConstantFoldInstOperandsImpl.
-  if (!all_of(I->operands(), [](Use &U) { return isa<Constant>(U); }))
+  if (!all_of(I->operands(), [](const Use &U) { return isa<Constant>(U); }))
     return nullptr;
 
   SmallDenseMap<Constant *, Constant *> FoldedOps;
@@ -1177,7 +1178,7 @@ Constant *llvm::ConstantFoldConstant(const Constant *C, const DataLayout &DL,
   return ConstantFoldConstantImpl(C, DL, TLI, FoldedOps);
 }
 
-Constant *llvm::ConstantFoldInstOperands(Instruction *I,
+Constant *llvm::ConstantFoldInstOperands(const Instruction *I,
                                          ArrayRef<Constant *> Ops,
                                          const DataLayout &DL,
                                          const TargetLibraryInfo *TLI,

@lukel97 lukel97 merged commit d33c676 into llvm:main May 1, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants