mirror of
https://github.com/RPCS3/llvm.git
synced 2026-07-24 20:25:32 -04:00
492b2bcbcf
------------------------------------------------------------------------ r309945 | spatel | 2017-08-03 08:07:37 -0700 (Thu, 03 Aug 2017) | 2 lines [BDCE] add tests to show invalid/incomplete transforms ------------------------------------------------------------------------ ------------------------------------------------------------------------ r310779 | spatel | 2017-08-12 09:41:08 -0700 (Sat, 12 Aug 2017) | 13 lines [BDCE] clear poison generators after turning a value into zero (PR33695, PR34037) nsw, nuw, and exact carry implicit assumptions about their operands, so we need to clear those after trivializing a value. We decided there was no danger for llvm.assume or metadata, so there's just a comment about that. This fixes miscompiles as shown in: https://bugs.llvm.org/show_bug.cgi?id=33695 https://bugs.llvm.org/show_bug.cgi?id=34037 Differential Revision: https://reviews.llvm.org/D36592 ------------------------------------------------------------------------ ------------------------------------------------------------------------ r310842 | spatel | 2017-08-14 08:13:46 -0700 (Mon, 14 Aug 2017) | 16 lines [BDCE] reduce scope of an assert (PR34179) The assert was added with r310779 and is usually correct, but as the test shows, not always. The 'volatile' on the load is needed to expose the faulty path because without it, DemandedBits would return that the load is just dead rather than not demanded, and so we wouldn't hit the bogus assert. Also, since the lambda is just a single-line now, get rid of it and inline the DB.isAllOnesValue() calls. This should fix (prevent execution of a faulty assert): https://bugs.llvm.org/show_bug.cgi?id=34179 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_50@310898 91177308-0d34-0410-b5e6-96231b3b80d8
163 lines
5.6 KiB
C++
163 lines
5.6 KiB
C++
//===---- BDCE.cpp - Bit-tracking dead code elimination -------------------===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file implements the Bit-Tracking Dead Code Elimination pass. Some
|
|
// instructions (shifts, some ands, ors, etc.) kill some of their input bits.
|
|
// We track these dead bits and remove instructions that compute only these
|
|
// dead bits.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/Transforms/Scalar/BDCE.h"
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
#include "llvm/ADT/SmallVector.h"
|
|
#include "llvm/ADT/Statistic.h"
|
|
#include "llvm/Analysis/DemandedBits.h"
|
|
#include "llvm/Analysis/GlobalsModRef.h"
|
|
#include "llvm/IR/CFG.h"
|
|
#include "llvm/IR/InstIterator.h"
|
|
#include "llvm/IR/Instructions.h"
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
#include "llvm/IR/Operator.h"
|
|
#include "llvm/Pass.h"
|
|
#include "llvm/Support/Debug.h"
|
|
#include "llvm/Support/raw_ostream.h"
|
|
#include "llvm/Transforms/Scalar.h"
|
|
using namespace llvm;
|
|
|
|
#define DEBUG_TYPE "bdce"
|
|
|
|
STATISTIC(NumRemoved, "Number of instructions removed (unused)");
|
|
STATISTIC(NumSimplified, "Number of instructions trivialized (dead bits)");
|
|
|
|
/// If an instruction is trivialized (dead), then the chain of users of that
|
|
/// instruction may need to be cleared of assumptions that can no longer be
|
|
/// guaranteed correct.
|
|
static void clearAssumptionsOfUsers(Instruction *I, DemandedBits &DB) {
|
|
assert(I->getType()->isIntegerTy() && "Trivializing a non-integer value?");
|
|
|
|
// Initialize the worklist with eligible direct users.
|
|
SmallVector<Instruction *, 16> WorkList;
|
|
for (User *JU : I->users()) {
|
|
// If all bits of a user are demanded, then we know that nothing below that
|
|
// in the def-use chain needs to be changed.
|
|
auto *J = dyn_cast<Instruction>(JU);
|
|
if (J && !DB.getDemandedBits(J).isAllOnesValue())
|
|
WorkList.push_back(J);
|
|
}
|
|
|
|
// DFS through subsequent users while tracking visits to avoid cycles.
|
|
SmallPtrSet<Instruction *, 16> Visited;
|
|
while (!WorkList.empty()) {
|
|
Instruction *J = WorkList.pop_back_val();
|
|
|
|
// NSW, NUW, and exact are based on operands that might have changed.
|
|
J->dropPoisonGeneratingFlags();
|
|
|
|
// We do not have to worry about llvm.assume or range metadata:
|
|
// 1. llvm.assume demands its operand, so trivializing can't change it.
|
|
// 2. range metadata only applies to memory accesses which demand all bits.
|
|
|
|
Visited.insert(J);
|
|
|
|
for (User *KU : J->users()) {
|
|
// If all bits of a user are demanded, then we know that nothing below
|
|
// that in the def-use chain needs to be changed.
|
|
auto *K = dyn_cast<Instruction>(KU);
|
|
if (K && !Visited.count(K) && !DB.getDemandedBits(K).isAllOnesValue())
|
|
WorkList.push_back(K);
|
|
}
|
|
}
|
|
}
|
|
|
|
static bool bitTrackingDCE(Function &F, DemandedBits &DB) {
|
|
SmallVector<Instruction*, 128> Worklist;
|
|
bool Changed = false;
|
|
for (Instruction &I : instructions(F)) {
|
|
// If the instruction has side effects and no non-dbg uses,
|
|
// skip it. This way we avoid computing known bits on an instruction
|
|
// that will not help us.
|
|
if (I.mayHaveSideEffects() && I.use_empty())
|
|
continue;
|
|
|
|
if (I.getType()->isIntegerTy() &&
|
|
!DB.getDemandedBits(&I).getBoolValue()) {
|
|
// For live instructions that have all dead bits, first make them dead by
|
|
// replacing all uses with something else. Then, if they don't need to
|
|
// remain live (because they have side effects, etc.) we can remove them.
|
|
DEBUG(dbgs() << "BDCE: Trivializing: " << I << " (all bits dead)\n");
|
|
|
|
clearAssumptionsOfUsers(&I, DB);
|
|
|
|
// FIXME: In theory we could substitute undef here instead of zero.
|
|
// This should be reconsidered once we settle on the semantics of
|
|
// undef, poison, etc.
|
|
Value *Zero = ConstantInt::get(I.getType(), 0);
|
|
++NumSimplified;
|
|
I.replaceNonMetadataUsesWith(Zero);
|
|
Changed = true;
|
|
}
|
|
if (!DB.isInstructionDead(&I))
|
|
continue;
|
|
|
|
Worklist.push_back(&I);
|
|
I.dropAllReferences();
|
|
Changed = true;
|
|
}
|
|
|
|
for (Instruction *&I : Worklist) {
|
|
++NumRemoved;
|
|
I->eraseFromParent();
|
|
}
|
|
|
|
return Changed;
|
|
}
|
|
|
|
PreservedAnalyses BDCEPass::run(Function &F, FunctionAnalysisManager &AM) {
|
|
auto &DB = AM.getResult<DemandedBitsAnalysis>(F);
|
|
if (!bitTrackingDCE(F, DB))
|
|
return PreservedAnalyses::all();
|
|
|
|
PreservedAnalyses PA;
|
|
PA.preserveSet<CFGAnalyses>();
|
|
PA.preserve<GlobalsAA>();
|
|
return PA;
|
|
}
|
|
|
|
namespace {
|
|
struct BDCELegacyPass : public FunctionPass {
|
|
static char ID; // Pass identification, replacement for typeid
|
|
BDCELegacyPass() : FunctionPass(ID) {
|
|
initializeBDCELegacyPassPass(*PassRegistry::getPassRegistry());
|
|
}
|
|
|
|
bool runOnFunction(Function &F) override {
|
|
if (skipFunction(F))
|
|
return false;
|
|
auto &DB = getAnalysis<DemandedBitsWrapperPass>().getDemandedBits();
|
|
return bitTrackingDCE(F, DB);
|
|
}
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
AU.setPreservesCFG();
|
|
AU.addRequired<DemandedBitsWrapperPass>();
|
|
AU.addPreserved<GlobalsAAWrapperPass>();
|
|
}
|
|
};
|
|
}
|
|
|
|
char BDCELegacyPass::ID = 0;
|
|
INITIALIZE_PASS_BEGIN(BDCELegacyPass, "bdce",
|
|
"Bit-Tracking Dead Code Elimination", false, false)
|
|
INITIALIZE_PASS_DEPENDENCY(DemandedBitsWrapperPass)
|
|
INITIALIZE_PASS_END(BDCELegacyPass, "bdce",
|
|
"Bit-Tracking Dead Code Elimination", false, false)
|
|
|
|
FunctionPass *llvm::createBitTrackingDCEPass() { return new BDCELegacyPass(); }
|