Razer
Razer

Reputation: 8201

How to determine if a llvm:Type is of type i8*?

How to determine if a llvm:Type is of type i8*? I iterate over the arguments of a function F and want to determine if an argument is of type i8*.

for(auto& arg : F.getArgumentList()) {
    arg.getType()->???

}

Upvotes: 1

Views: 2759

Answers (2)

Regis Portalez
Regis Portalez

Reputation: 4845

You could use llvm::isa or use high level cast such as llvm::cast.

Otherwise, you can just make plain old C++ : [not tested]

void Test(llvm::Function* f) {
        for (auto& arg : f->getArgumentList()) {
            llvm::Type* t = arg.getType();
            if (t->isPointerTy()) {
                llvm::Type* inner = t->getPointerElementType();
                if (inner->isIntegerTy()) {
                    llvm::IntegerType* it = (llvm::IntegerType*) inner;
                    if (it->getBitWidth() == 8) {
                        // i8* --> do something
                    }
                    // another int pointer (int32* for example)
                }
                // another pointer type
            }
            // not pointer type
        }
    }

Upvotes: 2

user7016017
user7016017

Reputation:

A compact way is to perform llvm::dyn_cast:

... // using namespace llvm
if (PointerType *PT = dyn_cast<PointerType>(arg.getType()))
    if (IntegerType *IT = dyn_cast<IntegerType>(PT->getPointerElementType()))
        if (IT->getBitWidth() == 8)
            // do stuff
...

. Note that types that aren't identified structures are uniqued structurally in LLVM IR. If you have a handle on the LLVMContext you can compare the pointer of the argument type to the built-in 8-bit int pointer:

... //using namespace llvm
if (PointerType *PT = dyn_cast<PointerType>(arg.getType()))
    if (PT == Type::getInt8PtrTy(ctx, PT->getPointerAddressSpace()))
        // do stuff
...

.

Upvotes: 2

Related Questions