GlobalVariable Recursive loop

Hi,

I have implemented a recursive loop to go through all the user of a global variable but I have one issue left.

Depending the code, the global variable is using itself:

@sSelectedAccount = internal constant %struct.KVObserver { %0* bitcast (%struct.__NSConstantString_tag* @unnamed_cfstring.744 to %0*), i8* bitcast (%struct.KVObserver* @sSelectedAccount to i8*), i32 3 }, align 4

With the IR code above, I am stuck in an infinite loop:

  1. The global sSelectedAccount has the following user: “i8* bitcast (%struct.KVObserver* @sSelectedAccount to i8*)
  2. The bit cast has the following user: “%struct.KVObserver { %“type 0x7ff164027360”* bitcast (%struct.__NSConstantString_tag* @unnamed_cfstring.744 to %“type 0x7ff164027360”), i8 bitcast (%struct.KVObserver* @sSelectedAccount to i8*), i32 3 }
  3. The previous has the user sSelectedAccount → so this create the infinite loop.

I was looking for a way to detect the loop and I think there is two possibilities:

  • from the global value, iterate through all the element and find the loop, but this need a lot of cast and another recursive loop (need to handle ConstantStruct, ConstantExpr, ConstantArray, etc.) Does anyone know if there is an easier way to do this?
    -during the recursive loop, can I find the “main” global value? e.g: when I am on "i8* bitcast (%struct.KVObserver* @sSelectedAccount to i8*)”, how can I get the global variable sSelectedAccount ?

Greetings,

Johan

What about keeping track of “visited” global in a side structure (hash map…) and skip these?

Note also that only GlobalValue can create cycles, so you’d need only to track these, not all constant.

Hi Mehdi,

What about keeping track of “visited” global in a side structure (hash map…) and skip these?

Note also that only GlobalValue can create cycles, so you’d need only to track these, not all constant.

Thanks for the proposition, I will try that.

Greetings,

Johan