Refactor isPointerOffset (NFC).
Summary: Simplify the API using Optional<> and address comments in https://reviews.llvm.org/D66165 Reviewers: vitalybuka Subscribers: hiraditya, llvm-commits, ostannard, pcc Tags: #llvm Differential Revision: https://reviews.llvm.org/D66317 llvm-svn: 369300
This commit is contained in:
parent
f7229ac7d8
commit
55ccd16354
|
@ -661,11 +661,11 @@ class Value;
|
||||||
const Instruction *ContextI,
|
const Instruction *ContextI,
|
||||||
const DataLayout &DL);
|
const DataLayout &DL);
|
||||||
|
|
||||||
/// Return true if Ptr1 is provably equal to Ptr2 plus a constant offset, and
|
/// If Ptr1 is provably equal to Ptr2 plus a constant offset, return that
|
||||||
/// return that constant offset. For example, Ptr1 might be &A[42], and Ptr2
|
/// offset. For example, Ptr1 might be &A[42], and Ptr2 might be &A[40]. In
|
||||||
/// might be &A[40]. In this case offset would be -8.
|
/// this case offset would be -8.
|
||||||
bool isPointerOffset(Value *Ptr1, Value *Ptr2, int64_t &Offset,
|
Optional<int64_t> isPointerOffset(const Value *Ptr1, const Value *Ptr2,
|
||||||
const DataLayout &DL);
|
const DataLayout &DL);
|
||||||
} // end namespace llvm
|
} // end namespace llvm
|
||||||
|
|
||||||
#endif // LLVM_ANALYSIS_VALUETRACKING_H
|
#endif // LLVM_ANALYSIS_VALUETRACKING_H
|
||||||
|
|
|
@ -5702,9 +5702,8 @@ ConstantRange llvm::computeConstantRange(const Value *V, bool UseInstrInfo) {
|
||||||
return CR;
|
return CR;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int64_t getOffsetFromIndex(const GEPOperator *GEP, unsigned Idx,
|
static Optional<int64_t>
|
||||||
bool &VariableIdxFound,
|
getOffsetFromIndex(const GEPOperator *GEP, unsigned Idx, const DataLayout &DL) {
|
||||||
const DataLayout &DL) {
|
|
||||||
// Skip over the first indices.
|
// Skip over the first indices.
|
||||||
gep_type_iterator GTI = gep_type_begin(GEP);
|
gep_type_iterator GTI = gep_type_begin(GEP);
|
||||||
for (unsigned i = 1; i != Idx; ++i, ++GTI)
|
for (unsigned i = 1; i != Idx; ++i, ++GTI)
|
||||||
|
@ -5715,7 +5714,7 @@ static int64_t getOffsetFromIndex(const GEPOperator *GEP, unsigned Idx,
|
||||||
for (unsigned i = Idx, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
|
for (unsigned i = Idx, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
|
||||||
ConstantInt *OpC = dyn_cast<ConstantInt>(GEP->getOperand(i));
|
ConstantInt *OpC = dyn_cast<ConstantInt>(GEP->getOperand(i));
|
||||||
if (!OpC)
|
if (!OpC)
|
||||||
return VariableIdxFound = true;
|
return None;
|
||||||
if (OpC->isZero())
|
if (OpC->isZero())
|
||||||
continue; // No offset.
|
continue; // No offset.
|
||||||
|
|
||||||
|
@ -5734,32 +5733,30 @@ static int64_t getOffsetFromIndex(const GEPOperator *GEP, unsigned Idx,
|
||||||
return Offset;
|
return Offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool llvm::isPointerOffset(Value *Ptr1, Value *Ptr2, int64_t &Offset,
|
Optional<int64_t> llvm::isPointerOffset(const Value *Ptr1, const Value *Ptr2,
|
||||||
const DataLayout &DL) {
|
const DataLayout &DL) {
|
||||||
Ptr1 = Ptr1->stripPointerCasts();
|
Ptr1 = Ptr1->stripPointerCasts();
|
||||||
Ptr2 = Ptr2->stripPointerCasts();
|
Ptr2 = Ptr2->stripPointerCasts();
|
||||||
|
|
||||||
// Handle the trivial case first.
|
// Handle the trivial case first.
|
||||||
if (Ptr1 == Ptr2) {
|
if (Ptr1 == Ptr2) {
|
||||||
Offset = 0;
|
return 0;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GEPOperator *GEP1 = dyn_cast<GEPOperator>(Ptr1);
|
const GEPOperator *GEP1 = dyn_cast<GEPOperator>(Ptr1);
|
||||||
GEPOperator *GEP2 = dyn_cast<GEPOperator>(Ptr2);
|
const GEPOperator *GEP2 = dyn_cast<GEPOperator>(Ptr2);
|
||||||
|
|
||||||
bool VariableIdxFound = false;
|
|
||||||
|
|
||||||
// If one pointer is a GEP and the other isn't, then see if the GEP is a
|
// If one pointer is a GEP and the other isn't, then see if the GEP is a
|
||||||
// constant offset from the base, as in "P" and "gep P, 1".
|
// constant offset from the base, as in "P" and "gep P, 1".
|
||||||
if (GEP1 && !GEP2 && GEP1->getOperand(0)->stripPointerCasts() == Ptr2) {
|
if (GEP1 && !GEP2 && GEP1->getOperand(0)->stripPointerCasts() == Ptr2) {
|
||||||
Offset = -getOffsetFromIndex(GEP1, 1, VariableIdxFound, DL);
|
auto Offset = getOffsetFromIndex(GEP1, 1, DL);
|
||||||
return !VariableIdxFound;
|
if (!Offset)
|
||||||
|
return None;
|
||||||
|
return -*Offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (GEP2 && !GEP1 && GEP2->getOperand(0)->stripPointerCasts() == Ptr1) {
|
if (GEP2 && !GEP1 && GEP2->getOperand(0)->stripPointerCasts() == Ptr1) {
|
||||||
Offset = getOffsetFromIndex(GEP2, 1, VariableIdxFound, DL);
|
return getOffsetFromIndex(GEP2, 1, DL);
|
||||||
return !VariableIdxFound;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Right now we handle the case when Ptr1/Ptr2 are both GEPs with an identical
|
// Right now we handle the case when Ptr1/Ptr2 are both GEPs with an identical
|
||||||
|
@ -5768,7 +5765,7 @@ bool llvm::isPointerOffset(Value *Ptr1, Value *Ptr2, int64_t &Offset,
|
||||||
// offset, which determines their offset from each other. At this point, we
|
// offset, which determines their offset from each other. At this point, we
|
||||||
// handle no other case.
|
// handle no other case.
|
||||||
if (!GEP1 || !GEP2 || GEP1->getOperand(0) != GEP2->getOperand(0))
|
if (!GEP1 || !GEP2 || GEP1->getOperand(0) != GEP2->getOperand(0))
|
||||||
return false;
|
return None;
|
||||||
|
|
||||||
// Skip any common indices and track the GEP types.
|
// Skip any common indices and track the GEP types.
|
||||||
unsigned Idx = 1;
|
unsigned Idx = 1;
|
||||||
|
@ -5776,11 +5773,9 @@ bool llvm::isPointerOffset(Value *Ptr1, Value *Ptr2, int64_t &Offset,
|
||||||
if (GEP1->getOperand(Idx) != GEP2->getOperand(Idx))
|
if (GEP1->getOperand(Idx) != GEP2->getOperand(Idx))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
int64_t Offset1 = getOffsetFromIndex(GEP1, Idx, VariableIdxFound, DL);
|
auto Offset1 = getOffsetFromIndex(GEP1, Idx, DL);
|
||||||
int64_t Offset2 = getOffsetFromIndex(GEP2, Idx, VariableIdxFound, DL);
|
auto Offset2 = getOffsetFromIndex(GEP2, Idx, DL);
|
||||||
if (VariableIdxFound)
|
if (!Offset1 || !Offset2)
|
||||||
return false;
|
return None;
|
||||||
|
return *Offset2 - *Offset1;
|
||||||
Offset = Offset2 - Offset1;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -358,12 +358,12 @@ Instruction *AArch64StackTagging::collectInitializers(Instruction *StartInst,
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Check to see if this store is to a constant offset from the start ptr.
|
// Check to see if this store is to a constant offset from the start ptr.
|
||||||
int64_t Offset;
|
Optional<int64_t> Offset =
|
||||||
if (!isPointerOffset(StartPtr, NextStore->getPointerOperand(), Offset,
|
isPointerOffset(StartPtr, NextStore->getPointerOperand(), *DL);
|
||||||
*DL))
|
if (!Offset)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (!IB.addStore(Offset, NextStore, DL))
|
if (!IB.addStore(*Offset, NextStore, DL))
|
||||||
break;
|
break;
|
||||||
LastInst = NextStore;
|
LastInst = NextStore;
|
||||||
} else {
|
} else {
|
||||||
|
@ -376,11 +376,11 @@ Instruction *AArch64StackTagging::collectInitializers(Instruction *StartInst,
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Check to see if this store is to a constant offset from the start ptr.
|
// Check to see if this store is to a constant offset from the start ptr.
|
||||||
int64_t Offset;
|
Optional<int64_t> Offset = isPointerOffset(StartPtr, MSI->getDest(), *DL);
|
||||||
if (!isPointerOffset(StartPtr, MSI->getDest(), Offset, *DL))
|
if (!Offset)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (!IB.addMemSet(Offset, MSI))
|
if (!IB.addMemSet(*Offset, MSI))
|
||||||
break;
|
break;
|
||||||
LastInst = MSI;
|
LastInst = MSI;
|
||||||
}
|
}
|
||||||
|
|
|
@ -335,12 +335,12 @@ Instruction *MemCpyOptPass::tryMergingIntoMemset(Instruction *StartInst,
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Check to see if this store is to a constant offset from the start ptr.
|
// Check to see if this store is to a constant offset from the start ptr.
|
||||||
int64_t Offset;
|
Optional<int64_t> Offset =
|
||||||
if (!isPointerOffset(StartPtr, NextStore->getPointerOperand(), Offset,
|
isPointerOffset(StartPtr, NextStore->getPointerOperand(), DL);
|
||||||
DL))
|
if (!Offset)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
Ranges.addStore(Offset, NextStore);
|
Ranges.addStore(*Offset, NextStore);
|
||||||
} else {
|
} else {
|
||||||
MemSetInst *MSI = cast<MemSetInst>(BI);
|
MemSetInst *MSI = cast<MemSetInst>(BI);
|
||||||
|
|
||||||
|
@ -349,11 +349,11 @@ Instruction *MemCpyOptPass::tryMergingIntoMemset(Instruction *StartInst,
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Check to see if this store is to a constant offset from the start ptr.
|
// Check to see if this store is to a constant offset from the start ptr.
|
||||||
int64_t Offset;
|
Optional<int64_t> Offset = isPointerOffset(StartPtr, MSI->getDest(), DL);
|
||||||
if (!isPointerOffset(StartPtr, MSI->getDest(), Offset, DL))
|
if (!Offset)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
Ranges.addMemSet(Offset, MSI);
|
Ranges.addMemSet(*Offset, MSI);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue