Skip to content

[RISCV] Hoist immediate addresses from loads/stores (#83644) #8345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ InstructionCost RISCVTTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx,
// split up large offsets in GEP into better parts than ConstantHoisting
// can.
return TTI::TCC_Free;
case Instruction::Store:
// If the address is a constant, use the materialization cost.
if (Idx == 1)
return getIntImmCost(Imm, Ty, CostKind);
return TTI::TCC_Free;
case Instruction::Load:
// If the address is a constant, use the materialization cost.
return getIntImmCost(Imm, Ty, CostKind);
case Instruction::And:
// zext.h
if (Imm == UINT64_C(0xffff) && ST->hasStdExtZbb())
Expand Down
58 changes: 58 additions & 0 deletions llvm/test/Transforms/ConstantHoisting/RISCV/immediates.ll
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,61 @@ define i64 @test14(i64 %a) nounwind {
ret i64 %2
}

; Check that we hoist the absolute address of the stores to the entry block.
define void @test17(ptr %s, i32 %size) nounwind {
; CHECK-LABEL: test17
; CHECK: %const = bitcast i32 -1073741792 to i32
; CHECK: %0 = inttoptr i32 %const to ptr
; CHECK: store i32 20, ptr %0
; CHECK: %1 = inttoptr i32 %const to ptr
; CHECK: store i32 10, ptr %1
entry:
%cond = icmp eq i32 %size, 0
br i1 %cond, label %if.true, label %exit
if.true:
store i32 20, ptr inttoptr (i32 -1073741792 to ptr)
br label %exit
exit:
store i32 10, ptr inttoptr (i32 -1073741792 to ptr)
ret void
}

; Check that we hoist the absolute address of the loads to the entry block.
define i32 @test18(ptr %s, i32 %size) nounwind {
; CHECK-LABEL: test18
; CHECK: %const = bitcast i32 -1073741792 to i32
; CHECK: %0 = inttoptr i32 %const to ptr
; CHECK: %1 = load i32, ptr %0
; CHECK: %2 = inttoptr i32 %const to ptr
; CHECK: %3 = load i32, ptr %2
entry:
%cond = icmp eq i32 %size, 0
br i1 %cond, label %if.true, label %if.false
if.true:
%0 = load i32, ptr inttoptr (i32 -1073741792 to ptr)
br label %return
if.false:
%1 = load i32, ptr inttoptr (i32 -1073741792 to ptr)
br label %return
return:
%val = phi i32 [%0, %if.true], [%1, %if.false]
ret i32 %val
}


; For addresses between [0, 2048), we can use ld/sd xN, address(zero), so don't
; hoist.
define void @test19(ptr %s, i32 %size) nounwind {
; CHECK-LABEL: test19
; CHECK: store i32 20, ptr inttoptr (i32 2044 to ptr)
; CHECK: store i32 10, ptr inttoptr (i32 2044 to ptr)
entry:
%cond = icmp eq i32 %size, 0
br i1 %cond, label %if.true, label %exit
if.true:
store i32 20, ptr inttoptr (i32 2044 to ptr)
br label %exit
exit:
store i32 10, ptr inttoptr (i32 2044 to ptr)
ret void
}