blob: 770321c740a0fc6f4309c43f2c907540358b3ed2 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukmanb1c93172005-04-21 23:48:37 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00008//
Misha Brukman373086d2003-05-20 21:01:22 +00009// This file implements constant propagation and merging:
Chris Lattner2f7c9632001-06-06 20:29:01 +000010//
11// Specifically, this:
Chris Lattnere5485072002-05-06 18:21:31 +000012// * Converts instructions like "add int 1, 2" into 3
Chris Lattner2f7c9632001-06-06 20:29:01 +000013//
14// Notice that:
15// * This pass has a habit of making definitions be dead. It is a good idea
Duncan Sandsc1e48b52008-08-01 12:23:49 +000016// to run a DIE pass sometime after running this pass.
Chris Lattner2f7c9632001-06-06 20:29:01 +000017//
18//===----------------------------------------------------------------------===//
19
Zhizhou Yangcc633af2018-11-13 00:31:22 +000020#include "llvm/ADT/SmallPtrSet.h"
21#include "llvm/ADT/SmallVector.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 Carruth6bda14b2017-06-06 11:49:48 +000024#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Constant.h"
Chandler Carruth83948572014-03-04 10:30:26 +000026#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Instruction.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000028#include "llvm/Pass.h"
Zhizhou Yangcc633af2018-11-13 00:31:22 +000029#include "llvm/Support/DebugCounter.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000030#include "llvm/Transforms/Scalar.h"
Zhizhou Yangcc633af2018-11-13 00:31:22 +000031#include "llvm/Transforms/Utils/Local.h"
Chris Lattner49525f82004-01-09 06:02:20 +000032using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000033
Chandler Carruth964daaa2014-04-22 02:55:47 +000034#define DEBUG_TYPE "constprop"
35
Chris Lattner79a42ac2006-12-19 21:40:18 +000036STATISTIC(NumInstKilled, "Number of instructions killed");
Zhizhou Yangcc633af2018-11-13 00:31:22 +000037DEBUG_COUNTER(CPCounter, "constprop-transform",
38 "Controls which instructions are killed");
Chris Lattnerbf3a0992002-10-01 22:38:41 +000039
Chris Lattner79a42ac2006-12-19 21:40:18 +000040namespace {
Chris Lattner2dd09db2009-09-02 06:11:42 +000041 struct ConstantPropagation : public FunctionPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000042 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000043 ConstantPropagation() : FunctionPass(ID) {
44 initializeConstantPropagationPass(*PassRegistry::getPassRegistry());
45 }
Devang Patel09f162c2007-05-01 21:15:47 +000046
Craig Topper3e4c6972014-03-05 09:10:37 +000047 bool runOnFunction(Function &F) override;
Chris Lattnerf12cc842002-04-28 21:27:06 +000048
Craig Topper3e4c6972014-03-05 09:10:37 +000049 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner820d9712002-10-21 20:00:28 +000050 AU.setPreservesCFG();
Chandler Carruthb98f63d2015-01-15 10:41:28 +000051 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chris Lattnerf12cc842002-04-28 21:27:06 +000052 }
Chris Lattner04805fa2002-02-26 21:46:54 +000053 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000054}
Chris Lattner04805fa2002-02-26 21:46:54 +000055
Dan Gohmand78c4002008-05-13 00:00:25 +000056char ConstantPropagation::ID = 0;
Chad Rosiere6de63d2011-12-01 21:29:16 +000057INITIALIZE_PASS_BEGIN(ConstantPropagation, "constprop",
58 "Simple constant propagation", false, false)
Chandler Carruthb98f63d2015-01-15 10:41:28 +000059INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Chad Rosiere6de63d2011-12-01 21:29:16 +000060INITIALIZE_PASS_END(ConstantPropagation, "constprop",
Owen Andersondf7a4f22010-10-07 22:25:06 +000061 "Simple constant propagation", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000062
Chris Lattner3e860842004-09-20 04:43:15 +000063FunctionPass *llvm::createConstantPropagationPass() {
Misha Brukman373086d2003-05-20 21:01:22 +000064 return new ConstantPropagation();
Chris Lattner04805fa2002-02-26 21:46:54 +000065}
66
Misha Brukman373086d2003-05-20 21:01:22 +000067bool ConstantPropagation::runOnFunction(Function &F) {
Andrew Kaylor50271f72016-05-03 22:32:30 +000068 if (skipFunction(F))
69 return false;
70
Chris Lattnere5485072002-05-06 18:21:31 +000071 // Initialize the worklist to all of the instructions ready to process...
Zhizhou Yangcc633af2018-11-13 00:31:22 +000072 SmallPtrSet<Instruction *, 16> WorkList;
73 // The SmallVector of WorkList ensures that we do iteration at stable order.
74 // We use two containers rather than one SetVector, since remove is
75 // linear-time, and we don't care enough to remove from Vec.
76 SmallVector<Instruction *, 16> WorkListVec;
77 for (Instruction &I : instructions(&F)) {
Sanjay Patele77c7de2016-04-04 23:05:06 +000078 WorkList.insert(&I);
Zhizhou Yangcc633af2018-11-13 00:31:22 +000079 WorkListVec.push_back(&I);
80 }
Sanjay Patele77c7de2016-04-04 23:05:06 +000081
Chris Lattnere5485072002-05-06 18:21:31 +000082 bool Changed = false;
Mehdi Amini46a43552015-03-04 18:43:29 +000083 const DataLayout &DL = F.getParent()->getDataLayout();
Chandler Carruthb98f63d2015-01-15 10:41:28 +000084 TargetLibraryInfo *TLI =
85 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chris Lattnere5485072002-05-06 18:21:31 +000086
87 while (!WorkList.empty()) {
Zhizhou Yangcc633af2018-11-13 00:31:22 +000088 SmallVector<Instruction*, 16> NewWorkListVec;
89 for (auto *I : WorkListVec) {
90 WorkList.erase(I); // Remove element from the worklist...
Chris Lattnere5485072002-05-06 18:21:31 +000091
Zhizhou Yangcc633af2018-11-13 00:31:22 +000092 if (!I->use_empty()) // Don't muck with dead instructions...
93 if (Constant *C = ConstantFoldInstruction(I, DL, TLI)) {
94 if (!DebugCounter::shouldExecute(CPCounter))
95 continue;
Misha Brukmanb1c93172005-04-21 23:48:37 +000096
Zhizhou Yangcc633af2018-11-13 00:31:22 +000097 // Add all of the users of this instruction to the worklist, they might
98 // be constant propagatable now...
99 for (User *U : I->users()) {
100 // If user not in the set, then add it to the vector.
101 if (WorkList.insert(cast<Instruction>(U)).second)
102 NewWorkListVec.push_back(cast<Instruction>(U));
103 }
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000104
Zhizhou Yangcc633af2018-11-13 00:31:22 +0000105 // Replace all of the uses of a variable with uses of the constant.
106 I->replaceAllUsesWith(C);
107
108 if (isInstructionTriviallyDead(I, TLI)) {
109 I->eraseFromParent();
110 ++NumInstKilled;
111 }
112
113 // We made a change to the function...
114 Changed = true;
David Majnemer522a9112016-07-22 04:54:44 +0000115 }
Zhizhou Yangcc633af2018-11-13 00:31:22 +0000116 }
117 WorkListVec = std::move(NewWorkListVec);
Chris Lattnere5485072002-05-06 18:21:31 +0000118 }
119 return Changed;
120}