Why should I know this?

computeKnownBitsFromCmp 본문

LLVM-STUDY

computeKnownBitsFromCmp

die4taoam 2024. 5. 1. 18:26

다음 코드를 통해 computeKnownBitsFromCmp 에서 KnownBit를 추산하는 방식을 알아보자.

  unsigned BitWidth = Known.getBitWidth();
  auto m_V =
      m_CombineOr(m_Specific(V), m_PtrToIntSameSize(Q.DL, m_Specific(V)));

  Value *Y;
  const APInt *Mask, *C;
  uint64_t ShAmt;
  switch (Pred) {
  case ICmpInst::ICMP_EQ:
    // assume(V = C)
    if (match(LHS, m_V) && match(RHS, m_APInt(C))) {
      Known = Known.unionWith(KnownBits::makeConstant(*C));
      // assume(V & Mask = C)
    } else if (match(LHS, m_c_And(m_V, m_Value(Y))) &&
               match(RHS, m_APInt(C))) {
      LLVM_DEBUG(dbgs() << "and] C " << *C; V->dump(); LHS->dump();
                 RHS->dump(););
      // if (C->isAllOnes()) {
      //   Known = KnownBits::makeConstant(*C);
      // }
      // For one bits in Mask, we can propagate bits from C to V.
      Known.One |= *C;
      if (match(Y, m_APInt(Mask)))
        Known.Zero |= ~*C & *Mask;
      // assume(V | Mask = C)
    }

 

if (match(LHS, m_c_And(m_V, m_Value(Y))) && match(RHS, m_APInt(C))) {

-> V & Y == C 이면, V와 Y의 공통 비트는 C와 같다. 

      // For one bits in Mask, we can propagate bits from C to V.
      Known.One |= *C;

      -> 그러므로 KnownBits의 One 에는 C 와 or 연산을 해준다. => 공통비트 합산

 

      if (match(Y, m_APInt(Mask)))

      -> 만약 Y도 특정 상수인 경우, Y & Mask == C 라면, 보다 상세한 비트 정의가 가능하다. ~C 의 비트와 MASK 의 공통 비트는 반드시 V에 설정되서는 안된다. 그러므로 Known.Zero 에 ~C&MASK 비트를 합산
        Known.Zero |= ~*C & *Mask;

}

 

 

'LLVM-STUDY' 카테고리의 다른 글

computeKnownBitsFromContext  (0) 2024.05.01
computeKnownBitsFromICmpCond  (0) 2024.05.01
computeKnownBitsFromCond  (0) 2024.05.01
LLVM 원라인 패치 목록  (0) 2024.03.17
LLVM 최적화 패치 제출까지의 순서 정리  (0) 2024.03.03
Comments