Skip to content

Commit

Permalink
Revert "[IR] Mark add constant expressions as undesirable"
Browse files Browse the repository at this point in the history
This reverts commit f8a36d8.

I believe this is causing an assertion failure on the
sanitizer-x86_64-linux buildbot:

clang++: /b/sanitizer-x86_64-linux/build/llvm-project/llvm/include/llvm/Support/Casting.h:578: decltype(auto) llvm::cast(From *) [To = llvm::BinaryOperator, From = llvm::Value]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed.

  #10 0x000055bdd7e82408 canonicalizeLogicFirst(llvm::BinaryOperator&, llvm::IRBuilder<llvm::TargetFolder, llvm::IRBuilderCallbackInserter>&) /b/sanitizer-x86_64-linux/build/llvm-project/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp:2131:5
  #11 0x000055bdd7e80183 llvm::InstCombinerImpl::visitAnd(llvm::BinaryOperator&) /b/sanitizer-x86_64-linux/build/llvm-project/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp:2661:20

Likely the code is encountering a constant expression in a case it
didn't before.
  • Loading branch information
nikic committed Jul 20, 2023
1 parent 8ef0448 commit 9dc391e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 28 deletions.
12 changes: 4 additions & 8 deletions clang/test/CodeGen/catch-nullptr-and-nonzero-offset.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,7 @@ char *one_zero(void) {
char *one_one_OK(void) {
// CHECK: define{{.*}} ptr @one_one_OK()
// CHECK-NEXT: [[ENTRY:.*]]:
// CHECK-SANITIZE-C-NEXT: %[[AND:.*]] = and i1 icmp ne (ptr inttoptr (i64 1 to ptr), ptr null), icmp ne (i64 add (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr inttoptr (i64 1 to ptr), i64 1) to i64), i64 1), i64 1), i64 0), !nosanitize
// CHECK-SANITIZE-C-NEXT: br i1 %[[AND]], label %[[CONT:.*]], label %[[HANDLER_POINTER_OVERFLOW:[^,]+]],{{.*}} !nosanitize
// CHECK-SANITIZE-C-NEXT: br i1 and (i1 icmp ne (ptr inttoptr (i64 1 to ptr), ptr null), i1 icmp ne (i64 add (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr inttoptr (i64 1 to ptr), i64 1) to i64), i64 1), i64 1), i64 0)), label %[[CONT:.*]], label %[[HANDLER_POINTER_OVERFLOW:[^,]+]],{{.*}} !nosanitize
// CHECK-SANITIZE-CPP-NEXT: br i1 xor (i1 icmp eq (ptr inttoptr (i64 1 to ptr), ptr null), i1 icmp ne (i64 add (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr inttoptr (i64 1 to ptr), i64 1) to i64), i64 1), i64 1), i64 0)), label %[[CONT:.*]], label %[[HANDLER_POINTER_OVERFLOW:[^,]+]],{{.*}} !nosanitize
// CHECK-SANITIZE: [[HANDLER_POINTER_OVERFLOW]]:
// CHECK-SANITIZE-NORECOVER-NEXT: call void @__ubsan_handle_pointer_overflow_abort(ptr @[[LINE_1100]], i64 1, i64 add (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr inttoptr (i64 1 to ptr), i64 1) to i64), i64 1), i64 1))
Expand All @@ -322,8 +321,7 @@ char *one_one_OK(void) {
char *one_allones_BAD(void) {
// CHECK: define{{.*}} ptr @one_allones_BAD()
// CHECK-NEXT: [[ENTRY:.*]]:
// CHECK-SANITIZE-C-NEXT: %[[AND:.*]] = and i1 icmp ne (ptr inttoptr (i64 1 to ptr), ptr null), icmp ne (i64 add (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr inttoptr (i64 1 to ptr), i64 -1) to i64), i64 1), i64 1), i64 0), !nosanitize
// CHECK-SANITIZE-C-NEXT: br i1 %[[AND]], label %[[CONT:.*]], label %[[HANDLER_POINTER_OVERFLOW:[^,]+]],{{.*}} !nosanitize
// CHECK-SANITIZE-C-NEXT: br i1 and (i1 icmp ne (ptr inttoptr (i64 1 to ptr), ptr null), i1 icmp ne (i64 add (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr inttoptr (i64 1 to ptr), i64 -1) to i64), i64 1), i64 1), i64 0)), label %[[CONT:.*]], label %[[HANDLER_POINTER_OVERFLOW:[^,]+]],{{.*}} !nosanitize
// CHECK-SANITIZE-CPP-NEXT: br i1 xor (i1 icmp eq (ptr inttoptr (i64 1 to ptr), ptr null), i1 icmp ne (i64 add (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr inttoptr (i64 1 to ptr), i64 -1) to i64), i64 1), i64 1), i64 0)), label %[[CONT:.*]], label %[[HANDLER_POINTER_OVERFLOW:[^,]+]],{{.*}} !nosanitize
// CHECK-SANITIZE: [[HANDLER_POINTER_OVERFLOW]]:
// CHECK-SANITIZE-NORECOVER-NEXT: call void @__ubsan_handle_pointer_overflow_abort(ptr @[[LINE_1200]], i64 1, i64 add (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr inttoptr (i64 1 to ptr), i64 -1) to i64), i64 1), i64 1))
Expand Down Expand Up @@ -392,8 +390,7 @@ char *allones_zero_OK(void) {
char *allones_one_BAD(void) {
// CHECK: define{{.*}} ptr @allones_one_BAD()
// CHECK-NEXT: [[ENTRY:.*]]:
// CHECK-SANITIZE-C-NEXT: %[[AND:.*]] = and i1 icmp ne (ptr inttoptr (i64 -1 to ptr), ptr null), icmp ne (i64 add (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr inttoptr (i64 -1 to ptr), i64 1) to i64), i64 -1), i64 -1), i64 0), !nosanitize
// CHECK-SANITIZE-C-NEXT: br i1 %[[AND]], label %[[CONT:.*]], label %[[HANDLER_POINTER_OVERFLOW:[^,]+]],{{.*}} !nosanitize
// CHECK-SANITIZE-C-NEXT: br i1 and (i1 icmp ne (ptr inttoptr (i64 -1 to ptr), ptr null), i1 icmp ne (i64 add (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr inttoptr (i64 -1 to ptr), i64 1) to i64), i64 -1), i64 -1), i64 0)), label %[[CONT:.*]], label %[[HANDLER_POINTER_OVERFLOW:[^,]+]],{{.*}} !nosanitize
// CHECK-SANITIZE-CPP-NEXT: br i1 xor (i1 icmp eq (ptr inttoptr (i64 -1 to ptr), ptr null), i1 icmp ne (i64 add (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr inttoptr (i64 -1 to ptr), i64 1) to i64), i64 -1), i64 -1), i64 0)), label %[[CONT:.*]], label %[[HANDLER_POINTER_OVERFLOW:[^,]+]],{{.*}} !nosanitize
// CHECK-SANITIZE: [[HANDLER_POINTER_OVERFLOW]]:
// CHECK-SANITIZE-NORECOVER-NEXT: call void @__ubsan_handle_pointer_overflow_abort(ptr @[[LINE_1500]], i64 -1, i64 add (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr inttoptr (i64 -1 to ptr), i64 1) to i64), i64 -1), i64 -1))
Expand All @@ -411,8 +408,7 @@ char *allones_one_BAD(void) {
char *allones_allones_OK(void) {
// CHECK: define{{.*}} ptr @allones_allones_OK()
// CHECK-NEXT: [[ENTRY:.*]]:
// CHECK-SANITIZE-C-NEXT: %[[AND:.*]] = and i1 icmp ne (ptr inttoptr (i64 -1 to ptr), ptr null), icmp ne (i64 add (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr inttoptr (i64 -1 to ptr), i64 -1) to i64), i64 -1), i64 -1), i64 0), !nosanitize
// CHECK-SANITIZE-C-NEXT: br i1 %[[AND]], label %[[CONT:.*]], label %[[HANDLER_POINTER_OVERFLOW:[^,]+]],{{.*}} !nosanitize
// CHECK-SANITIZE-C-NEXT: br i1 and (i1 icmp ne (ptr inttoptr (i64 -1 to ptr), ptr null), i1 icmp ne (i64 add (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr inttoptr (i64 -1 to ptr), i64 -1) to i64), i64 -1), i64 -1), i64 0)), label %[[CONT:.*]], label %[[HANDLER_POINTER_OVERFLOW:[^,]+]],{{.*}} !nosanitize
// CHECK-SANITIZE-CPP-NEXT: br i1 xor (i1 icmp eq (ptr inttoptr (i64 -1 to ptr), ptr null), i1 icmp ne (i64 add (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr inttoptr (i64 -1 to ptr), i64 -1) to i64), i64 -1), i64 -1), i64 0)), label %[[CONT:.*]], label %[[HANDLER_POINTER_OVERFLOW:[^,]+]],{{.*}} !nosanitize
// CHECK-SANITIZE: [[HANDLER_POINTER_OVERFLOW]]:
// CHECK-SANITIZE-NORECOVER-NEXT: call void @__ubsan_handle_pointer_overflow_abort(ptr @[[LINE_1600]], i64 -1, i64 add (i64 sub (i64 ptrtoint (ptr getelementptr inbounds (i8, ptr inttoptr (i64 -1 to ptr), i64 -1) to i64), i64 -1), i64 -1))
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/IR/ConstantFold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,8 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
case Instruction::Add:
case Instruction::Sub:
return ConstantExpr::getXor(C1, C2);
case Instruction::Mul:
return ConstantExpr::getAnd(C1, C2);
case Instruction::Shl:
case Instruction::LShr:
case Instruction::AShr:
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/IR/Constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2288,14 +2288,14 @@ bool ConstantExpr::isDesirableBinOp(unsigned Opcode) {
case Instruction::FMul:
case Instruction::FDiv:
case Instruction::FRem:
case Instruction::And:
return false;
case Instruction::Add:
case Instruction::Sub:
case Instruction::Mul:
case Instruction::Shl:
case Instruction::LShr:
case Instruction::AShr:
case Instruction::And:
case Instruction::Or:
case Instruction::Xor:
return true;
Expand Down
28 changes: 16 additions & 12 deletions llvm/test/CodeGen/Hexagon/atomic-opaque-basic.ll
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,34 @@ define void @f1() #0 {
; CHECK: .cfi_startproc
; CHECK-NEXT: // %bb.0: // %entry
; CHECK-NEXT: {
; CHECK-NEXT: r3 = ##g0
; CHECK-NEXT: r1:0 = combine(#1,##255)
; CHECK-NEXT: r2 = ##g0
; CHECK-NEXT: r0 = #255
; CHECK-NEXT: }
; CHECK-NEXT: {
; CHECK-NEXT: r2 = and(r3,#3)
; CHECK-NEXT: r3 = and(r3,#-4)
; CHECK-NEXT: r1 = and(r2,#3)
; CHECK-NEXT: }
; CHECK-NEXT: {
; CHECK-NEXT: r2 = asl(r2,#3)
; CHECK-NEXT: r1 = asl(r1,#3)
; CHECK-NEXT: }
; CHECK-NEXT: {
; CHECK-NEXT: r4 = r1
; CHECK-NEXT: }
; CHECK-NEXT: {
; CHECK-NEXT: r4 = asl(r0,r2)
; CHECK-NEXT: r4 = insert(r2,#2,#3)
; CHECK-NEXT: r2 = and(r2,#-4)
; CHECK-NEXT: }
; CHECK-NEXT: {
; CHECK-NEXT: r4 = sub(#-1,r4)
; CHECK-NEXT: r3 = lsl(#1,r4)
; CHECK-NEXT: r4 = asl(r0,r4)
; CHECK-NEXT: }
; CHECK-NEXT: .p2align 4
; CHECK-NEXT: .LBB1_1: // %cmpxchg.start
; CHECK-NEXT: // =>This Inner Loop Header: Depth=1
; CHECK-NEXT: {
; CHECK-NEXT: r5 = memw_locked(r3)
; CHECK-NEXT: r5 = memw_locked(r2)
; CHECK-NEXT: }
; CHECK-NEXT: {
; CHECK-NEXT: r6 = lsr(r5,r2)
; CHECK-NEXT: r6 = lsr(r5,r1)
; CHECK-NEXT: }
; CHECK-NEXT: {
; CHECK-NEXT: p0 = !bitsclr(r6,r0)
Expand All @@ -95,13 +99,13 @@ define void @f1() #0 {
; CHECK-NEXT: .LBB1_2: // %cmpxchg.trystore
; CHECK-NEXT: // in Loop: Header=BB1_1 Depth=1
; CHECK-NEXT: {
; CHECK-NEXT: r5 = and(r5,r4)
; CHECK-NEXT: r6 = r3
; CHECK-NEXT: }
; CHECK-NEXT: {
; CHECK-NEXT: r5 |= asl(r1,r2)
; CHECK-NEXT: r6 |= and(r5,~r4)
; CHECK-NEXT: }
; CHECK-NEXT: {
; CHECK-NEXT: memw_locked(r3,p0) = r5
; CHECK-NEXT: memw_locked(r2,p0) = r6
; CHECK-NEXT: }
; CHECK-NEXT: {
; CHECK-NEXT: if (!p0) jump:nt .LBB1_1
Expand Down
6 changes: 2 additions & 4 deletions llvm/test/Transforms/InstCombine/bswap-fold.ll
Original file line number Diff line number Diff line change
Expand Up @@ -786,8 +786,7 @@ define i64 @bs_all_operand64_multiuse_both(i64 %a, i64 %b) #0 {

define void @bs_and_constexpr(ptr %out, i64 %a) {
; CHECK-LABEL: @bs_and_constexpr(
; CHECK-NEXT: [[EXP:%.*]] = and i64 ptrtoint (ptr @gp to i64), 4095
; CHECK-NEXT: [[RES:%.*]] = call i64 @llvm.bswap.i64(i64 [[EXP]])
; CHECK-NEXT: [[RES:%.*]] = call i64 @llvm.bswap.i64(i64 and (i64 ptrtoint (ptr @gp to i64), i64 4095))
; CHECK-NEXT: store i64 [[RES]], ptr [[OUT:%.*]], align 8
; CHECK-NEXT: ret void
;
Expand All @@ -801,8 +800,7 @@ define void @bs_and_constexpr(ptr %out, i64 %a) {

define void @bs_and_bs_constexpr(ptr %out, i64 %a) {
; CHECK-LABEL: @bs_and_bs_constexpr(
; CHECK-NEXT: [[TMP1:%.*]] = and i64 ptrtoint (ptr @gp to i64), -67835469387268096
; CHECK-NEXT: store i64 [[TMP1]], ptr [[OUT:%.*]], align 8
; CHECK-NEXT: store i64 and (i64 ptrtoint (ptr @gp to i64), i64 -67835469387268096), ptr [[OUT:%.*]], align 8
; CHECK-NEXT: ret void
;
%gpi = ptrtoint ptr @gp to i64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@B = global i1 sub (i1 icmp ult (ptr @X, ptr @Y), i1 icmp ult (ptr @X, ptr @Z)), align 2
; CHECK: @B = global i1 xor (i1 icmp ult (ptr @X, ptr @Y), i1 icmp ult (ptr @X, ptr @Z))
@C = global i1 mul (i1 icmp ult (ptr @X, ptr @Y), i1 icmp ult (ptr @X, ptr @Z))
; CHECK: @C = global i1 mul (i1 icmp ult (ptr @X, ptr @Y), i1 icmp ult (ptr @X, ptr @Z))
; CHECK: @C = global i1 and (i1 icmp ult (ptr @X, ptr @Y), i1 icmp ult (ptr @X, ptr @Z))

@H = global i1 icmp ule (ptr @X, ptr @Y)
; CHECK: @H = global i1 icmp ule (ptr @X, ptr @Y)
Expand Down
3 changes: 1 addition & 2 deletions llvm/test/Transforms/InstSimplify/compare.ll
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,7 @@ define i1 @or(i32 %x) {
define i1 @or_constexp(i32 %x) {
; CHECK-LABEL: @or_constexp(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[TMP0:%.*]] = and i32 ptrtoint (ptr @GV to i32), 32
; CHECK-NEXT: [[O:%.*]] = or i32 [[X:%.*]], [[TMP0]]
; CHECK-NEXT: [[O:%.*]] = or i32 [[X:%.*]], and (i32 ptrtoint (ptr @GV to i32), i32 32)
; CHECK-NEXT: [[C:%.*]] = icmp eq i32 [[O]], 0
; CHECK-NEXT: ret i1 [[C]]
;
Expand Down

0 comments on commit 9dc391e

Please sign in to comment.