-/// getUnderlyingObject - This traverses the use chain to figure out what object
-/// the specified value points to. If the value points to, or is derived from,
-/// a global object, return it.
-static Value *getUnderlyingObject(Value *V) {
- if (!isa<PointerType>(V->getType())) return V;
-
- // If we are at some type of object... return it.
- if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV;
-
- // Traverse through different addressing mechanisms.
- if (Instruction *I = dyn_cast<Instruction>(V)) {
- if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I))
- return getUnderlyingObject(I->getOperand(0));
- } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
- if (CE->getOpcode() == Instruction::BitCast ||
- CE->getOpcode() == Instruction::GetElementPtr)
- return getUnderlyingObject(CE->getOperand(0));
- }
-
- // Otherwise, we don't know what this is, return it as the base pointer.
- return V;
-}
-