@@ -243,6 +243,14 @@ class StackSafetyLocalAnalysis {
243243
244244 const ConstantRange UnknownRange;
245245
246+ // / FIXME: This function is a bandaid, it's only needed
247+ // / because this pass doesn't handle address spaces of different pointer
248+ // / sizes.
249+ // /
250+ // / \returns \p Val's SCEV as a pointer of AS zero, or nullptr if it can't be
251+ // / converted to AS 0.
252+ const SCEV *getSCEVAsPointer (Value *Val);
253+
246254 ConstantRange offsetFrom (Value *Addr, Value *Base);
247255 ConstantRange getAccessRange (Value *Addr, Value *Base,
248256 const ConstantRange &SizeRange);
@@ -268,13 +276,29 @@ class StackSafetyLocalAnalysis {
268276 FunctionInfo<GlobalValue> run ();
269277};
270278
279+ const SCEV *StackSafetyLocalAnalysis::getSCEVAsPointer (Value *Val) {
280+ Type *ValTy = Val->getType ();
281+
282+ // We don't handle targets with multiple address spaces.
283+ if (!ValTy->isPointerTy ()) {
284+ auto *PtrTy = PointerType::getUnqual (SE.getContext ());
285+ return SE.getTruncateOrZeroExtend (SE.getSCEV (Val), PtrTy);
286+ }
287+
288+ if (ValTy->getPointerAddressSpace () != 0 )
289+ return nullptr ;
290+ return SE.getSCEV (Val);
291+ }
292+
271293ConstantRange StackSafetyLocalAnalysis::offsetFrom (Value *Addr, Value *Base) {
272294 if (!SE.isSCEVable (Addr->getType ()) || !SE.isSCEVable (Base->getType ()))
273295 return UnknownRange;
274296
275- auto *PtrTy = PointerType::getUnqual (SE.getContext ());
276- const SCEV *AddrExp = SE.getTruncateOrZeroExtend (SE.getSCEV (Addr), PtrTy);
277- const SCEV *BaseExp = SE.getTruncateOrZeroExtend (SE.getSCEV (Base), PtrTy);
297+ const SCEV *AddrExp = getSCEVAsPointer (Addr);
298+ const SCEV *BaseExp = getSCEVAsPointer (Base);
299+ if (!AddrExp || !BaseExp)
300+ return UnknownRange;
301+
278302 const SCEV *Diff = SE.getMinusSCEV (AddrExp, BaseExp);
279303 if (isa<SCEVCouldNotCompute>(Diff))
280304 return UnknownRange;
@@ -362,13 +386,11 @@ bool StackSafetyLocalAnalysis::isSafeAccess(const Use &U, AllocaInst *AI,
362386
363387 const auto *I = cast<Instruction>(U.getUser ());
364388
365- auto ToCharPtr = [&]( const SCEV *V) {
366- auto *PtrTy = PointerType::getUnqual (SE. getContext () );
367- return SE. getTruncateOrZeroExtend (V, PtrTy);
368- } ;
389+ const SCEV *AddrExp = getSCEVAsPointer (U. get ());
390+ const SCEV *BaseExp = getSCEVAsPointer (AI );
391+ if (!AddrExp || !BaseExp)
392+ return false ;
369393
370- const SCEV *AddrExp = ToCharPtr (SE.getSCEV (U.get ()));
371- const SCEV *BaseExp = ToCharPtr (SE.getSCEV (AI));
372394 const SCEV *Diff = SE.getMinusSCEV (AddrExp, BaseExp);
373395 if (isa<SCEVCouldNotCompute>(Diff))
374396 return false ;
0 commit comments