blob: 7085ef98006e21a54625886c02415c6e6a355d1e [file] [log] [blame]
Chris Lattner89df1b52002-05-06 18:21:31 +00001//===- ConstantProp.cpp - Code to perform Simple Constant Propogation -----===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
3// This file implements constant propogation and merging:
4//
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 Lattner59b6b8e2002-01-21 23:17:48 +000014#include "llvm/Transforms/Scalar/ConstantProp.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"
20#include <set>
Chris Lattner00950542001-06-06 20:29:01 +000021
Chris Lattnerbd0ef772002-02-26 21:46:54 +000022namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +000023 struct ConstantPropogation : public FunctionPass {
Chris Lattner96c466b2002-04-29 14:57:45 +000024 const char *getPassName() const { return "Simple Constant Propogation"; }
25
Chris Lattner89df1b52002-05-06 18:21:31 +000026 inline bool runOnFunction(Function *F);
Chris Lattner97e52e42002-04-28 21:27:06 +000027
28 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner89df1b52002-05-06 18:21:31 +000029 AU.preservesCFG();
Chris Lattner97e52e42002-04-28 21:27:06 +000030 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +000031 };
Chris Lattner00950542001-06-06 20:29:01 +000032}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000033
34Pass *createConstantPropogationPass() {
35 return new ConstantPropogation();
36}
37
Chris Lattner89df1b52002-05-06 18:21:31 +000038
39bool ConstantPropogation::runOnFunction(Function *F) {
40 // Initialize the worklist to all of the instructions ready to process...
41 std::set<Instruction*> WorkList(inst_begin(F), inst_end(F));
42 bool Changed = false;
43
44 while (!WorkList.empty()) {
45 Instruction *I = *WorkList.begin();
46 WorkList.erase(WorkList.begin()); // Get an element from the worklist...
47
48 if (!I->use_empty()) // Don't muck with dead instructions...
49 if (Constant *C = ConstantFoldInstruction(I)) {
50 // Add all of the users of this instruction to the worklist, they might
51 // be constant propogatable now...
52 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
53 UI != UE; ++UI)
54 WorkList.insert(cast<Instruction>(*UI));
55
56 // Replace all of the uses of a variable with uses of the constant.
57 I->replaceAllUsesWith(C);
58
59 // We made a change to the function...
60 Changed = true;
61 }
62 }
63 return Changed;
64}