Skip to content

Commit 3c6aa04

Browse files
authored
[CodeGenPrepare] Replace deleted ext instr with the promoted value. (llvm#71058)
This PR replaces the deleted ext with the promoted value in `AddrMode`. Fixes llvm#70938.
1 parent 69c2468 commit 3c6aa04

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3074,6 +3074,14 @@ struct ExtAddrMode : public TargetLowering::AddrMode {
30743074
void print(raw_ostream &OS) const;
30753075
void dump() const;
30763076

3077+
// Replace From in ExtAddrMode with To.
3078+
// E.g., SExt insts may be promoted and deleted. We should replace them with
3079+
// the promoted values.
3080+
void replaceWith(Value *From, Value *To) {
3081+
if (ScaledReg == From)
3082+
ScaledReg = To;
3083+
}
3084+
30773085
FieldName compare(const ExtAddrMode &other) {
30783086
// First check that the types are the same on each field, as differing types
30793087
// is something we can't cope with later on.
@@ -5365,6 +5373,9 @@ bool AddressingModeMatcher::matchOperationAddr(User *AddrInst, unsigned Opcode,
53655373
TPT.rollback(LastKnownGood);
53665374
return false;
53675375
}
5376+
5377+
// SExt has been deleted. Make sure it is not referenced by the AddrMode.
5378+
AddrMode.replaceWith(Ext, PromotedOperand);
53685379
return true;
53695380
}
53705381
case Instruction::Call:

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,3 +620,51 @@ 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+
define void @pr70938(ptr %f) {
626+
; CHECK-LABEL: define void @pr70938(
627+
; CHECK-SAME: ptr [[F:%.*]]) {
628+
; CHECK-NEXT: [[ENTRY:.*:]]
629+
; CHECK-NEXT: [[ADD:%.*]] = add nsw i64 0, 1
630+
; CHECK-NEXT: [[SUNKADDR:%.*]] = mul i64 [[ADD]], 2
631+
; CHECK-NEXT: [[SUNKADDR1:%.*]] = getelementptr i8, ptr [[F]], i64 [[SUNKADDR]]
632+
; CHECK-NEXT: [[SUNKADDR2:%.*]] = getelementptr i8, ptr [[SUNKADDR1]], i64 1
633+
; CHECK-NEXT: store i8 0, ptr [[SUNKADDR2]], align 1
634+
; CHECK-NEXT: ret void
635+
;
636+
entry:
637+
%add = add nsw i32 0, 1
638+
%idxprom3 = sext i32 %add to i64
639+
%arrayidx4 = getelementptr [2 x [1 x [2 x i8]]], ptr %f, i64 0, i64 %idxprom3
640+
%arrayidx8 = getelementptr [2 x i8], ptr %arrayidx4, i64 0, i64 %idxprom3
641+
br label %if.end
642+
643+
if.end: ; preds = %entry
644+
store i8 0, ptr %arrayidx8, align 1
645+
ret void
646+
}
647+
648+
define void @pr119429() {
649+
; CHECK-LABEL: define void @pr119429() {
650+
; CHECK-NEXT: [[ENTRY:.*:]]
651+
; CHECK-NEXT: [[AND:%.*]] = and i64 0, 0
652+
; CHECK-NEXT: [[SUNKADDR:%.*]] = inttoptr i64 [[AND]] to ptr
653+
; CHECK-NEXT: [[SUNKADDR1:%.*]] = mul i64 [[AND]], 2
654+
; CHECK-NEXT: [[SUNKADDR2:%.*]] = getelementptr i8, ptr [[SUNKADDR]], i64 [[SUNKADDR1]]
655+
; CHECK-NEXT: store i64 0, ptr [[SUNKADDR2]], align 8
656+
; CHECK-NEXT: ret void
657+
;
658+
entry:
659+
%and = and i32 0, 0
660+
%conv1 = zext i32 %and to i64
661+
%sub = add i64 %conv1, 0
662+
br label %if.end
663+
664+
if.end:
665+
%mul = shl i64 %sub, 1
666+
%add = add i64 %mul, %conv1
667+
%ptr = inttoptr i64 %add to ptr
668+
store i64 0, ptr %ptr, align 8
669+
ret void
670+
}

0 commit comments

Comments
 (0)