File: RemoveLoopDependency.cpp

package info (click to toggle)
intel-graphics-compiler2 2.20.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 107,552 kB
  • sloc: cpp: 807,012; lisp: 287,936; ansic: 16,397; python: 4,010; yacc: 2,588; lex: 1,666; pascal: 313; sh: 186; makefile: 37
file content (87 lines) | stat: -rw-r--r-- 2,648 bytes parent folder | download
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
/*========================== begin_copyright_notice ============================

Copyright (C) 2024 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

#include "RemoveLoopDependency.hpp"

#include "Compiler/IGCPassSupport.h"

#include "common/LLVMWarningsPush.hpp"
#include "llvmWrapper/IR/DerivedTypes.h"
#include <llvm/IR/Constants.h>
#include "common/LLVMWarningsPop.hpp"

#include <vector>

using namespace llvm;
using namespace IGC;

char RemoveLoopDependency::ID = 0;

#define PASS_FLAG "remove-loop-dependency"
#define PASS_DESCRIPTION "Removing of fantom loop dependency."
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(RemoveLoopDependency, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
IGC_INITIALIZE_PASS_END(RemoveLoopDependency, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)

RemoveLoopDependency::RemoveLoopDependency() : FunctionPass(ID) {
  initializeRemoveLoopDependencyPass(*PassRegistry::getPassRegistry());
}

bool RemoveLoopDependency::runOnFunction(Function &F) {
  bool changed = false;
  LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  for (auto *L : LI.getLoopsInPreorder())
    changed |= processLoop(L);
  return changed;
}

bool RemoveLoopDependency::processLoop(Loop *L) {
  bool changed = false;
  for (auto &PHI : L->getHeader()->phis()) {
    if (dyn_cast<IGCLLVM::FixedVectorType>(PHI.getType()))
      changed |= RemoveDependency(&PHI, L);
  }

  return changed;
}

bool RemoveLoopDependency::RemoveDependency(PHINode *PHI, Loop *L) {
  unsigned elemCount = cast<IGCLLVM::FixedVectorType>(PHI->getType())->getNumElements();
  std::vector<bool> overwritten(elemCount, false);
  Instruction *currentVector = PHI;

  for (unsigned i = 0; i < elemCount; ++i) {
    if (currentVector->getNumUses() != 1)
      return false;

    if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(currentVector->user_back())) {
      if (!L->contains(IEI))
        return false;

      if (ConstantInt *indexConst = dyn_cast<ConstantInt>(IEI->getOperand(2))) {
        uint64_t index = indexConst->getZExtValue();
        if (index >= elemCount || overwritten[index])
          return false;

        overwritten[index] = true;
        currentVector = IEI;
        continue;
      }
    }

    return false;
  }

  Instruction *firstAssignment = PHI->user_back();
  UndefValue *undefVector = UndefValue::get(firstAssignment->getOperand(0)->getType());
  firstAssignment->setOperand(0, undefVector);

  return true;
}