Get and compare global variables in pass

First, there is a global array variable in the program GLOBAL_VAL , the content type it stores is i8 *
After some operations, there may be some data stored in the array, and I want to iterate to get the elements that are not currently stored.
But I don’t know how to implement data comparison in pass, I tried the following method, but obviously, it is not correct.

int start_index = 0;
Value *test = IRB.getInt32(start_index);
Value *GV = IRB.CreateGEP(GLOBAL_VAL, test);
Value* gv = IRB.CreateInBoundsGEP(GV, ArrayRef<Value*>(test_element_2,2));
Value* gvtest = IRB.CreateZExtOrBitCast(gv, Type::getInt8PtrTy(*Ctx));

/* I can ensure that the above statement is correct to get the global variable, 
 * but how to compare whether it has been occupied (stored data) I am not clear how to do */

while(gvtest != NULL)
{
    start_index++;
    /*same code*/
}