blob: 226d1deb41b06eb0fceaa2bd6593b80e1daafde7 [file] [log] [blame]
Misha Brukman373086d2003-05-20 21:01:22 +00001//===- ConstantProp.cpp - Code to perform Simple Constant Propagation -----===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// 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.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Misha Brukman373086d2003-05-20 21:01:22 +000010// This file implements constant propagation and merging:
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12// Specifically, this:
Chris Lattnere5485072002-05-06 18:21:31 +000013// * Converts instructions like "add int 1, 2" into 3
Chris Lattner2f7c9632001-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 Lattnere5485072002-05-06 18:21:31 +000017// to to run a DIE pass sometime after running this pass.
Chris Lattner2f7c9632001-06-06 20:29:01 +000018//
19//===----------------------------------------------------------------------===//
20
Chris Lattner79a42ac2006-12-19 21:40:18 +000021#define DEBUG_TYPE "constprop"
Chris Lattnerb4cfa7f2002-05-07 20:03:00 +000022#include "llvm/Transforms/Scalar.h"
Chris Lattner024f4ab2007-01-30 23:46:24 +000023#include "llvm/Analysis/ConstantFolding.h"
Chris Lattnerd7ccc9e2004-01-12 19:15:20 +000024#include "llvm/Constant.h"
Chris Lattner560da702002-05-07 18:10:55 +000025#include "llvm/Instruction.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000026#include "llvm/Pass.h"
Reid Spencer557ab152007-02-05 23:32:05 +000027#include "llvm/Support/Compiler.h"
Chris Lattnere5485072002-05-06 18:21:31 +000028#include "llvm/Support/InstIterator.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000029#include "llvm/ADT/Statistic.h"
Chris Lattnere5485072002-05-06 18:21:31 +000030#include <set>
Chris Lattner49525f82004-01-09 06:02:20 +000031using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000032
Chris Lattner79a42ac2006-12-19 21:40:18 +000033STATISTIC(NumInstKilled, "Number of instructions killed");
Chris Lattnerbf3a0992002-10-01 22:38:41 +000034
Chris Lattner79a42ac2006-12-19 21:40:18 +000035namespace {
Reid Spencer557ab152007-02-05 23:32:05 +000036 struct VISIBILITY_HIDDEN ConstantPropagation : public FunctionPass {
Chris Lattner113f4f42002-06-25 16:13:24 +000037 bool runOnFunction(Function &F);
Chris Lattnerf12cc842002-04-28 21:27:06 +000038
39 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner820d9712002-10-21 20:00:28 +000040 AU.setPreservesCFG();
Chris Lattnerf12cc842002-04-28 21:27:06 +000041 }
Chris Lattner04805fa2002-02-26 21:46:54 +000042 };
Chris Lattnerb28b6802002-07-23 18:06:35 +000043
Chris Lattnerc2d3d312006-08-27 22:42:52 +000044 RegisterPass<ConstantPropagation> X("constprop",
45 "Simple constant propagation");
Chris Lattner2f7c9632001-06-06 20:29:01 +000046}
Chris Lattner04805fa2002-02-26 21:46:54 +000047
Chris Lattner3e860842004-09-20 04:43:15 +000048FunctionPass *llvm::createConstantPropagationPass() {
Misha Brukman373086d2003-05-20 21:01:22 +000049 return new ConstantPropagation();
Chris Lattner04805fa2002-02-26 21:46:54 +000050}
51
Chris Lattnere5485072002-05-06 18:21:31 +000052
Misha Brukman373086d2003-05-20 21:01:22 +000053bool ConstantPropagation::runOnFunction(Function &F) {
Chris Lattnere5485072002-05-06 18:21:31 +000054 // Initialize the worklist to all of the instructions ready to process...
Chris Lattner2d3a7a62004-04-27 15:13:33 +000055 std::set<Instruction*> WorkList;
56 for(inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
57 WorkList.insert(&*i);
58 }
Chris Lattnere5485072002-05-06 18:21:31 +000059 bool Changed = false;
60
61 while (!WorkList.empty()) {
62 Instruction *I = *WorkList.begin();
63 WorkList.erase(WorkList.begin()); // Get an element from the worklist...
64
65 if (!I->use_empty()) // Don't muck with dead instructions...
66 if (Constant *C = ConstantFoldInstruction(I)) {
67 // Add all of the users of this instruction to the worklist, they might
Misha Brukman373086d2003-05-20 21:01:22 +000068 // be constant propagatable now...
Chris Lattnere5485072002-05-06 18:21:31 +000069 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
70 UI != UE; ++UI)
71 WorkList.insert(cast<Instruction>(*UI));
Misha Brukmanb1c93172005-04-21 23:48:37 +000072
Chris Lattnere5485072002-05-06 18:21:31 +000073 // Replace all of the uses of a variable with uses of the constant.
74 I->replaceAllUsesWith(C);
Chris Lattner0b18c1d2002-05-10 15:38:35 +000075
Chris Lattnerd0dc6d52004-04-13 19:28:20 +000076 // Remove the dead instruction.
77 WorkList.erase(I);
78 I->getParent()->getInstList().erase(I);
79
Chris Lattnere5485072002-05-06 18:21:31 +000080 // We made a change to the function...
81 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +000082 ++NumInstKilled;
Chris Lattnere5485072002-05-06 18:21:31 +000083 }
84 }
85 return Changed;
86}