From 1dd20e654421bfd65e9bfe59f833c7e1a2312350 Mon Sep 17 00:00:00 2001 From: Craig Topper Date: Mon, 27 Mar 2017 05:47:03 +0000 Subject: [PATCH] [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 --- llvm/include/llvm/IR/Constant.h | 7 +-- llvm/include/llvm/IR/GlobalIndirectSymbol.h | 18 +++++--- llvm/include/llvm/IR/GlobalValue.h | 14 +++--- llvm/include/llvm/IR/Module.h | 17 +++++--- llvm/include/llvm/IR/ModuleSummaryIndex.h | 7 +-- llvm/include/llvm/IR/User.h | 14 ++++-- llvm/include/llvm/IR/Value.h | 47 ++++++++++++--------- llvm/lib/IR/Globals.cpp | 4 +- llvm/lib/IR/Module.cpp | 3 +- llvm/lib/IR/Value.cpp | 43 ++++++++++--------- 10 files changed, 100 insertions(+), 74 deletions(-) diff --git a/llvm/include/llvm/IR/Constant.h b/llvm/include/llvm/IR/Constant.h index 99c970ebb633..3b3694e7e60d 100644 --- a/llvm/include/llvm/IR/Constant.h +++ b/llvm/include/llvm/IR/Constant.h @@ -152,12 +152,13 @@ public: /// hanging off of the globals. void removeDeadConstantUsers() const; - Constant *stripPointerCasts() { + const Constant *stripPointerCasts() const { return cast(Value::stripPointerCasts()); } - const Constant *stripPointerCasts() const { - return const_cast(this)->stripPointerCasts(); + Constant *stripPointerCasts() { + return const_cast( + static_cast(this)->stripPointerCasts()); } }; diff --git a/llvm/include/llvm/IR/GlobalIndirectSymbol.h b/llvm/include/llvm/IR/GlobalIndirectSymbol.h index 671309e85d19..212703af7101 100644 --- a/llvm/include/llvm/IR/GlobalIndirectSymbol.h +++ b/llvm/include/llvm/IR/GlobalIndirectSymbol.h @@ -48,27 +48,31 @@ public: setOperand(0, Symbol); } const Constant *getIndirectSymbol() const { - return const_cast(this)->getIndirectSymbol(); + return getOperand(0); } Constant *getIndirectSymbol() { - return getOperand(0); + return const_cast( + static_cast(this)->getIndirectSymbol()); } const GlobalObject *getBaseObject() const { - return const_cast(this)->getBaseObject(); + return dyn_cast(getIndirectSymbol()->stripInBoundsOffsets()); } GlobalObject *getBaseObject() { - return dyn_cast(getIndirectSymbol()->stripInBoundsOffsets()); + return const_cast( + static_cast(this)->getBaseObject()); } const GlobalObject *getBaseObject(const DataLayout &DL, APInt &Offset) const { - return const_cast(this)->getBaseObject(DL, Offset); - } - GlobalObject *getBaseObject(const DataLayout &DL, APInt &Offset) { return dyn_cast( getIndirectSymbol()->stripAndAccumulateInBoundsConstantOffsets(DL, Offset)); } + GlobalObject *getBaseObject(const DataLayout &DL, APInt &Offset) { + return const_cast( + static_cast(this) + ->getBaseObject(DL, Offset)); + } // Methods for support type inquiry through isa, cast, and dyn_cast: static inline bool classof(const Value *V) { diff --git a/llvm/include/llvm/IR/GlobalValue.h b/llvm/include/llvm/IR/GlobalValue.h index c6398aaa4847..bb30fa8be867 100644 --- a/llvm/include/llvm/IR/GlobalValue.h +++ b/llvm/include/llvm/IR/GlobalValue.h @@ -211,9 +211,10 @@ public: } bool hasComdat() const { return getComdat() != nullptr; } - Comdat *getComdat(); - const Comdat *getComdat() const { - return const_cast(this)->getComdat(); + const Comdat *getComdat() const; + Comdat *getComdat() { + return const_cast( + static_cast(this)->getComdat()); } VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); } @@ -514,10 +515,11 @@ public: // increased. bool canIncreaseAlignment() const; - const GlobalObject *getBaseObject() const { - return const_cast(this)->getBaseObject(); + const GlobalObject *getBaseObject() const; + GlobalObject *getBaseObject() { + return const_cast( + static_cast(this)->getBaseObject()); } - GlobalObject *getBaseObject(); /// Returns whether this is a reference to an absolute symbol. bool isAbsoluteSymbolRef() const; diff --git a/llvm/include/llvm/IR/Module.h b/llvm/include/llvm/IR/Module.h index e13b8f36aa1a..de10b099a688 100644 --- a/llvm/include/llvm/IR/Module.h +++ b/llvm/include/llvm/IR/Module.h @@ -344,20 +344,23 @@ public: return getGlobalVariable(Name, false); } - GlobalVariable *getGlobalVariable(StringRef Name, bool AllowInternal) const { - return const_cast(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(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(this)->getNamedGlobal(Name); + GlobalVariable *getNamedGlobal(StringRef Name) { + return const_cast( + static_cast(this)->getNamedGlobal(Name)); } /// Look up the specified global in the module symbol table. diff --git a/llvm/include/llvm/IR/ModuleSummaryIndex.h b/llvm/include/llvm/IR/ModuleSummaryIndex.h index f1e87df2b8ff..09f6c1897009 100644 --- a/llvm/include/llvm/IR/ModuleSummaryIndex.h +++ b/llvm/include/llvm/IR/ModuleSummaryIndex.h @@ -233,12 +233,13 @@ public: void setAliasee(GlobalValueSummary *Aliasee) { AliaseeSummary = Aliasee; } const GlobalValueSummary &getAliasee() const { - return const_cast(this)->getAliasee(); + assert(AliaseeSummary && "Unexpected missing aliasee summary"); + return *AliaseeSummary; } GlobalValueSummary &getAliasee() { - assert(AliaseeSummary && "Unexpected missing aliasee summary"); - return *AliaseeSummary; + return const_cast( + static_cast(this)->getAliasee()); } }; diff --git a/llvm/include/llvm/IR/User.h b/llvm/include/llvm/IR/User.h index c907d6b670b5..54758a9b6d6a 100644 --- a/llvm/include/llvm/IR/User.h +++ b/llvm/include/llvm/IR/User.h @@ -122,8 +122,16 @@ protected: } private: + const Use *getHungOffOperands() const { + return *(reinterpret_cast(this) - 1); + } + Use *&getHungOffOperands() { return *(reinterpret_cast(this) - 1); } + const Use *getIntrusiveOperands() const { + return reinterpret_cast(this) - NumUserOperands; + } + Use *getIntrusiveOperands() { return reinterpret_cast(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(this)->getOperandList(); + Use *getOperandList() { + return const_cast(static_cast(this)->getOperandList()); } Value *getOperand(unsigned i) const { diff --git a/llvm/include/llvm/IR/Value.h b/llvm/include/llvm/IR/Value.h index 9c91f4e07e33..a4b48d7f3539 100644 --- a/llvm/include/llvm/IR/Value.h +++ b/llvm/include/llvm/IR/Value.h @@ -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(this)->stripPointerCasts(); + const Value *stripPointerCasts() const; + Value *stripPointerCasts() { + return const_cast( + static_cast(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(this)->stripPointerCastsNoFollowAliases(); + const Value *stripPointerCastsNoFollowAliases() const; + Value *stripPointerCastsNoFollowAliases() { + return const_cast( + static_cast(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(this)->stripInBoundsConstantOffsets(); + const Value *stripInBoundsConstantOffsets() const; + Value *stripInBoundsConstantOffsets() { + return const_cast( + static_cast(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(this) - ->stripAndAccumulateInBoundsConstantOffsets(DL, Offset); + APInt &Offset) const; + Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, + APInt &Offset) { + return const_cast(static_cast(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(this)->stripInBoundsOffsets(); + const Value *stripInBoundsOffsets() const; + Value *stripInBoundsOffsets() { + return const_cast( + static_cast(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(this)->DoPHITranslation(CurBB, PredBB); + const BasicBlock *PredBB) const; + + Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB) { + return const_cast( + static_cast(this)->DoPHITranslation(CurBB, PredBB)); } /// \brief The maximum alignment for instructions. diff --git a/llvm/lib/IR/Globals.cpp b/llvm/lib/IR/Globals.cpp index 54917e596b75..5f338f58d940 100644 --- a/llvm/lib/IR/Globals.cpp +++ b/llvm/lib/IR/Globals.cpp @@ -140,7 +140,7 @@ StringRef GlobalValue::getSection() const { return cast(this)->getSection(); } -Comdat *GlobalValue::getComdat() { +const Comdat *GlobalValue::getComdat() const { if (auto *GA = dyn_cast(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(this)) return GO; if (auto *GA = dyn_cast(this)) diff --git a/llvm/lib/IR/Module.cpp b/llvm/lib/IR/Module.cpp index 9297e5b43932..c3bfee5cb687 100644 --- a/llvm/lib/IR/Module.cpp +++ b/llvm/lib/IR/Module.cpp @@ -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(getNamedValue(Name))) if (AllowLocal || !Result->hasLocalLinkage()) diff --git a/llvm/lib/IR/Value.cpp b/llvm/lib/IR/Value.cpp index 8ca9f878518c..b07c57685a26 100644 --- a/llvm/lib/IR/Value.cpp +++ b/llvm/lib/IR/Value.cpp @@ -437,17 +437,17 @@ enum PointerStripKind { }; template -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 Visited; + SmallPtrSet Visited; Visited.insert(V); do { - if (GEPOperator *GEP = dyn_cast(V)) { + if (auto *GEP = dyn_cast(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(V)->getOperand(0); - } else if (GlobalAlias *GA = dyn_cast(V)) { + } else if (auto *GA = dyn_cast(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(this); } -Value *Value::stripPointerCastsNoFollowAliases() { +const Value *Value::stripPointerCastsNoFollowAliases() const { return stripPointerCastsAndOffsets(this); } -Value *Value::stripInBoundsConstantOffsets() { +const Value *Value::stripInBoundsConstantOffsets() const { return stripPointerCastsAndOffsets(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 Visited; + SmallPtrSet Visited; Visited.insert(this); - Value *V = this; + const Value *V = this; do { - if (GEPOperator *GEP = dyn_cast(V)) { + if (auto *GEP = dyn_cast(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(V)->getOperand(0); - } else if (GlobalAlias *GA = dyn_cast(V)) { + } else if (auto *GA = dyn_cast(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(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(this); +const Value *Value::DoPHITranslation(const BasicBlock *CurBB, + const BasicBlock *PredBB) const { + auto *PN = dyn_cast(this); if (PN && PN->getParent() == CurBB) return PN->getIncomingValueForBlock(PredBB); return this;