@@ -51,12 +51,6 @@ static cl::opt<bool> Widen16BitOps(
51
51
cl::ReallyHidden,
52
52
cl::init(true ));
53
53
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
-
60
54
static cl::opt<bool >
61
55
BreakLargePHIs (" amdgpu-codegenprepare-break-large-phis" ,
62
56
cl::desc (" Break large PHI nodes for DAGISel" ),
@@ -2021,10 +2015,39 @@ bool AMDGPUCodeGenPrepareImpl::visitPHINode(PHINode &I) {
2021
2015
return true ;
2022
2016
}
2023
2017
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 ;
2027
2030
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) {
2028
2051
// Check if this can be lowered to a amdgcn.addrspacecast.nonnull.
2029
2052
// This is only worthwhile for casts from/to priv/local to flat.
2030
2053
const unsigned SrcAS = I.getSrcAddressSpace ();
@@ -2040,25 +2063,13 @@ bool AMDGPUCodeGenPrepareImpl::visitAddrSpaceCastInst(AddrSpaceCastInst &I) {
2040
2063
if (!CanLower)
2041
2064
return false ;
2042
2065
2043
- // Check the Src operand, and look through Phis .
2066
+ // Check the Src operand, looking through any PHIs .
2044
2067
SmallVector<Value *, 4 > WorkList;
2045
2068
DenseSet<const PHINode *> SeenPHIs;
2046
2069
WorkList.push_back (I.getOperand (0 ));
2047
2070
while (!WorkList.empty ()) {
2048
2071
Value *Cur = getUnderlyingObject (WorkList.pop_back_val ());
2049
2072
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
-
2062
2073
// Look through PHIs - add all incoming values to the queue.
2063
2074
if (const auto *Phi = dyn_cast<PHINode>(Cur)) {
2064
2075
auto [It, Inserted] = SeenPHIs.insert (Phi);
@@ -2070,18 +2081,8 @@ bool AMDGPUCodeGenPrepareImpl::visitAddrSpaceCastInst(AddrSpaceCastInst &I) {
2070
2081
continue ;
2071
2082
}
2072
2083
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))
2082
2085
continue ;
2083
-
2084
- // Value is unknown so we can't lower.
2085
2086
return false ;
2086
2087
}
2087
2088
0 commit comments