Skip to content

Commit 950bdbc

Browse files
committed
[RISCV] Implement RISCVTTIImpl::shouldConsiderAddressTypePromotion for RISCV
1 parent 668559d commit 950bdbc

File tree

4 files changed

+47
-12
lines changed

4 files changed

+47
-12
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,6 +1507,7 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
15071507

15081508
// Disable strict node mutation.
15091509
IsStrictFPEnabled = true;
1510+
EnableExtLdPromotion = true;
15101511

15111512
// Let the subtarget decide if a predictable select is more expensive than the
15121513
// corresponding branch. This information is used in CGP/SelectOpt to decide

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,3 +1989,35 @@ bool RISCVTTIImpl::areInlineCompatible(const Function *Caller,
19891989
// target-features.
19901990
return (CallerBits & CalleeBits) == CalleeBits;
19911991
}
1992+
1993+
/// See if \p I should be considered for address type promotion. We check if \p
1994+
/// I is a sext with right type and used in memory accesses. If it used in a
1995+
/// "complex" getelementptr, we allow it to be promoted without finding other
1996+
/// sext instructions that sign extended the same initial value. A getelementptr
1997+
/// is considered as "complex" if it has more than 2 operands.
1998+
bool RISCVTTIImpl::shouldConsiderAddressTypePromotion(
1999+
const Instruction &I, bool &AllowPromotionWithoutCommonHeader) {
2000+
bool Considerable = false;
2001+
AllowPromotionWithoutCommonHeader = false;
2002+
if (!isa<SExtInst>(&I))
2003+
return false;
2004+
Type *ConsideredSExtType =
2005+
Type::getInt64Ty(I.getParent()->getParent()->getContext());
2006+
if (I.getType() != ConsideredSExtType)
2007+
return false;
2008+
// See if the sext is the one with the right type and used in at least one
2009+
// GetElementPtrInst.
2010+
for (const User *U : I.users()) {
2011+
if (const GetElementPtrInst *GEPInst = dyn_cast<GetElementPtrInst>(U)) {
2012+
Considerable = true;
2013+
// A getelementptr is considered as "complex" if it has more than 2
2014+
// operands. We will promote a SExt used in such complex GEP as we
2015+
// expect some computation to be merged if they are done on 64 bits.
2016+
if (GEPInst->getNumOperands() > 2) {
2017+
AllowPromotionWithoutCommonHeader = true;
2018+
break;
2019+
}
2020+
}
2021+
}
2022+
return Considerable;
2023+
}

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,9 @@ class RISCVTTIImpl : public BasicTTIImplBase<RISCVTTIImpl> {
397397
bool shouldFoldTerminatingConditionAfterLSR() const {
398398
return true;
399399
}
400-
400+
bool
401+
shouldConsiderAddressTypePromotion(const Instruction &I,
402+
bool &AllowPromotionWithoutCommonHeader);
401403
std::optional<unsigned> getMinPageSize() const { return 4096; }
402404
};
403405

