mirror of
https://github.com/llvm/llvm-project.git
synced 2025-05-03 01:16:13 +00:00
Factorize code: remove variants of "strip off
pointer bitcasts and GEP's", and centralize the logic in Value::getUnderlyingObject. The difference with stripPointerCasts is that stripPointerCasts only strips GEPs if all indices are zero, while getUnderlyingObject strips GEPs no matter what the indices are. llvm-svn: 56922
This commit is contained in:
parent
94798d31dd
commit
d65a4daeea
@ -228,11 +228,20 @@ public:
|
|||||||
|
|
||||||
/// stripPointerCasts - This method strips off any unneeded pointer
|
/// stripPointerCasts - This method strips off any unneeded pointer
|
||||||
/// casts from the specified value, returning the original uncasted value.
|
/// casts from the specified value, returning the original uncasted value.
|
||||||
/// Note that the returned value is guaranteed to have pointer type.
|
/// Note that the returned value has pointer type if the specified value does.
|
||||||
Value *stripPointerCasts();
|
Value *stripPointerCasts();
|
||||||
const Value *stripPointerCasts() const {
|
const Value *stripPointerCasts() const {
|
||||||
return const_cast<Value*>(this)->stripPointerCasts();
|
return const_cast<Value*>(this)->stripPointerCasts();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// getUnderlyingObject - This method strips off any GEP address adjustments
|
||||||
|
/// and pointer casts from the specified value, returning the original object
|
||||||
|
/// being addressed. Note that the returned value has pointer type if the
|
||||||
|
/// specified value does.
|
||||||
|
Value *getUnderlyingObject();
|
||||||
|
const Value *getUnderlyingObject() const {
|
||||||
|
return const_cast<Value*>(this)->getUnderlyingObject();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
|
inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
|
||||||
|
@ -76,30 +76,6 @@ static bool AddressMightEscape(const Value *V) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// getUnderlyingObject - This traverses the use chain to figure out what object
|
|
||||||
/// the specified value points to. If the value points to, or is derived from,
|
|
||||||
/// a unique object or an argument, return it. This returns:
|
|
||||||
/// Arguments, GlobalVariables, Functions, Allocas, Mallocs.
|
|
||||||
static const Value *getUnderlyingObject(const Value *V) {
|
|
||||||
if (!isa<PointerType>(V->getType())) return V;
|
|
||||||
|
|
||||||
// If we are at some type of object, return it. GlobalValues and Allocations
|
|
||||||
// have unique addresses.
|
|
||||||
if (isa<GlobalValue>(V) || isa<AllocationInst>(V) || isa<Argument>(V))
|
|
||||||
return V;
|
|
||||||
|
|
||||||
// Traverse through different addressing mechanisms...
|
|
||||||
if (const Instruction *I = dyn_cast<Instruction>(V)) {
|
|
||||||
if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I))
|
|
||||||
return getUnderlyingObject(I->getOperand(0));
|
|
||||||
} else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
|
|
||||||
if (CE->getOpcode() == Instruction::BitCast ||
|
|
||||||
CE->getOpcode() == Instruction::GetElementPtr)
|
|
||||||
return getUnderlyingObject(CE->getOperand(0));
|
|
||||||
}
|
|
||||||
return V;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const User *isGEP(const Value *V) {
|
static const User *isGEP(const Value *V) {
|
||||||
if (isa<GetElementPtrInst>(V) ||
|
if (isa<GetElementPtrInst>(V) ||
|
||||||
(isa<ConstantExpr>(V) &&
|
(isa<ConstantExpr>(V) &&
|
||||||
@ -314,7 +290,7 @@ ImmutablePass *llvm::createBasicAliasAnalysisPass() {
|
|||||||
/// global) or not.
|
/// global) or not.
|
||||||
bool BasicAliasAnalysis::pointsToConstantMemory(const Value *P) {
|
bool BasicAliasAnalysis::pointsToConstantMemory(const Value *P) {
|
||||||
if (const GlobalVariable *GV =
|
if (const GlobalVariable *GV =
|
||||||
dyn_cast<GlobalVariable>(getUnderlyingObject(P)))
|
dyn_cast<GlobalVariable>(P->getUnderlyingObject()))
|
||||||
return GV->isConstant();
|
return GV->isConstant();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -327,7 +303,7 @@ bool BasicAliasAnalysis::pointsToConstantMemory(const Value *P) {
|
|||||||
AliasAnalysis::ModRefResult
|
AliasAnalysis::ModRefResult
|
||||||
BasicAliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
|
BasicAliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
|
||||||
if (!isa<Constant>(P)) {
|
if (!isa<Constant>(P)) {
|
||||||
const Value *Object = getUnderlyingObject(P);
|
const Value *Object = P->getUnderlyingObject();
|
||||||
|
|
||||||
// If this is a tail call and P points to a stack location, we know that
|
// If this is a tail call and P points to a stack location, we know that
|
||||||
// the tail call cannot access or modify the local stack.
|
// the tail call cannot access or modify the local stack.
|
||||||
@ -390,8 +366,8 @@ BasicAliasAnalysis::alias(const Value *V1, unsigned V1Size,
|
|||||||
return alias(V1, V1Size, I->getOperand(0), V2Size);
|
return alias(V1, V1Size, I->getOperand(0), V2Size);
|
||||||
|
|
||||||
// Figure out what objects these things are pointing to if we can...
|
// Figure out what objects these things are pointing to if we can...
|
||||||
const Value *O1 = getUnderlyingObject(V1);
|
const Value *O1 = V1->getUnderlyingObject();
|
||||||
const Value *O2 = getUnderlyingObject(V2);
|
const Value *O2 = V2->getUnderlyingObject();
|
||||||
|
|
||||||
if (O1 != O2) {
|
if (O1 != O2) {
|
||||||
// If V1/V2 point to two different objects we know that we have no alias.
|
// If V1/V2 point to two different objects we know that we have no alias.
|
||||||
|
@ -157,29 +157,6 @@ static RegisterAnalysisGroup<AliasAnalysis> Y(X);
|
|||||||
|
|
||||||
Pass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); }
|
Pass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); }
|
||||||
|
|
||||||
/// getUnderlyingObject - This traverses the use chain to figure out what object
|
|
||||||
/// the specified value points to. If the value points to, or is derived from,
|
|
||||||
/// a global object, return it.
|
|
||||||
static Value *getUnderlyingObject(Value *V) {
|
|
||||||
if (!isa<PointerType>(V->getType())) return V;
|
|
||||||
|
|
||||||
// If we are at some type of object... return it.
|
|
||||||
if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV;
|
|
||||||
|
|
||||||
// Traverse through different addressing mechanisms.
|
|
||||||
if (Instruction *I = dyn_cast<Instruction>(V)) {
|
|
||||||
if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I))
|
|
||||||
return getUnderlyingObject(I->getOperand(0));
|
|
||||||
} else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
|
|
||||||
if (CE->getOpcode() == Instruction::BitCast ||
|
|
||||||
CE->getOpcode() == Instruction::GetElementPtr)
|
|
||||||
return getUnderlyingObject(CE->getOperand(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, we don't know what this is, return it as the base pointer.
|
|
||||||
return V;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// AnalyzeGlobals - Scan through the users of all of the internal
|
/// AnalyzeGlobals - Scan through the users of all of the internal
|
||||||
/// GlobalValue's in the program. If none of them have their "address taken"
|
/// GlobalValue's in the program. If none of them have their "address taken"
|
||||||
/// (really, their address passed to something nontrivial), record this fact,
|
/// (really, their address passed to something nontrivial), record this fact,
|
||||||
@ -304,7 +281,7 @@ bool GlobalsModRef::AnalyzeIndirectGlobalMemory(GlobalValue *GV) {
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Check the value being stored.
|
// Check the value being stored.
|
||||||
Value *Ptr = getUnderlyingObject(SI->getOperand(0));
|
Value *Ptr = SI->getOperand(0)->getUnderlyingObject();
|
||||||
|
|
||||||
if (isa<MallocInst>(Ptr)) {
|
if (isa<MallocInst>(Ptr)) {
|
||||||
// Okay, easy case.
|
// Okay, easy case.
|
||||||
@ -468,8 +445,8 @@ AliasAnalysis::AliasResult
|
|||||||
GlobalsModRef::alias(const Value *V1, unsigned V1Size,
|
GlobalsModRef::alias(const Value *V1, unsigned V1Size,
|
||||||
const Value *V2, unsigned V2Size) {
|
const Value *V2, unsigned V2Size) {
|
||||||
// Get the base object these pointers point to.
|
// Get the base object these pointers point to.
|
||||||
Value *UV1 = getUnderlyingObject(const_cast<Value*>(V1));
|
Value *UV1 = const_cast<Value*>(V1->getUnderlyingObject());
|
||||||
Value *UV2 = getUnderlyingObject(const_cast<Value*>(V2));
|
Value *UV2 = const_cast<Value*>(V2->getUnderlyingObject());
|
||||||
|
|
||||||
// If either of the underlying values is a global, they may be non-addr-taken
|
// If either of the underlying values is a global, they may be non-addr-taken
|
||||||
// globals, which we can answer queries about.
|
// globals, which we can answer queries about.
|
||||||
@ -526,7 +503,7 @@ GlobalsModRef::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
|
|||||||
|
|
||||||
// If we are asking for mod/ref info of a direct call with a pointer to a
|
// If we are asking for mod/ref info of a direct call with a pointer to a
|
||||||
// global we are tracking, return information if we have it.
|
// global we are tracking, return information if we have it.
|
||||||
if (GlobalValue *GV = dyn_cast<GlobalValue>(getUnderlyingObject(P)))
|
if (GlobalValue *GV = dyn_cast<GlobalValue>(P->getUnderlyingObject()))
|
||||||
if (GV->hasInternalLinkage())
|
if (GV->hasInternalLinkage())
|
||||||
if (Function *F = CS.getCalledFunction())
|
if (Function *F = CS.getCalledFunction())
|
||||||
if (NonAddressTakenGlobals.count(GV))
|
if (NonAddressTakenGlobals.count(GV))
|
||||||
|
@ -60,27 +60,6 @@ namespace {
|
|||||||
void DeleteDeadInstructionChains(Instruction *I,
|
void DeleteDeadInstructionChains(Instruction *I,
|
||||||
SetVector<Instruction*> &DeadInsts);
|
SetVector<Instruction*> &DeadInsts);
|
||||||
|
|
||||||
/// Find the base pointer that a pointer came from
|
|
||||||
/// Because this is used to find pointers that originate
|
|
||||||
/// from allocas, it is safe to ignore GEP indices, since
|
|
||||||
/// either the store will be in the alloca, and thus dead,
|
|
||||||
/// or beyond the end of the alloca, and thus undefined.
|
|
||||||
void TranslatePointerBitCasts(Value*& v, bool zeroGepsOnly = false) {
|
|
||||||
assert(isa<PointerType>(v->getType()) &&
|
|
||||||
"Translating a non-pointer type?");
|
|
||||||
while (true) {
|
|
||||||
if (BitCastInst* C = dyn_cast<BitCastInst>(v))
|
|
||||||
v = C->getOperand(0);
|
|
||||||
else if (GetElementPtrInst* G = dyn_cast<GetElementPtrInst>(v))
|
|
||||||
if (!zeroGepsOnly || G->hasAllZeroIndices()) {
|
|
||||||
v = G->getOperand(0);
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// getAnalysisUsage - We require post dominance frontiers (aka Control
|
// getAnalysisUsage - We require post dominance frontiers (aka Control
|
||||||
// Dependence Graph)
|
// Dependence Graph)
|
||||||
@ -122,14 +101,14 @@ bool DSE::runOnBasicBlock(BasicBlock &BB) {
|
|||||||
|
|
||||||
Value* pointer = 0;
|
Value* pointer = 0;
|
||||||
if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
|
if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
|
||||||
if (!S->isVolatile())
|
if (S->isVolatile())
|
||||||
pointer = S->getPointerOperand();
|
|
||||||
else
|
|
||||||
continue;
|
continue;
|
||||||
} else
|
pointer = S->getPointerOperand();
|
||||||
|
} else {
|
||||||
pointer = cast<FreeInst>(BBI)->getPointerOperand();
|
pointer = cast<FreeInst>(BBI)->getPointerOperand();
|
||||||
|
}
|
||||||
|
|
||||||
TranslatePointerBitCasts(pointer, true);
|
pointer = pointer->stripPointerCasts();
|
||||||
StoreInst*& last = lastStore[pointer];
|
StoreInst*& last = lastStore[pointer];
|
||||||
bool deletedStore = false;
|
bool deletedStore = false;
|
||||||
|
|
||||||
@ -302,9 +281,8 @@ bool DSE::handleEndBlock(BasicBlock& BB,
|
|||||||
// If we find a store whose pointer is dead...
|
// If we find a store whose pointer is dead...
|
||||||
if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
|
if (StoreInst* S = dyn_cast<StoreInst>(BBI)) {
|
||||||
if (!S->isVolatile()) {
|
if (!S->isVolatile()) {
|
||||||
Value* pointerOperand = S->getPointerOperand();
|
|
||||||
// See through pointer-to-pointer bitcasts
|
// See through pointer-to-pointer bitcasts
|
||||||
TranslatePointerBitCasts(pointerOperand);
|
Value* pointerOperand = S->getPointerOperand()->getUnderlyingObject();
|
||||||
|
|
||||||
// Alloca'd pointers or byval arguments (which are functionally like
|
// Alloca'd pointers or byval arguments (which are functionally like
|
||||||
// alloca's) are valid candidates for removal.
|
// alloca's) are valid candidates for removal.
|
||||||
@ -330,8 +308,7 @@ bool DSE::handleEndBlock(BasicBlock& BB,
|
|||||||
|
|
||||||
// We can also remove memcpy's to local variables at the end of a function
|
// We can also remove memcpy's to local variables at the end of a function
|
||||||
} else if (MemCpyInst* M = dyn_cast<MemCpyInst>(BBI)) {
|
} else if (MemCpyInst* M = dyn_cast<MemCpyInst>(BBI)) {
|
||||||
Value* dest = M->getDest();
|
Value* dest = M->getDest()->getUnderlyingObject();
|
||||||
TranslatePointerBitCasts(dest);
|
|
||||||
|
|
||||||
if (deadPointers.count(dest)) {
|
if (deadPointers.count(dest)) {
|
||||||
MD.removeInstruction(M);
|
MD.removeInstruction(M);
|
||||||
@ -481,7 +458,7 @@ bool DSE::handleEndBlock(BasicBlock& BB,
|
|||||||
if (!killPointer)
|
if (!killPointer)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
TranslatePointerBitCasts(killPointer);
|
killPointer = killPointer->getUnderlyingObject();
|
||||||
|
|
||||||
// Deal with undead pointers
|
// Deal with undead pointers
|
||||||
MadeChange |= RemoveUndeadPointers(killPointer, killPointerSize, BBI,
|
MadeChange |= RemoveUndeadPointers(killPointer, killPointerSize, BBI,
|
||||||
|
@ -992,16 +992,7 @@ bool GVN::processLoad(LoadInst *L, DenseMap<Value*, LoadInst*> &lastLoad,
|
|||||||
isa<AllocationInst>(dep)) {
|
isa<AllocationInst>(dep)) {
|
||||||
// Check that this load is actually from the
|
// Check that this load is actually from the
|
||||||
// allocation we found
|
// allocation we found
|
||||||
Value* v = L->getOperand(0);
|
if (L->getOperand(0)->getUnderlyingObject() == dep) {
|
||||||
while (true) {
|
|
||||||
if (BitCastInst *BC = dyn_cast<BitCastInst>(v))
|
|
||||||
v = BC->getOperand(0);
|
|
||||||
else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(v))
|
|
||||||
v = GEP->getOperand(0);
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (v == dep) {
|
|
||||||
// If this load depends directly on an allocation, there isn't
|
// If this load depends directly on an allocation, there isn't
|
||||||
// anything stored there; therefore, we can optimize this load
|
// anything stored there; therefore, we can optimize this load
|
||||||
// to undef.
|
// to undef.
|
||||||
|
@ -10367,28 +10367,6 @@ static bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// GetUnderlyingObject - Trace through a series of getelementptrs and bitcasts
|
|
||||||
/// until we find the underlying object a pointer is referring to or something
|
|
||||||
/// we don't understand. Note that the returned pointer may be offset from the
|
|
||||||
/// input, because we ignore GEP indices.
|
|
||||||
static Value *GetUnderlyingObject(Value *Ptr) {
|
|
||||||
while (1) {
|
|
||||||
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
|
|
||||||
if (CE->getOpcode() == Instruction::BitCast ||
|
|
||||||
CE->getOpcode() == Instruction::GetElementPtr)
|
|
||||||
Ptr = CE->getOperand(0);
|
|
||||||
else
|
|
||||||
return Ptr;
|
|
||||||
} else if (BitCastInst *BCI = dyn_cast<BitCastInst>(Ptr)) {
|
|
||||||
Ptr = BCI->getOperand(0);
|
|
||||||
} else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
|
|
||||||
Ptr = GEP->getOperand(0);
|
|
||||||
} else {
|
|
||||||
return Ptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
|
Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
|
||||||
Value *Op = LI.getOperand(0);
|
Value *Op = LI.getOperand(0);
|
||||||
|
|
||||||
@ -10479,7 +10457,7 @@ Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
|
|||||||
|
|
||||||
// If this load comes from anywhere in a constant global, and if the global
|
// If this load comes from anywhere in a constant global, and if the global
|
||||||
// is all undef or zero, we know what it loads.
|
// is all undef or zero, we know what it loads.
|
||||||
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(Op))) {
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op->getUnderlyingObject())){
|
||||||
if (GV->isConstant() && GV->hasInitializer()) {
|
if (GV->isConstant() && GV->hasInitializer()) {
|
||||||
if (GV->getInitializer()->isNullValue())
|
if (GV->getInitializer()->isNullValue())
|
||||||
return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType()));
|
return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType()));
|
||||||
|
@ -322,9 +322,11 @@ void Value::replaceAllUsesWith(Value *New) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Value *Value::stripPointerCasts() {
|
Value *Value::stripPointerCasts() {
|
||||||
|
if (!isa<PointerType>(getType()))
|
||||||
|
return this;
|
||||||
|
|
||||||
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(this)) {
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(this)) {
|
||||||
if (CE->getOpcode() == Instruction::BitCast) {
|
if (CE->getOpcode() == Instruction::BitCast) {
|
||||||
if (isa<PointerType>(CE->getOperand(0)->getType()))
|
|
||||||
return CE->getOperand(0)->stripPointerCasts();
|
return CE->getOperand(0)->stripPointerCasts();
|
||||||
} else if (CE->getOpcode() == Instruction::GetElementPtr) {
|
} else if (CE->getOpcode() == Instruction::GetElementPtr) {
|
||||||
for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
|
for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
|
||||||
@ -332,11 +334,7 @@ Value *Value::stripPointerCasts() {
|
|||||||
return this;
|
return this;
|
||||||
return CE->getOperand(0)->stripPointerCasts();
|
return CE->getOperand(0)->stripPointerCasts();
|
||||||
}
|
}
|
||||||
return this;
|
} else if (BitCastInst *CI = dyn_cast<BitCastInst>(this)) {
|
||||||
}
|
|
||||||
|
|
||||||
if (BitCastInst *CI = dyn_cast<BitCastInst>(this)) {
|
|
||||||
if (isa<PointerType>(CI->getOperand(0)->getType()))
|
|
||||||
return CI->getOperand(0)->stripPointerCasts();
|
return CI->getOperand(0)->stripPointerCasts();
|
||||||
} else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(this)) {
|
} else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(this)) {
|
||||||
if (GEP->hasAllZeroIndices())
|
if (GEP->hasAllZeroIndices())
|
||||||
@ -345,6 +343,21 @@ Value *Value::stripPointerCasts() {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Value *Value::getUnderlyingObject() {
|
||||||
|
if (!isa<PointerType>(getType()))
|
||||||
|
return this;
|
||||||
|
|
||||||
|
if (Instruction *I = dyn_cast<Instruction>(this)) {
|
||||||
|
if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I))
|
||||||
|
return I->getOperand(0)->getUnderlyingObject();
|
||||||
|
} else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(this)) {
|
||||||
|
if (CE->getOpcode() == Instruction::BitCast ||
|
||||||
|
CE->getOpcode() == Instruction::GetElementPtr)
|
||||||
|
return CE->getOperand(0)->getUnderlyingObject();
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
// User Class
|
// User Class
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
Loading…
x
Reference in New Issue
Block a user