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
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2021 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#include "common/debug/Debug.hpp"
#include "common/debug/Dump.hpp"
#include "common/Stats.hpp"
#include "common/LLVMUtils.h"
#include "common/LLVMWarningsPush.hpp"
#include "llvm/IR/Dominators.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/CFG.h"
#include "llvm/ADT/PostOrderIterator.h"
#include "common/LLVMWarningsPop.hpp"
#include "Compiler/CodeGenPublic.h"
#include "Compiler/CISACodeGen/AddressArithmeticSinking.hpp"
#include "Compiler/CISACodeGen/helper.h"
#include "Compiler/CISACodeGen/ShaderCodeGen.hpp"
#include "Compiler/IGCPassSupport.h"
#include "Probe/Assertion.h"
#include <queue>
using namespace llvm;
using namespace IGC::Debug;
namespace IGC {
#define PASS_FLAG "igc-address-arith-sinking"
#define PASS_DESCRIPTION "IGC Address Arithmetic Sinking"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(AddressArithmeticSinking, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
IGC_INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
IGC_INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
IGC_INITIALIZE_PASS_END(AddressArithmeticSinking, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
// This pass aimed to reduce register pressure by moving address arithmetic closer to load/store
AddressArithmeticSinking::AddressArithmeticSinking(unsigned SinkingDepth) :
FunctionPass(ID), m_SinkingDepth(SinkingDepth)
{
initializeAddressArithmeticSinkingPass(*PassRegistry::getPassRegistry());
}
bool AddressArithmeticSinking::runOnFunction(Function& F)
{
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
bool changed = false;
for (po_iterator<DomTreeNode*> domIter = po_begin(DT->getRootNode()),
domEnd = po_end(DT->getRootNode()); domIter != domEnd; ++domIter) {
BasicBlock* BB = domIter->getBlock();
changed |= ProcessBB(BB);
}
return changed;
}
static bool isAddressArithmetic(Instruction* I)
{
if (isa<GetElementPtrInst>(I) ||
isa<ExtractElementInst>(I) ||
isa<InsertElementInst>(I) ||
isa<InsertValueInst>(I) ||
(isa<UnaryInstruction>(I) && !isa<LoadInst>(I)) ||
isa<BinaryOperator>(I))
return true;
return false;
}
static void findArithmeticForPtr(Instruction* I, unsigned depth, SmallPtrSetImpl<Instruction*>& AddressArithmetic)
{
std::queue<Instruction*> IQ;
IQ.push(I);
while (depth && !IQ.empty()) {
Instruction* CurI = IQ.front();
IQ.pop();
for (Use &U : CurI->operands()) {
auto UseI = dyn_cast<Instruction>(U.get());
if(UseI == nullptr)
continue;
if (!isAddressArithmetic(UseI))
continue;
if (UseI->getParent() != I->getParent())
continue;
AddressArithmetic.insert(UseI);
IQ.push(UseI);
}
depth--;
}
}
bool AddressArithmeticSinking::ProcessBB(BasicBlock* BB)
{
bool changed = false;
SmallPtrSet<Instruction*, 16> AddressArithmetic;
for (auto II = BB->rbegin(), IE = BB->rend(); II != IE; ) {
Instruction* I = &*II++;
if (isa<IntToPtrInst>(I)) {
findArithmeticForPtr(I, m_SinkingDepth, AddressArithmetic);
changed |= sink(I);
continue;
}
if (AddressArithmetic.find(I) != AddressArithmetic.end())
changed |= sink(I);
}
return changed;
}
BasicBlock* AddressArithmeticSinking::FindSinkTarget(Instruction* I)
{
BasicBlock* tgtBB = nullptr;
for (Value::user_iterator UI = I->user_begin(), UE = I->user_end(); UI != UE; ++UI) {
Instruction* useInst = cast<Instruction>(*UI);
BasicBlock* useBlock = useInst->getParent();
if (PHINode * PN = dyn_cast<PHINode>(useInst)) {
Use& U = UI.getUse();
unsigned num = PHINode::getIncomingValueNumForOperand(U.getOperandNo());
useBlock = PN->getIncomingBlock(num);
}
else {
if (useBlock == I->getParent())
return nullptr;
}
if (tgtBB != nullptr) {
tgtBB = DT->findNearestCommonDominator(tgtBB, useBlock);
if (tgtBB == nullptr)
return nullptr;
}
else {
tgtBB = useBlock;
}
}
return tgtBB;
}
static Instruction* findFirstUse(BasicBlock* BB, const SmallPtrSetImpl<Instruction*>& usesInBB)
{
for (auto II = BB->begin(), IE = BB->end(); II != IE; ++II) {
Instruction* Use = &(*II);
if (usesInBB.count(Use))
return Use;
}
return nullptr;
}
bool AddressArithmeticSinking::sink(Instruction* I)
{
BasicBlock* tgtBB = FindSinkTarget(I);
if (tgtBB == nullptr)
return false;
SmallPtrSet<Instruction*, 8> usesInBB;
for (Value::user_iterator UI = I->user_begin(), UE = I->user_end(); UI != UE; ++UI) {
Instruction* useInst = cast<Instruction>(*UI);
BasicBlock* useBlock = useInst->getParent();
if (useBlock == tgtBB)
usesInBB.insert(useInst);
}
if (usesInBB.empty())
I->moveBefore(tgtBB->getTerminator());
else if (usesInBB.size() == 1) {
Instruction* use = *(usesInBB.begin());
if (!isa<PHINode>(use))
I->moveBefore(use);
}
else {
Instruction* firstUse = findFirstUse(tgtBB, usesInBB);
IGC_ASSERT(firstUse);
if (!isa<PHINode>(firstUse))
I->moveBefore(firstUse);
}
return true;
}
char AddressArithmeticSinking::ID = 0;
} // namespace IGC
|