9 #include <llvm/IR/BasicBlock.h>
10 #include <llvm/IR/CallSite.h>
11 #include <llvm/IR/Constants.h>
12 #include <llvm/IR/Function.h>
13 #include <llvm/IR/InstIterator.h>
14 #include <llvm/IR/Instruction.h>
15 #include <llvm/IR/Instructions.h>
16 #include <llvm/IR/Value.h>
18 #include <llvm/Support/Casting.h>
19 #include <llvm/Support/FileSystem.h>
20 #include <llvm/Support/Format.h>
21 #include <llvm/Support/raw_ostream.h>
23 #include <llvm/Analysis/MemoryBuiltins.h>
24 #include <llvm/Target/TargetLibraryInfo.h>
26 #include "Analysis/Bitcode/BitcodeUtils.h"
30 #include <unordered_map>
31 #include <unordered_set>
71 bool hasName()
const {
return Val->hasName(); }
94 if (isa<ConstantInt>(Val)) {
109 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
110 if (CI->getSExtValue() == constantInt) {
158 return isFreeCall(dyn_cast<Instruction>(Val), TLI);
166 assert(isInstruction() &&
"Not an instruction");
167 return isa<LoadInst>(Val);
174 assert(isInstruction() &&
"Not an instruction");
175 auto *Inst = dyn_cast<Instruction>(Val);
176 return Inst->getOpcode() == Instruction::UDiv ||
177 Inst->getOpcode() == Instruction::SDiv ||
178 Inst->getOpcode() == Instruction::FDiv ||
179 Inst->getOpcode() == Instruction::URem ||
180 Inst->getOpcode() == Instruction::SRem ||
181 Inst->getOpcode() == Instruction::FRem;
184 bool isConstantString() {
185 if (
auto *GV = dyn_cast<GlobalVariable>(Val)) {
186 if (!GV->hasInitializer()) {
189 Constant *CI = GV->getInitializer();
190 if (isa<ConstantDataArray>(CI) || isa<ConstantDataSequential>(CI) ||
191 isa<ConstantDataVector>(CI)) {
198 StringRef getConstantString() {
199 assert(isConstantString() &&
"Current SEGValue is not a constant string!");
200 if (
auto *GV = dyn_cast<GlobalVariable>(Val)) {
201 Constant *CI = GV->getInitializer();
202 if (
auto *CA = dyn_cast<ConstantDataArray>(CI)) {
204 }
else if (
auto *CS = dyn_cast<ConstantDataSequential>(CI)) {
206 }
else if (
auto *CV = dyn_cast<ConstantDataVector>(CI)) {
221 if (isInstruction()) {
222 auto *Inst = dyn_cast<Instruction>(Val);
223 return new SEGValue(Inst->getOperand(offset));
235 if (isInstruction()) {
236 kvec<Value *> DerefPtrs;
237 getDerefPtrFromInst(*(dyn_cast<Instruction>(Val)), DerefPtrs);
238 if (DerefPtrs.find(sValue->
getValue()) != DerefPtrs.end()) {
251 return isa<CallInst>(Val) || isa<InvokeInst>(Val);
258 return CallSite(dyn_cast<Instruction>(Val)).arg_size();
267 assert((isa<CallInst>(Val) || isa<InvokeInst>(Val)) &&
268 "Val is nullptr or neither a CallInst nor InvokeInst");
269 return CallSite(Val).getCalledFunction() !=
nullptr;
276 assert(Val && (isa<CallInst>(Val) || isa<InvokeInst>(Val)) &&
277 "Val is nullptr or neither a CallInst nor InvokeInst");
279 return CS.getCalledFunction();
286 assert(Val && (isa<CallInst>(Val) || isa<InvokeInst>(Val)) &&
287 "Val is nullptr or neither a CallInst nor InvokeInst");
288 Function *callee = CallSite(Val).getCalledFunction();
289 return callee->arg_size();
301 assert((isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) &&
302 "Val is nullptr or neither a CallInst nor InvokeInst");
304 return Val == CS.getArgument(offset);
311 assert(Val && (isa<CallInst>(Val) || isa<InvokeInst>(Val)) &&
312 "Val is nullptr or neither a CallInst nor InvokeInst");
314 Function *Func = CS.getCalledFunction();
315 if (Func !=
nullptr) {
316 return Func->getName();
321 bool hasFuncName()
const {
322 assert(Val && (isa<CallInst>(Val) || isa<InvokeInst>(Val)) &&
323 "Val is nullptr or neither a CallInst nor InvokeInst");
325 Function *Func = CS.getCalledFunction();
326 return Func && Func->hasName();
StringRef getValueName() const
Get the name of Value.
Definition: SEGValue.h:76
bool isFuncNthArgument(SEGValue *sValue, size_t offset) const
Determine whether the Value is one of the arguments of callee function.
Definition: SEGValue.h:299
bool isCallOrInvoke() const
Determine whether CallSite is a CallInst or a InvokeInst.
Definition: SEGValue.h:250
bool isDerefPtr(SEGValue *sValue) const
Determine whether the pointer in a dereference instruction points to the specific Value.
Definition: SEGValue.h:234
bool isEqualValue(SEGValue *sValue) const
Determine whether Value equals to the other Value.
Definition: SEGValue.h:83
bool hasCalledFunction() const
Determine whether CallSite has a callee function.
Definition: SEGValue.h:266
bool isLoadInst() const
Determine whether it is a load instruction.
Definition: SEGValue.h:165
SEGValue * getInstOperand(size_t offset)
Get the target operand of instruction.
Definition: SEGValue.h:220
size_t getFuncArgSize() const
Get the argument size of Function.
Definition: SEGValue.h:285
~SEGValue()
Destructor.
Definition: SEGValue.h:59
StringRef getFuncName() const
Get the fucntion name.
Definition: SEGValue.h:310
bool isDivInst() const
Determine whether it is an instruction about division.
Definition: SEGValue.h:173
Value * getValue()
Get Value.
Definition: SEGValue.h:66
bool isPointerType() const
Determine whether the type of Value is pointer.
Definition: SEGValue.h:127
Definition: SymbolicExprGraph.h:76
bool hasName() const
Determine whether Value has its name.
Definition: SEGValue.h:71
bool isSame(SEGValue *sValue) const
Determine whether the instruction equals to the other instruction.
Definition: SEGValue.h:146
Definition: SEGValue.h:38
bool isInstruction() const
Determine whether the type of Value is instruction.
Definition: SEGValue.h:139
Function * getCalledFunction() const
Get the callee function of CallSite.
Definition: SEGValue.h:275
bool isConstantPointerNull() const
Determine whether the type of Value is pointer.
Definition: SEGValue.h:132
bool isConstantInt() const
Determine whether Value is a Constant int.
Definition: SEGValue.h:90
size_t getCallSiteArgSize() const
Get the argument size of CallSite.
Definition: SEGValue.h:257
bool isUndefined() const
Determine whether Value is undefined.
Definition: SEGValue.h:122
bool isConstantInt(int constantInt) const
Determine whether Value is a Constant int.
Definition: SEGValue.h:105
Definition: SymbolicExprGraph.h:663
bool isPhiInst() const
Determine whether it is a PHI instruction.
Definition: SEGValue.h:151
SEGValue(Value *val)
Constructor.
Definition: SEGValue.h:57
The node base of symbolic expression graph.
Definition: SymbolicExprGraph.h:244
bool isFreeInst(TargetLibraryInfo *TLI) const
Determine whether it is a free instruction.
Definition: SEGValue.h:156