Help to identify the type of instruction

Hi all,
so I’m working on a simple LLVM plugin and I need some help to identify the type of instruction. The ll is roughly the following:

; ModuleID = 'globalString.bc'
source_filename = "llvm-link"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

@.str = private unnamed_addr constant [12 x i8] c"Hello Hello\00", align 1
@string = dso_local global i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i32 0, i32 0), align 8

; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32 @main() #0 {
...
%3 = load i8*, i8** @string, align 8
...

Inside the visitLoadinst of my plugin I added the following code:

if (GlobalVariable *G = dyn_cast<GlobalVariable>(I.getOperand(0))) {
    if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(G->getOperand(0))) {
        I.dump();
    } else {
        std::cout << "Unsupported!" << "\n";
        G->getOperand(0)->dump();
        G->getOperand(0)->getType()->dump();
        I.dump();
   }
}

Unfortunately I can only see the Unsupported message so it means it cannot be casted to a GEP.

What kind of cast should I use in this case? I have a feeling that the cast does not work because it is not a GEP instruction but some sort of pointer to a GEP.

Thanks
Alberto

Hi,

The GEP in the global is a constant expression (see LLVM Language Reference Manual — LLVM 16.0.0git documentation). I think you should be able to use cast<GEPOperator> to match both GEP instructions and constant expressions

(adding the full response as the email import included only the first line)

Hi @fhahn ,
I’m sorry about the late reply. Indee the use of cast<GEPOperator> worked and it allowed me to understand if an operator is a GEP. Also, I’m now able to get the reference to str via GEP->getOperand(0) and cast it to Constant but I don’t know how to get the actual value ( e.g. Hello Hello ) in this case.

Any help on that?

Thanks