~louis/ubuntu/trusty/clamav/lp799623_fix_logrotate

« back to all changes in this revision

Viewing changes to libclamav/c++/llvm/lib/CodeGen/OptimizePHIs.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-03-12 11:30:04 UTC
  • mfrom: (0.41.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100312113004-b0fop4bkycszdd0z
Tags: 0.96~rc1+dfsg-0ubuntu1
* New upstream RC - FFE (LP: #537636):
  - Add OfficialDatabaseOnly option to clamav-base.postinst.in
  - Add LocalSocketGroup option to clamav-base.postinst.in
  - Add LocalSocketMode option to clamav-base.postinst.in
  - Add CrossFilesystems option to clamav-base.postinst.in
  - Add ClamukoScannerCount option to clamav-base.postinst.in
  - Add BytecodeSecurity opiton to clamav-base.postinst.in
  - Add DetectionStatsHostID option to clamav-freshclam.postinst.in
  - Add Bytecode option to clamav-freshclam.postinst.in
  - Add MilterSocketGroup option to clamav-milter.postinst.in
  - Add MilterSocketMode option to clamav-milter.postinst.in
  - Add ReportHostname option to clamav-milter.postinst.in
  - Bump libclamav SO version to 6.1.0 in libclamav6.install
  - Drop clamdmon from clamav.examples (no longer shipped by upstream)
  - Drop libclamav.a from libclamav-dev.install (not built by upstream)
  - Update SO version for lintian override for libclamav6
  - Add new Bytecode Testing Tool, usr/bin/clambc, to clamav.install
  - Add build-depends on python and python-setuptools for new test suite
  - Update debian/copyright for the embedded copy of llvm (using the system
    llvm is not currently feasible)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//===-- OptimizePHIs.cpp - Optimize machine instruction PHIs --------------===//
 
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 pass optimizes machine instruction PHIs to take advantage of
 
11
// opportunities created during DAG legalization.
 
12
//
 
13
//===----------------------------------------------------------------------===//
 
14
 
 
15
#define DEBUG_TYPE "phi-opt"
 
16
#include "llvm/CodeGen/Passes.h"
 
17
#include "llvm/CodeGen/MachineFunctionPass.h"
 
18
#include "llvm/CodeGen/MachineInstr.h"
 
19
#include "llvm/CodeGen/MachineRegisterInfo.h"
 
20
#include "llvm/Target/TargetInstrInfo.h"
 
21
#include "llvm/Function.h"
 
22
#include "llvm/ADT/SmallPtrSet.h"
 
23
#include "llvm/ADT/Statistic.h"
 
24
using namespace llvm;
 
25
 
 
26
STATISTIC(NumPHICycles, "Number of PHI cycles replaced");
 
27
STATISTIC(NumDeadPHICycles, "Number of dead PHI cycles");
 
28
 
 
29
namespace {
 
30
  class OptimizePHIs : public MachineFunctionPass {
 
31
    MachineRegisterInfo *MRI;
 
32
    const TargetInstrInfo *TII;
 
33
 
 
34
  public:
 
35
    static char ID; // Pass identification
 
36
    OptimizePHIs() : MachineFunctionPass(&ID) {}
 
37
 
 
38
    virtual bool runOnMachineFunction(MachineFunction &MF);
 
39
 
 
40
    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
 
41
      AU.setPreservesCFG();
 
42
      MachineFunctionPass::getAnalysisUsage(AU);
 
43
    }
 
44
 
 
45
  private:
 
46
    typedef SmallPtrSet<MachineInstr*, 16> InstrSet;
 
47
    typedef SmallPtrSetIterator<MachineInstr*> InstrSetIterator;
 
48
 
 
49
    bool IsSingleValuePHICycle(MachineInstr *MI, unsigned &SingleValReg,
 
50
                               InstrSet &PHIsInCycle);
 
51
    bool IsDeadPHICycle(MachineInstr *MI, InstrSet &PHIsInCycle);
 
52
    bool OptimizeBB(MachineBasicBlock &MBB);
 
53
  };
 
54
}
 
55
 
 
56
char OptimizePHIs::ID = 0;
 
57
static RegisterPass<OptimizePHIs>
 
58
X("opt-phis", "Optimize machine instruction PHIs");
 
59
 
 
60
FunctionPass *llvm::createOptimizePHIsPass() { return new OptimizePHIs(); }
 
61
 
 
62
bool OptimizePHIs::runOnMachineFunction(MachineFunction &Fn) {
 
63
  MRI = &Fn.getRegInfo();
 
64
  TII = Fn.getTarget().getInstrInfo();
 
65
 
 
66
  // Find dead PHI cycles and PHI cycles that can be replaced by a single
 
67
  // value.  InstCombine does these optimizations, but DAG legalization may
 
68
  // introduce new opportunities, e.g., when i64 values are split up for
 
69
  // 32-bit targets.
 
70
  bool Changed = false;
 
71
  for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
 
72
    Changed |= OptimizeBB(*I);
 
73
 
 
74
  return Changed;
 
75
}
 
76
 
 
77
/// IsSingleValuePHICycle - Check if MI is a PHI where all the source operands
 
78
/// are copies of SingleValReg, possibly via copies through other PHIs.  If
 
79
/// SingleValReg is zero on entry, it is set to the register with the single
 
80
/// non-copy value.  PHIsInCycle is a set used to keep track of the PHIs that
 
81
/// have been scanned.
 
82
bool OptimizePHIs::IsSingleValuePHICycle(MachineInstr *MI,
 
83
                                         unsigned &SingleValReg,
 
84
                                         InstrSet &PHIsInCycle) {
 
85
  assert(MI->isPHI() && "IsSingleValuePHICycle expects a PHI instruction");
 
86
  unsigned DstReg = MI->getOperand(0).getReg();
 
87
 
 
88
  // See if we already saw this register.
 
89
  if (!PHIsInCycle.insert(MI))
 
90
    return true;
 
91
 
 
92
  // Don't scan crazily complex things.
 
93
  if (PHIsInCycle.size() == 16)
 
94
    return false;
 
95
 
 
96
  // Scan the PHI operands.
 
97
  for (unsigned i = 1; i != MI->getNumOperands(); i += 2) {
 
98
    unsigned SrcReg = MI->getOperand(i).getReg();
 
99
    if (SrcReg == DstReg)
 
100
      continue;
 
101
    MachineInstr *SrcMI = MRI->getVRegDef(SrcReg);
 
102
 
 
103
    // Skip over register-to-register moves.
 
104
    unsigned MvSrcReg, MvDstReg, SrcSubIdx, DstSubIdx;
 
105
    if (SrcMI &&
 
106
        TII->isMoveInstr(*SrcMI, MvSrcReg, MvDstReg, SrcSubIdx, DstSubIdx) &&
 
107
        SrcSubIdx == 0 && DstSubIdx == 0 &&
 
108
        TargetRegisterInfo::isVirtualRegister(MvSrcReg))
 
109
      SrcMI = MRI->getVRegDef(MvSrcReg);
 
110
    if (!SrcMI)
 
111
      return false;
 
112
 
 
113
    if (SrcMI->isPHI()) {
 
114
      if (!IsSingleValuePHICycle(SrcMI, SingleValReg, PHIsInCycle))
 
115
        return false;
 
116
    } else {
 
117
      // Fail if there is more than one non-phi/non-move register.
 
118
      if (SingleValReg != 0)
 
119
        return false;
 
120
      SingleValReg = SrcReg;
 
121
    }
 
122
  }
 
123
  return true;
 
124
}
 
125
 
 
126
/// IsDeadPHICycle - Check if the register defined by a PHI is only used by
 
127
/// other PHIs in a cycle.
 
128
bool OptimizePHIs::IsDeadPHICycle(MachineInstr *MI, InstrSet &PHIsInCycle) {
 
129
  assert(MI->isPHI() && "IsDeadPHICycle expects a PHI instruction");
 
130
  unsigned DstReg = MI->getOperand(0).getReg();
 
131
  assert(TargetRegisterInfo::isVirtualRegister(DstReg) &&
 
132
         "PHI destination is not a virtual register");
 
133
 
 
134
  // See if we already saw this register.
 
135
  if (!PHIsInCycle.insert(MI))
 
136
    return true;
 
137
 
 
138
  // Don't scan crazily complex things.
 
139
  if (PHIsInCycle.size() == 16)
 
140
    return false;
 
141
 
 
142
  for (MachineRegisterInfo::use_iterator I = MRI->use_begin(DstReg),
 
143
         E = MRI->use_end(); I != E; ++I) {
 
144
    MachineInstr *UseMI = &*I;
 
145
    if (!UseMI->isPHI() || !IsDeadPHICycle(UseMI, PHIsInCycle))
 
146
      return false;
 
147
  }
 
148
 
 
149
  return true;
 
150
}
 
151
 
 
152
/// OptimizeBB - Remove dead PHI cycles and PHI cycles that can be replaced by
 
153
/// a single value.
 
154
bool OptimizePHIs::OptimizeBB(MachineBasicBlock &MBB) {
 
155
  bool Changed = false;
 
156
  for (MachineBasicBlock::iterator
 
157
         MII = MBB.begin(), E = MBB.end(); MII != E; ) {
 
158
    MachineInstr *MI = &*MII++;
 
159
    if (!MI->isPHI())
 
160
      break;
 
161
 
 
162
    // Check for single-value PHI cycles.
 
163
    unsigned SingleValReg = 0;
 
164
    InstrSet PHIsInCycle;
 
165
    if (IsSingleValuePHICycle(MI, SingleValReg, PHIsInCycle) &&
 
166
        SingleValReg != 0) {
 
167
      MRI->replaceRegWith(MI->getOperand(0).getReg(), SingleValReg);
 
168
      MI->eraseFromParent();
 
169
      ++NumPHICycles;
 
170
      Changed = true;
 
171
      continue;
 
172
    }
 
173
 
 
174
    // Check for dead PHI cycles.
 
175
    PHIsInCycle.clear();
 
176
    if (IsDeadPHICycle(MI, PHIsInCycle)) {
 
177
      for (InstrSetIterator PI = PHIsInCycle.begin(), PE = PHIsInCycle.end();
 
178
           PI != PE; ++PI) {
 
179
        MachineInstr *PhiMI = *PI;
 
180
        if (&*MII == PhiMI)
 
181
          ++MII;
 
182
        PhiMI->eraseFromParent();
 
183
      }
 
184
      ++NumDeadPHICycles;
 
185
      Changed = true;
 
186
    }
 
187
  }
 
188
  return Changed;
 
189
}