2003-05-21 05:01:22 +08:00
|
|
|
//===- ConstantProp.cpp - Code to perform Simple Constant Propagation -----===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-07 04:29:01 +08:00
|
|
|
//
|
2003-05-21 05:01:22 +08:00
|
|
|
// This file implements constant propagation and merging:
|
2001-06-07 04:29:01 +08:00
|
|
|
//
|
|
|
|
// Specifically, this:
|
2002-05-07 02:21:31 +08:00
|
|
|
// * Converts instructions like "add int 1, 2" into 3
|
2001-06-07 04:29:01 +08:00
|
|
|
//
|
|
|
|
// Notice that:
|
|
|
|
// * This pass has a habit of making definitions be dead. It is a good idea
|
2008-08-01 20:23:49 +08:00
|
|
|
// to run a DIE pass sometime after running this pass.
|
2001-06-07 04:29:01 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-11-13 08:31:22 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2007-01-31 07:46:24 +08:00
|
|
|
#include "llvm/Analysis/ConstantFolding.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constant.h"
|
2014-03-04 18:30:26 +08:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Instruction.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2002-02-27 05:46:54 +08:00
|
|
|
#include "llvm/Pass.h"
|
2018-11-13 08:31:22 +08:00
|
|
|
#include "llvm/Support/DebugCounter.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2018-11-13 08:31:22 +08:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2004-01-09 14:02:20 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "constprop"
|
|
|
|
|
2006-12-20 05:40:18 +08:00
|
|
|
STATISTIC(NumInstKilled, "Number of instructions killed");
|
2018-11-13 08:31:22 +08:00
|
|
|
DEBUG_COUNTER(CPCounter, "constprop-transform",
|
|
|
|
"Controls which instructions are killed");
|
2002-10-02 06:38:41 +08:00
|
|
|
|
2006-12-20 05:40:18 +08:00
|
|
|
namespace {
|
2009-09-02 14:11:42 +08:00
|
|
|
struct ConstantPropagation : public FunctionPass {
|
2007-05-06 21:37:16 +08:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-10-20 01:21:58 +08:00
|
|
|
ConstantPropagation() : FunctionPass(ID) {
|
|
|
|
initializeConstantPropagationPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2007-05-02 05:15:47 +08:00
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
bool runOnFunction(Function &F) override;
|
2002-04-29 05:27:06 +08:00
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2002-10-22 04:00:28 +08:00
|
|
|
AU.setPreservesCFG();
|
2015-01-15 18:41:28 +08:00
|
|
|
AU.addRequired<TargetLibraryInfoWrapperPass>();
|
2002-04-29 05:27:06 +08:00
|
|
|
}
|
2002-02-27 05:46:54 +08:00
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
char ConstantPropagation::ID = 0;
|
2011-12-02 05:29:16 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(ConstantPropagation, "constprop",
|
|
|
|
"Simple constant propagation", false, false)
|
2015-01-15 18:41:28 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
|
2011-12-02 05:29:16 +08:00
|
|
|
INITIALIZE_PASS_END(ConstantPropagation, "constprop",
|
2010-10-08 06:25:06 +08:00
|
|
|
"Simple constant propagation", false, false)
|
2008-05-13 08:00:25 +08:00
|
|
|
|
2004-09-20 12:43:15 +08:00
|
|
|
FunctionPass *llvm::createConstantPropagationPass() {
|
2003-05-21 05:01:22 +08:00
|
|
|
return new ConstantPropagation();
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
2002-02-27 05:46:54 +08:00
|
|
|
|
2003-05-21 05:01:22 +08:00
|
|
|
bool ConstantPropagation::runOnFunction(Function &F) {
|
2016-05-04 06:32:30 +08:00
|
|
|
if (skipFunction(F))
|
|
|
|
return false;
|
|
|
|
|
2002-05-07 02:21:31 +08:00
|
|
|
// Initialize the worklist to all of the instructions ready to process...
|
2018-11-13 08:31:22 +08:00
|
|
|
SmallPtrSet<Instruction *, 16> WorkList;
|
|
|
|
// The SmallVector of WorkList ensures that we do iteration at stable order.
|
|
|
|
// We use two containers rather than one SetVector, since remove is
|
|
|
|
// linear-time, and we don't care enough to remove from Vec.
|
|
|
|
SmallVector<Instruction *, 16> WorkListVec;
|
|
|
|
for (Instruction &I : instructions(&F)) {
|
2016-04-05 07:05:06 +08:00
|
|
|
WorkList.insert(&I);
|
2018-11-13 08:31:22 +08:00
|
|
|
WorkListVec.push_back(&I);
|
|
|
|
}
|
2016-04-05 07:05:06 +08:00
|
|
|
|
2002-05-07 02:21:31 +08:00
|
|
|
bool Changed = false;
|
2015-03-05 02:43:29 +08:00
|
|
|
const DataLayout &DL = F.getParent()->getDataLayout();
|
2015-01-15 18:41:28 +08:00
|
|
|
TargetLibraryInfo *TLI =
|
Change TargetLibraryInfo analysis passes to always require Function
Summary:
This is the first change to enable the TLI to be built per-function so
that -fno-builtin* handling can be migrated to use function attributes.
See discussion on D61634 for background. This is an enabler for fixing
handling of these options for LTO, for example.
This change should not affect behavior, as the provided function is not
yet used to build a specifically per-function TLI, but rather enables
that migration.
Most of the changes were very mechanical, e.g. passing a Function to the
legacy analysis pass's getTLI interface, or in Module level cases,
adding a callback. This is similar to the way the per-function TTI
analysis works.
There was one place where we were looking for builtins but not in the
context of a specific function. See FindCXAAtExit in
lib/Transforms/IPO/GlobalOpt.cpp. I'm somewhat concerned my workaround
could provide the wrong behavior in some corner cases. Suggestions
welcome.
Reviewers: chandlerc, hfinkel
Subscribers: arsenm, dschuff, jvesely, nhaehnle, mehdi_amini, javed.absar, sbc100, jgravelle-google, eraman, aheejin, steven_wu, george.burgess.iv, dexonsmith, jfb, asbirlea, gchatelet, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D66428
llvm-svn: 371284
2019-09-07 11:09:36 +08:00
|
|
|
&getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
|
2002-05-07 02:21:31 +08:00
|
|
|
|
|
|
|
while (!WorkList.empty()) {
|
2018-11-13 08:31:22 +08:00
|
|
|
SmallVector<Instruction*, 16> NewWorkListVec;
|
|
|
|
for (auto *I : WorkListVec) {
|
|
|
|
WorkList.erase(I); // Remove element from the worklist...
|
|
|
|
|
|
|
|
if (!I->use_empty()) // Don't muck with dead instructions...
|
|
|
|
if (Constant *C = ConstantFoldInstruction(I, DL, TLI)) {
|
|
|
|
if (!DebugCounter::shouldExecute(CPCounter))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Add all of the users of this instruction to the worklist, they might
|
|
|
|
// be constant propagatable now...
|
|
|
|
for (User *U : I->users()) {
|
|
|
|
// If user not in the set, then add it to the vector.
|
|
|
|
if (WorkList.insert(cast<Instruction>(U)).second)
|
|
|
|
NewWorkListVec.push_back(cast<Instruction>(U));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace all of the uses of a variable with uses of the constant.
|
|
|
|
I->replaceAllUsesWith(C);
|
|
|
|
|
|
|
|
if (isInstructionTriviallyDead(I, TLI)) {
|
|
|
|
I->eraseFromParent();
|
|
|
|
++NumInstKilled;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We made a change to the function...
|
|
|
|
Changed = true;
|
2016-07-22 12:54:44 +08:00
|
|
|
}
|
2018-11-13 08:31:22 +08:00
|
|
|
}
|
|
|
|
WorkListVec = std::move(NewWorkListVec);
|
2002-05-07 02:21:31 +08:00
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|