From 0b12ecf6ff6b5d3a144178257b6206f0c4788792 Mon Sep 17 00:00:00 2001 From: Anton Korobeynikov Date: Wed, 7 May 2008 22:54:15 +0000 Subject: [PATCH] Turn StripPointerCast() into a method git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50836 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/IntrinsicInst.h | 6 +-- include/llvm/Value.h | 10 ++-- lib/CodeGen/Collector.cpp | 4 +- lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp | 4 +- lib/CodeGen/ShadowStackCollector.cpp | 2 +- lib/Linker/LinkModules.cpp | 3 +- .../Scalar/InstructionCombining.cpp | 2 +- lib/VMCore/Value.cpp | 52 +++++++++---------- lib/VMCore/Verifier.cpp | 4 +- 9 files changed, 42 insertions(+), 45 deletions(-) diff --git a/include/llvm/IntrinsicInst.h b/include/llvm/IntrinsicInst.h index 494eaaec4de..c674e47b3a0 100644 --- a/include/llvm/IntrinsicInst.h +++ b/include/llvm/IntrinsicInst.h @@ -183,7 +183,7 @@ namespace llvm { /// getDest - This is just like getRawDest, but it strips off any cast /// instructions that feed it, giving the original input. The returned /// value is guaranteed to be a pointer. - Value *getDest() const { return StripPointerCasts(getRawDest()); } + Value *getDest() const { return getRawDest()->stripPointerCasts(); } /// set* - Set the specified arguments of the instruction. /// @@ -234,7 +234,7 @@ namespace llvm { /// getSource - This is just like getRawSource, but it strips off any cast /// instructions that feed it, giving the original input. The returned /// value is guaranteed to be a pointer. - Value *getSource() const { return StripPointerCasts(getRawSource()); } + Value *getSource() const { return getRawSource()->stripPointerCasts(); } void setSource(Value *Ptr) { @@ -264,7 +264,7 @@ namespace llvm { /// getSource - This is just like getRawSource, but it strips off any cast /// instructions that feed it, giving the original input. The returned /// value is guaranteed to be a pointer. - Value *getSource() const { return StripPointerCasts(getRawSource()); } + Value *getSource() const { return getRawSource()->stripPointerCasts(); } void setSource(Value *Ptr) { assert(getRawSource()->getType() == Ptr->getType() && diff --git a/include/llvm/Value.h b/include/llvm/Value.h index e3e64749598..2bcac08a952 100644 --- a/include/llvm/Value.h +++ b/include/llvm/Value.h @@ -220,6 +220,11 @@ public: /// getRawType - This should only be used to implement the vmcore library. /// const Type *getRawType() const { return Ty.getRawType(); } + + /// stripPointerCasts - This method strips off any unneeded pointer + /// casts from the specified value, returning the original uncasted value. + /// Note that the returned value is guaranteed to have pointer type. + Value *stripPointerCasts(); }; inline std::ostream &operator<<(std::ostream &OS, const Value &V) { @@ -272,11 +277,6 @@ template <> inline bool isa_impl(const Value &Val) { return isa(Val) || isa(Val) || isa(Val); } -/// StripPointerCasts - This function strips off any unneeded pointer -/// casts from the specified value, returning the original uncasted value. -/// Note that the returned value is guaranteed to have pointer type. -Value *StripPointerCasts(Value *Ptr); - } // End llvm namespace #endif diff --git a/lib/CodeGen/Collector.cpp b/lib/CodeGen/Collector.cpp index 07869a0ff55..6c5263d73e3 100644 --- a/lib/CodeGen/Collector.cpp +++ b/lib/CodeGen/Collector.cpp @@ -179,7 +179,7 @@ bool LowerIntrinsics::InsertRootInitializers(Function &F, AllocaInst **Roots, for (; !CouldBecomeSafePoint(IP); ++IP) if (StoreInst *SI = dyn_cast(IP)) if (AllocaInst *AI = - dyn_cast(StripPointerCasts(SI->getOperand(1)))) + dyn_cast(SI->getOperand(1)->stripPointerCasts())) InitedRoots.insert(AI); // Add root initializers. @@ -294,7 +294,7 @@ bool LowerIntrinsics::PerformDefaultLowering(Function &F, Collector &Coll) { // Initialize the GC root, but do not delete the intrinsic. The // backend needs the intrinsic to flag the stack slot. Roots.push_back(cast( - StripPointerCasts(CI->getOperand(1)))); + CI->getOperand(1)->stripPointerCasts())); } break; default: diff --git a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp index ea047e683f5..7ac5218ecfd 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp @@ -2712,7 +2712,7 @@ void SelectionDAGLowering::visitTargetIntrinsic(CallInst &I, /// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V. static GlobalVariable *ExtractTypeInfo (Value *V) { - V = StripPointerCasts(V); + V = V->stripPointerCasts(); GlobalVariable *GV = dyn_cast(V); assert ((GV || isa(V)) && "TypeInfo must be a global variable or NULL"); @@ -3150,7 +3150,7 @@ SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) { return 0; case Intrinsic::init_trampoline: { - const Function *F = cast(StripPointerCasts(I.getOperand(2))); + const Function *F = cast(I.getOperand(2)->stripPointerCasts()); SDOperand Ops[6]; Ops[0] = getRoot(); diff --git a/lib/CodeGen/ShadowStackCollector.cpp b/lib/CodeGen/ShadowStackCollector.cpp index 121dfc24077..092671ce562 100644 --- a/lib/CodeGen/ShadowStackCollector.cpp +++ b/lib/CodeGen/ShadowStackCollector.cpp @@ -325,7 +325,7 @@ void ShadowStackCollector::CollectRoots(Function &F) { if (Function *F = CI->getCalledFunction()) if (F->getIntrinsicID() == Intrinsic::gcroot) { std::pair Pair = std::make_pair( - CI, cast(StripPointerCasts(CI->getOperand(1)))); + CI, cast(CI->getOperand(1)->stripPointerCasts())); if (IsNullValue(CI->getOperand(2))) Roots.push_back(Pair); else diff --git a/lib/Linker/LinkModules.cpp b/lib/Linker/LinkModules.cpp index ddc9081d654..57df01495b7 100644 --- a/lib/Linker/LinkModules.cpp +++ b/lib/Linker/LinkModules.cpp @@ -758,7 +758,8 @@ static bool LinkGlobalInits(Module *Dest, const Module *Src, Constant *SInit = cast(RemapOperand(SGV->getInitializer(), ValueMap)); - GlobalVariable *DGV = cast(StripPointerCasts(ValueMap[SGV])); + GlobalVariable *DGV = + cast(ValueMap[SGV]->stripPointerCasts()); if (DGV->hasInitializer()) { if (SGV->hasExternalLinkage()) { if (DGV->getInitializer() != SInit) diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 66d5326b9f3..a3340a0d82a 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -9131,7 +9131,7 @@ Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) { IntrinsicInst *Tramp = cast(cast(Callee)->getOperand(0)); - Function *NestF = cast(StripPointerCasts(Tramp->getOperand(2))); + Function *NestF = cast(Tramp->getOperand(2)->stripPointerCasts()); const PointerType *NestFPTy = cast(NestF->getType()); const FunctionType *NestFTy = cast(NestFPTy->getElementType()); diff --git a/lib/VMCore/Value.cpp b/lib/VMCore/Value.cpp index 93a71518eb1..ff056ba74ac 100644 --- a/lib/VMCore/Value.cpp +++ b/lib/VMCore/Value.cpp @@ -310,6 +310,30 @@ void Value::replaceAllUsesWith(Value *New) { uncheckedReplaceAllUsesWith(New); } +Value *Value::stripPointerCasts() { + if (ConstantExpr *CE = dyn_cast(this)) { + if (CE->getOpcode() == Instruction::BitCast) { + if (isa(CE->getOperand(0)->getType())) + return CE->getOperand(0)->stripPointerCasts(); + } else if (CE->getOpcode() == Instruction::GetElementPtr) { + for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) + if (!CE->getOperand(i)->isNullValue()) + return this; + return CE->getOperand(0)->stripPointerCasts(); + } + return this; + } + + if (BitCastInst *CI = dyn_cast(this)) { + if (isa(CI->getOperand(0)->getType())) + return CI->getOperand(0)->stripPointerCasts(); + } else if (GetElementPtrInst *GEP = dyn_cast(this)) { + if (GEP->hasAllZeroIndices()) + return GEP->getOperand(0)->stripPointerCasts(); + } + return this; +} + //===----------------------------------------------------------------------===// // User Class //===----------------------------------------------------------------------===// @@ -331,31 +355,3 @@ void User::replaceUsesOfWith(Value *From, Value *To) { setOperand(i, To); // Fix it now... } } - -//===----------------------------------------------------------------------===// -// Utility functions -//===----------------------------------------------------------------------===// - -Value *llvm::StripPointerCasts(Value *Ptr) { - if (ConstantExpr *CE = dyn_cast(Ptr)) { - if (CE->getOpcode() == Instruction::BitCast) { - if (isa(CE->getOperand(0)->getType())) - return StripPointerCasts(CE->getOperand(0)); - } else if (CE->getOpcode() == Instruction::GetElementPtr) { - for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) - if (!CE->getOperand(i)->isNullValue()) - return Ptr; - return StripPointerCasts(CE->getOperand(0)); - } - return Ptr; - } - - if (BitCastInst *CI = dyn_cast(Ptr)) { - if (isa(CI->getOperand(0)->getType())) - return StripPointerCasts(CI->getOperand(0)); - } else if (GetElementPtrInst *GEP = dyn_cast(Ptr)) { - if (GEP->hasAllZeroIndices()) - return StripPointerCasts(GEP->getOperand(0)); - } - return Ptr; -} diff --git a/lib/VMCore/Verifier.cpp b/lib/VMCore/Verifier.cpp index be546738699..75636d0bdda 100644 --- a/lib/VMCore/Verifier.cpp +++ b/lib/VMCore/Verifier.cpp @@ -1271,7 +1271,7 @@ void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) { "Intrinsic parameter #1 is not i8**.", &CI); Assert1(CI.getOperand(2)->getType() == PtrTy, "Intrinsic parameter #2 is not i8*.", &CI); - Assert1(isa(StripPointerCasts(CI.getOperand(1))), + Assert1(isa(CI.getOperand(1)->stripPointerCasts()), "llvm.gcroot parameter #1 must be an alloca.", &CI); Assert1(isa(CI.getOperand(2)), "llvm.gcroot parameter #2 must be a constant.", &CI); @@ -1297,7 +1297,7 @@ void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) { &CI); } break; case Intrinsic::init_trampoline: - Assert1(isa(StripPointerCasts(CI.getOperand(2))), + Assert1(isa(CI.getOperand(2)->stripPointerCasts()), "llvm.init_trampoline parameter #2 must resolve to a function.", &CI); break;