Skip to content

Commit c336625

Browse files
authored
[InstCombine] Call InstSimplify for cast instructions (llvm#162849)
InstCombine currently fails to call into InstSimplify for cast instructions. I noticed this because the transform from llvm#98649 can be triggered via `-passes=instsimplify` but not `-passes=instcombine`, which is not supposed to happen.
1 parent c46fec8 commit c336625

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ Instruction *InstCombinerImpl::commonCastTransforms(CastInst &CI) {
156156
Value *Src = CI.getOperand(0);
157157
Type *Ty = CI.getType();
158158

159-
if (auto *SrcC = dyn_cast<Constant>(Src))
160-
if (Constant *Res = ConstantFoldCastOperand(CI.getOpcode(), SrcC, Ty, DL))
161-
return replaceInstUsesWith(CI, Res);
159+
if (Value *Res =
160+
simplifyCastInst(CI.getOpcode(), Src, Ty, SQ.getWithInstruction(&CI)))
161+
return replaceInstUsesWith(CI, Res);
162162

163163
// Try to eliminate a cast of a cast.
164164
if (auto *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast

llvm/test/Transforms/InstCombine/cast-set-preserve-signed-dbg-val.ll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ define i16 @test5(i16 %A) !dbg !34 {
1111
call void @llvm.dbg.value(metadata i32 %C, metadata !37, metadata !DIExpression()), !dbg !41
1212

1313
; Preserve the dbg.value for the DCE'd 32-bit 'and'.
14-
;
15-
; The high 16 bits of the original 'and' require sign-extending the new 16-bit and:
1614
; CHECK-NEXT: #dbg_value(i16 [[and]], [[C:![0-9]+]],
17-
; CHECK-SAME: !DIExpression(DW_OP_LLVM_convert, 16, DW_ATE_signed, DW_OP_LLVM_convert, 32, DW_ATE_signed, DW_OP_stack_value)
15+
; CHECK-SAME: !DIExpression(DW_OP_LLVM_convert, 16, DW_ATE_unsigned, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_stack_value)
1816

1917
%D = trunc i32 %C to i16, !dbg !42
2018
call void @llvm.dbg.value(metadata i16 %D, metadata !38, metadata !DIExpression()), !dbg !42

llvm/test/Transforms/InstCombine/ptr-int-cast.ll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,14 @@ define <4 x ptr> @test7(<4 x i128> %arg) nounwind {
8686
%p1 = inttoptr <4 x i128> %arg to <4 x ptr>
8787
ret <4 x ptr> %p1
8888
}
89+
90+
define i64 @ptrtoint_gep_sub(ptr %ptr, i64 %end.addr) {
91+
; CHECK-LABEL: @ptrtoint_gep_sub(
92+
; CHECK-NEXT: ret i64 [[END_ADDR:%.*]]
93+
;
94+
%ptr.addr = ptrtoint ptr %ptr to i64
95+
%size = sub i64 %end.addr, %ptr.addr
96+
%end = getelementptr i8, ptr %ptr, i64 %size
97+
%end.addr2 = ptrtoint ptr %end to i64
98+
ret i64 %end.addr2
99+
}

0 commit comments

Comments
 (0)