brintos

brintos / llvm-project-archived public Read only

0
0
Text · 7.4 KiB · ee5480c Raw
181 lines · cpp
1//===- ScalarEvolutionAliasAnalysis.cpp - SCEV-based Alias Analysis -------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// This file defines the ScalarEvolutionAliasAnalysis pass, which implements a10// simple alias analysis implemented in terms of ScalarEvolution queries.11//12// This differs from traditional loop dependence analysis in that it tests13// for dependencies within a single iteration of a loop, rather than14// dependencies between different iterations.15//16// ScalarEvolution has a more complete understanding of pointer arithmetic17// than BasicAliasAnalysis' collection of ad-hoc analyses.18//19//===----------------------------------------------------------------------===//20 21#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"22#include "llvm/Analysis/ScalarEvolution.h"23#include "llvm/Analysis/ScalarEvolutionExpressions.h"24#include "llvm/InitializePasses.h"25using namespace llvm;26 27static bool canComputePointerDiff(ScalarEvolution &SE,28                                  const SCEV *A, const SCEV *B) {29  if (SE.getEffectiveSCEVType(A->getType()) !=30      SE.getEffectiveSCEVType(B->getType()))31    return false;32 33  return SE.instructionCouldExistWithOperands(A, B);34}35 36AliasResult SCEVAAResult::alias(const MemoryLocation &LocA,37                                const MemoryLocation &LocB, AAQueryInfo &AAQI,38                                const Instruction *) {39  // If either of the memory references is empty, it doesn't matter what the40  // pointer values are. This allows the code below to ignore this special41  // case.42  if (LocA.Size.isZero() || LocB.Size.isZero())43    return AliasResult::NoAlias;44 45  // This is SCEVAAResult. Get the SCEVs!46  const SCEV *AS = SE.getSCEV(const_cast<Value *>(LocA.Ptr));47  const SCEV *BS = SE.getSCEV(const_cast<Value *>(LocB.Ptr));48 49  // If they evaluate to the same expression, it's a MustAlias.50  if (AS == BS)51    return AliasResult::MustAlias;52 53  // If something is known about the difference between the two addresses,54  // see if it's enough to prove a NoAlias.55  if (canComputePointerDiff(SE, AS, BS)) {56    unsigned BitWidth = SE.getTypeSizeInBits(AS->getType());57    APInt ASizeInt(BitWidth, LocA.Size.hasValue()58                                 ? static_cast<uint64_t>(LocA.Size.getValue())59                                 : MemoryLocation::UnknownSize);60    APInt BSizeInt(BitWidth, LocB.Size.hasValue()61                                 ? static_cast<uint64_t>(LocB.Size.getValue())62                                 : MemoryLocation::UnknownSize);63 64    // Firstly, try to convert the two pointers into ptrtoint expressions to65    // handle two pointers with different pointer bases.66    // Either both pointers are used with ptrtoint or neither, so we can't end67    // up with a ptr + int mix.68    const SCEV *AInt =69        SE.getPtrToIntExpr(AS, SE.getEffectiveSCEVType(AS->getType()));70    const SCEV *BInt =71        SE.getPtrToIntExpr(BS, SE.getEffectiveSCEVType(BS->getType()));72    if (!isa<SCEVCouldNotCompute>(AInt) && !isa<SCEVCouldNotCompute>(BInt)) {73      AS = AInt;74      BS = BInt;75    }76 77    // Compute the difference between the two pointers.78    const SCEV *BA = SE.getMinusSCEV(BS, AS);79 80    // Test whether the difference is known to be great enough that memory of81    // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt82    // are non-zero, which is special-cased above.83    if (!isa<SCEVCouldNotCompute>(BA) &&84        ASizeInt.ule(SE.getUnsignedRange(BA).getUnsignedMin()) &&85        (-BSizeInt).uge(SE.getUnsignedRange(BA).getUnsignedMax()))86      return AliasResult::NoAlias;87 88    // Folding the subtraction while preserving range information can be tricky89    // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS90    // and try again to see if things fold better that way.91 92    // Compute the difference between the two pointers.93    const SCEV *AB = SE.getMinusSCEV(AS, BS);94 95    // Test whether the difference is known to be great enough that memory of96    // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt97    // are non-zero, which is special-cased above.98    if (!isa<SCEVCouldNotCompute>(AB) &&99        BSizeInt.ule(SE.getUnsignedRange(AB).getUnsignedMin()) &&100        (-ASizeInt).uge(SE.getUnsignedRange(AB).getUnsignedMax()))101      return AliasResult::NoAlias;102  }103 104  // If ScalarEvolution can find an underlying object, form a new query.105  // The correctness of this depends on ScalarEvolution not recognizing106  // inttoptr and ptrtoint operators.107  Value *AO = GetBaseValue(AS);108  Value *BO = GetBaseValue(BS);109  if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr))110    if (alias(MemoryLocation(AO ? AO : LocA.Ptr,111                             AO ? LocationSize::beforeOrAfterPointer()112                                : LocA.Size,113                             AO ? AAMDNodes() : LocA.AATags),114              MemoryLocation(BO ? BO : LocB.Ptr,115                             BO ? LocationSize::beforeOrAfterPointer()116                                : LocB.Size,117                             BO ? AAMDNodes() : LocB.AATags),118              AAQI, nullptr) == AliasResult::NoAlias)119      return AliasResult::NoAlias;120 121  return AliasResult::MayAlias;122}123 124/// Given an expression, try to find a base value.125///126/// Returns null if none was found.127Value *SCEVAAResult::GetBaseValue(const SCEV *S) {128  if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {129    // In an addrec, assume that the base will be in the start, rather130    // than the step.131    return GetBaseValue(AR->getStart());132  } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {133    // If there's a pointer operand, it'll be sorted at the end of the list.134    const SCEV *Last = A->getOperand(A->getNumOperands() - 1);135    if (Last->getType()->isPointerTy())136      return GetBaseValue(Last);137  } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {138    // This is a leaf node.139    return U->getValue();140  }141  // No Identified object found.142  return nullptr;143}144 145bool SCEVAAResult::invalidate(Function &Fn, const PreservedAnalyses &PA,146                              FunctionAnalysisManager::Invalidator &Inv) {147  // We don't care if this analysis itself is preserved, it has no state. But148  // we need to check that the analyses it depends on have been.149  return Inv.invalidate<ScalarEvolutionAnalysis>(Fn, PA);150}151 152AnalysisKey SCEVAA::Key;153 154SCEVAAResult SCEVAA::run(Function &F, FunctionAnalysisManager &AM) {155  return SCEVAAResult(AM.getResult<ScalarEvolutionAnalysis>(F));156}157 158char SCEVAAWrapperPass::ID = 0;159INITIALIZE_PASS_BEGIN(SCEVAAWrapperPass, "scev-aa",160                      "ScalarEvolution-based Alias Analysis", false, true)161INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)162INITIALIZE_PASS_END(SCEVAAWrapperPass, "scev-aa",163                    "ScalarEvolution-based Alias Analysis", false, true)164 165FunctionPass *llvm::createSCEVAAWrapperPass() {166  return new SCEVAAWrapperPass();167}168 169SCEVAAWrapperPass::SCEVAAWrapperPass() : FunctionPass(ID) {}170 171bool SCEVAAWrapperPass::runOnFunction(Function &F) {172  Result.reset(173      new SCEVAAResult(getAnalysis<ScalarEvolutionWrapperPass>().getSE()));174  return false;175}176 177void SCEVAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {178  AU.setPreservesAll();179  AU.addRequired<ScalarEvolutionWrapperPass>();180}181