- GlobalIsNeeded(GV);
- else {
- // Loop over all of the operands of the constant, adding any globals they
- // use to the list of needed globals.
- for (User::op_iterator I = C->op_begin(), E = C->op_end(); I != E; ++I)
- MarkUsedGlobalsAsNeeded(cast<Constant>(*I));
+ return GlobalIsNeeded(GV);
+
+ // Loop over all of the operands of the constant, adding any globals they
+ // use to the list of needed globals.
+ for (User::op_iterator I = C->op_begin(), E = C->op_end(); I != E; ++I) {
+ // If we've already processed this constant there's no need to do it again.
+ Constant *Op = dyn_cast<Constant>(*I);
+ if (Op && SeenConstants.insert(Op))
+ MarkUsedGlobalsAsNeeded(Op);
+ }
+}
+
+bool GlobalDCE::ContainsUsedGlobal(Constant *C) {
+ // C contains a used global If C is alive or we visited it while marking
+ // values alive.
+ if (AliveGlobals.count(C) || SeenConstants.count(C))
+ return true;
+
+ // Now check all operands of a ConstantExpr.
+ for (User::op_iterator I = C->op_begin(), E = C->op_end(); I != E; ++I) {
+ Constant *Op = dyn_cast<Constant>(*I);
+ if (Op && ContainsUsedGlobal(Op))
+ return true;