-//===----------------------------------------------------------------------===//
-
-/// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
-/// specified value in their initializer somewhere.
-static void
-getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
- // Scan though value users.
- for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
- if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
- // If the user is a GlobalVariable then add to result.
- Result.push_back(GV);
- } else if (Constant *C = dyn_cast<Constant>(*I)) {
- // If the user is a constant variable then scan its users
- getGlobalVariablesUsing(C, Result);
- }
- }
-}
-
-/// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
-/// named GlobalVariable.
-static void
-getGlobalVariablesUsing(Module &M, const std::string &RootName,
- std::vector<GlobalVariable*> &Result) {
- std::vector<const Type*> FieldTypes;
- FieldTypes.push_back(Type::Int32Ty);
- FieldTypes.push_back(Type::Int32Ty);
-
- // Get the GlobalVariable root.
- GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
- StructType::get(FieldTypes));
-
- // If present and linkonce then scan for users.
- if (UseRoot && UseRoot->hasLinkOnceLinkage())
- getGlobalVariablesUsing(UseRoot, Result);
-}
-
-/// isStringValue - Return true if the given value can be coerced to a string.
-///
-static bool isStringValue(Value *V) {
- if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
- if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
- ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
- return Init->isString();
- }
- } else if (Constant *C = dyn_cast<Constant>(V)) {
- if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
- return isStringValue(GV);
- else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
- if (CE->getOpcode() == Instruction::GetElementPtr) {
- if (CE->getNumOperands() == 3 &&
- cast<Constant>(CE->getOperand(1))->isNullValue() &&
- isa<ConstantInt>(CE->getOperand(2))) {
- return isStringValue(CE->getOperand(0));
- }
- }
- }
- }
- return false;
-}
-
-/// getGlobalVariable - Return either a direct or cast Global value.
-///
-static GlobalVariable *getGlobalVariable(Value *V) {
- if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
- return GV;
- } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
- if (CE->getOpcode() == Instruction::BitCast) {
- return dyn_cast<GlobalVariable>(CE->getOperand(0));
- } else if (CE->getOpcode() == Instruction::GetElementPtr) {
- for (unsigned int i=1; i<CE->getNumOperands(); i++) {
- if (!CE->getOperand(i)->isNullValue())
- return NULL;
- }
- return dyn_cast<GlobalVariable>(CE->getOperand(0));
- }
- }
- return NULL;
-}
-
-/// isGlobalVariable - Return true if the given value can be coerced to a
-/// GlobalVariable.
-static bool isGlobalVariable(Value *V) {
- if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
- return true;
- } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
- if (CE->getOpcode() == Instruction::BitCast) {
- return isa<GlobalVariable>(CE->getOperand(0));
- } else if (CE->getOpcode() == Instruction::GetElementPtr) {
- for (unsigned int i=1; i<CE->getNumOperands(); i++) {
- if (!CE->getOperand(i)->isNullValue())
- return false;
- }
- return isa<GlobalVariable>(CE->getOperand(0));
- }
- }
- return false;
-}
-
-/// getUIntOperand - Return ith operand if it is an unsigned integer.
-///
-static ConstantInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
- // Make sure the GlobalVariable has an initializer.
- if (!GV->hasInitializer()) return NULL;
-
- // Get the initializer constant.
- ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
- if (!CI) return NULL;
-
- // Check if there is at least i + 1 operands.
- unsigned N = CI->getNumOperands();
- if (i >= N) return NULL;
-
- // Check constant.
- return dyn_cast<ConstantInt>(CI->getOperand(i));
-}