Skip to content

Commit

Permalink
handle range attr in fn call return value
Browse files Browse the repository at this point in the history
  • Loading branch information
nunoplopes committed Apr 10, 2024
1 parent d85a46a commit 4bf3031
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions llvm_util/llvm2alive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,20 @@ class llvm2alive_ : public llvm::InstVisitor<llvm2alive_, unique_ptr<Instr>> {
}

call->setApproximated(approx);
return call;

unique_ptr<Instr> val = std::move(call);
auto range_check = [&](const auto &attr) {
auto *ptr = val.get();
BB->addInstr(std::move(val));
val = handleRangeAttrNoInsert(attr, *ptr);
};

if (fn_decl && fn_decl->hasRetAttribute(llvm::Attribute::Range))
range_check(fn_decl->getRetAttribute(llvm::Attribute::Range));
if (i.hasRetAttr(llvm::Attribute::Range))
range_check(i.getRetAttr(llvm::Attribute::Range));

return val;
}

RetTy visitMemSetInst(llvm::MemSetInst &i) {
Expand Down Expand Up @@ -820,8 +833,7 @@ class llvm2alive_ : public llvm::InstVisitor<llvm2alive_, unique_ptr<Instr>> {
if (!av)
return error(i);

BB->addInstr(
make_unique<Assume>(*av, Assume::WellDefined));
BB->addInstr(make_unique<Assume>(*av, Assume::WellDefined));
}
} else if (name == "align") {
llvm::Value *ptr = bundle.Inputs[0].get();
Expand Down Expand Up @@ -850,8 +862,7 @@ class llvm2alive_ : public llvm::InstVisitor<llvm2alive_, unique_ptr<Instr>> {
if (!aptr)
return error(i);

BB->addInstr(
make_unique<Assume>(*aptr, Assume::NonNull));
BB->addInstr(make_unique<Assume>(*aptr, Assume::NonNull));
} else {
return error(i);
}
Expand Down Expand Up @@ -1303,8 +1314,7 @@ class llvm2alive_ : public llvm::InstVisitor<llvm2alive_, unique_ptr<Instr>> {
? Assume::Dereferenceable : Assume::DereferenceableOrNull;
auto bytes = get_operand(
llvm::mdconst::extract<llvm::ConstantInt>(Node->getOperand(0)));
BB->addInstr(
make_unique<Assume>(vector<Value*>{i, bytes}, kind));
BB->addInstr(make_unique<Assume>(vector<Value*>{i, bytes}, kind));
break;
}

Expand All @@ -1331,13 +1341,18 @@ class llvm2alive_ : public llvm::InstVisitor<llvm2alive_, unique_ptr<Instr>> {
return true;
}

Value* handleRangeAttr(const llvm::Attribute &attr, Value &val) {
unique_ptr<Instr>
handleRangeAttrNoInsert(const llvm::Attribute &attr, Value &val) {
auto CR = attr.getValueAsConstantRange();
vector<Value *> bounds{ make_intconst(CR.getLower()),
make_intconst(CR.getUpper()) };
auto assume
= make_unique<AssumeVal>(val.getType(), "%#range_" + val.getName(), val,
std::move(bounds), AssumeVal::Range);
vector<Value*> bounds{ make_intconst(CR.getLower()),
make_intconst(CR.getUpper()) };
return
make_unique<AssumeVal>(val.getType(), "%#range_" + val.getName(), val,
std::move(bounds), AssumeVal::Range);
}

Value* handleRangeAttr(const llvm::Attribute &attr, Value &val) {
auto assume = handleRangeAttrNoInsert(attr, val);
auto ret = assume.get();
BB->addInstr(std::move(assume));
return ret;
Expand Down

0 comments on commit 4bf3031

Please sign in to comment.