1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2017-2024 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
//
/// GenXDeadVectorRemoval
/// -----------------------
///
/// GenXDeadVectorRemoval - dead code removal pass that uses analysis of
/// individual elements of vector types
///
/// Pass can do the following modifications to the code:
///
/// 1. If any operand in the instruction turns out to be unused, it is
/// completely replaced with undef. If operand is constant and partially
// unused, these unused elements are replaced with undefs to simplify work
// for ConstantLoader in future
// 2. If whole value is not used it is removed from the code like in
// traditional DCE
//===----------------------------------------------------------------------===//
#include "GenX.h"
#include "GenXGotoJoin.h"
#include "GenXLiveElements.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/Pass.h"
#include "llvm/Support/raw_ostream.h"
#include "Probe/Assertion.h"
#include "vc/Utils/General/IndexFlattener.h"
#define DEBUG_TYPE "GENX_DEAD_VECTOR_REMOVAL"
using namespace llvm;
using namespace genx;
STATISTIC(NumDeletedInsts, "Number of deleted instructions");
STATISTIC(NumSimplifiedUses, "Number of simplified uses");
namespace {
// GenXDeadVectorRemoval : dead vector element removal pass
class GenXDeadVectorRemoval : public FunctionPass {
public:
static char ID;
explicit GenXDeadVectorRemoval() : FunctionPass(ID) {}
StringRef getPassName() const override {
return "GenX dead vector element removal pass";
}
void getAnalysisUsage(AnalysisUsage &AU) const override;
bool runOnFunction(Function &F) override;
private:
Value *trySimplify(Value *V, const LiveElements &LiveElems);
Constant *trySimplify(Constant *C, const LiveElements &LiveElems);
Constant *trySimplify(ConstantAggregate *CA, const LiveElements &LiveElems);
Constant *trySimplify(ConstantData *CD, const LiveElements &LiveElems);
Constant *trySimplify(ConstantDataSequential *CDS,
const LiveElements &LiveElems);
};
} // end anonymous namespace
char GenXDeadVectorRemoval::ID = 0;
namespace llvm {
void initializeGenXDeadVectorRemovalPass(PassRegistry &);
}
INITIALIZE_PASS_BEGIN(GenXDeadVectorRemoval, "GenXDeadVectorRemoval",
"GenXDeadVectorRemoval", false, false)
INITIALIZE_PASS_DEPENDENCY(GenXFuncLiveElements)
INITIALIZE_PASS_END(GenXDeadVectorRemoval, "GenXDeadVectorRemoval",
"GenXDeadVectorRemoval", false, false)
FunctionPass *llvm::createGenXDeadVectorRemovalPass() {
initializeGenXDeadVectorRemovalPass(*PassRegistry::getPassRegistry());
return new GenXDeadVectorRemoval();
}
void GenXDeadVectorRemoval::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<GenXFuncLiveElements>();
AU.setPreservesCFG();
}
/***********************************************************************
* GenXDeadVectorRemoval::runOnFunction : process one function
*/
bool GenXDeadVectorRemoval::runOnFunction(Function &F) {
auto &LE = getAnalysis<GenXFuncLiveElements>();
bool Modified = false;
SmallVector<Instruction *, 8> ToErase;
for (auto &I : instructions(F)) {
LLVM_DEBUG(dbgs() << "Processing instruction " << I << "\n");
for (auto &U : I.operands()) {
auto *OldV = cast<Value>(U);
auto *NewV = trySimplify(OldV, LE.getLiveElements(&U));
if (NewV != OldV) {
LLVM_DEBUG(dbgs() << "Replacing " << *OldV << " with " << *NewV
<< "\n");
U.set(NewV);
NumSimplifiedUses++;
Modified = true;
}
}
if (LE.getLiveElements(&I).isAllDead()) {
ToErase.push_back(&I);
} else if (GenXIntrinsic::isWrRegion(&I) ||
vc::getAnyIntrinsicID(&I) == GenXIntrinsic::genx_wrpredregion) {
auto NewValueOp =
I.getOperand(GenXIntrinsic::GenXRegion::NewValueOperandNum);
if (isa<UndefValue>(NewValueOp)) {
LLVM_DEBUG(dbgs() << "Bypassing " << I << "\n");
auto OldValueOp = I.getOperand(GenXIntrinsic::GenXRegion::OldValueOperandNum);
I.replaceAllUsesWith(OldValueOp);
ToErase.push_back(&I);
}
} else if (auto *Sel = dyn_cast<SelectInst>(&I)) {
if (GotoJoin::isEMValue(Sel->getCondition()))
continue;
if (isa<UndefValue>(Sel->getTrueValue())) {
LLVM_DEBUG(dbgs() << "Bypassing " << I << "\n");
I.replaceAllUsesWith(Sel->getFalseValue());
ToErase.push_back(&I);
} else if (isa<UndefValue>(Sel->getFalseValue())) {
LLVM_DEBUG(dbgs() << "Bypassing " << I << "\n");
I.replaceAllUsesWith(Sel->getTrueValue());
ToErase.push_back(&I);
}
}
}
for (auto *Inst : ToErase) {
IGC_ASSERT(Inst->use_empty());
LLVM_DEBUG(dbgs() << "Deleting instruction " << *Inst << "\n");
Inst->eraseFromParent();
NumDeletedInsts++;
Modified = true;
}
return Modified;
}
Value *GenXDeadVectorRemoval::trySimplify(Value *V,
const LiveElements &LiveElems) {
if (LiveElems.isAllDead())
return UndefValue::get(V->getType());
if (auto *C = dyn_cast<Constant>(V))
return trySimplify(C, LiveElems);
return V;
}
Constant *GenXDeadVectorRemoval::trySimplify(Constant *C,
const LiveElements &LiveElems) {
if (!LiveElems.isAnyDead())
return C;
// Do not modify predicates. GenXConstant gets no befefit from it
if (C->getType()->getScalarType()->isIntegerTy(1))
return C;
if (auto CA = dyn_cast<ConstantAggregate>(C))
return trySimplify(CA, LiveElems);
if (auto CD = dyn_cast<ConstantData>(C))
return trySimplify(CD, LiveElems);
return C;
}
Constant *GenXDeadVectorRemoval::trySimplify(ConstantAggregate *CA,
const LiveElements &LiveElems) {
if (isa<ConstantVector>(CA)) {
IGC_ASSERT(LiveElems.size() == 1);
SmallVector<Constant *, 8> Elems;
for (auto &U : CA->operands())
Elems.push_back(LiveElems[0][U.getOperandNo()]
? cast<Constant>(U.get())
: UndefValue::get(U->getType()));
return ConstantVector::get(Elems);
}
if (isa<ConstantStruct>(CA)) {
IGC_ASSERT(LiveElems.size() == CA->getNumOperands());
SmallVector<Constant *, 8> Elems;
for (auto &U : CA->operands()) {
auto LE = LiveElems[U.getOperandNo()];
auto OpLiveElems = LiveElements(std::move(LE));
Elems.push_back(OpLiveElems.isAllDead()
? UndefValue::get(U->getType())
: trySimplify(cast<Constant>(U.get()), OpLiveElems));
}
return ConstantStruct::get(cast<StructType>(CA->getType()), Elems);
}
return CA;
}
Constant *GenXDeadVectorRemoval::trySimplify(ConstantData *CD,
const LiveElements &LiveElems) {
if (isa<UndefValue>(CD) || isa<ConstantAggregateZero>(CD))
return CD;
IGC_ASSERT(LiveElems.size() == 1);
if (auto CDS = dyn_cast<ConstantDataSequential>(CD))
return trySimplify(CDS, LiveElems);
IGC_ASSERT(LiveElems[0].size() == 1);
return LiveElems[0][0] ? CD : UndefValue::get(CD->getType());
}
Constant *GenXDeadVectorRemoval::trySimplify(ConstantDataSequential *CDS,
const LiveElements &LiveElems) {
if (auto CDV = dyn_cast<ConstantDataVector>(CDS)) {
SmallVector<Constant *, 8> Elems;
for (unsigned Idx = 0; Idx < CDV->getNumElements(); Idx++) {
auto Elem = CDV->getElementAsConstant(Idx);
Elems.push_back(LiveElems[0][Idx] ? Elem
: UndefValue::get(Elem->getType()));
}
return ConstantVector::get(Elems);
}
return CDS;
}
|