blob: d9a297cdf1f4e3b6915a7393e1da17519aa7c99d [file] [log] [blame]
Misha Brukman82c89b92003-05-20 21:01:22 +00001//===- ConstantProp.cpp - Code to perform Simple Constant Propagation -----===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Misha Brukman82c89b92003-05-20 21:01:22 +000010// This file implements constant propagation and merging:
Chris Lattner00950542001-06-06 20:29:01 +000011//
12// Specifically, this:
Chris Lattner89df1b52002-05-06 18:21:31 +000013// * Converts instructions like "add int 1, 2" into 3
Chris Lattner00950542001-06-06 20:29:01 +000014//
15// Notice that:
16// * This pass has a habit of making definitions be dead. It is a good idea
Chris Lattner89df1b52002-05-06 18:21:31 +000017// to to run a DIE pass sometime after running this pass.
Chris Lattner00950542001-06-06 20:29:01 +000018//
19//===----------------------------------------------------------------------===//
20
Chris Lattner022103b2002-05-07 20:03:00 +000021#include "llvm/Transforms/Scalar.h"
Chris Lattner2ed01d82002-05-07 18:10:55 +000022#include "llvm/Transforms/Utils/Local.h"
Chris Lattnerb6ac8bc2004-01-12 19:15:20 +000023#include "llvm/Constant.h"
Chris Lattner2ed01d82002-05-07 18:10:55 +000024#include "llvm/Instruction.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000025#include "llvm/Pass.h"
Chris Lattner89df1b52002-05-06 18:21:31 +000026#include "llvm/Support/InstIterator.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000027#include "Support/Statistic.h"
Chris Lattner89df1b52002-05-06 18:21:31 +000028#include <set>
Chris Lattnerd7456022004-01-09 06:02:20 +000029using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattnerbd0ef772002-02-26 21:46:54 +000031namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000032 Statistic<> NumInstKilled("constprop", "Number of instructions killed");
33
Misha Brukman82c89b92003-05-20 21:01:22 +000034 struct ConstantPropagation : public FunctionPass {
Chris Lattner7e708292002-06-25 16:13:24 +000035 bool runOnFunction(Function &F);
Chris Lattner97e52e42002-04-28 21:27:06 +000036
37 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000038 AU.setPreservesCFG();
Chris Lattner97e52e42002-04-28 21:27:06 +000039 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +000040 };
Chris Lattnerf6293092002-07-23 18:06:35 +000041
Misha Brukman82c89b92003-05-20 21:01:22 +000042 RegisterOpt<ConstantPropagation> X("constprop","Simple constant propagation");
Chris Lattner00950542001-06-06 20:29:01 +000043}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000044
Chris Lattnerd7456022004-01-09 06:02:20 +000045Pass *llvm::createConstantPropagationPass() {
Misha Brukman82c89b92003-05-20 21:01:22 +000046 return new ConstantPropagation();
Chris Lattnerbd0ef772002-02-26 21:46:54 +000047}
48
Chris Lattner89df1b52002-05-06 18:21:31 +000049
Misha Brukman82c89b92003-05-20 21:01:22 +000050bool ConstantPropagation::runOnFunction(Function &F) {
Chris Lattner89df1b52002-05-06 18:21:31 +000051 // Initialize the worklist to all of the instructions ready to process...
52 std::set<Instruction*> WorkList(inst_begin(F), inst_end(F));
53 bool Changed = false;
54
55 while (!WorkList.empty()) {
56 Instruction *I = *WorkList.begin();
57 WorkList.erase(WorkList.begin()); // Get an element from the worklist...
58
59 if (!I->use_empty()) // Don't muck with dead instructions...
60 if (Constant *C = ConstantFoldInstruction(I)) {
61 // Add all of the users of this instruction to the worklist, they might
Misha Brukman82c89b92003-05-20 21:01:22 +000062 // be constant propagatable now...
Chris Lattner89df1b52002-05-06 18:21:31 +000063 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
64 UI != UE; ++UI)
65 WorkList.insert(cast<Instruction>(*UI));
66
67 // Replace all of the uses of a variable with uses of the constant.
68 I->replaceAllUsesWith(C);
Chris Lattner3dec1f22002-05-10 15:38:35 +000069
Chris Lattner89df1b52002-05-06 18:21:31 +000070 // We made a change to the function...
71 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +000072 ++NumInstKilled;
Chris Lattner89df1b52002-05-06 18:21:31 +000073 }
74 }
75 return Changed;
76}