Skip to content

Commit 17e2fc6

Browse files
committed
[CodeGenPrepare] Replace deleted sext instr with the promoted value.
1 parent 98d6dd3 commit 17e2fc6

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
@@ -3074,6 +3074,15 @@ struct ExtAddrMode : public TargetLowering::AddrMode {
30743074
void print(raw_ostream &OS) const;
30753075
void dump() const;
30763076

3077+
void replaceWith(Value *From, Value *To) {
3078+
if (BaseReg == From)
3079+
BaseReg = To;
3080+
if (ScaledReg == From)
3081+
ScaledReg = To;
3082+
if (OriginalValue == From)
3083+
OriginalValue = To;
3084+
}
3085+
30773086
FieldName compare(const ExtAddrMode &other) {
30783087
// First check that the types are the same on each field, as differing types
30793088
// is something we can't cope with later on.
@@ -5365,6 +5374,9 @@ bool AddressingModeMatcher::matchOperationAddr(User *AddrInst, unsigned Opcode,
53655374
TPT.rollback(LastKnownGood);
53665375
return false;
53675376
}
5377+
5378+
// SExt has been deleted. Make sure it is not referenced by the AddrMode.
5379+
AddrMode.replaceWith(Ext, PromotedOperand);
53685380
return true;
53695381
}
53705382
case Instruction::Call:

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,3 +620,25 @@ define i8 @oneArgPromotionBlockSExtZExt(i1 %arg1, ptr %base) {
620620
%res = load i8, ptr %arrayidx
621621
ret i8 %res
622622
}
623+
624+
; Check that we replace the deleted sext with the promoted value.
625+
; CHECK-LABEL: define void @pr70938(
626+
; CHECK-SAME: ptr [[F:%.*]]) {
627+
; CHECK-NEXT: entry:
628+
; CHECK-NEXT: [[ADD:%.*]] = add nsw i64 0, 0
629+
; CHECK-NEXT: [[SUNKADDR:%.*]] = mul i64 [[ADD]], 2
630+
; CHECK-NEXT: [[SUNKADDR1:%.*]] = getelementptr i8, ptr [[F]], i64 [[SUNKADDR]]
631+
; CHECK-NEXT: store i8 0, ptr [[SUNKADDR1]], align 1
632+
; CHECK-NEXT: ret void
633+
define void @pr70938(ptr %f) {
634+
entry:
635+
%add = add nsw i32 0, 0
636+
%idxprom3 = sext i32 %add to i64
637+
%arrayidx4 = getelementptr [2 x [1 x [2 x i8]]], ptr %f, i64 0, i64 %idxprom3
638+
%arrayidx8 = getelementptr [2 x i8], ptr %arrayidx4, i64 0, i64 %idxprom3
639+
br label %if.end
640+
641+
if.end: ; preds = %entry
642+
store i8 0, ptr %arrayidx8, align 1
643+
ret void
644+
}

0 commit comments

Comments
 (0)