~ubuntu-branches/ubuntu/maverick/clamav/maverick-backports

« back to all changes in this revision

Viewing changes to libclamav/c++/llvm/lib/Analysis/ScalarEvolutionAliasAnalysis.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Stephen Gran, Stephen Gran, Michael Tautschnig
  • Date: 2010-04-26 21:41:18 UTC
  • mfrom: (2.1.6 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100426214118-i6lo606wnh7ywfj6
Tags: 0.96+dfsg-4
[ Stephen Gran ]
* Fixed typo in clamav-milter's postinst

[ Michael Tautschnig ]
* Fixed typo in clamav-freshclam's postinst (closes: #579271)
* Debconf translation updates
  - Portuguese (closes: #579068)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//===- ScalarEvolutionAliasAnalysis.cpp - SCEV-based Alias Analysis -------===//
 
2
//
 
3
//                     The LLVM Compiler Infrastructure
 
4
//
 
5
// This file is distributed under the University of Illinois Open Source
 
6
// License. See LICENSE.TXT for details.
 
7
//
 
8
//===----------------------------------------------------------------------===//
 
9
//
 
10
// This file defines the ScalarEvolutionAliasAnalysis pass, which implements a
 
11
// simple alias analysis implemented in terms of ScalarEvolution queries.
 
12
//
 
13
// This differs from traditional loop dependence analysis in that it tests
 
14
// for dependencies within a single iteration of a loop, rather than
 
15
// dependences between different iterations.
 
16
//
 
17
// ScalarEvolution has a more complete understanding of pointer arithmetic
 
18
// than BasicAliasAnalysis' collection of ad-hoc analyses.
 
19
//
 
20
//===----------------------------------------------------------------------===//
 
21
 
 
22
#include "llvm/Analysis/AliasAnalysis.h"
 
23
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
 
24
#include "llvm/Analysis/Passes.h"
 
25
#include "llvm/Pass.h"
 
26
using namespace llvm;
 
27
 
 
28
namespace {
 
29
  /// ScalarEvolutionAliasAnalysis - This is a simple alias analysis
 
30
  /// implementation that uses ScalarEvolution to answer queries.
 
31
  class ScalarEvolutionAliasAnalysis : public FunctionPass,
 
32
                                       public AliasAnalysis {
 
33
    ScalarEvolution *SE;
 
34
 
 
35
  public:
 
36
    static char ID; // Class identification, replacement for typeinfo
 
37
    ScalarEvolutionAliasAnalysis() : FunctionPass(&ID), SE(0) {}
 
38
 
 
39
    /// getAdjustedAnalysisPointer - This method is used when a pass implements
 
40
    /// an analysis interface through multiple inheritance.  If needed, it
 
41
    /// should override this to adjust the this pointer as needed for the
 
42
    /// specified pass info.
 
43
    virtual void *getAdjustedAnalysisPointer(const PassInfo *PI) {
 
44
      if (PI->isPassID(&AliasAnalysis::ID))
 
45
        return (AliasAnalysis*)this;
 
46
      return this;
 
47
    }
 
48
 
 
49
  private:
 
50
    virtual void getAnalysisUsage(AnalysisUsage &AU) const;
 
51
    virtual bool runOnFunction(Function &F);
 
52
    virtual AliasResult alias(const Value *V1, unsigned V1Size,
 
53
                              const Value *V2, unsigned V2Size);
 
54
 
 
55
    Value *GetBaseValue(const SCEV *S);
 
56
  };
 
57
}  // End of anonymous namespace
 
58
 
 
59
// Register this pass...
 
60
char ScalarEvolutionAliasAnalysis::ID = 0;
 
61
static RegisterPass<ScalarEvolutionAliasAnalysis>
 
62
X("scev-aa", "ScalarEvolution-based Alias Analysis", false, true);
 
63
 
 
64
// Declare that we implement the AliasAnalysis interface
 
65
static RegisterAnalysisGroup<AliasAnalysis> Y(X);
 
66
 
 
67
FunctionPass *llvm::createScalarEvolutionAliasAnalysisPass() {
 
68
  return new ScalarEvolutionAliasAnalysis();
 
69
}
 
70
 
 
71
void
 
72
ScalarEvolutionAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
 
73
  AU.addRequiredTransitive<ScalarEvolution>();
 
74
  AU.setPreservesAll();
 
75
  AliasAnalysis::getAnalysisUsage(AU);
 
76
}
 
77
 
 
78
bool
 
79
ScalarEvolutionAliasAnalysis::runOnFunction(Function &F) {
 
80
  InitializeAliasAnalysis(this);
 
81
  SE = &getAnalysis<ScalarEvolution>();
 
82
  return false;
 
83
}
 
84
 
 
85
/// GetBaseValue - Given an expression, try to find a
 
86
/// base value. Return null is none was found.
 
87
Value *
 
88
ScalarEvolutionAliasAnalysis::GetBaseValue(const SCEV *S) {
 
89
  if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
 
90
    // In an addrec, assume that the base will be in the start, rather
 
91
    // than the step.
 
92
    return GetBaseValue(AR->getStart());
 
93
  } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
 
94
    // If there's a pointer operand, it'll be sorted at the end of the list.
 
95
    const SCEV *Last = A->getOperand(A->getNumOperands()-1);
 
96
    if (Last->getType()->isPointerTy())
 
97
      return GetBaseValue(Last);
 
98
  } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
 
