Skip to content

Commit

Permalink
Enable constant folding for fields
Browse files Browse the repository at this point in the history
  • Loading branch information
makxenov committed Feb 28, 2024
1 parent 8187d30 commit b40c343
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions llvm/lib/IR/ConstantFold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "llvm/IR/Operator.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/ZK/FieldArithmetics.h"
using namespace llvm;
using namespace llvm::PatternMatch;

Expand Down Expand Up @@ -1221,6 +1222,32 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
}
}

if (auto *CF1 = dyn_cast<ConstantField>(C1)) {
if (auto *CF2 = dyn_cast<ConstantField>(C2)) {
FieldElem Elem1 = CF1->getValue();
FieldElem Elem2 = CF2->getValue();
assert(Elem1.getKind() == Elem2.getKind());
FieldOperation Op;
switch (Opcode) {
case Instruction::Add:
Op = F_Add;
break;
case Instruction::Sub:
Op = F_Sub;
break;
case Instruction::Mul:
Op = F_Mul;
break;
case Instruction::UDiv:
case Instruction::SDiv:
Op = F_Div;
break;
}
FieldElem Res = FieldBinOp(Op, Elem1, Elem2);
return ConstantField::get(llvm::cast<GaloisFieldType>(C1->getType()), Res);
}
}

if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
// There are many possible foldings we could do here. We should probably
// at least fold add of a pointer with an integer into the appropriate
Expand Down

0 comments on commit b40c343

Please sign in to comment.