LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SimplifyInstructions.cpp
Go to the documentation of this file.
1 //===------ SimplifyInstructions.cpp - Remove redundant instructions ------===//
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 is a utility pass used for testing the InstructionSimplify analysis.
11 // The analysis is applied to every instruction, and if it simplifies then the
12 // instruction is replaced by the simplification. If you are looking for a pass
13 // that performs serious instruction folding, use the instcombine pass instead.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #define DEBUG_TYPE "instsimplify"
18 #include "llvm/Transforms/Scalar.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/Statistic.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/IR/Type.h"
27 #include "llvm/Pass.h"
30 using namespace llvm;
31 
32 STATISTIC(NumSimplified, "Number of redundant instructions removed");
33 
34 namespace {
35  struct InstSimplifier : public FunctionPass {
36  static char ID; // Pass identification, replacement for typeid
37  InstSimplifier() : FunctionPass(ID) {
39  }
40 
41  void getAnalysisUsage(AnalysisUsage &AU) const {
42  AU.setPreservesCFG();
44  }
45 
46  /// runOnFunction - Remove instructions that simplify.
47  bool runOnFunction(Function &F) {
48  const DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>();
49  const DataLayout *TD = getAnalysisIfAvailable<DataLayout>();
50  const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();
51  SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
52  bool Changed = false;
53 
54  do {
56  DE = df_end(&F.getEntryBlock()); DI != DE; ++DI)
57  for (BasicBlock::iterator BI = DI->begin(), BE = DI->end(); BI != BE;) {
58  Instruction *I = BI++;
59  // The first time through the loop ToSimplify is empty and we try to
60  // simplify all instructions. On later iterations ToSimplify is not
61  // empty and we only bother simplifying instructions that are in it.
62  if (!ToSimplify->empty() && !ToSimplify->count(I))
63  continue;
64  // Don't waste time simplifying unused instructions.
65  if (!I->use_empty())
66  if (Value *V = SimplifyInstruction(I, TD, TLI, DT)) {
67  // Mark all uses for resimplification next time round the loop.
68  for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
69  UI != UE; ++UI)
70  Next->insert(cast<Instruction>(*UI));
71  I->replaceAllUsesWith(V);
72  ++NumSimplified;
73  Changed = true;
74  }
76  }
77 
78  // Place the list of instructions to simplify on the next loop iteration
79  // into ToSimplify.
80  std::swap(ToSimplify, Next);
81  Next->clear();
82  } while (!ToSimplify->empty());
83 
84  return Changed;
85  }
86  };
87 }
88 
89 char InstSimplifier::ID = 0;
90 INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
91  "Remove redundant instructions", false, false)
94  "Remove redundant instructions", false, false)
95 char &llvm::InstructionSimplifierID = InstSimplifier::ID;
96 
97 // Public interface to the simplify instructions pass.
99  return new InstSimplifier();
100 }
use_iterator use_end()
Definition: Value.h:152
static PassRegistry * getPassRegistry()
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=0)
Definition: Local.cpp:316
F(f)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:167
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:172
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
char & InstructionSimplifierID
INITIALIZE_PASS_BEGIN(InstSimplifier,"instsimplify","Remove redundant instructions", false, false) INITIALIZE_PASS_END(InstSimplifier
void replaceAllUsesWith(Value *V)
Definition: Value.cpp:303
df_iterator< T > df_end(const T &G)
void initializeInstSimplifierPass(PassRegistry &)
Value * SimplifyInstruction(Instruction *I, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
STATISTIC(NumSimplified,"Number of redundant instructions removed")
void setPreservesCFG()
Definition: Pass.cpp:249
const BasicBlock & getEntryBlock() const
Definition: Function.h:380
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:591
df_iterator< T > df_begin(const T &G)
use_iterator use_begin()
Definition: Value.h:150
#define I(x, y, z)
Definition: MD5.cpp:54
bool use_empty() const
Definition: Value.h:149
LLVM Value Representation.
Definition: Value.h:66
FunctionPass * createInstructionSimplifierPass()
Remove redundant instructions
INITIALIZE_PASS(GlobalMerge,"global-merge","Global Merge", false, false) bool GlobalMerge const DataLayout * TD
Remove redundant false