+/// findGlobalVariableBase - Recurse into a ConstantExpr to find the underlying
+/// GlobalVariable, if there is one.
+static GlobalVariable* findGlobalVariableBase(ConstantExpr* CE) {
+ if (isa<GlobalVariable>(CE))
+ return dyn_cast<GlobalVariable>(CE);
+ if (CE->getOpcode()==Instruction::GetElementPtr ||
+ CE->getOpcode()==Instruction::BitCast) {
+ if (isa<GlobalVariable>(CE->getOperand(0)))
+ return dyn_cast<GlobalVariable>(CE->getOperand(0));
+ if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(CE->getOperand(0)))
+ return findGlobalVariableBase(CE2);
+ }
+ return NULL;
+}