blob: 27efde53cd4c108d6943812de52543ad732e8389 [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//
Chris Lattner4ee451d2007-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 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
Duncan Sands58921fe2008-08-01 12:23:49 +000017// 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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/ADT/Statistic.h"
Chris Lattner79066fa2007-01-30 23:46:24 +000024#include "llvm/Analysis/ConstantFolding.h"
Chris Lattnerb6ac8bc2004-01-12 19:15:20 +000025#include "llvm/Constant.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/DataLayout.h"
Chris Lattner2ed01d82002-05-07 18:10:55 +000027#include "llvm/Instruction.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000028#include "llvm/Pass.h"
Chris Lattner89df1b52002-05-06 18:21:31 +000029#include "llvm/Support/InstIterator.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000030#include "llvm/Target/TargetLibraryInfo.h"
Chris Lattner89df1b52002-05-06 18:21:31 +000031#include <set>
Chris Lattnerd7456022004-01-09 06:02:20 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Chris Lattner0e5f4992006-12-19 21:40:18 +000034STATISTIC(NumInstKilled, "Number of instructions killed");
Chris Lattnera92f6962002-10-01 22:38:41 +000035
Chris Lattner0e5f4992006-12-19 21:40:18 +000036namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +000037 struct ConstantPropagation : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +000038 static char ID; // Pass identification, replacement for typeid
Owen Anderson081c34b2010-10-19 17:21:58 +000039 ConstantPropagation() : FunctionPass(ID) {
40 initializeConstantPropagationPass(*PassRegistry::getPassRegistry());
41 }
Devang Patel794fd752007-05-01 21:15:47 +000042
Chris Lattner7e708292002-06-25 16:13:24 +000043 bool runOnFunction(Function &F);
Chris Lattner97e52e42002-04-28 21:27:06 +000044
45 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000046 AU.setPreservesCFG();
Chad Rosier00737bd2011-12-01 21:29:16 +000047 AU.addRequired<TargetLibraryInfo>();
Chris Lattner97e52e42002-04-28 21:27:06 +000048 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +000049 };
Chris Lattner00950542001-06-06 20:29:01 +000050}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000051
Dan Gohman844731a2008-05-13 00:00:25 +000052char ConstantPropagation::ID = 0;
Chad Rosier00737bd2011-12-01 21:29:16 +000053INITIALIZE_PASS_BEGIN(ConstantPropagation, "constprop",
54 "Simple constant propagation", false, false)
55INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
56INITIALIZE_PASS_END(ConstantPropagation, "constprop",
Owen Andersonce665bd2010-10-07 22:25:06 +000057 "Simple constant propagation", false, false)
Dan Gohman844731a2008-05-13 00:00:25 +000058
Chris Lattner4b501562004-09-20 04:43:15 +000059FunctionPass *llvm::createConstantPropagationPass() {
Misha Brukman82c89b92003-05-20 21:01:22 +000060 return new ConstantPropagation();
Chris Lattnerbd0ef772002-02-26 21:46:54 +000061}
62
Misha Brukman82c89b92003-05-20 21:01:22 +000063bool ConstantPropagation::runOnFunction(Function &F) {
Chris Lattner89df1b52002-05-06 18:21:31 +000064 // Initialize the worklist to all of the instructions ready to process...
Chris Lattner6ffe5512004-04-27 15:13:33 +000065 std::set<Instruction*> WorkList;
66 for(inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) {
67 WorkList.insert(&*i);
68 }
Chris Lattner89df1b52002-05-06 18:21:31 +000069 bool Changed = false;
Micah Villmow3574eca2012-10-08 16:38:25 +000070 DataLayout *TD = getAnalysisIfAvailable<DataLayout>();
Chad Rosier00737bd2011-12-01 21:29:16 +000071 TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();
Chris Lattner89df1b52002-05-06 18:21:31 +000072
73 while (!WorkList.empty()) {
74 Instruction *I = *WorkList.begin();
75 WorkList.erase(WorkList.begin()); // Get an element from the worklist...
76
77 if (!I->use_empty()) // Don't muck with dead instructions...
Chad Rosier00737bd2011-12-01 21:29:16 +000078 if (Constant *C = ConstantFoldInstruction(I, TD, TLI)) {
Chris Lattner89df1b52002-05-06 18:21:31 +000079 // Add all of the users of this instruction to the worklist, they might
Misha Brukman82c89b92003-05-20 21:01:22 +000080 // be constant propagatable now...
Chris Lattner89df1b52002-05-06 18:21:31 +000081 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
82 UI != UE; ++UI)
83 WorkList.insert(cast<Instruction>(*UI));
Misha Brukmanfd939082005-04-21 23:48:37 +000084
Chris Lattner89df1b52002-05-06 18:21:31 +000085 // Replace all of the uses of a variable with uses of the constant.
86 I->replaceAllUsesWith(C);
Chris Lattner3dec1f22002-05-10 15:38:35 +000087
Chris Lattner4647b7c2004-04-13 19:28:20 +000088 // Remove the dead instruction.
89 WorkList.erase(I);
Dan Gohman1adec832008-06-21 22:08:46 +000090 I->eraseFromParent();
Chris Lattner4647b7c2004-04-13 19:28:20 +000091
Chris Lattner89df1b52002-05-06 18:21:31 +000092 // We made a change to the function...
93 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +000094 ++NumInstKilled;
Chris Lattner89df1b52002-05-06 18:21:31 +000095 }
96 }
97 return Changed;
98}