+// DeclareNewGlobalValue - Patch up forward references to global values in the
+// form of ConstantPointerRef.
+//
+void BytecodeParser::DeclareNewGlobalValue(GlobalValue *GV, unsigned Slot) {
+ // Check to see if there is a forward reference to this global variable...
+ // if there is, eliminate it and patch the reference to use the new def'n.
+ GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), Slot));
+
+ if (I != GlobalRefs.end()) {
+ GlobalVariable *OldGV = I->second; // Get the placeholder...
+ BCR_TRACE(3, "Mutating CPPR Forward Ref!\n");
+
+ // Loop over all of the uses of the GlobalValue. The only thing they are
+ // allowed to be at this point is ConstantPointerRef's.
+ assert(OldGV->use_size() == 1 && "Only one reference should exist!");
+ while (!OldGV->use_empty()) {
+ User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
+ ConstantPointerRef *CPPR = cast<ConstantPointerRef>(U);
+ assert(CPPR->getValue() == OldGV && "Something isn't happy");
+
+ BCR_TRACE(4, "Mutating Forward Ref!\n");
+
+ // Change the const pool reference to point to the real global variable
+ // now. This should drop a use from the OldGV.
+ CPPR->mutateReference(GV);
+ }
+
+ // Remove GV from the module...
+ GV->getParent()->getGlobalList().remove(OldGV);
+ delete OldGV; // Delete the old placeholder
+
+ // Remove the map entry for the global now that it has been created...
+ GlobalRefs.erase(I);
+ }
+}