@@ -243,6 +243,14 @@ class StackSafetyLocalAnalysis {
243
243
244
244
const ConstantRange UnknownRange;
245
245
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
+
246
254
ConstantRange offsetFrom (Value *Addr, Value *Base);
247
255
ConstantRange getAccessRange (Value *Addr, Value *Base,
248
256
const ConstantRange &SizeRange);
@@ -268,13 +276,29 @@ class StackSafetyLocalAnalysis {
268
276
FunctionInfo<GlobalValue> run ();
269
277
};
270
278
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
+
271
293
ConstantRange StackSafetyLocalAnalysis::offsetFrom (Value *Addr, Value *Base) {
272
294
if (!SE.isSCEVable (Addr->getType ()) || !SE.isSCEVable (Base->getType ()))
273
295
return UnknownRange;
274
296
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
+
278
302
const SCEV *Diff = SE.getMinusSCEV (AddrExp, BaseExp);
279
303
if (isa<SCEVCouldNotCompute>(Diff))
280
304
return UnknownRange;
@@ -362,13 +386,11 @@ bool StackSafetyLocalAnalysis::isSafeAccess(const Use &U, AllocaInst *AI,
362
386
363
387
const auto *I = cast<Instruction>(U.getUser ());
364
388
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 ;
369
393
370
- const SCEV *AddrExp = ToCharPtr (SE.getSCEV (U.get ()));
371
- const SCEV *BaseExp = ToCharPtr (SE.getSCEV (AI));
372
394
const SCEV *Diff = SE.getMinusSCEV (AddrExp, BaseExp);
373
395
if (isa<SCEVCouldNotCompute>(Diff))
374
396
return false ;
0 commit comments