blob: 66e78960f78d2f9e7bb9a38ccf72e13bdd3bb60b [file] [log] [blame]
Misha Brukman373086d2003-05-20 21:01:22 +00001//===- ConstantProp.cpp - Code to perform Simple Constant Propagation -----===//
John Criswell482202a2003-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 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 Lattnerb4cfa7f2002-05-07 20:03:00 +000021#include "llvm/Transforms/Scalar.h"
Chris Lattner560da702002-05-07 18:10:55 +000022#include "llvm/Transforms/Utils/Local.h"
Chris Lattner65b529f2002-04-08 20:18:09 +000023#include "llvm/ConstantHandling.h"
Chris Lattner560da702002-05-07 18:10:55 +000024#include "llvm/Instruction.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000025#include "llvm/Pass.h"
Chris Lattnere5485072002-05-06 18:21:31 +000026#include "llvm/Support/InstIterator.h"
Chris Lattnerbf3a0992002-10-01 22:38:41 +000027#include "Support/Statistic.h"
Chris Lattnere5485072002-05-06 18:21:31 +000028#include <set>
Chris Lattner2f7c9632001-06-06 20:29:01 +000029
Brian Gaeke960707c2003-11-11 22:41:34 +000030namespace llvm {
31
Chris Lattner04805fa2002-02-26 21:46:54 +000032namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000033 Statistic<> NumInstKilled("constprop", "Number of instructions killed");
34
Misha Brukman373086d2003-05-20 21:01:22 +000035 struct ConstantPropagation : public FunctionPass {
Chris Lattner113f4f42002-06-25 16:13:24 +000036 bool runOnFunction(Function &F);
Chris Lattnerf12cc842002-04-28 21:27:06 +000037
38 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner820d9712002-10-21 20:00:28 +000039 AU.setPreservesCFG();
Chris Lattnerf12cc842002-04-28 21:27:06 +000040 }
Chris Lattner04805fa2002-02-26 21:46:54 +000041 };
Chris Lattnerb28b6802002-07-23 18:06:35 +000042
Misha Brukman373086d2003-05-20 21:01:22 +000043 RegisterOpt<ConstantPropagation> X("constprop","Simple constant propagation");
Chris Lattner2f7c9632001-06-06 20:29:01 +000044}
Chris Lattner04805fa2002-02-26 21:46:54 +000045
Misha Brukman373086d2003-05-20 21:01:22 +000046Pass *createConstantPropagationPass() {
47 return new ConstantPropagation();
Chris Lattner04805fa2002-02-26 21:46:54 +000048}
49
Chris Lattnere5485072002-05-06 18:21:31 +000050
Misha Brukman373086d2003-05-20 21:01:22 +000051bool ConstantPropagation::runOnFunction(Function &F) {
Chris Lattnere5485072002-05-06 18:21:31 +000052 // Initialize the worklist to all of the instructions ready to process...
53 std::set<Instruction*> WorkList(inst_begin(F), inst_end(F));
54 bool Changed = false;
55
56 while (!WorkList.empty()) {
57 Instruction *I = *WorkList.begin();
58 WorkList.erase(WorkList.begin()); // Get an element from the worklist...
59
60 if (!I->use_empty()) // Don't muck with dead instructions...
61 if (Constant *C = ConstantFoldInstruction(I)) {
62 // Add all of the users of this instruction to the worklist, they might
Misha Brukman373086d2003-05-20 21:01:22 +000063 // be constant propagatable now...
Chris Lattnere5485072002-05-06 18:21:31 +000064 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
65 UI != UE; ++UI)
66 WorkList.insert(cast<Instruction>(*UI));
67
68 // Replace all of the uses of a variable with uses of the constant.
69 I->replaceAllUsesWith(C);
Chris Lattner0b18c1d2002-05-10 15:38:35 +000070
Chris Lattnere5485072002-05-06 18:21:31 +000071 // We made a change to the function...
72 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +000073 ++NumInstKilled;
Chris Lattnere5485072002-05-06 18:21:31 +000074 }
75 }
76 return Changed;
77}
Brian Gaeke960707c2003-11-11 22:41:34 +000078
79} // End llvm namespace