[llvm] [InstSimplify] Fold trunc to constant when all result bits are known (PR #212123)
Timur Golubovich via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 28 17:56:59 PDT 2026
https://github.com/timurgol007 updated https://github.com/llvm/llvm-project/pull/212123
>From 6403747773e4a8c45a9404c882cd6c49508db6d0 Mon Sep 17 00:00:00 2001
From: Timur Golubovich <timur.golubovich at intel.com>
Date: Sat, 18 Jul 2026 16:09:20 +0200
Subject: [PATCH 1/2] [InstSimplify] Fold trunc to constant when all result
bits are known
Teach simplifyCastInst to fold trunc to a constant via
computeKnownBits when all surviving bits are determined.
---
llvm/lib/Analysis/InstructionSimplify.cpp | 8 ++++
.../AddressSanitizer/asan-funclet.ll | 44 +++++++++----------
.../Transforms/InstCombine/zext-or-icmp.ll | 7 +--
.../X86/epilog-vectorization-inductions.ll | 6 +--
.../X86/SROA-after-final-loop-unrolling-2.ll | 3 +-
5 files changed, 33 insertions(+), 35 deletions(-)
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index ddaa44f43e631..5090d51e6dbfa 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5679,6 +5679,14 @@ static Value *simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
}
}
+ // trunc Op to Ty -> constant if all surviving bits are known
+ if (CastOpc == Instruction::Trunc && Ty->isIntegerTy()) {
+ unsigned DestWidth = Ty->getScalarSizeInBits();
+ KnownBits Known = computeKnownBits(Op, Q);
+ if ((Known.Zero | Known.One).countr_one() >= DestWidth)
+ return ConstantInt::get(Ty, Known.One.trunc(DestWidth));
+ }
+
return nullptr;
}
diff --git a/llvm/test/Instrumentation/AddressSanitizer/asan-funclet.ll b/llvm/test/Instrumentation/AddressSanitizer/asan-funclet.ll
index 2a7216ff70cda..ee3a08e85e4b6 100644
--- a/llvm/test/Instrumentation/AddressSanitizer/asan-funclet.ll
+++ b/llvm/test/Instrumentation/AddressSanitizer/asan-funclet.ll
@@ -23,7 +23,7 @@ declare i32 @dummyPersonality(...)
define void @FuncletPersonality(ptr %ptrParam) sanitize_address personality ptr @__CxxFrameHandler3 {
; CHECK-INLINE-LABEL: define void @FuncletPersonality(
-; CHECK-INLINE-SAME: ptr [[PTRPARAM:%.*]]) #[[ATTR2:[0-9]+]] personality ptr @__CxxFrameHandler3 {
+; CHECK-INLINE-SAME: ptr [[PTRPARAM:%.*]]) #[[ATTR3:[0-9]+]] personality ptr @__CxxFrameHandler3 {
; CHECK-INLINE-NEXT: entry:
; CHECK-INLINE-NEXT: [[TMP0:%.*]] = alloca i64, align 32
; CHECK-INLINE-NEXT: store i64 0, ptr [[TMP0]], align 8
@@ -146,13 +146,12 @@ define void @FuncletPersonality(ptr %ptrParam) sanitize_address personality ptr
; CHECK-INLINE-NEXT: br i1 [[TMP84]], label [[TMP85:%.*]], label [[TMP90:%.*]], !prof [[PROF1]]
; CHECK-INLINE: 79:
; CHECK-INLINE-NEXT: [[TMP86:%.*]] = and i64 [[TMP77]], 7
-; CHECK-INLINE-NEXT: [[TMP87:%.*]] = trunc i64 [[TMP86]] to i8
-; CHECK-INLINE-NEXT: [[TMP88:%.*]] = icmp sge i8 [[TMP87]], [[TMP83]]
+; CHECK-INLINE-NEXT: [[TMP88:%.*]] = icmp sge i8 0, [[TMP83]]
; CHECK-INLINE-NEXT: br i1 [[TMP88]], label [[TMP89:%.*]], label [[TMP90]]
-; CHECK-INLINE: 83:
+; CHECK-INLINE: 82:
; CHECK-INLINE-NEXT: call void @__asan_report_store1(i64 [[TMP77]]) #[[ATTR7]]
; CHECK-INLINE-NEXT: unreachable
-; CHECK-INLINE: 84:
+; CHECK-INLINE: 83:
; CHECK-INLINE-NEXT: store volatile i8 0, ptr [[TMP79]], align 1
; CHECK-INLINE-NEXT: invoke void @MayThrowFunc()
; CHECK-INLINE-NEXT: to label [[INVOKE_CONT:%.*]] unwind label [[EHCLEANUP:%.*]]
@@ -164,15 +163,15 @@ define void @FuncletPersonality(ptr %ptrParam) sanitize_address personality ptr
; CHECK-INLINE-NEXT: store i64 1172321806, ptr [[TMP23]], align 8
; CHECK-INLINE-NEXT: [[TMP93:%.*]] = icmp ne i64 [[TMP7]], 0
; CHECK-INLINE-NEXT: br i1 [[TMP93]], label [[TMP94:%.*]], label [[TMP95:%.*]]
-; CHECK-INLINE: 88:
+; CHECK-INLINE: 87:
; CHECK-INLINE-NEXT: call void @__asan_stack_free_8(i64 [[TMP7]], i64 8544)
; CHECK-INLINE-NEXT: br label [[TMP97:%.*]]
-; CHECK-INLINE: 89:
+; CHECK-INLINE: 88:
; CHECK-INLINE-NEXT: call void @__asan_set_shadow_00(i64 [[TMP29]], i64 4)
; CHECK-INLINE-NEXT: [[TMP96:%.*]] = add i64 [[TMP29]], 1028
; CHECK-INLINE-NEXT: call void @__asan_set_shadow_00(i64 [[TMP96]], i64 40)
; CHECK-INLINE-NEXT: br label [[TMP97]]
-; CHECK-INLINE: 91:
+; CHECK-INLINE: 90:
; CHECK-INLINE-NEXT: ret void
; CHECK-INLINE: ehcleanup:
; CHECK-INLINE-NEXT: [[TMP98:%.*]] = cleanuppad within none []
@@ -183,15 +182,14 @@ define void @FuncletPersonality(ptr %ptrParam) sanitize_address personality ptr
; CHECK-INLINE-NEXT: [[TMP102:%.*]] = load i8, ptr [[TMP101]], align 1
; CHECK-INLINE-NEXT: [[TMP103:%.*]] = icmp ne i8 [[TMP102]], 0
; CHECK-INLINE-NEXT: br i1 [[TMP103]], label [[TMP104:%.*]], label [[TMP109:%.*]], !prof [[PROF1]]
-; CHECK-INLINE: 98:
+; CHECK-INLINE: 97:
; CHECK-INLINE-NEXT: [[TMP105:%.*]] = and i64 [[TMP54]], 7
-; CHECK-INLINE-NEXT: [[TMP106:%.*]] = trunc i64 [[TMP105]] to i8
-; CHECK-INLINE-NEXT: [[TMP107:%.*]] = icmp sge i8 [[TMP106]], [[TMP102]]
+; CHECK-INLINE-NEXT: [[TMP107:%.*]] = icmp sge i8 0, [[TMP102]]
; CHECK-INLINE-NEXT: br i1 [[TMP107]], label [[TMP108:%.*]], label [[TMP109]]
-; CHECK-INLINE: 102:
+; CHECK-INLINE: 100:
; CHECK-INLINE-NEXT: call void @__asan_report_store1(i64 [[TMP54]]) #[[ATTR7]] [ "funclet"(token [[TMP98]]) ]
; CHECK-INLINE-NEXT: unreachable
-; CHECK-INLINE: 103:
+; CHECK-INLINE: 101:
; CHECK-INLINE-NEXT: store volatile i8 0, ptr [[TMP56]], align 1
; CHECK-INLINE-NEXT: call void @__asan_poison_stack_memory(i64 [[TMP54]], i64 4) [ "funclet"(token [[TMP98]]) ]
; CHECK-INLINE-NEXT: call void @DeInit(ptr [[TMP14]]) [ "funclet"(token [[TMP98]]) ]
@@ -214,27 +212,27 @@ define void @FuncletPersonality(ptr %ptrParam) sanitize_address personality ptr
; CHECK-INLINE-NEXT: [[TMP120:%.*]] = load i8, ptr [[TMP119]], align 1
; CHECK-INLINE-NEXT: [[TMP121:%.*]] = icmp ne i8 [[TMP120]], 0
; CHECK-INLINE-NEXT: br i1 [[TMP121]], label [[TMP122:%.*]], label [[TMP127:%.*]], !prof [[PROF1]]
-; CHECK-INLINE: 118:
+; CHECK-INLINE: 116:
; CHECK-INLINE-NEXT: [[TMP123:%.*]] = and i64 [[TMP116]], 7
; CHECK-INLINE-NEXT: [[TMP124:%.*]] = trunc i64 [[TMP123]] to i8
; CHECK-INLINE-NEXT: [[TMP125:%.*]] = icmp sge i8 [[TMP124]], [[TMP120]]
; CHECK-INLINE-NEXT: br i1 [[TMP125]], label [[TMP126:%.*]], label [[TMP127]]
-; CHECK-INLINE: 122:
+; CHECK-INLINE: 120:
; CHECK-INLINE-NEXT: call void @__asan_report_store_n(i64 [[TMP116]], i64 8) #[[ATTR7]] [ "funclet"(token [[TMP98]]) ]
; CHECK-INLINE-NEXT: unreachable
-; CHECK-INLINE: 123:
+; CHECK-INLINE: 121:
; CHECK-INLINE-NEXT: [[TMP128:%.*]] = lshr i64 [[TMP114]], 3
; CHECK-INLINE-NEXT: [[TMP129:%.*]] = add i64 [[TMP128]], [[TMP1]]
; CHECK-INLINE-NEXT: [[TMP130:%.*]] = inttoptr i64 [[TMP129]] to ptr
; CHECK-INLINE-NEXT: [[TMP131:%.*]] = load i8, ptr [[TMP130]], align 1
; CHECK-INLINE-NEXT: [[TMP132:%.*]] = icmp ne i8 [[TMP131]], 0
; CHECK-INLINE-NEXT: br i1 [[TMP132]], label [[TMP133:%.*]], label [[EHEXIT:%.*]], !prof [[PROF1]]
-; CHECK-INLINE: 129:
+; CHECK-INLINE: 127:
; CHECK-INLINE-NEXT: [[TMP134:%.*]] = and i64 [[TMP114]], 7
; CHECK-INLINE-NEXT: [[TMP135:%.*]] = trunc i64 [[TMP134]] to i8
; CHECK-INLINE-NEXT: [[TMP136:%.*]] = icmp sge i8 [[TMP135]], [[TMP131]]
; CHECK-INLINE-NEXT: br i1 [[TMP136]], label [[TMP137:%.*]], label [[EHEXIT]]
-; CHECK-INLINE: 133:
+; CHECK-INLINE: 131:
; CHECK-INLINE-NEXT: call void @__asan_report_store_n(i64 [[TMP114]], i64 8) #[[ATTR7]] [ "funclet"(token [[TMP98]]) ]
; CHECK-INLINE-NEXT: unreachable
; CHECK-INLINE: ehexit:
@@ -247,19 +245,19 @@ define void @FuncletPersonality(ptr %ptrParam) sanitize_address personality ptr
; CHECK-INLINE-NEXT: store i64 1172321806, ptr [[TMP23]], align 8
; CHECK-INLINE-NEXT: [[TMP142:%.*]] = icmp ne i64 [[TMP7]], 0
; CHECK-INLINE-NEXT: br i1 [[TMP142]], label [[TMP143:%.*]], label [[TMP144:%.*]]
-; CHECK-INLINE: 139:
+; CHECK-INLINE: 137:
; CHECK-INLINE-NEXT: call void @__asan_stack_free_8(i64 [[TMP7]], i64 8544) [ "funclet"(token [[TMP98]]) ]
; CHECK-INLINE-NEXT: br label [[TMP146:%.*]]
-; CHECK-INLINE: 140:
+; CHECK-INLINE: 138:
; CHECK-INLINE-NEXT: call void @__asan_set_shadow_00(i64 [[TMP29]], i64 4) [ "funclet"(token [[TMP98]]) ]
; CHECK-INLINE-NEXT: [[TMP145:%.*]] = add i64 [[TMP29]], 1028
; CHECK-INLINE-NEXT: call void @__asan_set_shadow_00(i64 [[TMP145]], i64 40) [ "funclet"(token [[TMP98]]) ]
; CHECK-INLINE-NEXT: br label [[TMP146]]
-; CHECK-INLINE: 142:
+; CHECK-INLINE: 140:
; CHECK-INLINE-NEXT: cleanupret from [[TMP98]] unwind to caller
;
; CHECK-OUTLINE-LABEL: define void @FuncletPersonality(
-; CHECK-OUTLINE-SAME: ptr [[PTRPARAM:%.*]]) #[[ATTR2:[0-9]+]] personality ptr @__CxxFrameHandler3 {
+; CHECK-OUTLINE-SAME: ptr [[PTRPARAM:%.*]]) #[[ATTR3:[0-9]+]] personality ptr @__CxxFrameHandler3 {
; CHECK-OUTLINE-NEXT: entry:
; CHECK-OUTLINE-NEXT: [[TMP0:%.*]] = alloca i64, align 32
; CHECK-OUTLINE-NEXT: store i64 0, ptr [[TMP0]], align 8
@@ -483,7 +481,7 @@ nopredecessor:
; Non-Windows personality, ensure no funclet gets attached to asan runtime call.
define void @OtherPersonality(ptr %ptrParam) sanitize_address personality ptr @dummyPersonality {
; CHECK-LABEL: define void @OtherPersonality(
-; CHECK-SAME: ptr [[PTRPARAM:%.*]]) #[[ATTR2:[0-9]+]] personality ptr @dummyPersonality {
+; CHECK-SAME: ptr [[PTRPARAM:%.*]]) #[[ATTR3:[0-9]+]] personality ptr @dummyPersonality {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[TMP0:%.*]] = load i64, ptr @__asan_shadow_memory_dynamic_address, align 8
; CHECK-NEXT: [[ASAN_LOCAL_STACK_BASE:%.*]] = alloca i64, align 8
diff --git a/llvm/test/Transforms/InstCombine/zext-or-icmp.ll b/llvm/test/Transforms/InstCombine/zext-or-icmp.ll
index feb4be9e37050..fc77acfb0b35b 100644
--- a/llvm/test/Transforms/InstCombine/zext-or-icmp.ll
+++ b/llvm/test/Transforms/InstCombine/zext-or-icmp.ll
@@ -179,12 +179,9 @@ define i8 @PR49475_infloop(i32 %t0, i16 %insert, i64 %e, i8 %i162) "instcombine-
; CHECK-NEXT: [[SUB17:%.*]] = sub i64 [[CONV16]], [[E:%.*]]
; CHECK-NEXT: [[SEXT:%.*]] = shl i64 [[SUB17]], 32
; CHECK-NEXT: [[CONV18:%.*]] = ashr exact i64 [[SEXT]], 32
-; CHECK-NEXT: [[CMP:%.*]] = icmp sge i64 [[XOR]], [[CONV18]]
-; CHECK-NEXT: [[TRUNC44:%.*]] = zext i1 [[CMP]] to i8
-; CHECK-NEXT: [[INC:%.*]] = add i8 [[I162]], [[TRUNC44]]
-; CHECK-NEXT: [[TOBOOL23_NOT:%.*]] = xor i1 [[CMP]], true
+; CHECK-NEXT: [[TOBOOL23_NOT:%.*]] = icmp slt i64 [[XOR]], [[CONV18]]
; CHECK-NEXT: call void @llvm.assume(i1 [[TOBOOL23_NOT]])
-; CHECK-NEXT: ret i8 [[INC]]
+; CHECK-NEXT: ret i8 [[I162]]
;
%b = icmp eq i32 %t0, 0
%b2 = icmp eq i16 %insert, 0
diff --git a/llvm/test/Transforms/LoopVectorize/X86/epilog-vectorization-inductions.ll b/llvm/test/Transforms/LoopVectorize/X86/epilog-vectorization-inductions.ll
index 4f6c9a01786a0..2abba6ff12637 100644
--- a/llvm/test/Transforms/LoopVectorize/X86/epilog-vectorization-inductions.ll
+++ b/llvm/test/Transforms/LoopVectorize/X86/epilog-vectorization-inductions.ll
@@ -265,14 +265,10 @@ define i8 @multiple_inductions_start_at_0() {
; CHECK-NEXT: br i1 false, label [[VEC_EPILOG_SCALAR_PH]], label [[VEC_EPILOG_PH]], !prof [[PROF11:![0-9]+]]
; CHECK: vec.epilog.ph:
; CHECK-NEXT: [[VEC_EPILOG_RESUME_VAL:%.*]] = phi i32 [ 1024, [[VEC_EPILOG_ITER_CHECK]] ], [ 0, [[VECTOR_MAIN_LOOP_ITER_CHECK]] ]
-; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i32 [ 0, [[VEC_EPILOG_ITER_CHECK]] ], [ 0, [[VECTOR_MAIN_LOOP_ITER_CHECK]] ]
-; CHECK-NEXT: [[TMP2:%.*]] = trunc i32 [[BC_RESUME_VAL]] to i8
-; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i8> poison, i8 [[TMP2]], i64 0
-; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i8> [[BROADCAST_SPLATINSERT]], <4 x i8> poison, <4 x i32> zeroinitializer
; CHECK-NEXT: br label [[VEC_EPILOG_VECTOR_BODY:%.*]]
; CHECK: vec.epilog.vector.body:
; CHECK-NEXT: [[INDEX1:%.*]] = phi i32 [ [[VEC_EPILOG_RESUME_VAL]], [[VEC_EPILOG_PH]] ], [ [[INDEX_NEXT3:%.*]], [[VEC_EPILOG_VECTOR_BODY]] ]
-; CHECK-NEXT: [[VEC_IND2:%.*]] = phi <4 x i8> [ [[BROADCAST_SPLAT]], [[VEC_EPILOG_PH]] ], [ [[VEC_IND2]], [[VEC_EPILOG_VECTOR_BODY]] ]
+; CHECK-NEXT: [[VEC_IND2:%.*]] = phi <4 x i8> [ zeroinitializer, [[VEC_EPILOG_PH]] ], [ [[VEC_IND2]], [[VEC_EPILOG_VECTOR_BODY]] ]
; CHECK-NEXT: [[INDEX_NEXT3]] = add nuw i32 [[INDEX1]], 4
; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[INDEX_NEXT3]], 1052
; CHECK-NEXT: br i1 [[TMP3]], label [[VEC_EPILOG_MIDDLE_BLOCK:%.*]], label [[VEC_EPILOG_VECTOR_BODY]], !llvm.loop [[LOOP12:![0-9]+]]
diff --git a/llvm/test/Transforms/PhaseOrdering/X86/SROA-after-final-loop-unrolling-2.ll b/llvm/test/Transforms/PhaseOrdering/X86/SROA-after-final-loop-unrolling-2.ll
index 51ea03f465c7e..30565aaddc5b7 100644
--- a/llvm/test/Transforms/PhaseOrdering/X86/SROA-after-final-loop-unrolling-2.ll
+++ b/llvm/test/Transforms/PhaseOrdering/X86/SROA-after-final-loop-unrolling-2.ll
@@ -171,7 +171,6 @@ attributes #3 = { nounwind }
; CHECK: [[CHAR_TBAA5]] = !{[[META6:![0-9]+]], [[META6]], i64 0}
; CHECK: [[META6]] = !{!"omnipotent char", [[META7:![0-9]+]], i64 0}
; CHECK: [[META7]] = !{!"Simple C++ TBAA"}
-; CHECK: [[LOOP8]] = distinct !{[[LOOP8]], [[META9:![0-9]+]], [[META10:![0-9]+]]}
+; CHECK: [[LOOP8]] = distinct !{[[LOOP8]], [[META9:![0-9]+]]}
; CHECK: [[META9]] = !{!"llvm.loop.mustprogress"}
-; CHECK: [[META10]] = !{!"llvm.loop.isvectorized", i32 1}
;.
>From 8c384714b4954e412797e404fae5d1370580e525 Mon Sep 17 00:00:00 2001
From: Timur Golubovich <timur.golubovich at intel.com>
Date: Mon, 27 Jul 2026 12:38:35 +0200
Subject: [PATCH 2/2] finished idea
---
.../InstCombine/InstCombineCasts.cpp | 12 +++-
.../InstCombine/trunc-multiuse-phi.ll | 65 +++++++++++++++++++
2 files changed, 75 insertions(+), 2 deletions(-)
create mode 100644 llvm/test/Transforms/InstCombine/trunc-multiuse-phi.ll
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 2db6396b9d661..1a9879882125e 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -270,9 +270,17 @@ Instruction *InstCombinerImpl::commonCastTransforms(CastInst &CI) {
// Don't do this if it would create a PHI node with an illegal type from a
// legal type.
if (!Src->getType()->isIntegerTy() || !CI.getType()->isIntegerTy() ||
- shouldChangeType(CI.getSrcTy(), CI.getType()))
- if (Instruction *NV = foldOpIntoPhi(CI, PN))
+ shouldChangeType(CI.getSrcTy(), CI.getType())) {
+ bool AllConst =
+ !PN->hasOneUse() && all_of(PN->blocks(), [&](BasicBlock *InBB) {
+ Value *V = simplifyCastInst(
+ CI.getOpcode(), PN->getIncomingValueForBlock(InBB),
+ CI.getType(), SQ.getWithInstruction(InBB->getTerminator()));
+ return V && isa<Constant>(V);
+ });
+ if (Instruction *NV = foldOpIntoPhi(CI, PN, AllConst))
return NV;
+ }
}
// Canonicalize a unary shuffle after the cast if neither operation changes
diff --git a/llvm/test/Transforms/InstCombine/trunc-multiuse-phi.ll b/llvm/test/Transforms/InstCombine/trunc-multiuse-phi.ll
new file mode 100644
index 0000000000000..dd3021e39f6c8
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/trunc-multiuse-phi.ll
@@ -0,0 +1,65 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+; Reduced from libc++ basic_string short-string optimization.
+; The low bit of the header word distinguishes heap-allocated (bit0=1) from
+; inline (bit0=0). The trunc to i1 checks whether to deallocate, and the phi
+; also feeds the capacity mask (and i64 %hdr, -2). Because the phi has multiple
+; uses, InstCombine does not push the trunc through it, so it misses that
+; the trunc result is simply !%is.short (the branch condition).
+
+declare void @_ZdlPvm(ptr, i64)
+declare noalias nonnull ptr @_Znwm(i64)
+
+define void @trunc_multiuse_phi(i64 %len, ptr %buf, ptr %src) {
+; CHECK-LABEL: @trunc_multiuse_phi(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[IS_SHORT:%.*]] = icmp ugt i64 [[LEN:%.*]], 22
+; CHECK-NEXT: br i1 [[IS_SHORT]], label [[SHORT:%.*]], label [[LONG:%.*]]
+; CHECK: short:
+; CHECK-NEXT: [[HDR_SHORT:%.*]] = shl nuw nsw i64 [[LEN]], 1
+; CHECK-NEXT: br label [[MERGE:%.*]]
+; CHECK: long:
+; CHECK-NEXT: [[CAP_RAW:%.*]] = and i64 [[LEN]], -16
+; CHECK-NEXT: [[CAP:%.*]] = add nuw i64 [[CAP_RAW]], 16
+; CHECK-NEXT: [[HEAP:%.*]] = call noalias ptr @_Znwm(i64 [[CAP]])
+; CHECK-NEXT: br label [[MERGE]]
+; CHECK: merge:
+; CHECK-NEXT: [[CAP_MASK:%.*]] = phi i64 [ [[HDR_SHORT]], [[LONG]] ], [ [[CAP]], [[SHORT]] ]
+; CHECK-NEXT: [[DATA:%.*]] = phi ptr [ [[BUF:%.*]], [[LONG]] ], [ [[HEAP]], [[SHORT]] ]
+; CHECK-NEXT: br i1 [[IS_SHORT]], label [[DO_FREE:%.*]], label [[DONE:%.*]]
+; CHECK: do.free:
+; CHECK-NEXT: call void @_ZdlPvm(ptr [[DATA]], i64 [[CAP_MASK]])
+; CHECK-NEXT: br label [[DONE]]
+; CHECK: done:
+; CHECK-NEXT: ret void
+;
+entry:
+ %is.short = icmp ult i64 %len, 23
+ br i1 %is.short, label %short, label %long
+
+short:
+ %hdr.short = shl nuw nsw i64 %len, 1
+ br label %merge
+
+long:
+ %cap.raw = and i64 %len, -16
+ %cap = add nuw i64 %cap.raw, 16
+ %heap = call noalias ptr @_Znwm(i64 %cap)
+ %hdr.long = or disjoint i64 %cap, 1
+ br label %merge
+
+merge:
+ %hdr = phi i64 [ %hdr.short, %short ], [ %hdr.long, %long ]
+ %data = phi ptr [ %buf, %short ], [ %heap, %long ]
+ %is.heap = trunc i64 %hdr to i1
+ br i1 %is.heap, label %do.free, label %done
+
+do.free:
+ %cap.mask = and i64 %hdr, -2
+ call void @_ZdlPvm(ptr %data, i64 %cap.mask)
+ br label %done
+
+done:
+ ret void
+}
More information about the llvm-commits
mailing list