title: How to re-initialize an llvm::GlobalVariable
body: <p>I 'm trying to create a function in llvm IR which re-assigns all <strong>llvm::GlobalVariable</strong> to their initial values.  </p>
  
  <p>I tried to tackle the problem by first iterating over all <strong>llvm::GlobalVariable</strong> of an <strong>llvm::Module</strong> and then, for each <strong>llvm::GlobalVariable</strong> which has an initializer and isn't a constant, to add to my new function an <strong>llvm::StoreInst</strong> which will perform the assignment.</p>
  
  <p>My problem is that <strong>llvm::GlobalVariable::getInitializer()</strong> returns an  <strong>*llvm::Constant</strong> which is a base class and I am unsure of how to get access to the initialization values especially for custom structs and classes. Should I use some sort of downcasting or is there any cleaner solution ?</p>
  
  <p>This is my code so far which compiles well but crashes on runtime:  (<em>Assertion `getOperand(1)->getType()->isPointerTy() &amp;&amp; "Ptr must have pointer type!"' failed.</em>)</p>
  
  <pre><code>Module::GlobalListType* glist = &amp;M.getGlobalList();
  
  for(auto it=glist-&gt;begin(); it!=glist-&gt;end(); ++it){
  
    if(it-&gt;hasInitializer() &amp;&amp; !it-&gt;isConstant()){
      Constant* con = it-&gt;getInitializer();
      StoreInst(it, con, RET);//RET is a pointer to an existing instruction.
    }
  
  }
  </code></pre>
  
  <p>Thanks for your time!</p>