Skip to content

Commit

Permalink
Improve ASTSize
Browse files Browse the repository at this point in the history
  • Loading branch information
adam committed Apr 27, 2024
1 parent e67850f commit e30579e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CSiMBA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ void SimplifyLLVMModule() {
outs() << "[+] Loading LLVM Module: '" << StrIR << "'\n";

LSiMBA::LLVMParser Parser(StrIR, Output, RunParallel, UseFastCheck,
RunOptimizer, RunOptimizer, Debug, ProveZ3);
false, RunOptimizer, Debug, ProveZ3);

auto start = high_resolution_clock::now();

Expand Down
20 changes: 11 additions & 9 deletions LLVMParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ int LLVMParser::extractAndSimplify() {
}

// Optimize if any replacements
if (MBASimplified && this->OptimizeBefore) {
if (MBASimplified && this->OptimizeAfter) {
this->runLLVMOptimizer();
}

Expand Down Expand Up @@ -506,7 +506,7 @@ bool LLVMParser::verify(llvm::Function *F0, llvm::Function *F1,
return true;
}

bool LLVMParser::verify(llvm::SmallVectorImpl<BFSEntry> &AST,
bool LLVMParser::verify(int ASTSize, llvm::SmallVectorImpl<BFSEntry> &AST,
std::string &SimpExpr,
llvm::SmallVectorImpl<llvm::Value *> &Variables) {
int VNumber = Variables.size();
Expand Down Expand Up @@ -543,9 +543,9 @@ bool LLVMParser::verify(llvm::SmallVectorImpl<BFSEntry> &AST,
auto AP_R1 = eval(Expr1_replVar, par, BitWidth, &Operations);

// Check if replacement is cheaper than original expression
if (getASTSize(AST) <= Operations) {
if (ASTSize <= Operations) {
#ifdef DEBUG_SIMPLIFICATION
outs() << "[!] Simplification is no improvement: AST: " << getASTSize(AST)
outs() << "[!] Simplification is no improvement: AST: " << ASTSize
<< " Operations: " << Operations << "\n";
#endif
return false;
Expand Down Expand Up @@ -905,6 +905,7 @@ bool LLVMParser::findReplacements(llvm::DominatorTree *DT,
for (int i = 0; i < Candidates.size(); i++) {
auto &Cand = Candidates[i];
getAST(DT, Cand.Candidate, Cand.AST, Cand.Variables, true);
Cand.ASTSize = getASTSize(Cand.AST);
}

auto EndTime = high_resolution_clock::now();
Expand All @@ -925,8 +926,7 @@ bool LLVMParser::findReplacements(llvm::DominatorTree *DT,

for (int i = 0; i < Candidates.size(); i++) {
auto &Cand = Candidates[i];
int s = getASTSize(Cand.AST);
if (s < MinASTSize) {
if (Cand.ASTSize < MinASTSize) {
continue;
}

Expand Down Expand Up @@ -1009,7 +1009,8 @@ bool LLVMParser::findReplacements(llvm::DominatorTree *DT,

// Verify is replacement is valid
if (!SkipVerify) {
Cand.isValid = this->verify(Cand.AST, Cand.Replacement, Cand.Variables);
Cand.isValid = this->verify(Cand.ASTSize, Cand.AST, Cand.Replacement,
Cand.Variables);
}

if (Cand.isValid == false) {
Expand Down Expand Up @@ -1083,8 +1084,9 @@ bool LLVMParser::walkSubAST(llvm::DominatorTree *DT,
MBACandidate C;
C.Candidate = BinOp;
this->getAST(DT, BinOp, C.AST, C.Variables, true);
C.ASTSize = getASTSize(C.AST);

if (getASTSize(C.AST) < MinASTSize) continue;
if (C.ASTSize < MinASTSize) continue;

int BitWidth = C.AST.front().I->getType()->getIntegerBitWidth();
if (BitWidth == 0 || BitWidth > 64) continue;
Expand Down Expand Up @@ -1126,7 +1128,7 @@ bool LLVMParser::walkSubAST(llvm::DominatorTree *DT,
#endif

if (!SkipVerify) {
C.isValid = this->verify(C.AST, C.Replacement, C.Variables);
C.isValid = this->verify(C.ASTSize, C.AST, C.Replacement, C.Variables);
}

if (C.isValid) {
Expand Down
9 changes: 5 additions & 4 deletions LLVMParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ typedef struct BFSEntry {
typedef struct MBACandidate {
llvm::Instruction *Candidate;

int ASTSize = 0;
llvm::SmallVector<BFSEntry, 16> AST;

llvm::SmallVector<llvm::Value *, 8> Variables;
Expand All @@ -53,15 +54,15 @@ class LLVMParser {
public:
LLVMParser(const std::string &filename, const std::string &OutputFile,
bool Parallel = true, bool Verify = true,
bool OptimizeBefore = true, bool OptimizeAfter = true,
bool OptimizeBefore = false, bool OptimizeAfter = true,
bool Debug = false, bool Prove = false);

LLVMParser(llvm::Module *M, bool Parallel = true, bool Verify = true,
bool OptimizeBefore = true, bool OptimizeAfter = true,
bool OptimizeBefore = false, bool OptimizeAfter = true,
bool Debug = false, bool Prove = false);

LLVMParser(llvm::Function *F, bool Parallel = true, bool Verify = true,
bool OptimizeBefore = true, bool OptimizeAfter = true,
bool OptimizeBefore = false, bool OptimizeAfter = true,
bool Debug = false, bool Prove = false);

~LLVMParser();
Expand Down Expand Up @@ -127,7 +128,7 @@ class LLVMParser {

bool verify(llvm::Function *F0, llvm::Function *F1, llvm::APInt &Modulus);

bool verify(llvm::SmallVectorImpl<BFSEntry> &AST, std::string &SimpExpr,
bool verify(int ASTSize, llvm::SmallVectorImpl<BFSEntry> &AST, std::string &SimpExpr,
llvm::SmallVectorImpl<llvm::Value *> &Variables);

llvm::Instruction *getSingleTerminator(llvm::Function &F);
Expand Down

0 comments on commit e30579e

Please sign in to comment.