Skip to content

Commit 4c9b94d

Browse files
committed
comments
1 parent 772d36d commit 4c9b94d

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

llvm/lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@ static cl::opt<bool> Widen16BitOps(
5151
cl::ReallyHidden,
5252
cl::init(true));
5353

54-
static cl::opt<bool> LowerAddrSpaceCast(
55-
"amdgpu-codegenprepare-addrspacecast",
56-
cl::desc("Detect non-null addrspacecast source and lower them early to "
57-
"avoid the null pointer check"),
58-
cl::ReallyHidden, cl::init(true));
59-
6054
static cl::opt<bool>
6155
BreakLargePHIs("amdgpu-codegenprepare-break-large-phis",
6256
cl::desc("Break large PHI nodes for DAGISel"),
@@ -2021,10 +2015,39 @@ bool AMDGPUCodeGenPrepareImpl::visitPHINode(PHINode &I) {
20212015
return true;
20222016
}
20232017

2024-
bool AMDGPUCodeGenPrepareImpl::visitAddrSpaceCastInst(AddrSpaceCastInst &I) {
2025-
if (!LowerAddrSpaceCast)
2026-
return false;
2018+
/// \param V Value to check
2019+
/// \param DL DataLayout
2020+
/// \param TM TargetMachine (TODO: remove once DL contains nullptr values)
2021+
/// \param AS Target Address Space
2022+
/// \return true if \p V cannot be the null value of \p AS, false otherwise.
2023+
static bool isPtrKnownNeverNull(Value *V, const DataLayout &DL,
2024+
const AMDGPUTargetMachine &TM, unsigned AS) {
2025+
// Pointer cannot be null if it's a block address, GV or alloca.
2026+
// NOTE: We don't support extern_weak, but if we did, we'd need to check for
2027+
// it as the symbol could be null in such cases.
2028+
if (isa<BlockAddress>(V) || isa<GlobalValue>(V) || isa<AllocaInst>(V))
2029+
return true;
20272030

2031+
// Check nonnull arguments.
2032+
if (const auto *Arg = dyn_cast<Argument>(V); Arg && Arg->hasNonNullAttr())
2033+
return true;
2034+
2035+
// TODO: Calls that return nonnull?
2036+
2037+
// For all other things, use KnownBits.
2038+
// We either use 0 or all bits set to indicate null, so check whether the
2039+
// value can be zero or all ones.
2040+
//
2041+
// TODO: Use ValueTracking's isKnownNeverNull if it becomes aware that some
2042+
// address spaces have non-zero null values.
2043+
auto SrcPtrKB = computeKnownBits(V, DL).trunc(DL.getPointerSizeInBits(AS));
2044+
const auto NullVal = TM.getNullPointerValue(AS);
2045+
assert((NullVal == 0 || NullVal == -1) &&
2046+
"don't know how to check for this null value!");
2047+
return NullVal ? !SrcPtrKB.getMaxValue().isAllOnes() : SrcPtrKB.isNonZero();
2048+
}
2049+
2050+
bool AMDGPUCodeGenPrepareImpl::visitAddrSpaceCastInst(AddrSpaceCastInst &I) {
20282051
// Check if this can be lowered to a amdgcn.addrspacecast.nonnull.
20292052
// This is only worthwhile for casts from/to priv/local to flat.
20302053
const unsigned SrcAS = I.getSrcAddressSpace();
@@ -2040,25 +2063,13 @@ bool AMDGPUCodeGenPrepareImpl::visitAddrSpaceCastInst(AddrSpaceCastInst &I) {
20402063
if (!CanLower)
20412064
return false;
20422065

2043-
// Check the Src operand, and look through Phis.
2066+
// Check the Src operand, looking through any PHIs.
20442067
SmallVector<Value *, 4> WorkList;
20452068
DenseSet<const PHINode *> SeenPHIs;
20462069
WorkList.push_back(I.getOperand(0));
20472070
while (!WorkList.empty()) {
20482071
Value *Cur = getUnderlyingObject(WorkList.pop_back_val());
20492072

2050-
// Pointer cannot be null if it's a block address, GV or alloca.
2051-
// NOTE: We don't support extern_weak, but if we did, we'd need to check for
2052-
// it as the symbol could be null in such cases.
2053-
if (isa<BlockAddress>(Cur) || isa<GlobalValue>(Cur) || isa<AllocaInst>(Cur))
2054-
continue;
2055-
2056-
// Check nonnull arguments.
2057-
if (const auto *Arg = dyn_cast<Argument>(Cur); Arg && Arg->hasNonNullAttr())
2058-
continue;
2059-
2060-
// TODO: Calls that return nonnull?
2061-
20622073
// Look through PHIs - add all incoming values to the queue.
20632074
if (const auto *Phi = dyn_cast<PHINode>(Cur)) {
20642075
auto [It, Inserted] = SeenPHIs.insert(Phi);
@@ -2070,18 +2081,8 @@ bool AMDGPUCodeGenPrepareImpl::visitAddrSpaceCastInst(AddrSpaceCastInst &I) {
20702081
continue;
20712082
}
20722083

2073-
// For all other things, use KnownBits.
2074-
// We either use 0 or all bits set to indicate null, so check whether the
2075-
// value can be zero or all ones.
2076-
auto SrcPtrKB =
2077-
computeKnownBits(Cur, *DL).trunc(DL->getPointerSizeInBits(SrcAS));
2078-
const auto NullVal = TM->getNullPointerValue(SrcAS);
2079-
assert((NullVal == 0 || NullVal == -1) &&
2080-
"don't know how to check for this null value!");
2081-
if (NullVal ? !SrcPtrKB.getMaxValue().isAllOnes() : SrcPtrKB.isNonZero())
2084+
if (isPtrKnownNeverNull(Cur, *DL, *TM, SrcAS))
20822085
continue;
2083-
2084-
// Value is unknown so we can't lower.
20852086
return false;
20862087
}
20872088

0 commit comments

Comments
 (0)