blob: c974ebb9456f879c637accaab7322f7435535c3a [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 Lattnerb4cfa7f2002-05-07 20:03:00 +000021#include "llvm/Transforms/Scalar.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/ADT/Statistic.h"
Chris Lattner024f4ab2007-01-30 23:46:24 +000023#include "llvm/Analysis/ConstantFolding.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Constant.h"
Chandler Carruth83948572014-03-04 10:30:26 +000025#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Instruction.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000027#include "llvm/Pass.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000028#include "llvm/Analysis/TargetLibraryInfo.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
Chandler Carruth964daaa2014-04-22 02:55:47 +000032#define DEBUG_TYPE "constprop"
33
Chris Lattner79a42ac2006-12-19 21:40:18 +000034STATISTIC(NumInstKilled, "Number of instructions killed");
Chris Lattnerbf3a0992002-10-01 22:38:41 +000035
Chris Lattner79a42ac2006-12-19 21:40:18 +000036namespace {
Chris Lattner2dd09db2009-09-02 06:11:42 +000037 struct ConstantPropagation : public FunctionPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000038 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000039 ConstantPropagation() : FunctionPass(ID) {
40 initializeConstantPropagationPass(*PassRegistry::getPassRegistry());
41 }
Devang Patel09f162c2007-05-01 21:15:47 +000042
Craig Topper3e4c6972014-03-05 09:10:37 +000043 bool runOnFunction(Function &F) override;
Chris Lattnerf12cc842002-04-28 21:27:06 +000044
Craig Topper3e4c6972014-03-05 09:10:37 +000045 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner820d9712002-10-21 20:00:28 +000046 AU.setPreservesCFG();
Chandler Carruthb98f63d2015-01-15 10:41:28 +000047 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chris Lattnerf12cc842002-04-28 21:27:06 +000048 }
Chris Lattner04805fa2002-02-26 21:46:54 +000049 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000050}
Chris Lattner04805fa2002-02-26 21:46:54 +000051
Dan Gohmand78c4002008-05-13 00:00:25 +000052char ConstantPropagation::ID = 0;
Chad Rosiere6de63d2011-12-01 21:29:16 +000053INITIALIZE_PASS_BEGIN(ConstantPropagation, "constprop",
54 "Simple constant propagation", false, false)
Chandler Carruthb98f63d2015-01-15 10:41:28 +000055INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Chad Rosiere6de63d2011-12-01 21:29:16 +000056INITIALIZE_PASS_END(ConstantPropagation, "constprop",
Owen Andersondf7a4f22010-10-07 22:25:06 +000057 "Simple constant propagation", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000058
Chris Lattner3e860842004-09-20 04:43:15 +000059FunctionPass *llvm::createConstantPropagationPass() {
Misha Brukman373086d2003-05-20 21:01:22 +000060 return new ConstantPropagation();
Chris Lattner04805fa2002-02-26 21:46:54 +000061}
62
Misha Brukman373086d2003-05-20 21:01:22 +000063bool ConstantPropagation::runOnFunction(Function &F) {
Chris Lattnere5485072002-05-06 18:21:31 +000064 // Initialize the worklist to all of the instructions ready to process...
Chris Lattner2d3a7a62004-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 Lattnere5485072002-05-06 18:21:31 +000069 bool Changed = false;
Mehdi Amini46a43552015-03-04 18:43:29 +000070 const DataLayout &DL = F.getParent()->getDataLayout();
Chandler Carruthb98f63d2015-01-15 10:41:28 +000071 TargetLibraryInfo *TLI =
72 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chris Lattnere5485072002-05-06 18:21:31 +000073
74 while (!WorkList.empty()) {
75 Instruction *I = *WorkList.begin();
76 WorkList.erase(WorkList.begin()); // Get an element from the worklist...
77
78 if (!I->use_empty()) // Don't muck with dead instructions...
Mehdi Aminia28d91d2015-03-10 02:37:25 +000079 if (Constant *C = ConstantFoldInstruction(I, DL, TLI)) {
Chris Lattnere5485072002-05-06 18:21:31 +000080 // Add all of the users of this instruction to the worklist, they might
Misha Brukman373086d2003-05-20 21:01:22 +000081 // be constant propagatable now...
Chandler Carruthcdf47882014-03-09 03:16:01 +000082 for (User *U : I->users())
83 WorkList.insert(cast<Instruction>(U));
Misha Brukmanb1c93172005-04-21 23:48:37 +000084
Chris Lattnere5485072002-05-06 18:21:31 +000085 // Replace all of the uses of a variable with uses of the constant.
86 I->replaceAllUsesWith(C);
Chris Lattner0b18c1d2002-05-10 15:38:35 +000087
Chris Lattnerd0dc6d52004-04-13 19:28:20 +000088 // Remove the dead instruction.
89 WorkList.erase(I);
Dan Gohman158ff2c2008-06-21 22:08:46 +000090 I->eraseFromParent();
Chris Lattnerd0dc6d52004-04-13 19:28:20 +000091
Chris Lattnere5485072002-05-06 18:21:31 +000092 // We made a change to the function...
93 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +000094 ++NumInstKilled;
Chris Lattnere5485072002-05-06 18:21:31 +000095 }
96 }
97 return Changed;
98}