#include “DivZeroAnalysis.h”
#include “Utils.h”
namespace dataflow {
Copyright By PowCoder代写 加微信 powcoder
* @brief Is the given instruction a user input?
* @param Inst The instruction to check.
* @return true If it is a user input, false otherwise.
bool isInput(Instruction *Inst) {
if (auto Call = dyn_cast
if (auto Fun = Call->getCalledFunction()) {
return (Fun->getName().equals(“getchar”) ||
Fun->getName().equals(“fgetc”));
return false;
* Evaluate a PHINode to get its Domain.
* @param Phi PHINode to evaluate
* @param InMem InMemory of Phi
* @return Domain of Phi
Domain *eval(PHINode *Phi, const Memory *InMem) {
if (auto ConstantVal = Phi->hasConstantValue()) {
return new Domain(extractFromValue(ConstantVal));
Domain *Joined = new Domain(Domain::Uninit);
for (unsigned int i = 0; i < Phi->getNumIncomingValues(); i++) {
auto Dom = getOrExtract(InMem, Phi->getIncomingValue(i));
Joined = Domain::join(Joined, Dom);
return Joined;
* @brief Evaluate the +, -, * and / BinaryOperator instructions
* using the Domain of its operands and return the Domain of the result.
* @param BinOp BinaryOperator to evaluate
* @param InMem InMemory of BinOp
* @return Domain of BinOp
Domain *eval(BinaryOperator *BinOp, const Memory *InMem) {
* TODO: Write your code here that evaluates +, -, * and /
* based on the Domains of the operands.
return NULL;
* @brief Evaluate Cast instructions.
* @param Cast Cast instruction to evaluate
* @param InMem InMemory of Instruction
* @return Domain of Cast
Domain *eval(CastInst *Cast, const Memory *InMem) {
* TODO: Write your code here to evaluate Cast instruction.
return NULL;
* @brief Evaluate the ==, !=, <, <=, >=, and > Comparision operators using
* the Domain of its operands to compute the Domain of the result.
* @param instruction to evaluate
* @param InMem InMemory of Cmp
* @return Domain of Cmp
Domain *eval(CmpInst *Cmp, const Memory *InMem) {
* TODO: Write your code here that evaluates:
* ==, !=, <, <=, >=, and > based on the Domains of the operands.
* NOTE: There is a lot of scope for refining this, but you can just return
* MaybeZero for comparisons other than equality.
return NULL;
void DivZeroAnalysis::transfer(Instruction *Inst, const Memory *In,
Memory &NOut, PointerAnalysis *PA,
SetVector
if (isInput(Inst)) {
// The instruction is a user controlled input, it can have any value.
NOut[variable(Inst)] = new Domain(Domain::MaybeZero);
} else if (auto Phi = dyn_cast
// Evaluate PHI node
NOut[variable(Phi)] = eval(Phi, In);
} else if (auto BinOp = dyn_cast
// Evaluate BinaryOperator
NOut[variable(BinOp)] = eval(BinOp, In);
} else if (auto Cast = dyn_cast
// Evaluate Cast instruction
NOut[variable(Cast)] = eval(Cast, In);
} else if (auto Cmp = dyn_cast
// Evaluate Comparision instruction
NOut[variable(Cmp)] = eval(Cmp, In);
} else if (auto Alloca = dyn_cast
// Do nothing here.
} else if (auto Store = dyn_cast
* TODO: Store instruction can either add new variables or overwrite existing variables into memory maps.
* To update the memory map, we rely on the points-to graph constructed in PointerAnalysis.
* To build the abstract memory map, you need to ensure all pointer references are in-sync, and
* will converge upon a precise abstract value. To achieve this, implement the following workflow:
* Iterate through the provided PointerSet:
* – If there is a may-alias (i.e., `alias()` returns true) between two variables:
* + Get the abstract values of each variable.
* + Join the abstract values using Domain::join().
* + Update the memory map for the current assignment with the joined abstract value.
* + Update the memory map for all may-alias assignments with the joined abstract value.
* Hint: You may find getOperand(), getValueOperand(), and getPointerOperand() useful.
} else if (auto Load = dyn_cast
* TODO: Rely on the existing variables defined within the `In` memory to
* know what abstract domain should be for the new variable
* introduced by a load instruction.
* If the memory map already contains the variable, propagate the existing
* abstract value to NOut.
* Otherwise, initialize the memory map for it.
* Hint: You may use getPointerOperand().
} else if (auto Branch = dyn_cast
// Analysis is flow-insensitive, so do nothing here.
} else if (auto Call = dyn_cast
* TODO: Populate the NOut with an appropriate abstract domain.
* You only need to consider calls with int return type.
} else if (auto Return = dyn_cast
// Analysis is intra-procedural, so do nothing here.
errs() << "Unhandled instruction: " << *Inst << "\n";
} // namespace dataflow
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com