blob: 301e600fb9d0d0daebb3ed6a79c424a5adf832a7 [file] [log] [blame]
Cameron Zwarich832f6112011-01-03 00:25:16 +00001//===- LoopInstSimplify.cpp - Loop Instruction Simplification 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 pass performs lightweight instruction simplification on loop bodies.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "loop-instsimplify"
Cameron Zwaricha1cb5852011-01-04 00:12:46 +000015#include "llvm/Function.h"
16#include "llvm/Pass.h"
Cameron Zwarich832f6112011-01-03 00:25:16 +000017#include "llvm/Analysis/Dominators.h"
18#include "llvm/Analysis/InstructionSimplify.h"
Cameron Zwaricha1cb5852011-01-04 00:12:46 +000019#include "llvm/Analysis/LoopInfo.h"
Cameron Zwarich832f6112011-01-03 00:25:16 +000020#include "llvm/Target/TargetData.h"
21#include "llvm/Transforms/Scalar.h"
22#include "llvm/Transforms/Utils/Local.h"
23#include "llvm/ADT/Statistic.h"
24using namespace llvm;
25
26STATISTIC(NumSimplified, "Number of redundant instructions simplified");
27
28namespace {
Cameron Zwaricha1cb5852011-01-04 00:12:46 +000029 class LoopInstSimplify : public FunctionPass {
Cameron Zwarich832f6112011-01-03 00:25:16 +000030 public:
31 static char ID; // Pass ID, replacement for typeid
Cameron Zwaricha1cb5852011-01-04 00:12:46 +000032 LoopInstSimplify() : FunctionPass(ID) {
Cameron Zwarich832f6112011-01-03 00:25:16 +000033 initializeLoopInstSimplifyPass(*PassRegistry::getPassRegistry());
34 }
35
Cameron Zwaricha1cb5852011-01-04 00:12:46 +000036 bool runOnFunction(Function&);
Cameron Zwarich832f6112011-01-03 00:25:16 +000037
38 virtual void getAnalysisUsage(AnalysisUsage& AU) const {
39 AU.setPreservesCFG();
Cameron Zwarich832f6112011-01-03 00:25:16 +000040 AU.addRequired<LoopInfo>();
41 AU.addPreserved<LoopInfo>();
42 AU.addPreservedID(LCSSAID);
43 }
44 };
45}
46
47char LoopInstSimplify::ID = 0;
48INITIALIZE_PASS_BEGIN(LoopInstSimplify, "loop-instsimplify",
49 "Simplify instructions in loops", false, false)
50INITIALIZE_PASS_DEPENDENCY(DominatorTree)
51INITIALIZE_PASS_DEPENDENCY(LoopInfo)
52INITIALIZE_PASS_DEPENDENCY(LCSSA)
53INITIALIZE_PASS_END(LoopInstSimplify, "loop-instsimplify",
54 "Simplify instructions in loops", false, false)
55
56Pass* llvm::createLoopInstSimplifyPass() {
57 return new LoopInstSimplify();
58}
59
Cameron Zwaricha1cb5852011-01-04 00:12:46 +000060bool LoopInstSimplify::runOnFunction(Function& F) {
61 DominatorTree* DT = getAnalysisIfAvailable<DominatorTree>();
62 LoopInfo* LI = &getAnalysis<LoopInfo>();
Cameron Zwarich832f6112011-01-03 00:25:16 +000063 const TargetData* TD = getAnalysisIfAvailable<TargetData>();
64
65 bool Changed = false;
66 bool LocalChanged;
67 do {
68 LocalChanged = false;
69
Cameron Zwaricha1cb5852011-01-04 00:12:46 +000070 for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()),
71 DE = df_end(&F.getEntryBlock()); DI != DE; ++DI)
72 for (BasicBlock::iterator BI = DI->begin(), BE = DI->end(); BI != BE;) {
Cameron Zwarich832f6112011-01-03 00:25:16 +000073 Instruction* I = BI++;
74 // Don't bother simplifying unused instructions.
75 if (!I->use_empty()) {
Cameron Zwaricha1cb5852011-01-04 00:12:46 +000076 Value* V = SimplifyInstruction(I, TD, DT);
77 if (V && LI->replacementPreservesLCSSAForm(I, V)) {
Cameron Zwarich832f6112011-01-03 00:25:16 +000078 I->replaceAllUsesWith(V);
79 LocalChanged = true;
80 ++NumSimplified;
81 }
82 }
83 LocalChanged |= RecursivelyDeleteTriviallyDeadInstructions(I);
84 }
Cameron Zwarich832f6112011-01-03 00:25:16 +000085
Cameron Zwaricha1cb5852011-01-04 00:12:46 +000086 Changed |= LocalChanged;
Cameron Zwarich832f6112011-01-03 00:25:16 +000087 } while (LocalChanged);
88
Cameron Zwarich832f6112011-01-03 00:25:16 +000089 return Changed;
90}