Skip to content

Commit

Permalink
doWhile: start using the negateCondition attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihalko committed Mar 14, 2024
1 parent 2e60dee commit b703560
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
13 changes: 6 additions & 7 deletions parser/parseBreaks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,9 @@ void parseLoop(Func* func, const llvm::Loop *loop) {
auto doWhileTerminatorInst = llvm::dyn_cast<llvm::BranchInst>(
loop->getLoopLatch()->getTerminator());

auto whileCondIsTrueBlock = llvm::dyn_cast<llvm::BasicBlock>(doWhileTerminatorInst->getOperand(2));
Expr* cmp = func->getExpr(
doWhileTerminatorInst->getCondition()
);
if ( whileCondIsTrueBlock != loop->getHeader() ) {
auto cmpExpr = llvm::dyn_cast<CmpExpr>( cmp );
cmpExpr->comparsion = getComparePredicate(negateCmpInst(llvm::dyn_cast<llvm::CmpInst>(doWhileTerminatorInst->getCondition())));

llvm::errs() << "flipped\n";
}
// do { goto loop.header() } while (c)

// #2 create the doWhile body `do { goto loopHeader; }`
Expand All @@ -193,6 +186,12 @@ void parseLoop(Func* func, const llvm::Loop *loop) {
loopPreheader->addOwnership(std::move( gotoExpr ));
loopPreheader->addOwnership(std::move( list ));

auto whileCondIsTrueBlock = llvm::dyn_cast<llvm::BasicBlock>(doWhileTerminatorInst->getOperand(2));
auto doWhile = std::make_unique<DoWhile>(createListOfOneGoto( func->getBlock( loop->getLoopLatch() ),
doWhileBody, true /*isLoop = true*/),
cmp,
whileCondIsTrueBlock != loop->getHeader());

// #4 it's safe to inline loopheader:
// loopHeader.succ_first() == loopLatch, loopHeader.succ_second() = loopPreheader
// we transform the edge from the loopLatch to loopHeader into to do{}while() block
Expand Down
4 changes: 4 additions & 0 deletions writer/ExprWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,10 @@ void ExprWriter::visit(DoWhile& expr) {
indentCount--;
indent();
ss << "} while (";
if (expr.negateCondition)
ss << " !(";
expr.cond->accept(*this);
if (expr.negateCondition)
ss << " )";
ss << ")";
}

0 comments on commit b703560

Please sign in to comment.