From 82c02b28f3a1a3adef1638e801999952a2e47f1b Mon Sep 17 00:00:00 2001 From: Anton Korobeynikov Date: Tue, 6 May 2008 22:52:30 +0000 Subject: [PATCH] Make StripPointerCast a common function (should we mak it method of Value instead?) llvm-svn: 50775 --- llvm/include/llvm/IntrinsicInst.h | 6 ---- llvm/include/llvm/Value.h | 5 ++++ llvm/lib/CodeGen/Collector.cpp | 6 ++-- .../CodeGen/SelectionDAG/SelectionDAGISel.cpp | 5 ++-- llvm/lib/CodeGen/ShadowStackCollector.cpp | 3 +- .../Scalar/InstructionCombining.cpp | 3 +- llvm/lib/VMCore/Function.cpp | 24 ---------------- llvm/lib/VMCore/Value.cpp | 28 +++++++++++++++++++ llvm/lib/VMCore/Verifier.cpp | 5 ++-- 9 files changed, 42 insertions(+), 43 deletions(-) diff --git a/llvm/include/llvm/IntrinsicInst.h b/llvm/include/llvm/IntrinsicInst.h index d3a53168f193..494eaaec4de9 100644 --- a/llvm/include/llvm/IntrinsicInst.h +++ b/llvm/include/llvm/IntrinsicInst.h @@ -38,12 +38,6 @@ namespace llvm { IntrinsicInst(const IntrinsicInst&); // DO NOT IMPLEMENT void operator=(const IntrinsicInst&); // DO NOT IMPLEMENT public: - - /// StripPointerCasts - This static 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. - static Value *StripPointerCasts(Value *Ptr); - /// getIntrinsicID - Return the intrinsic ID of this intrinsic. /// Intrinsic::ID getIntrinsicID() const { diff --git a/llvm/include/llvm/Value.h b/llvm/include/llvm/Value.h index c1c728270e4c..e3e64749598d 100644 --- a/llvm/include/llvm/Value.h +++ b/llvm/include/llvm/Value.h @@ -272,6 +272,11 @@ 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/llvm/lib/CodeGen/Collector.cpp b/llvm/lib/CodeGen/Collector.cpp index fe5119ea2db6..07869a0ff553 100644 --- a/llvm/lib/CodeGen/Collector.cpp +++ b/llvm/lib/CodeGen/Collector.cpp @@ -178,8 +178,8 @@ bool LowerIntrinsics::InsertRootInitializers(Function &F, AllocaInst **Roots, SmallPtrSet InitedRoots; for (; !CouldBecomeSafePoint(IP); ++IP) if (StoreInst *SI = dyn_cast(IP)) - if (AllocaInst *AI = dyn_cast( - IntrinsicInst::StripPointerCasts(SI->getOperand(1)))) + if (AllocaInst *AI = + dyn_cast(StripPointerCasts(SI->getOperand(1)))) 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( - IntrinsicInst::StripPointerCasts(CI->getOperand(1)))); + StripPointerCasts(CI->getOperand(1)))); } break; default: diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp index 8c606b886b68..ea047e683f5b 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp +++ b/llvm/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 = IntrinsicInst::StripPointerCasts(V); + V = StripPointerCasts(V); GlobalVariable *GV = dyn_cast(V); assert ((GV || isa(V)) && "TypeInfo must be a global variable or NULL"); @@ -3150,8 +3150,7 @@ SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) { return 0; case Intrinsic::init_trampoline: { - const Function *F = - cast(IntrinsicInst::StripPointerCasts(I.getOperand(2))); + const Function *F = cast(StripPointerCasts(I.getOperand(2))); SDOperand Ops[6]; Ops[0] = getRoot(); diff --git a/llvm/lib/CodeGen/ShadowStackCollector.cpp b/llvm/lib/CodeGen/ShadowStackCollector.cpp index d41e83c712de..121dfc240773 100644 --- a/llvm/lib/CodeGen/ShadowStackCollector.cpp +++ b/llvm/lib/CodeGen/ShadowStackCollector.cpp @@ -325,8 +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( - IntrinsicInst::StripPointerCasts(CI->getOperand(1)))); + CI, cast(StripPointerCasts(CI->getOperand(1)))); if (IsNullValue(CI->getOperand(2))) Roots.push_back(Pair); else diff --git a/llvm/lib/Transforms/Scalar/InstructionCombining.cpp b/llvm/lib/Transforms/Scalar/InstructionCombining.cpp index de29efa497d2..2afa6c51622c 100644 --- a/llvm/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/llvm/lib/Transforms/Scalar/InstructionCombining.cpp @@ -9131,8 +9131,7 @@ Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) { IntrinsicInst *Tramp = cast(cast(Callee)->getOperand(0)); - Function *NestF = - cast(IntrinsicInst::StripPointerCasts(Tramp->getOperand(2))); + Function *NestF = cast(StripPointerCasts(Tramp->getOperand(2))); const PointerType *NestFPTy = cast(NestF->getType()); const FunctionType *NestFTy = cast(NestFPTy->getElementType()); diff --git a/llvm/lib/VMCore/Function.cpp b/llvm/lib/VMCore/Function.cpp index f14c455bce02..546cadb0f3b1 100644 --- a/llvm/lib/VMCore/Function.cpp +++ b/llvm/lib/VMCore/Function.cpp @@ -357,28 +357,4 @@ Function *Intrinsic::getDeclaration(Module *M, ID id, const Type **Tys, getType(id, Tys, numTys))); } -Value *IntrinsicInst::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; -} - // vim: sw=2 ai diff --git a/llvm/lib/VMCore/Value.cpp b/llvm/lib/VMCore/Value.cpp index e55e558e0f99..93a71518eb1b 100644 --- a/llvm/lib/VMCore/Value.cpp +++ b/llvm/lib/VMCore/Value.cpp @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "llvm/Constant.h" +#include "llvm/Constants.h" #include "llvm/DerivedTypes.h" #include "llvm/InstrTypes.h" #include "llvm/Instructions.h" @@ -331,3 +332,30 @@ void User::replaceUsesOfWith(Value *From, Value *To) { } } +//===----------------------------------------------------------------------===// +// 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/llvm/lib/VMCore/Verifier.cpp b/llvm/lib/VMCore/Verifier.cpp index 7449fe7ed6fb..be546738699a 100644 --- a/llvm/lib/VMCore/Verifier.cpp +++ b/llvm/lib/VMCore/Verifier.cpp @@ -1271,8 +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( - IntrinsicInst::StripPointerCasts(CI.getOperand(1))), + Assert1(isa(StripPointerCasts(CI.getOperand(1))), "llvm.gcroot parameter #1 must be an alloca.", &CI); Assert1(isa(CI.getOperand(2)), "llvm.gcroot parameter #2 must be a constant.", &CI); @@ -1298,7 +1297,7 @@ void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) { &CI); } break; case Intrinsic::init_trampoline: - Assert1(isa(IntrinsicInst::StripPointerCasts(CI.getOperand(2))), + Assert1(isa(StripPointerCasts(CI.getOperand(2))), "llvm.init_trampoline parameter #2 must resolve to a function.", &CI); break;