diff options
author | eric-xtang1008 <eric.tang@starfivetech.com> | 2024-10-01 20:51:01 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-01 14:51:01 +0200 |
commit | a59e5d8115bce7d75330c5206b321ea88f183e09 (patch) | |
tree | 07407b7d42221d5dca36e09e09ce46cde820b9a1 /llvm/lib/IR/ConstantFold.cpp | |
parent | 9f81acf4ef39e05bb2833cb3d79914940f31ac6c (diff) | |
download | llvm-a59e5d8115bce7d75330c5206b321ea88f183e09.zip llvm-a59e5d8115bce7d75330c5206b321ea88f183e09.tar.gz llvm-a59e5d8115bce7d75330c5206b321ea88f183e09.tar.bz2 |
[ConstantFold][RFC] Add AllowLHSConstant parameter in getBinOpAbsorber (#109736)
Add a AllowLHSConstant parameter in getBinOpAbsorber function for
supporting more binary operators.
Diffstat (limited to 'llvm/lib/IR/ConstantFold.cpp')
-rw-r--r-- | llvm/lib/IR/ConstantFold.cpp | 32 |
1 files changed, 8 insertions, 24 deletions
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp index 05ab096..a6f46da 100644 --- a/llvm/lib/IR/ConstantFold.cpp +++ b/llvm/lib/IR/ConstantFold.cpp @@ -731,11 +731,11 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1, // Handle simplifications when the RHS is a constant int. if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) { + if (C2 == ConstantExpr::getBinOpAbsorber(Opcode, C2->getType(), + /*AllowLHSConstant*/ false)) + return C2; + switch (Opcode) { - case Instruction::Mul: - if (CI2->isZero()) - return C2; // X * 0 == 0 - break; case Instruction::UDiv: case Instruction::SDiv: if (CI2->isZero()) @@ -749,9 +749,7 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1, return PoisonValue::get(CI2->getType()); // X % 0 == poison break; case Instruction::And: - if (CI2->isZero()) - return C2; // X & 0 == 0 - + assert(!CI2->isZero() && "And zero handled above"); if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) { // If and'ing the address of a global with a constant, fold it. if (CE1->getOpcode() == Instruction::PtrToInt && @@ -791,10 +789,6 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1, } } break; - case Instruction::Or: - if (CI2->isMinusOne()) - return C2; // X | -1 == -1 - break; } } else if (isa<ConstantInt>(C1)) { // If C1 is a ConstantInt and C2 is not, swap the operands. @@ -854,19 +848,9 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1, } } - switch (Opcode) { - case Instruction::SDiv: - case Instruction::UDiv: - case Instruction::URem: - case Instruction::SRem: - case Instruction::LShr: - case Instruction::AShr: - case Instruction::Shl: - if (CI1->isZero()) return C1; - break; - default: - break; - } + if (C1 == ConstantExpr::getBinOpAbsorber(Opcode, C1->getType(), + /*AllowLHSConstant*/ true)) + return C1; } else if (ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) { if (ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) { const APFloat &C1V = CFP1->getValueAPF(); |