~ubuntu-branches/ubuntu/wily/clamav/wily-proposed

« back to all changes in this revision

Viewing changes to libclamav/c++/llvm/lib/Transforms/Scalar/LoopRotation.cpp

  • Committer: Package Import Robot
  • Author(s): Scott Kitterman, Sebastian Andrzej Siewior, Andreas Cadhalpun, Scott Kitterman, Javier Fernández-Sanguino
  • Date: 2015-01-28 00:25:13 UTC
  • mfrom: (0.48.14 sid)
  • Revision ID: package-import@ubuntu.com-20150128002513-lil2oi74cooy4lzr
Tags: 0.98.6+dfsg-1
[ Sebastian Andrzej Siewior ]
* update "fix-ssize_t-size_t-off_t-printf-modifier", include of misc.h was
  missing but was pulled in via the systemd patch.
* Don't leak return codes from libmspack to clamav API. (Closes: #774686).

[ Andreas Cadhalpun ]
* Add patch to avoid emitting incremental progress messages when not
  outputting to a terminal. (Closes: #767350)
* Update lintian-overrides for unused-file-paragraph-in-dep5-copyright.
* clamav-base.postinst: always chown /var/log/clamav and /var/lib/clamav
  to clamav:clamav, not only on fresh installations. (Closes: #775400)
* Adapt the clamav-daemon and clamav-freshclam logrotate scripts,
  so that they correctly work under systemd.
* Move the PidFile variable from the clamd/freshclam configuration files
  to the init scripts. This makes the init scripts more robust against
  misconfiguration and avoids error messages with systemd. (Closes: #767353)
* debian/copyright: drop files from Files-Excluded only present in github
  tarballs
* Drop Workaround-a-bug-in-libc-on-Hurd.patch, because hurd got fixed.
  (see #752237)
* debian/rules: Remove useless --with-system-tommath --without-included-ltdl
  configure options.

[ Scott Kitterman ]
* Stop stripping llvm when repacking the tarball as the system llvm on some
  releases is too old to use
* New upstream bugfix release
  - Library shared object revisions.
  - Includes a patch from Sebastian Andrzej Siewior making ClamAV pid files
    compatible with systemd.
  - Fix a heap out of bounds condition with crafted Yoda's crypter files.
    This issue was discovered by Felix Groebert of the Google Security Team.
  - Fix a heap out of bounds condition with crafted mew packer files. This
    issue was discovered by Felix Groebert of the Google Security Team.
  - Fix a heap out of bounds condition with crafted upx packer files. This
    issue was discovered by Kevin Szkudlapski of Quarkslab.
  - Fix a heap out of bounds condition with crafted upack packer files. This
    issue was discovered by Sebastian Andrzej Siewior. CVE-2014-9328.
  - Compensate a crash due to incorrect compiler optimization when handling
    crafted petite packer files. This issue was discovered by Sebastian
    Andrzej Siewior.
* Update lintian override for embedded zlib to match new so version

[ Javier Fernández-Sanguino ]
* Updated Spanish Debconf template translation (Closes: #773563)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//===- LoopRotation.cpp - Loop Rotation Pass ------------------------------===//
 
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 implements Loop Rotation Pass.
 
11
//
 
12
//===----------------------------------------------------------------------===//
 
13
 
 
14
#define DEBUG_TYPE "loop-rotate"
 
15
#include "llvm/Transforms/Scalar.h"
 
16
#include "llvm/Function.h"
 
17
#include "llvm/IntrinsicInst.h"
 
18
#include "llvm/Analysis/LoopPass.h"
 
19
#include "llvm/Analysis/Dominators.h"
 
20
#include "llvm/Analysis/ScalarEvolution.h"
 
21
#include "llvm/Transforms/Utils/Local.h"
 
22
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
 
23
#include "llvm/Transforms/Utils/SSAUpdater.h"
 
24
#include "llvm/Support/CommandLine.h"
 
25
#include "llvm/Support/Debug.h"
 
26
#include "llvm/ADT/Statistic.h"
 
27
#include "llvm/ADT/SmallVector.h"
 
28
using namespace llvm;
 
29
 
 
30
#define MAX_HEADER_SIZE 16
 
31
 
 
32
STATISTIC(NumRotated, "Number of loops rotated");
 
33
namespace {
 
34
 
 
35
  class LoopRotate : public LoopPass {
 
36
  public:
 
37
    static char ID; // Pass ID, replacement for typeid
 
38
    LoopRotate() : LoopPass(ID) {}
 
39
 
 
40
    // Rotate Loop L as many times as possible. Return true if
 
41
    // loop is rotated at least once.
 
42
    bool runOnLoop(Loop *L, LPPassManager &LPM);
 
43
 
 
44
    // LCSSA form makes instruction renaming easier.
 
45
    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
 
46
      AU.addPreserved<DominatorTree>();
 
47
      AU.addPreserved<DominanceFrontier>();
 
48
      AU.addRequired<LoopInfo>();
 
49
      AU.addPreserved<LoopInfo>();
 
50
      AU.addRequiredID(LoopSimplifyID);
 
51
      AU.addPreservedID(LoopSimplifyID);
 
52
      AU.addRequiredID(LCSSAID);
 
53
      AU.addPreservedID(LCSSAID);
 
54
      AU.addPreserved<ScalarEvolution>();
 
55
    }
 
56
 
 
57
    // Helper functions
 
58
 
 
59
    /// Do actual work
 
60
    bool rotateLoop(Loop *L, LPPassManager &LPM);
 
61
    
 
62
    /// Initialize local data
 
63
    void initialize();
 
64
 
 
65
    /// After loop rotation, loop pre-header has multiple sucessors.
 
66
    /// Insert one forwarding basic block to ensure that loop pre-header
 
67
    /// has only one successor.
 
68
    void preserveCanonicalLoopForm(LPPassManager &LPM);
 
69
 
 
70
  private:
 
71
    Loop *L;
 
72
    BasicBlock *OrigHeader;
 
73
    BasicBlock *OrigPreHeader;
 
74
    BasicBlock *OrigLatch;
 
75
    BasicBlock *NewHeader;
 
76
    BasicBlock *Exit;
 
77
    LPPassManager *LPM_Ptr;
 
78
  };
 
79
}
 
80
  
 
81
char LoopRotate::ID = 0;
 
82
INITIALIZE_PASS(LoopRotate, "loop-rotate", "Rotate Loops", false, false);
 
83
 
 
84
Pass *llvm::createLoopRotatePass() { return new LoopRotate(); }
 
85
 
 
86
/// Rotate Loop L as many times as possible. Return true if
 
87
/// the loop is rotated at least once.
 
88
bool LoopRotate::runOnLoop(Loop *Lp, LPPassManager &LPM) {
 
89
 
 
90
  bool RotatedOneLoop = false;
 
91
  initialize();
 
92
  LPM_Ptr = &LPM;
 
93
 
 
94
  // One loop can be rotated multiple times.
 
95
  while (rotateLoop(Lp,LPM)) {
 
96
    RotatedOneLoop = true;
 
97
    initialize();
 
98
  }
 
99
 
 
100
  return RotatedOneLoop;
 
101
}
 
102
 
 
103
/// Rotate loop LP. Return true if the loop is rotated.
 
104
bool LoopRotate::rotateLoop(Loop *Lp, LPPassManager &LPM) {
 
105
  L = Lp;
 
106
 
 
107
  OrigPreHeader = L->getLoopPreheader();
 
108
  if (!OrigPreHeader) return false;
 
109
 
 
110
  OrigLatch = L->getLoopLatch();
 
111
  if (!OrigLatch) return false;
 
112
 
 
113
  OrigHeader =  L->getHeader();
 
114
 
 
115
  // If the loop has only one block then there is not much to rotate.
 
116
  if (L->getBlocks().size() == 1)
 
117
    return false;
 
118
 
 
119
  // If the loop header is not one of the loop exiting blocks then
 
120
  // either this loop is already rotated or it is not
 
121
  // suitable for loop rotation transformations.
 
122
  if (!L->isLoopExiting(OrigHeader))
 
123
    return false;
 
124
 
 
125
  BranchInst *BI = dyn_cast<BranchInst>(OrigHeader->getTerminator());
 
126
  if (!BI)
 
127
    return false;
 
128
  assert(BI->isConditional() && "Branch Instruction is not conditional");
 
129
 
 
130
  // Updating PHInodes in loops with multiple exits adds complexity. 
 
131
  // Keep it simple, and restrict loop rotation to loops with one exit only.
 
132
  // In future, lift this restriction and support for multiple exits if
 
133
  // required.
 
134
  SmallVector<BasicBlock*, 8> ExitBlocks;
 
135
  L->getExitBlocks(ExitBlocks);
 
136
  if (ExitBlocks.size() > 1)
 
137
    return false;
 
138
 
 
139
  // Check size of original header and reject
 
140
  // loop if it is very big.
 
141
  unsigned Size = 0;
 
142
  
 
143
  // FIXME: Use common api to estimate size.
 
144
  for (BasicBlock::const_iterator OI = OrigHeader->begin(), 
 
145
         OE = OrigHeader->end(); OI != OE; ++OI) {
 
146
      if (isa<PHINode>(OI)) 
 
147
        continue;           // PHI nodes don't count.
 
148
      if (isa<DbgInfoIntrinsic>(OI))
 
149
        continue;  // Debug intrinsics don't count as size.
 
150
      ++Size;
 
151
  }
 
152
 
 
153
  if (Size > MAX_HEADER_SIZE)
 
154
    return false;
 
155
 
 
156
  // Now, this loop is suitable for rotation.
 
157
 
 
158
  // Anything ScalarEvolution may know about this loop or the PHI nodes
 
159
  // in its header will soon be invalidated.
 
160
  if (ScalarEvolution *SE = getAnalysisIfAvailable<ScalarEvolution>())
 
161
    SE->forgetLoop(L);
 
162
 
 
163
  // Find new Loop header. NewHeader is a Header's one and only successor
 
164
  // that is inside loop.  Header's other successor is outside the
 
165
  // loop.  Otherwise loop is not suitable for rotation.
 
166
  Exit = BI->getSuccessor(0);
 
167
  NewHeader = BI->getSuccessor(1);
 
168
  if (L->contains(Exit))
 
169
    std::swap(Exit, NewHeader);
 
170
  assert(NewHeader && "Unable to determine new loop header");
 
171
  assert(L->contains(NewHeader) && !L->contains(Exit) && 
 
172
         "Unable to determine loop header and exit blocks");
 
173
  
 
174
  // This code assumes that the new header has exactly one predecessor.
 
175
  // Remove any single-entry PHI nodes in it.
 
176
  assert(NewHeader->getSinglePredecessor() &&
 
177
         "New header doesn't have one pred!");
 
178
  FoldSingleEntryPHINodes(NewHeader);
 
179
 
 
180
  // Begin by walking OrigHeader and populating ValueMap with an entry for
 
181
  // each Instruction.
 
182
  BasicBlock::iterator I = OrigHeader->begin(), E = OrigHeader->end();
 
183
  DenseMap<const Value *, Value *> ValueMap;
 
184
 
 
185
  // For PHI nodes, the value available in OldPreHeader is just the
 
186
  // incoming value from OldPreHeader.
 
187
  for (; PHINode *PN = dyn_cast<PHINode>(I); ++I)
 
188
    ValueMap[PN] = PN->getIncomingValue(PN->getBasicBlockIndex(OrigPreHeader));
 
189
 
 
190
  // For the rest of the instructions, create a clone in the OldPreHeader.
 
191
  TerminatorInst *LoopEntryBranch = OrigPreHeader->getTerminator();
 
192
  for (; I != E; ++I) {
 
193
    Instruction *C = I->clone();
 
194
    C->setName(I->getName());
 
195
    C->insertBefore(LoopEntryBranch);
 
196
    ValueMap[I] = C;
 
197
  }
 
198
 
 
199
  // Along with all the other instructions, we just cloned OrigHeader's
 
200
  // terminator into OrigPreHeader. Fix up the PHI nodes in each of OrigHeader's
 
201
  // successors by duplicating their incoming values for OrigHeader.
 
202
  TerminatorInst *TI = OrigHeader->getTerminator();
 
203
  for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
 
204
    for (BasicBlock::iterator BI = TI->getSuccessor(i)->begin();
 
205
         PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
 
206
      PN->addIncoming(PN->getIncomingValueForBlock(OrigHeader), OrigPreHeader);
 
207
 
 
208
  // Now that OrigPreHeader has a clone of OrigHeader's terminator, remove
 
209
  // OrigPreHeader's old terminator (the original branch into the loop), and
 
210
  // remove the corresponding incoming values from the PHI nodes in OrigHeader.
 
211
  LoopEntryBranch->eraseFromParent();
 
212
  for (I = OrigHeader->begin(); PHINode *PN = dyn_cast<PHINode>(I); ++I)
 
213
    PN->removeIncomingValue(PN->getBasicBlockIndex(OrigPreHeader));
 
214
 
 
215
  // Now fix up users of the instructions in OrigHeader, inserting PHI nodes
 
216
  // as necessary.
 
217
  SSAUpdater SSA;
 
218
  for (I = OrigHeader->begin(); I != E; ++I) {
 
219
    Value *OrigHeaderVal = I;
 
220
    Value *OrigPreHeaderVal = ValueMap[OrigHeaderVal];
 
221
 
 
222
    // The value now exits in two versions: the initial value in the preheader
 
223
    // and the loop "next" value in the original header.
 
224
    SSA.Initialize(OrigHeaderVal->getType(), OrigHeaderVal->getName());
 
225
    SSA.AddAvailableValue(OrigHeader, OrigHeaderVal);
 
226
    SSA.AddAvailableValue(OrigPreHeader, OrigPreHeaderVal);
 
227
 
 
228
    // Visit each use of the OrigHeader instruction.
 
229
    for (Value::use_iterator UI = OrigHeaderVal->use_begin(),
 
230
         UE = OrigHeaderVal->use_end(); UI != UE; ) {
 
231
      // Grab the use before incrementing the iterator.
 
232
      Use &U = UI.getUse();
 
233
 
 
234
      // Increment the iterator before removing the use from the list.
 
235
      ++UI;
 
236
 
 
237
      // SSAUpdater can't handle a non-PHI use in the same block as an
 
238
      // earlier def. We can easily handle those cases manually.
 
239
      Instruction *UserInst = cast<Instruction>(U.getUser());
 
240
      if (!isa<PHINode>(UserInst)) {
 
241
        BasicBlock *UserBB = UserInst->getParent();
 
242
 
 
243
        // The original users in the OrigHeader are already using the
 
244
        // original definitions.
 
245
        if (UserBB == OrigHeader)
 
246
          continue;
 
247
 
 
248
        // Users in the OrigPreHeader need to use the value to which the
 
249
        // original definitions are mapped.
 
250
        if (UserBB == OrigPreHeader) {
 
251
          U = OrigPreHeaderVal;
 
252
          continue;
 
253
        }
 
254
      }
 
255
 
 
256
      // Anything else can be handled by SSAUpdater.
 
257
      SSA.RewriteUse(U);
 
258
    }
 
259
  }
 
260
 
 
261
  // NewHeader is now the header of the loop.
 
262
  L->moveToHeader(NewHeader);
 
263
 
 
264
  // Move the original header to the bottom of the loop, where it now more
 
265
  // naturally belongs. This isn't necessary for correctness, and CodeGen can
 
266
  // usually reorder blocks on its own to fix things like this up, but it's
 
267
  // still nice to keep the IR readable.
 
268
  //
 
269
  // The original header should have only one predecessor at this point, since
 
270
  // we checked that the loop had a proper preheader and unique backedge before
 
271
  // we started.
 
272
  assert(OrigHeader->getSinglePredecessor() &&
 
273
         "Original loop header has too many predecessors after loop rotation!");
 
274
  OrigHeader->moveAfter(OrigHeader->getSinglePredecessor());
 
275
 
 
276
  // Also, since this original header only has one predecessor, zap its
 
277
  // PHI nodes, which are now trivial.
 
278
  FoldSingleEntryPHINodes(OrigHeader);
 
279
 
 
280
  // TODO: We could just go ahead and merge OrigHeader into its predecessor
 
281
  // at this point, if we don't mind updating dominator info.
 
282
 
 
283
  // Establish a new preheader, update dominators, etc.
 
284
  preserveCanonicalLoopForm(LPM);
 
285
 
 
286
  ++NumRotated;
 
287
  return true;
 
288
}
 
289
 
 
290
/// Initialize local data
 
291
void LoopRotate::initialize() {
 
292
  L = NULL;
 
293
  OrigHeader = NULL;
 
294
  OrigPreHeader = NULL;
 
295
  NewHeader = NULL;
 
296
  Exit = NULL;
 
297
}
 
298
 
 
299
/// After loop rotation, loop pre-header has multiple sucessors.
 
300
/// Insert one forwarding basic block to ensure that loop pre-header
 
301
/// has only one successor.
 
302
void LoopRotate::preserveCanonicalLoopForm(LPPassManager &LPM) {
 
303
 
 
304
  // Right now original pre-header has two successors, new header and
 
305
  // exit block. Insert new block between original pre-header and
 
306
  // new header such that loop's new pre-header has only one successor.
 
307
  BasicBlock *NewPreHeader = BasicBlock::Create(OrigHeader->getContext(),
 
308
                                                "bb.nph",
 
309
                                                OrigHeader->getParent(), 
 
310
                                                NewHeader);
 
311
  LoopInfo &LI = getAnalysis<LoopInfo>();
 
312
  if (Loop *PL = LI.getLoopFor(OrigPreHeader))
 
313
    PL->addBasicBlockToLoop(NewPreHeader, LI.getBase());
 
314
  BranchInst::Create(NewHeader, NewPreHeader);
 
315
  
 
316
  BranchInst *OrigPH_BI = cast<BranchInst>(OrigPreHeader->getTerminator());
 
317
  if (OrigPH_BI->getSuccessor(0) == NewHeader)
 
318
    OrigPH_BI->setSuccessor(0, NewPreHeader);
 
319
  else {
 
320
    assert(OrigPH_BI->getSuccessor(1) == NewHeader &&
 
321
           "Unexpected original pre-header terminator");
 
322
    OrigPH_BI->setSuccessor(1, NewPreHeader);
 
323
  }
 
324
 
 
325
  PHINode *PN;
 
326
  for (BasicBlock::iterator I = NewHeader->begin();
 
327
       (PN = dyn_cast<PHINode>(I)); ++I) {
 
328
    int index = PN->getBasicBlockIndex(OrigPreHeader);
 
329
    assert(index != -1 && "Expected incoming value from Original PreHeader");
 
330
    PN->setIncomingBlock(index, NewPreHeader);
 
331
    assert(PN->getBasicBlockIndex(OrigPreHeader) == -1 && 
 
332
           "Expected only one incoming value from Original PreHeader");
 
333
  }
 
334
 
 
335
  if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>()) {
 
336
    DT->addNewBlock(NewPreHeader, OrigPreHeader);
 
337
    DT->changeImmediateDominator(L->getHeader(), NewPreHeader);
 
338
    DT->changeImmediateDominator(Exit, OrigPreHeader);
 
339
    for (Loop::block_iterator BI = L->block_begin(), BE = L->block_end();
 
340
         BI != BE; ++BI) {
 
341
      BasicBlock *B = *BI;
 
342
      if (L->getHeader() != B) {
 
343
        DomTreeNode *Node = DT->getNode(B);
 
344
        if (Node && Node->getBlock() == OrigHeader)
 
345
          DT->changeImmediateDominator(*BI, L->getHeader());
 
346
      }
 
347
    }
 
348
    DT->changeImmediateDominator(OrigHeader, OrigLatch);
 
349
  }
 
350
 
 
351
  if (DominanceFrontier *DF = getAnalysisIfAvailable<DominanceFrontier>()) {
 
352
    // New Preheader's dominance frontier is Exit block.
 
353
    DominanceFrontier::DomSetType NewPHSet;
 
354
    NewPHSet.insert(Exit);
 
355
    DF->addBasicBlock(NewPreHeader, NewPHSet);
 
356
 
 
357
    // New Header's dominance frontier now includes itself and Exit block
 
358
    DominanceFrontier::iterator HeadI = DF->find(L->getHeader());
 
359
    if (HeadI != DF->end()) {
 
360
      DominanceFrontier::DomSetType & HeaderSet = HeadI->second;
 
361
      HeaderSet.clear();
 
362
      HeaderSet.insert(L->getHeader());
 
363
      HeaderSet.insert(Exit);
 
364
    } else {
 
365
      DominanceFrontier::DomSetType HeaderSet;
 
366
      HeaderSet.insert(L->getHeader());
 
367
      HeaderSet.insert(Exit);
 
368
      DF->addBasicBlock(L->getHeader(), HeaderSet);
 
369
    }
 
370
 
 
371
    // Original header (new Loop Latch)'s dominance frontier is Exit.
 
372
    DominanceFrontier::iterator LatchI = DF->find(L->getLoopLatch());
 
373
    if (LatchI != DF->end()) {
 
374
      DominanceFrontier::DomSetType &LatchSet = LatchI->second;
 
375
      LatchSet = LatchI->second;
 
376
      LatchSet.clear();
 
377
      LatchSet.insert(Exit);
 
378
    } else {
 
379
      DominanceFrontier::DomSetType LatchSet;
 
380
      LatchSet.insert(Exit);
 
381
      DF->addBasicBlock(L->getHeader(), LatchSet);
 
382
    }
 
383
 
 
384
    // If a loop block dominates new loop latch then add to its frontiers
 
385
    // new header and Exit and remove new latch (which is equal to original
 
386
    // header).
 
387
    BasicBlock *NewLatch = L->getLoopLatch();
 
388
 
 
389
    assert(NewLatch == OrigHeader && "NewLatch is inequal to OrigHeader");
 
390
 
 
391
    if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>()) {
 
392
      for (Loop::block_iterator BI = L->block_begin(), BE = L->block_end();
 
393
           BI != BE; ++BI) {
 
394
        BasicBlock *B = *BI;
 
395
        if (DT->dominates(B, NewLatch)) {
 
396
          DominanceFrontier::iterator BDFI = DF->find(B);
 
397
          if (BDFI != DF->end()) {
 
398
            DominanceFrontier::DomSetType &BSet = BDFI->second;
 
399
            BSet.erase(NewLatch);
 
400
            BSet.insert(L->getHeader());
 
401
            BSet.insert(Exit);
 
402
          } else {
 
403
            DominanceFrontier::DomSetType BSet;
 
404
            BSet.insert(L->getHeader());
 
405
            BSet.insert(Exit);
 
406
            DF->addBasicBlock(B, BSet);
 
407
          }
 
408
        }
 
409
      }
 
410
    }
 
411
  }
 
412
 
 
413
  // Preserve canonical loop form, which means Exit block should
 
414
  // have only one predecessor.
 
415
  SplitEdge(L->getLoopLatch(), Exit, this);
 
416
 
 
417
  assert(NewHeader && L->getHeader() == NewHeader &&
 
418
         "Invalid loop header after loop rotation");
 
419
  assert(NewPreHeader && L->getLoopPreheader() == NewPreHeader &&
 
420
         "Invalid loop preheader after loop rotation");
 
421
  assert(L->getLoopLatch() &&
 
422
         "Invalid loop latch after loop rotation");
 
423
}