diff --git a/include/Token/Token.h b/include/Token/Token.h index 9f0a526..f737509 100644 --- a/include/Token/Token.h +++ b/include/Token/Token.h @@ -2,7 +2,6 @@ #define TOKEN_H #include "string" -#include "llvm/ADT/BitVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/IR/Argument.h" #include "llvm/IR/Function.h" @@ -11,19 +10,11 @@ #include "llvm/IR/Type.h" #include "llvm/IR/Value.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/ADT/BitVector.h" namespace spatial { -enum opTokenTy { - isArray, - isAlloca, - isOpBitcast, - isPhiGEPOpd, - isIcmpGEPOpd, - isOneGEPIndx, - isFunPtr, - isFunArg -}; +enum opTokenTy {isArray, isAlloca, isOpBitcast, isPhiGEPOpd, isIcmpGEPOpd, isOneGEPIndx, isFunPtr, isFunArg, isRetGEP}; class Token { private: @@ -50,16 +41,17 @@ class Token { llvm::BitVector opTokenTy; unsigned int TyLength; + public: void setIndex(llvm::GetElementPtrInst *GEPInst); void setIndex(llvm::GEPOperator *GEPOp); - void setIndex(Token *, std::string); - void setIndex(Token *); + void setIndex(Token*, std::string); + void setIndex(Token*); void resetIndex(); void resetIndexToZero(); void resetIndexToZero(std::string); std::string getIndex(llvm::GEPOperator *GEPOp); - + Token(llvm::Value *Val, std::string Index = ""); Token(llvm::GEPOperator *GOP, llvm::Function *Func, std::string Index = ""); Token(llvm::Argument *Arg, std::string Index = ""); @@ -73,7 +65,7 @@ class Token { std::string getMemTypeName() const; std::string getFunctionName() const; std::string getFieldIndex() const; - + friend std::ostream &operator<<(std::ostream &OS, const Token &A); bool isMem() const; @@ -93,10 +85,10 @@ class Token { void setIsGlobal(); void setIsArray(); bool getIsArray(); - void setIsAlloca(); + void setIsAlloca(); bool getIsAlloca(); void setIsOpBitcast(); - bool getIsOpBitcast(); + bool getIsOpBitcast(); void setIsPhiGEPOpd(); bool getIsPhiGEPOpd(); void setIsIcmpGEPOpd(); @@ -107,16 +99,18 @@ class Token { bool getIsFunPtr(); void setIsFunArg(); bool getIsFunArg(); + void setIsRetGEP(); + bool getIsRetGEP(); template bool isGEPOperandArrayTy(GOP *, int); - template std::vector getGEPArrayIndex(GEP *); + template std::vector getGEPArrayIndex(GEP*); bool isNullToken(); void setNullToken(); - llvm::Type *getTy(); - void setTy(llvm::Type *); - void setFunction(llvm::Function *); - llvm::Function *getFunction(); + llvm::Type* getTy(); + void setTy(llvm::Type*); + void setFunction(llvm::Function*); + llvm::Function* getFunction(); }; } // namespace spatial diff --git a/lib/Token/Token.cpp b/lib/Token/Token.cpp index 0bacd9c..130ed97 100644 --- a/lib/Token/Token.cpp +++ b/lib/Token/Token.cpp @@ -3,8 +3,8 @@ namespace spatial { Token::Token() { - this->TyLength = 8; - this->opTokenTy = llvm::BitVector(this->TyLength, false); + this->TyLength = 9; + this->opTokenTy = llvm::BitVector(this->TyLength, false); } void Token::set(llvm::Value *Val, unsigned int Kind, std::string Index, @@ -17,7 +17,7 @@ void Token::set(llvm::Value *Val, unsigned int Kind, std::string Index, this->IsGlobal = Global; if (!Func) this->IsGlobal = true; -} + } void Token::set(llvm::Type *Ty, unsigned int Kind, std::string Index) { this->Ty = Ty; @@ -48,17 +48,18 @@ void Token::set(std::string S, unsigned int Kind, std::string Index, this->Func = Func; } -Token::Token(llvm::Value *Val, std::string Index) { - this->TyLength = 8; +Token::Token(llvm::Value *Val, std::string Index) { + this->TyLength = 9; this->opTokenTy = llvm::BitVector(this->TyLength, false); - if (llvm::Argument *Arg = llvm::dyn_cast(Val)) { + + if (llvm::Argument *Arg = llvm::dyn_cast(Val)) { set(Arg, /* Kind = */ 2, Index, Arg->getParent()); this->setIsGlobal(); this->setIsFunArg(); - } else { + } else { // If Val is GEPOperator, it must be coming from load/store - if (llvm::GEPOperator *GOP = llvm::dyn_cast(Val)) { + if (llvm::GEPOperator *GOP = llvm::dyn_cast(Val)) { // We handle GEPInst separately if (!llvm::isa(GOP)) { Val = GOP->getPointerOperand(); @@ -73,41 +74,44 @@ Token::Token(llvm::Value *Val, std::string Index) { else set(Val, /* Kind = */ 0, Index, func); } - if (Val->getName() == "") { // Operand could be NULL - this->Kind = 3; - this->name = "NULL"; - // NULL operand is set as Global + + if( Val->getName() == "") { //Operand could be NULL + this->Kind = 3; + this->name = "NULL"; + //NULL operand is set as Global } + } -Token::Token(llvm::GEPOperator *GOP, llvm::Function *Func, std::string Index) { - this->TyLength = 8; +Token::Token(llvm::GEPOperator *GOP, llvm::Function *Func, std::string Index) { + this->TyLength = 9; this->opTokenTy = llvm::BitVector(this->TyLength, false); + llvm::Value *Val = GOP->getPointerOperand(); Index = this->getIndex(GOP); set(Val, /* Kind = */ 0, Index, Func); } -Token::Token(llvm::Argument *Arg, std::string Index) { - this->TyLength = 8; - this->opTokenTy = llvm::BitVector(this->TyLength, false); - set(Arg, /* Kind = */ 2, Index, Arg->getParent()); +Token::Token(llvm::Argument *Arg, std::string Index) { + this->TyLength = 9; + this->opTokenTy = llvm::BitVector(this->TyLength, false); + set(Arg, /* Kind = */ 2, Index, Arg->getParent()); } Token::Token(llvm::Type *Ty, std::string Index) { - this->TyLength = 8; - this->opTokenTy = llvm::BitVector(this->TyLength, false); - set(Ty, /* Kind = */ 1, Index); + this->TyLength = 9; + this->opTokenTy = llvm::BitVector(this->TyLength, false); + set(Ty, /* Kind = */ 1, Index); } -Token::Token(std::string S, llvm::Function *Func, std::string Index) { - this->TyLength = 8; +Token::Token(std::string S, llvm::Function *Func, std::string Index) { + this->TyLength = 9; this->opTokenTy = llvm::BitVector(this->TyLength, false); set(S, /* Kind = */ 3, Index, Func); } Token::Token(Token *A) { - this->TyLength = 8; + this->TyLength = 9; this->opTokenTy = llvm::BitVector(this->TyLength, false); unsigned int Kind = A->Kind; if (Kind == 0) { @@ -133,14 +137,18 @@ void Token::setIndex(llvm::GEPOperator *GEPOp) { this->Index = getIndex(GEPOp); } + /// setIndex - For a Token. Required in case of nested structures /// wherein the token belongs to a chain of GEP instructions void Token::setIndex(Token *T, std::string indx) { this->Index = indx + T->Index; } -/// setIndex - For a Token -void Token::setIndex(Token *T) { this->Index = T->Index; } + +/// setIndex - For a Token +void Token::setIndex(Token *T) { + this->Index = T->Index; +} /// resetIndex - Resets the index back to an empty string void Token::resetIndex() { this->Index = ""; } @@ -156,46 +164,42 @@ std::string Token::getIndex(llvm::GEPOperator *GEPOp) { auto Iter = GEPOp->idx_begin(); std::string Index = "["; - while (Iter != GEPOp->idx_end()) { - + while (Iter != GEPOp->idx_end()) { llvm::Value *temp = &(*Iter->get()); - if (llvm::ConstantInt *CI = llvm::dyn_cast(temp)) + if (llvm::ConstantInt *CI = llvm::dyn_cast(temp)) Index += CI->getValue().toString(10, true) + "]["; - else - Index += temp->getName().str() + "]["; + else + Index += temp->getName().str() + "]["; Iter++; } return Index.substr(0, Index.size() - 1); } + template bool Token::isGEPOperandArrayTy(GOP *G, int Idx) { llvm::Type *StructType = G->getOperand(0)->getType(); - if (StructType->getNumContainedTypes() > 0) - StructType = StructType->getContainedType(0); + if(StructType->getNumContainedTypes() > 0) + StructType = StructType->getContainedType(0); int OpNum = G->getNumOperands(); Idx += 1; - if (Idx > 1) - OpNum = Idx; + if(Idx > 1) + OpNum = Idx; for (int i = 2; i < OpNum; i++) { - while (StructType->isArrayTy()) { - StructType = StructType->getArrayElementType(); - i = i + 1; + while(StructType->isArrayTy()){ + StructType = StructType->getArrayElementType(); + i = i + 1; } - if (i >= OpNum) - break; + if(i >= OpNum) break; llvm::Value *IdxV = G->getOperand(i); - llvm::ConstantInt *Idx = llvm::cast(IdxV); + llvm::ConstantInt *Idx = llvm::cast(IdxV); StructType = StructType->getStructElementType(Idx->getSExtValue()); } return StructType->isArrayTy(); } -template bool -Token::isGEPOperandArrayTy(llvm::GetElementPtrInst *G, - int Idx = -1); -template bool -Token::isGEPOperandArrayTy(llvm::GEPOperator *G, - int Idx = -1); +template bool Token::isGEPOperandArrayTy( + llvm::GetElementPtrInst *G, int Idx = -1); +template bool Token::isGEPOperandArrayTy(llvm::GEPOperator *G, int Idx = -1); /// getValue - Returns the underlying Value* for the alias llvm::Value *Token::getValue() const { @@ -297,9 +301,9 @@ bool Token::isBasePointerType() const { } bool Token::isValPointerType() const { - if (this->Ty->isPointerTy()) - return true; - return false; + if (this->Ty->isPointerTy()) + return true; + return false; } /// getHash - Calculates the hash for alias to avoid multiple enteries of same @@ -311,16 +315,16 @@ std::string Token::getHash() const { hash += this->getName().str(); hash += this->getFunctionName(); hash += this->getMemTypeName(); - hash += this->getFieldIndex(); + hash += this->getFieldIndex(); return hash; } bool Token::operator<(const Token &TheToken) const { - return (this->getHash() < TheToken.getHash()); + return (this->getHash() < TheToken.getHash()); } bool Token::operator==(const Token &TheToken) const { - return (this->getHash() == TheToken.getHash()); + return (this->getHash() == TheToken.getHash()); } void Token::operator=(const Token &TheToken) { @@ -336,67 +340,71 @@ void Token::operator=(const Token &TheToken) { } } -void Token::setIsGlobal() { this->IsGlobal = true; } + void Token::setIsGlobal() { this->IsGlobal = true; } -void Token::setIsArray() { this->opTokenTy.set(isArray); } + void Token::setIsArray() { this->opTokenTy.set(isArray); } -bool Token::getIsArray() { return opTokenTy.test(isArray); } + bool Token::getIsArray() { return opTokenTy.test(isArray); } -void Token::setIsAlloca() { this->opTokenTy.set(isAlloca); } + void Token::setIsAlloca() { this->opTokenTy.set(isAlloca); } + + bool Token::getIsAlloca() { return opTokenTy.test(isAlloca); } -bool Token::getIsAlloca() { return opTokenTy.test(isAlloca); } + void Token::setIsOpBitcast() { this->opTokenTy.set(isOpBitcast); } -void Token::setIsOpBitcast() { this->opTokenTy.set(isOpBitcast); } + bool Token::getIsOpBitcast() { return opTokenTy.test(isOpBitcast); } -bool Token::getIsOpBitcast() { return opTokenTy.test(isOpBitcast); } + void Token::setIsPhiGEPOpd() { this->opTokenTy.set(isPhiGEPOpd); } -void Token::setIsPhiGEPOpd() { this->opTokenTy.set(isPhiGEPOpd); } + bool Token::getIsPhiGEPOpd() { return opTokenTy.test(isPhiGEPOpd); } -bool Token::getIsPhiGEPOpd() { return opTokenTy.test(isPhiGEPOpd); } + void Token::setIsIcmpGEPOpd() { this->opTokenTy.set(isIcmpGEPOpd); } -void Token::setIsIcmpGEPOpd() { this->opTokenTy.set(isIcmpGEPOpd); } + bool Token::getIsIcmpGEPOpd() { return opTokenTy.test(isIcmpGEPOpd); } -bool Token::getIsIcmpGEPOpd() { return opTokenTy.test(isIcmpGEPOpd); } + void Token::setIsOneGEPIndx() { this->opTokenTy.set(isOneGEPIndx); } -void Token::setIsOneGEPIndx() { this->opTokenTy.set(isOneGEPIndx); } + bool Token::getIsOneGEPIndx() { return opTokenTy.test(isOneGEPIndx); } -bool Token::getIsOneGEPIndx() { return opTokenTy.test(isOneGEPIndx); } + void Token::setIsFunPtr() { this->opTokenTy.set(isFunPtr); } -void Token::setIsFunPtr() { this->opTokenTy.set(isFunPtr); } + bool Token::getIsFunPtr() { return opTokenTy.test(isFunPtr); } -bool Token::getIsFunPtr() { return opTokenTy.test(isFunPtr); } + void Token::setIsFunArg() { this->opTokenTy.set(isFunArg); } -void Token::setIsFunArg() { this->opTokenTy.set(isFunArg); } + bool Token::getIsFunArg() { return opTokenTy.test(isFunArg); } -bool Token::getIsFunArg() { return opTokenTy.test(isFunArg); } + void Token::setIsRetGEP() { this->opTokenTy.set(isRetGEP); } + + bool Token::getIsRetGEP() { return opTokenTy.test(isRetGEP); } template std::vector Token::getGEPArrayIndex(GOP *G) { std::vector Idx; for (int i = 2; i < G->getNumOperands(); i++) { - if (isGEPOperandArrayTy(G, i - 1)) { - Idx.push_back(i); - } + if(isGEPOperandArrayTy(G, i - 1)){ + Idx.push_back(i); + } } return Idx; } -template std::vector -Token::getGEPArrayIndex(llvm::GetElementPtrInst *G); -template std::vector -Token::getGEPArrayIndex(llvm::GEPOperator *G); +template std::vector Token::getGEPArrayIndex(llvm::GetElementPtrInst *G); +template std::vector Token::getGEPArrayIndex(llvm::GEPOperator *G); -bool Token::isNullToken() { - if (this->name == "NULL" and this->Kind == 3) - return true; - return false; -} -llvm::Type *Token::getTy() { return this->Ty; } + bool Token::isNullToken() { + if(this->name == "NULL" and this->Kind == 3) + return true; + return false; + } -void Token::setTy(llvm::Type *type) { this->Ty = type; } + llvm::Type* Token::getTy() { return this->Ty;} -void Token::setFunction(llvm::Function *F) { this->Func = F; } + void Token::setTy(llvm::Type* type) { this->Ty = type;} -llvm::Function *Token::getFunction() { return this->Func; } + void Token::setFunction(llvm::Function* F) { this->Func = F; } + + llvm::Function* Token::getFunction() { return this->Func; } } // namespace spatial +