blob: 8fbed88b13e56e3ccb6acfe74ec2a43e06dc78cd [file] [log] [blame]
Misha Brukman82c89b92003-05-20 21:01:22 +00001//===- ConstantProp.cpp - Code to perform Simple Constant Propagation -----===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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 Lattner0e5f4992006-12-19 21:40:18 +000021#define DEBUG_TYPE "constprop"
Chris Lattner022103b2002-05-07 20:03:00 +000022#include "llvm/Transforms/Scalar.h"
Chris Lattner79066fa2007-01-30 23:46:24 +000023#include "llvm/Analysis/ConstantFolding.h"
Chris Lattnerb6ac8bc2004-01-12 19:15:20 +000024#include "llvm/Constant.h"
Chris Lattner2ed01d82002-05-07 18:10:55 +000025#include "llvm/Instruction.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000026#include "llvm/Pass.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000027#include "llvm/Support/Compiler.h"
Chris Lattner89df1b52002-05-06 18:21:31 +000028#include "llvm/Support/InstIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000029#include "llvm/ADT/Statistic.h"
Chris Lattner89df1b52002-05-06 18:21:31 +000030#include <set>
Chris Lattnerd7456022004-01-09 06:02:20 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattner0e5f4992006-12-19 21:40:18 +000033STATISTIC(NumInstKilled, "Number of instructions killed");
Chris Lattnera92f6962002-10-01 22:38:41 +000034
Chris Lattner0e5f4992006-12-19 21:40:18 +000035namespace {
Reid Spencer9133fe22007-02-05 23:32:05 +000036 struct VISIBILITY_HIDDEN ConstantPropagation : public FunctionPass {
Devang Patel19974732007-05-03 01:11:54 +000037 static char ID; // Pass identifcation, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000038 ConstantPropagation() : FunctionPass((intptr_t)&ID) {}
39
Chris Lattner7e708292002-06-25 16:13:24 +000040 bool runOnFunction(Function &F);
Chris Lattner97e52e42002-04-28 21:27:06 +000041
42 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000043 AU.setPreservesCFG();
Chris Lattner97e52e42002-04-28 21:27:06 +000044 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +000045 };
Chris Lattnerf6293092002-07-23 18:06:35 +000046
Devang Patel19974732007-05-03 01:11:54 +000047 char ConstantPropagation::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000048 RegisterPass<ConstantPropagation> X("constprop",
49 "Simple constant propagation");
Chris Lattner00950542001-06-06 20:29:01 +000050}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000051
Chris Lattner4b501562004-09-20 04:43:15 +000052FunctionPass *llvm::createConstantPropagationPass() {
Misha Brukman82c89b92003-05-20 21:01:22 +000053 return new ConstantPropagation();
Chris Lattnerbd0ef772002-02-26 21:46:54 +000054}
55
Chris Lattner89df1b52002-05-06 18:21:31 +000056
Misha Brukman82c89b92003-05-20 21:01:22 +000057bool ConstantPropagation::runOnFunction(Function &F) {
Chris Lattner89df1b52002-05-06 18:21:31 +000058 // Initialize the worklist to all of the instructions ready to process...
Chris Lattner6ffe5512004-04-27 15:13:33 +000059 std::set<Instruction*> WorkList;
60 for(inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
61 WorkList.insert(&*i);
62 }
Chris Lattner89df1b52002-05-06 18:21:31 +000063 bool Changed = false;
64
65 while (!WorkList.empty()) {
66 Instruction *I = *WorkList.begin();
67 WorkList.erase(WorkList.begin()); // Get an element from the worklist...
68
69 if (!I->use_empty()) // Don't muck with dead instructions...
70 if (Constant *C = ConstantFoldInstruction(I)) {
71 // Add all of the users of this instruction to the worklist, they might
Misha Brukman82c89b92003-05-20 21:01:22 +000072 // be constant propagatable now...
Chris Lattner89df1b52002-05-06 18:21:31 +000073 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
74 UI != UE; ++UI)
75 WorkList.insert(cast<Instruction>(*UI));
Misha Brukmanfd939082005-04-21 23:48:37 +000076
Chris Lattner89df1b52002-05-06 18:21:31 +000077 // Replace all of the uses of a variable with uses of the constant.
78 I->replaceAllUsesWith(C);
Chris Lattner3dec1f22002-05-10 15:38:35 +000079
Chris Lattner4647b7c2004-04-13 19:28:20 +000080 // Remove the dead instruction.
81 WorkList.erase(I);
82 I->getParent()->getInstList().erase(I);
83
Chris Lattner89df1b52002-05-06 18:21:31 +000084 // We made a change to the function...
85 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +000086 ++NumInstKilled;
Chris Lattner89df1b52002-05-06 18:21:31 +000087 }
88 }
89 return Changed;
90}