[IR] Implement pairs of non-const and const methods using the const version instead of the non-const version. NFCI

This removes a const_cast of the this pointer.

llvm-svn: 298831
This commit is contained in:
Craig Topper 2017-03-27 05:47:03 +00:00
parent 1c78f4a0ff
commit 1dd20e6544
10 changed files with 100 additions and 74 deletions

View File

@ -152,12 +152,13 @@ public:
/// hanging off of the globals.
void removeDeadConstantUsers() const;
Constant *stripPointerCasts() {
const Constant *stripPointerCasts() const {
return cast<Constant>(Value::stripPointerCasts());
}
const Constant *stripPointerCasts() const {
return const_cast<Constant*>(this)->stripPointerCasts();
Constant *stripPointerCasts() {
return const_cast<Constant*>(
static_cast<const Constant *>(this)->stripPointerCasts());
}
};

View File

@ -48,27 +48,31 @@ public:
setOperand(0, Symbol);
}
const Constant *getIndirectSymbol() const {
return const_cast<GlobalIndirectSymbol *>(this)->getIndirectSymbol();
return getOperand(0);
}
Constant *getIndirectSymbol() {
return getOperand(0);
return const_cast<Constant *>(
static_cast<const GlobalIndirectSymbol *>(this)->getIndirectSymbol());
}
const GlobalObject *getBaseObject() const {
return const_cast<GlobalIndirectSymbol *>(this)->getBaseObject();
return dyn_cast<GlobalObject>(getIndirectSymbol()->stripInBoundsOffsets());
}
GlobalObject *getBaseObject() {
return dyn_cast<GlobalObject>(getIndirectSymbol()->stripInBoundsOffsets());
return const_cast<GlobalObject *>(
static_cast<const GlobalIndirectSymbol *>(this)->getBaseObject());
}
const GlobalObject *getBaseObject(const DataLayout &DL, APInt &Offset) const {
return const_cast<GlobalIndirectSymbol *>(this)->getBaseObject(DL, Offset);
}
GlobalObject *getBaseObject(const DataLayout &DL, APInt &Offset) {
return dyn_cast<GlobalObject>(
getIndirectSymbol()->stripAndAccumulateInBoundsConstantOffsets(DL,
Offset));
}
GlobalObject *getBaseObject(const DataLayout &DL, APInt &Offset) {
return const_cast<GlobalObject *>(
static_cast<const GlobalIndirectSymbol *>(this)
->getBaseObject(DL, Offset));
}
// Methods for support type inquiry through isa, cast, and dyn_cast:
static inline bool classof(const Value *V) {

View File

@ -211,9 +211,10 @@ public:
}
bool hasComdat() const { return getComdat() != nullptr; }
Comdat *getComdat();
const Comdat *getComdat() const {
return const_cast<GlobalValue *>(this)->getComdat();
const Comdat *getComdat() const;
Comdat *getComdat() {
return const_cast<Comdat *>(
static_cast<const GlobalValue *>(this)->getComdat());
}
VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
@ -514,10 +515,11 @@ public:
// increased.
bool canIncreaseAlignment() const;
const GlobalObject *getBaseObject() const {
return const_cast<GlobalValue *>(this)->getBaseObject();
const GlobalObject *getBaseObject() const;
GlobalObject *getBaseObject() {
return const_cast<GlobalObject *>(
static_cast<const GlobalValue *>(this)->getBaseObject());
}
GlobalObject *getBaseObject();
/// Returns whether this is a reference to an absolute symbol.
bool isAbsoluteSymbolRef() const;

View File

@ -344,20 +344,23 @@ public:
return getGlobalVariable(Name, false);
}
GlobalVariable *getGlobalVariable(StringRef Name, bool AllowInternal) const {
return const_cast<Module *>(this)->getGlobalVariable(Name, AllowInternal);
}
GlobalVariable *getGlobalVariable(StringRef Name, bool AllowInternal) const;
GlobalVariable *getGlobalVariable(StringRef Name, bool AllowInternal = false);
GlobalVariable *getGlobalVariable(StringRef Name,
bool AllowInternal = false) {
return static_cast<const Module *>(this)->getGlobalVariable(Name,
AllowInternal);
}
/// Return the global variable in the module with the specified name, of
/// arbitrary type. This method returns null if a global with the specified
/// name is not found.
GlobalVariable *getNamedGlobal(StringRef Name) {
const GlobalVariable *getNamedGlobal(StringRef Name) const {
return getGlobalVariable(Name, true);
}
const GlobalVariable *getNamedGlobal(StringRef Name) const {
return const_cast<Module *>(this)->getNamedGlobal(Name);
GlobalVariable *getNamedGlobal(StringRef Name) {
return const_cast<GlobalVariable *>(
static_cast<const Module *>(this)->getNamedGlobal(Name));
}
/// Look up the specified global in the module symbol table.

View File

@ -233,12 +233,13 @@ public:
void setAliasee(GlobalValueSummary *Aliasee) { AliaseeSummary = Aliasee; }
const GlobalValueSummary &getAliasee() const {
return const_cast<AliasSummary *>(this)->getAliasee();
assert(AliaseeSummary && "Unexpected missing aliasee summary");
return *AliaseeSummary;
}
GlobalValueSummary &getAliasee() {
assert(AliaseeSummary && "Unexpected missing aliasee summary");
return *AliaseeSummary;
return const_cast<GlobalValueSummary &>(
static_cast<const AliasSummary *>(this)->getAliasee());
}
};

View File

@ -122,8 +122,16 @@ protected:
}
private:
const Use *getHungOffOperands() const {
return *(reinterpret_cast<const Use *const *>(this) - 1);
}
Use *&getHungOffOperands() { return *(reinterpret_cast<Use **>(this) - 1); }
const Use *getIntrusiveOperands() const {
return reinterpret_cast<const Use *>(this) - NumUserOperands;
}
Use *getIntrusiveOperands() {
return reinterpret_cast<Use *>(this) - NumUserOperands;
}
@ -135,11 +143,11 @@ private:
}
public:
Use *getOperandList() {
const Use *getOperandList() const {
return HasHungOffUses ? getHungOffOperands() : getIntrusiveOperands();
}
const Use *getOperandList() const {
return const_cast<User *>(this)->getOperandList();
Use *getOperandList() {
return const_cast<Use *>(static_cast<const User *>(this)->getOperandList());
}
Value *getOperand(unsigned i) const {

View File

@ -476,27 +476,30 @@ public:
///
/// Returns the original uncasted value. If this is called on a non-pointer
/// value, it returns 'this'.
Value *stripPointerCasts();
const Value *stripPointerCasts() const {
return const_cast<Value*>(this)->stripPointerCasts();
const Value *stripPointerCasts() const;
Value *stripPointerCasts() {
return const_cast<Value *>(
static_cast<const Value *>(this)->stripPointerCasts());
}
/// \brief Strip off pointer casts and all-zero GEPs.
///
/// Returns the original uncasted value. If this is called on a non-pointer
/// value, it returns 'this'.
Value *stripPointerCastsNoFollowAliases();
const Value *stripPointerCastsNoFollowAliases() const {
return const_cast<Value*>(this)->stripPointerCastsNoFollowAliases();
const Value *stripPointerCastsNoFollowAliases() const;
Value *stripPointerCastsNoFollowAliases() {
return const_cast<Value *>(
static_cast<const Value *>(this)->stripPointerCastsNoFollowAliases());
}
/// \brief Strip off pointer casts and all-constant inbounds GEPs.
///
/// Returns the original pointer value. If this is called on a non-pointer
/// value, it returns 'this'.
Value *stripInBoundsConstantOffsets();
const Value *stripInBoundsConstantOffsets() const {
return const_cast<Value*>(this)->stripInBoundsConstantOffsets();
const Value *stripInBoundsConstantOffsets() const;
Value *stripInBoundsConstantOffsets() {
return const_cast<Value *>(
static_cast<const Value *>(this)->stripInBoundsConstantOffsets());
}
/// \brief Accumulate offsets from \a stripInBoundsConstantOffsets().
@ -506,21 +509,22 @@ public:
/// correct bitwidth for an offset of this pointer type.
///
/// If this is called on a non-pointer value, it returns 'this'.
Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
APInt &Offset);
const Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
APInt &Offset) const {
return const_cast<Value *>(this)
->stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
APInt &Offset) const;
Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
APInt &Offset) {
return const_cast<Value *>(static_cast<const Value *>(this)
->stripAndAccumulateInBoundsConstantOffsets(DL, Offset));
}
/// \brief Strip off pointer casts and inbounds GEPs.
///
/// Returns the original pointer value. If this is called on a non-pointer
/// value, it returns 'this'.
Value *stripInBoundsOffsets();
const Value *stripInBoundsOffsets() const {
return const_cast<Value*>(this)->stripInBoundsOffsets();
const Value *stripInBoundsOffsets() const;
Value *stripInBoundsOffsets() {
return const_cast<Value *>(
static_cast<const Value *>(this)->stripInBoundsOffsets());
}
/// \brief Returns the number of bytes known to be dereferenceable for the
@ -543,11 +547,12 @@ public:
/// the PHI node corresponding to PredBB. If not, return ourself. This is
/// useful if you want to know the value something has in a predecessor
/// block.
Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
const Value *DoPHITranslation(const BasicBlock *CurBB,
const BasicBlock *PredBB) const{
return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
const BasicBlock *PredBB) const;
Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB) {
return const_cast<Value *>(
static_cast<const Value *>(this)->DoPHITranslation(CurBB, PredBB));
}
/// \brief The maximum alignment for instructions.

View File

@ -140,7 +140,7 @@ StringRef GlobalValue::getSection() const {
return cast<GlobalObject>(this)->getSection();
}
Comdat *GlobalValue::getComdat() {
const Comdat *GlobalValue::getComdat() const {
if (auto *GA = dyn_cast<GlobalAlias>(this)) {
// In general we cannot compute this at the IR level, but we try.
if (const GlobalObject *GO = GA->getBaseObject())
@ -230,7 +230,7 @@ bool GlobalValue::canIncreaseAlignment() const {
return true;
}
GlobalObject *GlobalValue::getBaseObject() {
const GlobalObject *GlobalValue::getBaseObject() const {
if (auto *GO = dyn_cast<GlobalObject>(this))
return GO;
if (auto *GA = dyn_cast<GlobalAlias>(this))

View File

@ -206,7 +206,8 @@ Function *Module::getFunction(StringRef Name) const {
/// If AllowLocal is set to true, this function will return types that
/// have an local. By default, these types are not returned.
///
GlobalVariable *Module::getGlobalVariable(StringRef Name, bool AllowLocal) {
GlobalVariable *Module::getGlobalVariable(StringRef Name,
bool AllowLocal) const {
if (GlobalVariable *Result =
dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
if (AllowLocal || !Result->hasLocalLinkage())

View File

@ -437,17 +437,17 @@ enum PointerStripKind {
};
template <PointerStripKind StripKind>
static Value *stripPointerCastsAndOffsets(Value *V) {
static const Value *stripPointerCastsAndOffsets(const Value *V) {
if (!V->getType()->isPointerTy())
return V;
// Even though we don't look through PHI nodes, we could be called on an
// instruction in an unreachable block, which may be on a cycle.
SmallPtrSet<Value *, 4> Visited;
SmallPtrSet<const Value *, 4> Visited;
Visited.insert(V);
do {
if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
if (auto *GEP = dyn_cast<GEPOperator>(V)) {
switch (StripKind) {
case PSK_ZeroIndicesAndAliases:
case PSK_ZeroIndices:
@ -467,13 +467,13 @@ static Value *stripPointerCastsAndOffsets(Value *V) {
} else if (Operator::getOpcode(V) == Instruction::BitCast ||
Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
V = cast<Operator>(V)->getOperand(0);
} else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
} else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
if (StripKind == PSK_ZeroIndices || GA->isInterposable())
return V;
V = GA->getAliasee();
} else {
if (auto CS = CallSite(V))
if (Value *RV = CS.getReturnedArgOperand()) {
if (auto CS = ImmutableCallSite(V))
if (const Value *RV = CS.getReturnedArgOperand()) {
V = RV;
continue;
}
@ -487,20 +487,21 @@ static Value *stripPointerCastsAndOffsets(Value *V) {
}
} // end anonymous namespace
Value *Value::stripPointerCasts() {
const Value *Value::stripPointerCasts() const {
return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this);
}
Value *Value::stripPointerCastsNoFollowAliases() {
const Value *Value::stripPointerCastsNoFollowAliases() const {
return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this);
}
Value *Value::stripInBoundsConstantOffsets() {
const Value *Value::stripInBoundsConstantOffsets() const {
return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this);
}
Value *Value::stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
APInt &Offset) {
const Value *
Value::stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
APInt &Offset) const {
if (!getType()->isPointerTy())
return this;
@ -510,11 +511,11 @@ Value *Value::stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
// Even though we don't look through PHI nodes, we could be called on an
// instruction in an unreachable block, which may be on a cycle.
SmallPtrSet<Value *, 4> Visited;
SmallPtrSet<const Value *, 4> Visited;
Visited.insert(this);
Value *V = this;
const Value *V = this;
do {
if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
if (auto *GEP = dyn_cast<GEPOperator>(V)) {
if (!GEP->isInBounds())
return V;
APInt GEPOffset(Offset);
@ -524,11 +525,11 @@ Value *Value::stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
V = GEP->getPointerOperand();
} else if (Operator::getOpcode(V) == Instruction::BitCast) {
V = cast<Operator>(V)->getOperand(0);
} else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
} else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
V = GA->getAliasee();
} else {
if (auto CS = CallSite(V))
if (Value *RV = CS.getReturnedArgOperand()) {
if (auto CS = ImmutableCallSite(V))
if (const Value *RV = CS.getReturnedArgOperand()) {
V = RV;
continue;
}
@ -541,7 +542,7 @@ Value *Value::stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
return V;
}
Value *Value::stripInBoundsOffsets() {
const Value *Value::stripInBoundsOffsets() const {
return stripPointerCastsAndOffsets<PSK_InBounds>(this);
}
@ -643,9 +644,9 @@ unsigned Value::getPointerAlignment(const DataLayout &DL) const {
return Align;
}
Value *Value::DoPHITranslation(const BasicBlock *CurBB,
const BasicBlock *PredBB) {
PHINode *PN = dyn_cast<PHINode>(this);
const Value *Value::DoPHITranslation(const BasicBlock *CurBB,
const BasicBlock *PredBB) const {
auto *PN = dyn_cast<PHINode>(this);
if (PN && PN->getParent() == CurBB)
return PN->getIncomingValueForBlock(PredBB);
return this;