Commit 05a47706 authored by Nikita Popov's avatar Nikita Popov
Browse files

[ConstantFold] Fix incorrect type assumptions

If a pointer isn't a constant expression, global or block address,
it's not guaranteed to be a null pointer. It can also be a no_cfi
or dso_local_equivalent constant.
parent 236197a0
Loading
Loading
Loading
Loading
+4 −10
Original line number Diff line number Diff line
@@ -1172,30 +1172,24 @@ static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2) {
  }

  if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
    // Now we know that the RHS is a BlockAddress or simple
    // constant (which, since the types must match, means that it is a
    // ConstantPointerNull).
    // Now we know that the RHS is a BlockAddress or simple constant.
    if (const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
      // Block address in another function can't equal this one, but block
      // addresses in the current function might be the same if blocks are
      // empty.
      if (BA2->getFunction() != BA->getFunction())
        return ICmpInst::ICMP_NE;
    } else {
      // Block addresses aren't null.
      assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
    } else if (isa<ConstantPointerNull>(V2)) {
      return ICmpInst::ICMP_NE;
    }
  } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
    // Now we know that the RHS is a GlobalValue, BlockAddress or simple
    // constant (which, since the types must match, means that it's a
    // ConstantPointerNull).
    // constant.
    if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
      return areGlobalsPotentiallyEqual(GV, GV2);
    } else if (isa<BlockAddress>(V2)) {
      return ICmpInst::ICMP_NE; // Globals never equal labels.
    } else {
      assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
    } else if (isa<ConstantPointerNull>(V2)) {
      // GlobalVals can never be null unless they have external weak linkage.
      // We don't try to evaluate aliases here.
      // NOTE: We should not be doing this constant folding if null pointer
+23 −0
Original line number Diff line number Diff line
@@ -275,3 +275,26 @@ define i1 @global_gep_ugt_global_gep_complex() {
  %cmp = icmp ugt ptr %gep3, @g
  ret i1 %cmp
}

declare void @func()

define i1 @global_no_cfi() {
; CHECK-LABEL: @global_no_cfi(
; CHECK-NEXT:    ret i1 icmp eq (ptr @func, ptr no_cfi @func)
;
  %cmp = icmp eq ptr @func, no_cfi @func
  ret i1 %cmp
}

define i1 @blockaddr_no_cfi() {
; CHECK-LABEL: @blockaddr_no_cfi(
; CHECK-NEXT:    br label [[BB:%.*]]
; CHECK:       bb:
; CHECK-NEXT:    ret i1 icmp eq (ptr blockaddress(@blockaddr_no_cfi, [[BB]]), ptr no_cfi @func)
;
  br label %bb

bb:
  %cmp = icmp eq ptr blockaddress(@blockaddr_no_cfi, %bb), no_cfi @func
  ret i1 %cmp
}