llvm/test/CodeGen/RISCV/riscv-codegen-prepare-atp.ll

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ define void @promoteTwoOne(i32 %i, i32 %j, ptr %P1, ptr %P2 ) {
1111
; CHECK-LABEL: define void @promoteTwoOne(
1212
; CHECK-SAME: i32 [[I:%.*]], i32 [[J:%.*]], ptr [[P1:%.*]], ptr [[P2:%.*]]) {
1313
; CHECK-NEXT: entry:
14-
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[I]], [[J]]
15-
; CHECK-NEXT: [[S:%.*]] = sext i32 [[ADD]] to i64
14+
; CHECK-NEXT: [[S2:%.*]] = sext i32 [[I]] to i64
15+
; CHECK-NEXT: [[PROMOTED2:%.*]] = sext i32 [[J]] to i64
16+
; CHECK-NEXT: [[S:%.*]] = add nsw i64 [[S2]], [[PROMOTED2]]
1617
; CHECK-NEXT: [[ADDR1:%.*]] = getelementptr inbounds i64, ptr [[P1]], i64 [[S]]
1718
; CHECK-NEXT: store i64 [[S]], ptr [[ADDR1]], align 8
18-
; CHECK-NEXT: [[S2:%.*]] = sext i32 [[I]] to i64
1919
; CHECK-NEXT: [[ADDR2:%.*]] = getelementptr inbounds i64, ptr [[P2]], i64 [[S2]]
2020
; CHECK-NEXT: store i64 [[S2]], ptr [[ADDR2]], align 8
2121
; CHECK-NEXT: ret void
@@ -36,12 +36,13 @@ define void @promoteTwoTwo(i32 %i, i32 %j, i32 %k, ptr %P1, ptr %P2) {
3636
; CHECK-LABEL: define void @promoteTwoTwo(
3737
; CHECK-SAME: i32 [[I:%.*]], i32 [[J:%.*]], i32 [[K:%.*]], ptr [[P1:%.*]], ptr [[P2:%.*]]) {
3838
; CHECK-NEXT: entry:
39-
; CHECK-NEXT: [[ADD1:%.*]] = add nsw i32 [[J]], [[I]]
40-
; CHECK-NEXT: [[S:%.*]] = sext i32 [[ADD1]] to i64
39+
; CHECK-NEXT: [[PROMOTED3:%.*]] = sext i32 [[J]] to i64
40+
; CHECK-NEXT: [[PROMOTED4:%.*]] = sext i32 [[I]] to i64
41+
; CHECK-NEXT: [[S:%.*]] = add nsw i64 [[PROMOTED3]], [[PROMOTED4]]
4142
; CHECK-NEXT: [[ADDR1:%.*]] = getelementptr inbounds i64, ptr [[P1]], i64 [[S]]
4243
; CHECK-NEXT: store i64 [[S]], ptr [[ADDR1]], align 8
43-
; CHECK-NEXT: [[ADD2:%.*]] = add nsw i32 [[J]], [[K]]
44-
; CHECK-NEXT: [[S2:%.*]] = sext i32 [[ADD2]] to i64
44+
; CHECK-NEXT: [[PROMOTED2:%.*]] = sext i32 [[K]] to i64
45+
; CHECK-NEXT: [[S2:%.*]] = add nsw i64 [[PROMOTED3]], [[PROMOTED2]]
4546
; CHECK-NEXT: [[ADDR2:%.*]] = getelementptr inbounds i64, ptr [[P2]], i64 [[S2]]
4647
; CHECK-NEXT: store i64 [[S2]], ptr [[ADDR2]], align 8
4748
; CHECK-NEXT: ret void
@@ -62,11 +63,10 @@ define i64 @promoteGEPSunk(i1 %cond, ptr %base, i32 %i) {
6263
; CHECK-LABEL: define i64 @promoteGEPSunk(
6364
; CHECK-SAME: i1 [[COND:%.*]], ptr [[BASE:%.*]], i32 [[I:%.*]]) {
6465
; CHECK-NEXT: entry:
65-
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[I]], 1
66-
; CHECK-NEXT: [[S:%.*]] = sext i32 [[ADD]] to i64
66+
; CHECK-NEXT: [[PROMOTED1:%.*]] = sext i32 [[I]] to i64
67+
; CHECK-NEXT: [[S:%.*]] = add nsw i64 [[PROMOTED1]], 1
6768
; CHECK-NEXT: [[ADDR:%.*]] = getelementptr inbounds i64, ptr [[BASE]], i64 [[S]]
68-
; CHECK-NEXT: [[ADD2:%.*]] = add nsw i32 [[I]], 2
69-
; CHECK-NEXT: [[S2:%.*]] = sext i32 [[ADD2]] to i64
69+
; CHECK-NEXT: [[S2:%.*]] = add nsw i64 [[PROMOTED1]], 2
7070
; CHECK-NEXT: [[ADDR2:%.*]] = getelementptr inbounds i64, ptr [[BASE]], i64 [[S2]]
7171
; CHECK-NEXT: br i1 [[COND]], label [[IF_THEN:%.*]], label [[IF_THEN2:%.*]]
7272
; CHECK: if.then:

0 commit comments

Comments
 (0)