2018-06-30 07:36:03 +08:00
|
|
|
//===- InstSimplifyPass.cpp -----------------------------------------------===//
|
2010-12-21 05:07:42 +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
|
2010-12-21 05:07:42 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-06-30 07:36:03 +08:00
|
|
|
#include "llvm/Transforms/Scalar/InstSimplifyPass.h"
|
2011-01-01 01:49:05 +08:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
2011-01-03 18:50:04 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2010-12-21 05:07:42 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2016-12-19 16:22:17 +08:00
|
|
|
#include "llvm/Analysis/AssumptionCache.h"
|
2010-12-21 05:07:42 +08:00
|
|
|
#include "llvm/Analysis/InstructionSimplify.h"
|
2017-10-10 07:19:02 +08:00
|
|
|
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
|
2015-11-25 02:57:06 +08:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-01-13 17:26:24 +08:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Type.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"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Pass.h"
|
2020-07-30 04:54:07 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2018-03-29 01:44:36 +08:00
|
|
|
#include "llvm/Transforms/Utils.h"
|
2018-06-30 07:36:03 +08:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2020-08-28 15:34:42 +08:00
|
|
|
|
2010-12-21 05:07:42 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "instsimplify"
|
|
|
|
|
2010-12-21 05:07:42 +08:00
|
|
|
STATISTIC(NumSimplified, "Number of redundant instructions removed");
|
|
|
|
|
2017-04-26 21:52:16 +08:00
|
|
|
static bool runImpl(Function &F, const SimplifyQuery &SQ,
|
2017-02-07 02:26:06 +08:00
|
|
|
OptimizationRemarkEmitter *ORE) {
|
2016-11-27 23:53:48 +08:00
|
|
|
SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
|
2016-07-08 05:14:36 +08:00
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
do {
|
2019-07-27 22:05:51 +08:00
|
|
|
for (BasicBlock &BB : F) {
|
2019-09-04 23:12:55 +08:00
|
|
|
// Unreachable code can take on strange forms that we are not prepared to
|
|
|
|
// handle. For example, an instruction may have itself as an operand.
|
|
|
|
if (!SQ.DT->isReachableFromEntry(&BB))
|
|
|
|
continue;
|
|
|
|
|
2020-01-24 06:21:08 +08:00
|
|
|
SmallVector<WeakTrackingVH, 8> DeadInstsInBB;
|
2019-07-27 22:05:51 +08:00
|
|
|
for (Instruction &I : BB) {
|
|
|
|
// The first time through the loop, ToSimplify is empty and we try to
|
|
|
|
// simplify all instructions. On later iterations, ToSimplify is not
|
2016-07-08 05:14:36 +08:00
|
|
|
// empty and we only bother simplifying instructions that are in it.
|
2019-07-27 22:05:51 +08:00
|
|
|
if (!ToSimplify->empty() && !ToSimplify->count(&I))
|
2016-07-08 05:14:36 +08:00
|
|
|
continue;
|
2016-11-27 23:53:48 +08:00
|
|
|
|
2019-07-27 22:05:51 +08:00
|
|
|
// Don't waste time simplifying dead/unused instructions.
|
|
|
|
if (isInstructionTriviallyDead(&I)) {
|
|
|
|
DeadInstsInBB.push_back(&I);
|
2019-08-09 15:08:25 +08:00
|
|
|
Changed = true;
|
2019-07-27 22:05:51 +08:00
|
|
|
} else if (!I.use_empty()) {
|
|
|
|
if (Value *V = SimplifyInstruction(&I, SQ, ORE)) {
|
2016-07-08 05:14:36 +08:00
|
|
|
// Mark all uses for resimplification next time round the loop.
|
2019-07-27 22:05:51 +08:00
|
|
|
for (User *U : I.users())
|
2016-07-08 05:14:36 +08:00
|
|
|
Next->insert(cast<Instruction>(U));
|
2019-07-27 22:05:51 +08:00
|
|
|
I.replaceAllUsesWith(V);
|
2016-07-08 05:14:36 +08:00
|
|
|
++NumSimplified;
|
|
|
|
Changed = true;
|
2019-07-27 22:05:51 +08:00
|
|
|
// A call can get simplified, but it may not be trivially dead.
|
|
|
|
if (isInstructionTriviallyDead(&I))
|
|
|
|
DeadInstsInBB.push_back(&I);
|
2016-07-08 05:14:36 +08:00
|
|
|
}
|
2016-11-27 23:53:48 +08:00
|
|
|
}
|
2016-07-08 05:14:36 +08:00
|
|
|
}
|
2019-07-27 22:05:51 +08:00
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(DeadInstsInBB, SQ.TLI);
|
2016-11-27 23:53:48 +08:00
|
|
|
}
|
2016-07-08 05:14:36 +08:00
|
|
|
|
|
|
|
// Place the list of instructions to simplify on the next loop iteration
|
|
|
|
// into ToSimplify.
|
|
|
|
std::swap(ToSimplify, Next);
|
|
|
|
Next->clear();
|
|
|
|
} while (!ToSimplify->empty());
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2010-12-21 05:07:42 +08:00
|
|
|
namespace {
|
2018-06-30 07:36:03 +08:00
|
|
|
struct InstSimplifyLegacyPass : public FunctionPass {
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
InstSimplifyLegacyPass() : FunctionPass(ID) {
|
|
|
|
initializeInstSimplifyLegacyPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
AU.addRequired<DominatorTreeWrapperPass>();
|
|
|
|
AU.addRequired<AssumptionCacheTracker>();
|
|
|
|
AU.addRequired<TargetLibraryInfoWrapperPass>();
|
|
|
|
AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
|
|
|
|
}
|
|
|
|
|
2019-09-04 23:12:55 +08:00
|
|
|
/// Remove instructions that simplify.
|
2018-06-30 07:36:03 +08:00
|
|
|
bool runOnFunction(Function &F) override {
|
|
|
|
if (skipFunction(F))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const DominatorTree *DT =
|
|
|
|
&getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
|
|
|
const 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);
|
2018-06-30 07:36:03 +08:00
|
|
|
AssumptionCache *AC =
|
|
|
|
&getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
|
|
|
|
OptimizationRemarkEmitter *ORE =
|
|
|
|
&getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
|
|
|
|
const DataLayout &DL = F.getParent()->getDataLayout();
|
|
|
|
const SimplifyQuery SQ(DL, TLI, DT, AC);
|
|
|
|
return runImpl(F, SQ, ORE);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
char InstSimplifyLegacyPass::ID = 0;
|
|
|
|
INITIALIZE_PASS_BEGIN(InstSimplifyLegacyPass, "instsimplify",
|
2011-12-01 11:08:23 +08:00
|
|
|
"Remove redundant instructions", false, false)
|
2016-12-19 16:22:17 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
|
2016-09-07 06:17:16 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
2015-01-15 18:41:28 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
|
2017-02-07 02:26:06 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
|
2018-06-30 07:36:03 +08:00
|
|
|
INITIALIZE_PASS_END(InstSimplifyLegacyPass, "instsimplify",
|
2011-12-01 11:08:23 +08:00
|
|
|
"Remove redundant instructions", false, false)
|
2010-12-21 05:07:42 +08:00
|
|
|
|
|
|
|
// Public interface to the simplify instructions pass.
|
2018-06-30 07:36:03 +08:00
|
|
|
FunctionPass *llvm::createInstSimplifyLegacyPass() {
|
|
|
|
return new InstSimplifyLegacyPass();
|
2010-12-21 05:07:42 +08:00
|
|
|
}
|
2016-07-08 05:14:36 +08:00
|
|
|
|
2018-06-30 07:36:03 +08:00
|
|
|
PreservedAnalyses InstSimplifyPass::run(Function &F,
|
|
|
|
FunctionAnalysisManager &AM) {
|
2016-09-07 06:17:16 +08:00
|
|
|
auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
|
2016-07-08 05:14:36 +08:00
|
|
|
auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
|
2016-12-19 16:22:17 +08:00
|
|
|
auto &AC = AM.getResult<AssumptionAnalysis>(F);
|
2017-02-07 02:26:06 +08:00
|
|
|
auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
|
2017-04-26 21:52:16 +08:00
|
|
|
const DataLayout &DL = F.getParent()->getDataLayout();
|
|
|
|
const SimplifyQuery SQ(DL, &TLI, &DT, &AC);
|
|
|
|
bool Changed = runImpl(F, SQ, &ORE);
|
2016-07-08 05:14:36 +08:00
|
|
|
if (!Changed)
|
|
|
|
return PreservedAnalyses::all();
|
2017-01-15 14:32:49 +08:00
|
|
|
|
|
|
|
PreservedAnalyses PA;
|
|
|
|
PA.preserveSet<CFGAnalyses>();
|
|
|
|
return PA;
|
2016-07-08 05:14:36 +08:00
|
|
|
}
|