blob: efc0551b7eb7723ea9ccf343ea8d5b57d38a8c64 [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//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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
Duncan Sandsc1e48b52008-08-01 12:23:49 +000017// 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"
Chris Lattnere5485072002-05-06 18:21:31 +000027#include "llvm/Support/InstIterator.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000028#include "llvm/ADT/Statistic.h"
Chris Lattnere5485072002-05-06 18:21:31 +000029#include <set>
Chris Lattner49525f82004-01-09 06:02:20 +000030using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000031
Chris Lattner79a42ac2006-12-19 21:40:18 +000032STATISTIC(NumInstKilled, "Number of instructions killed");
Chris Lattnerbf3a0992002-10-01 22:38:41 +000033
Chris Lattner79a42ac2006-12-19 21:40:18 +000034namespace {
Chris Lattner2dd09db2009-09-02 06:11:42 +000035 struct ConstantPropagation : public FunctionPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000036 static char ID; // Pass identification, replacement for typeid
Owen Andersona7aed182010-08-06 18:33:48 +000037 ConstantPropagation() : FunctionPass(ID) {}
Devang Patel09f162c2007-05-01 21:15:47 +000038
Chris Lattner113f4f42002-06-25 16:13:24 +000039 bool runOnFunction(Function &F);
Chris Lattnerf12cc842002-04-28 21:27:06 +000040
41 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner820d9712002-10-21 20:00:28 +000042 AU.setPreservesCFG();
Chris Lattnerf12cc842002-04-28 21:27:06 +000043 }
Chris Lattner04805fa2002-02-26 21:46:54 +000044 };
Chris Lattner2f7c9632001-06-06 20:29:01 +000045}
Chris Lattner04805fa2002-02-26 21:46:54 +000046
Dan Gohmand78c4002008-05-13 00:00:25 +000047char ConstantPropagation::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +000048INITIALIZE_PASS(ConstantPropagation, "constprop",
Owen Andersondf7a4f22010-10-07 22:25:06 +000049 "Simple constant propagation", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000050
Chris Lattner3e860842004-09-20 04:43:15 +000051FunctionPass *llvm::createConstantPropagationPass() {
Misha Brukman373086d2003-05-20 21:01:22 +000052 return new ConstantPropagation();
Chris Lattner04805fa2002-02-26 21:46:54 +000053}
54
Chris Lattnere5485072002-05-06 18:21:31 +000055
Misha Brukman373086d2003-05-20 21:01:22 +000056bool ConstantPropagation::runOnFunction(Function &F) {
Chris Lattnere5485072002-05-06 18:21:31 +000057 // Initialize the worklist to all of the instructions ready to process...
Chris Lattner2d3a7a62004-04-27 15:13:33 +000058 std::set<Instruction*> WorkList;
59 for(inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
60 WorkList.insert(&*i);
61 }
Chris Lattnere5485072002-05-06 18:21:31 +000062 bool Changed = false;
63
64 while (!WorkList.empty()) {
65 Instruction *I = *WorkList.begin();
66 WorkList.erase(WorkList.begin()); // Get an element from the worklist...
67
68 if (!I->use_empty()) // Don't muck with dead instructions...
Chris Lattner46b5c642009-11-06 04:27:31 +000069 if (Constant *C = ConstantFoldInstruction(I)) {
Chris Lattnere5485072002-05-06 18:21:31 +000070 // Add all of the users of this instruction to the worklist, they might
Misha Brukman373086d2003-05-20 21:01:22 +000071 // be constant propagatable now...
Chris Lattnere5485072002-05-06 18:21:31 +000072 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
73 UI != UE; ++UI)
74 WorkList.insert(cast<Instruction>(*UI));
Misha Brukmanb1c93172005-04-21 23:48:37 +000075
Chris Lattnere5485072002-05-06 18:21:31 +000076 // Replace all of the uses of a variable with uses of the constant.
77 I->replaceAllUsesWith(C);
Chris Lattner0b18c1d2002-05-10 15:38:35 +000078
Chris Lattnerd0dc6d52004-04-13 19:28:20 +000079 // Remove the dead instruction.
80 WorkList.erase(I);
Dan Gohman158ff2c2008-06-21 22:08:46 +000081 I->eraseFromParent();
Chris Lattnerd0dc6d52004-04-13 19:28:20 +000082
Chris Lattnere5485072002-05-06 18:21:31 +000083 // We made a change to the function...
84 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +000085 ++NumInstKilled;
Chris Lattnere5485072002-05-06 18:21:31 +000086 }
87 }
88 return Changed;
89}