Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ Daedalus is an out-of-tree LLVM pass. Therefore, you can compile and install it

```shell
$ mkdir build
$ cd build
$ cmake -DLLVM_DIR={path_to_llvm_project} ../
$ cmake --build .
$ cmake -DLLVM_DIR=$(llvm-config --cmakedir) -S . -B build
$ cmake --build build
```

**Disclaimer**: This pass depends on a custom fork of [LLVM 17](https://github.com/Casperento/llvm-project/tree/merge-functions-pass).
Expand Down
2 changes: 1 addition & 1 deletion include/ProgramSlice.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class ProgramSlice {
BasicBlock *unreachableBlock,
const PostDominatorTree &PDT);

Value *getClonedOrUndef(Value *origCond, BasicBlock *context);
Value *getClonedCond(Value *origCond);

/// Determines the target block for a successor, potentially finding a
/// dominated node if direct mapping fails.
Expand Down
2 changes: 1 addition & 1 deletion lib/PHIGateAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ void PHIGateAnalyzer::collectGates(
collectGates(arg.Lhs, Gates, Visited);
collectGates(arg.Rhs, Gates, Visited);
} else if constexpr (std::is_same_v<T, SpecialExpr>) {
// ~secial case~ The paper assumes that SpecialExpr is an
// ~special case~ The paper assumes that SpecialExpr is an
// unconditional branch, then a lambda expression is set with the
// controlling predecessor. But we want to propagate the gate of that
// predecessor, so we collect the predecessor's gates.
Expand Down
32 changes: 17 additions & 15 deletions lib/ProgramSlice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ std::pair<Status, dataDependence> getDataDependencies(

// Helper to process operands
auto processOperand = [&](const Value *operand) {
if (isa<GlobalVariable>(operand)) { // ~special case~
if (isa<GlobalVariable>(operand)) {
status = {false, "Some dependency is on a Global Variable."};
return false;
}
Expand Down Expand Up @@ -672,13 +672,13 @@ void ProgramSlice::reconstructTerminator(BasicBlock &BB,
BranchInst::Create(targets[0], &BB);
updatePHINodesForSuccessor(targets[0], origBB, &BB);
} else {
Value *cond = getClonedOrUndef(origBranch->getCondition(), &BB);
Value *cond = getClonedCond(origBranch->getCondition());
BranchInst::Create(targets[0], targets[1], cond, &BB);
updatePHINodesForSuccessor(targets[0], origBB, &BB);
updatePHINodesForSuccessor(targets[1], origBB, &BB);
}
} else if (auto *origSwitch = dyn_cast<SwitchInst>(origTerm)) {
Value *cond = getClonedOrUndef(origSwitch->getCondition(), &BB);
Value *cond = getClonedCond(origSwitch->getCondition());
SwitchInst *newSwitch = SwitchInst::Create(cond, unreachableBlock,
origSwitch->getNumCases(), &BB);
for (auto &Case : origSwitch->cases()) {
Expand All @@ -690,7 +690,6 @@ void ProgramSlice::reconstructTerminator(BasicBlock &BB,
BasicBlock *def =
getOrCreateTargetBlock(origSwitch->getDefaultDest(), origBB, DT, PDT);
newSwitch->setDefaultDest(def ? def : unreachableBlock);
// Optionally, update PHI nodes here too
}
}

Expand Down Expand Up @@ -721,15 +720,17 @@ void ProgramSlice::rerouteTerminatorSuccessors(
updatePHINodesForSuccessor(target, origBB, &BB);
}
}
// Optionally handle other multi-successor terminators
}

Value *ProgramSlice::getClonedOrUndef(Value *origCond, BasicBlock *context) {
if (!origCond) return UndefValue::get(Type::getInt1Ty(context->getContext()));
Value *ProgramSlice::getClonedCond(Value *origCond) {
if (auto *inst = dyn_cast<Instruction>(origCond)) {
auto it = _origToNewInst.find(inst);
if (it != _origToNewInst.end()) return it->second;
}
if (auto *arg = dyn_cast<Argument>(origCond)) {
auto it = std::find(_depArgs.begin(), _depArgs.end(), arg);
if (it != _depArgs.end()) return *it;
}
return UndefValue::get(origCond->getType());
}

Expand All @@ -753,13 +754,8 @@ BasicBlock *ProgramSlice::getOrCreateTargetBlock(const BasicBlock *successor,
const BasicBlock *originalBB,
const DominatorTree &DT,
const PostDominatorTree &PDT) {
if (_loop && !_loop->contains(successor)) {
return nullptr;
}

BasicBlock *newTarget =
getNewTargetByFirstDominator(successor, originalBB, DT, PDT);

return newTarget;
}

Expand Down Expand Up @@ -838,11 +834,17 @@ BasicBlock *ProgramSlice::getNewTargetByFirstDominator(
if (!visited.insert(curBB).second) continue;
if (curBB == originalBB) continue; // skip the source block itself

// Skip traversing from loop latches or blocks outside the current loop
if (_loop) {
// Skip traversing from loop header or blocks outside the current loop
if (_loop && !_loop->isInvalid()) {
if (_loop->contains(curBB)) {
if (_loop->isLoopLatch(curBB)) continue;
if (_loop->getHeader() == curBB) {
LLVM_DEBUG(dbgs() << "\t\tCFG block is a loop header: "
<< curBB->getName() << "\n");
continue;
}
} else {
LLVM_DEBUG(dbgs() << "\t\tCFG block is not in loop: "
<< curBB->getName() << "\n");
continue;
}
}
Expand Down
2 changes: 0 additions & 2 deletions lib/daedalus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,6 @@ uint removeInstructions(const std::vector<SliceStruct> &allSlices,
const std::set<Function *> &mergeTo,
std::set<Function *> &toSimplify) {
std::set<Instruction *> toRemove;
std::map<Instruction *, Function *> newCalls;

uint dontMerge = 0;

for (const SliceStruct &slice : allSlices) {
Expand Down