99
    // This is a leaf node.
 
100
    return U->getValue();
 
101
  }
 
102
  // No Identified object found.
 
103
  return 0;
 
104
}
 
105
 
 
106
AliasAnalysis::AliasResult
 
107
ScalarEvolutionAliasAnalysis::alias(const Value *A, unsigned ASize,
 
108
                                    const Value *B, unsigned BSize) {
 
109
  // This is ScalarEvolutionAliasAnalysis. Get the SCEVs!
 
110
  const SCEV *AS = SE->getSCEV(const_cast<Value *>(A));
 
111
  const SCEV *BS = SE->getSCEV(const_cast<Value *>(B));
 
112
 
 
113
  // If they evaluate to the same expression, it's a MustAlias.
 
114
  if (AS == BS) return MustAlias;
 
115
 
 
116
  // If something is known about the difference between the two addresses,
 
117
  // see if it's enough to prove a NoAlias.
 
118
  if (SE->getEffectiveSCEVType(AS->getType()) ==
 
119
      SE->getEffectiveSCEVType(BS->getType())) {
 
120
    unsigned BitWidth = SE->getTypeSizeInBits(AS->getType());
 
121
    APInt AI(BitWidth, ASize);
 
122
    const SCEV *BA = SE->getMinusSCEV(BS, AS);
 
123
    if (AI.ule(SE->getUnsignedRange(BA).getUnsignedMin())) {
 
124
      APInt BI(BitWidth, BSize);
 
125
      const SCEV *AB = SE->getMinusSCEV(AS, BS);
 
126
      if (BI.ule(SE->getUnsignedRange(AB).getUnsignedMin()))
 
127
        return NoAlias;
 
128
    }
 
129
  }
 
130
 
 
131
  // If ScalarEvolution can find an underlying object, form a new query.
 
132
  // The correctness of this depends on ScalarEvolution not recognizing
 
133
  // inttoptr and ptrtoint operators.
 
134
  Value *AO = GetBaseValue(AS);
 
135
  Value *BO = GetBaseValue(BS);
 
136
  if ((AO && AO != A) || (BO && BO != B))
 
137
    if (alias(AO ? AO : A, AO ? ~0u : ASize,
 
138
              BO ? BO : B, BO ? ~0u : BSize) == NoAlias)
 
139
      return NoAlias;
 
140
 
 
141
  // Forward the query to the next analysis.
 
142
  return AliasAnalysis::alias(A, ASize, B, BSize);
 
143
}