Skip to content

Commit 0f117f9

Browse files
committed
[CodeGenPrepare] Replace deleted sext instr with the promoted value.
1 parent 6477b41 commit 0f117f9

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2634,6 +2634,15 @@ struct ExtAddrMode : public TargetLowering::AddrMode {
26342634
void print(raw_ostream &OS) const;
26352635
void dump() const;
26362636

2637+
void replaceWith(Value *From, Value *To) {
2638+
if (BaseReg == From)
2639+
BaseReg = To;
2640+
if (ScaledReg == From)
2641+
ScaledReg = To;
2642+
if (OriginalValue == From)
2643+
OriginalValue = To;
2644+
}
2645+
26372646
FieldName compare(const ExtAddrMode &other) {
26382647
// First check that the types are the same on each field, as differing types
26392648
// is something we can't cope with later on.
@@ -4933,6 +4942,9 @@ bool AddressingModeMatcher::matchOperationAddr(User *AddrInst, unsigned Opcode,
49334942
TPT.rollback(LastKnownGood);
49344943
return false;
49354944
}
4945+
4946+
// SExt has been deleted. Make sure it is not referenced by the AddrMode.
4947+
AddrMode.replaceWith(Ext, PromotedOperand);
49364948
return true;
49374949
}
49384950
}

llvm/test/CodeGen/X86/codegen-prepare-addrmode-sext.ll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,3 +507,25 @@ define i8 @oneArgPromotionBlockSExtZExt(i1 %arg1, ptr %base) {
507507
%res = load i8, ptr %arrayidx
508508
ret i8 %res
509509
}
510+
511+
; Check that we replace the deleted sext with the promoted value.
512+
; CHECK-LABEL: define void @pr70938(
513+
; CHECK-SAME: ptr [[F:%.*]]) {
514+
; CHECK-NEXT: entry:
515+
; CHECK-NEXT: [[ADD:%.*]] = add nsw i64 0, 0
516+
; CHECK-NEXT: [[SUNKADDR:%.*]] = mul i64 [[ADD]], 2
517+
; CHECK-NEXT: [[SUNKADDR1:%.*]] = getelementptr i8, ptr [[F]], i64 [[SUNKADDR]]
518+
; CHECK-NEXT: store i8 0, ptr [[SUNKADDR1]], align 1
519+
; CHECK-NEXT: ret void
520+
define void @pr70938(ptr %f) {
521+
entry:
522+
%add = add nsw i32 0, 0
523+
%idxprom3 = sext i32 %add to i64
524+
%arrayidx4 = getelementptr [2 x [1 x [2 x i8]]], ptr %f, i64 0, i64 %idxprom3
525+
%arrayidx8 = getelementptr [2 x i8], ptr %arrayidx4, i64 0, i64 %idxprom3
526+
br label %if.end
527+
528+
if.end: ; preds = %entry
529+
store i8 0, ptr %arrayidx8, align 1
530+
ret void
531+
}

0 commit comments

Comments
 (0)