blob: 4b8e9513c823db7fce1d7022dab0226c05c17685 [file] [log] [blame]
Misha Brukman82c89b92003-05-20 21:01:22 +00001//===- ConstantProp.cpp - Code to perform Simple Constant Propagation -----===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
Misha Brukman82c89b92003-05-20 21:01:22 +00003// This file implements constant propagation and merging:
Chris Lattner00950542001-06-06 20:29:01 +00004//
5// Specifically, this:
Chris Lattner89df1b52002-05-06 18:21:31 +00006// * Converts instructions like "add int 1, 2" into 3
Chris Lattner00950542001-06-06 20:29:01 +00007//
8// Notice that:
9// * This pass has a habit of making definitions be dead. It is a good idea
Chris Lattner89df1b52002-05-06 18:21:31 +000010// to to run a DIE pass sometime after running this pass.
Chris Lattner00950542001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner022103b2002-05-07 20:03:00 +000014#include "llvm/Transforms/Scalar.h"
Chris Lattner2ed01d82002-05-07 18:10:55 +000015#include "llvm/Transforms/Utils/Local.h"
Chris Lattner968ddc92002-04-08 20:18:09 +000016#include "llvm/ConstantHandling.h"
Chris Lattner2ed01d82002-05-07 18:10:55 +000017#include "llvm/Instruction.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000018#include "llvm/Pass.h"
Chris Lattner89df1b52002-05-06 18:21:31 +000019#include "llvm/Support/InstIterator.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000020#include "Support/Statistic.h"
Chris Lattner89df1b52002-05-06 18:21:31 +000021#include <set>
Chris Lattner00950542001-06-06 20:29:01 +000022
Chris Lattnerbd0ef772002-02-26 21:46:54 +000023namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000024 Statistic<> NumInstKilled("constprop", "Number of instructions killed");
25
Misha Brukman82c89b92003-05-20 21:01:22 +000026 struct ConstantPropagation : public FunctionPass {
Chris Lattner7e708292002-06-25 16:13:24 +000027 bool runOnFunction(Function &F);
Chris Lattner97e52e42002-04-28 21:27:06 +000028
29 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000030 AU.setPreservesCFG();
Chris Lattner97e52e42002-04-28 21:27:06 +000031 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +000032 };
Chris Lattnerf6293092002-07-23 18:06:35 +000033
Misha Brukman82c89b92003-05-20 21:01:22 +000034 RegisterOpt<ConstantPropagation> X("constprop","Simple constant propagation");
Chris Lattner00950542001-06-06 20:29:01 +000035}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000036
Misha Brukman82c89b92003-05-20 21:01:22 +000037Pass *createConstantPropagationPass() {
38 return new ConstantPropagation();
Chris Lattnerbd0ef772002-02-26 21:46:54 +000039}
40
Chris Lattner89df1b52002-05-06 18:21:31 +000041
Misha Brukman82c89b92003-05-20 21:01:22 +000042bool ConstantPropagation::runOnFunction(Function &F) {
Chris Lattner89df1b52002-05-06 18:21:31 +000043 // Initialize the worklist to all of the instructions ready to process...
44 std::set<Instruction*> WorkList(inst_begin(F), inst_end(F));
45 bool Changed = false;
46
47 while (!WorkList.empty()) {
48 Instruction *I = *WorkList.begin();
49 WorkList.erase(WorkList.begin()); // Get an element from the worklist...
50
51 if (!I->use_empty()) // Don't muck with dead instructions...
52 if (Constant *C = ConstantFoldInstruction(I)) {
53 // Add all of the users of this instruction to the worklist, they might
Misha Brukman82c89b92003-05-20 21:01:22 +000054 // be constant propagatable now...
Chris Lattner89df1b52002-05-06 18:21:31 +000055 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
56 UI != UE; ++UI)
57 WorkList.insert(cast<Instruction>(*UI));
58
59 // Replace all of the uses of a variable with uses of the constant.
60 I->replaceAllUsesWith(C);
Chris Lattner3dec1f22002-05-10 15:38:35 +000061
Chris Lattner89df1b52002-05-06 18:21:31 +000062 // We made a change to the function...
63 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +000064 ++NumInstKilled;
Chris Lattner89df1b52002-05-06 18:21:31 +000065 }
66 }
67 return Changed;
68}