Skip to content

Commit 736f2dc

Browse files
authored
Merge pull request #8345 from francisvm/eng/PR-123275318
[RISCV] Hoist immediate addresses from loads/stores (llvm#83644)
2 parents d85445d + 5b4d656 commit 736f2dc

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ InstructionCost RISCVTTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx,
120120
// split up large offsets in GEP into better parts than ConstantHoisting
121121
// can.
122122
return TTI::TCC_Free;
123+
case Instruction::Store:
124+
// If the address is a constant, use the materialization cost.
125+
if (Idx == 1)
126+
return getIntImmCost(Imm, Ty, CostKind);
127+
return TTI::TCC_Free;
128+
case Instruction::Load:
129+
// If the address is a constant, use the materialization cost.
130+
return getIntImmCost(Imm, Ty, CostKind);
123131
case Instruction::And:
124132
// zext.h
125133
if (Imm == UINT64_C(0xffff) && ST->hasStdExtZbb())

llvm/test/Transforms/ConstantHoisting/RISCV/immediates.ll

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,61 @@ define i64 @test14(i64 %a) nounwind {
132132
ret i64 %2
133133
}
134134

135+
; Check that we hoist the absolute address of the stores to the entry block.
136+
define void @test17(ptr %s, i32 %size) nounwind {
137+
; CHECK-LABEL: test17
138+
; CHECK: %const = bitcast i32 -1073741792 to i32
139+
; CHECK: %0 = inttoptr i32 %const to ptr
140+
; CHECK: store i32 20, ptr %0
141+
; CHECK: %1 = inttoptr i32 %const to ptr
142+
; CHECK: store i32 10, ptr %1
143+
entry:
144+
%cond = icmp eq i32 %size, 0
145+
br i1 %cond, label %if.true, label %exit
146+
if.true:
147+
store i32 20, ptr inttoptr (i32 -1073741792 to ptr)
148+
br label %exit
149+
exit:
150+
store i32 10, ptr inttoptr (i32 -1073741792 to ptr)
151+
ret void
152+
}
153+
154+
; Check that we hoist the absolute address of the loads to the entry block.
155+
define i32 @test18(ptr %s, i32 %size) nounwind {
156+
; CHECK-LABEL: test18
157+
; CHECK: %const = bitcast i32 -1073741792 to i32
158+
; CHECK: %0 = inttoptr i32 %const to ptr
159+
; CHECK: %1 = load i32, ptr %0
160+
; CHECK: %2 = inttoptr i32 %const to ptr
161+
; CHECK: %3 = load i32, ptr %2
162+
entry:
163+
%cond = icmp eq i32 %size, 0
164+
br i1 %cond, label %if.true, label %if.false
165+
if.true:
166+
%0 = load i32, ptr inttoptr (i32 -1073741792 to ptr)
167+
br label %return
168+
if.false:
169+
%1 = load i32, ptr inttoptr (i32 -1073741792 to ptr)
170+
br label %return
171+
return:
172+
%val = phi i32 [%0, %if.true], [%1, %if.false]
173+
ret i32 %val
174+
}
175+
176+
177+
; For addresses between [0, 2048), we can use ld/sd xN, address(zero), so don't
178+
; hoist.
179+
define void @test19(ptr %s, i32 %size) nounwind {
180+
; CHECK-LABEL: test19
181+
; CHECK: store i32 20, ptr inttoptr (i32 2044 to ptr)
182+
; CHECK: store i32 10, ptr inttoptr (i32 2044 to ptr)
183+
entry:
184+
%cond = icmp eq i32 %size, 0
185+
br i1 %cond, label %if.true, label %exit
186+
if.true:
187+
store i32 20, ptr inttoptr (i32 2044 to ptr)
188+
br label %exit
189+
exit:
190+
store i32 10, ptr inttoptr (i32 2044 to ptr)
191+
ret void
192+
}

0 commit comments

Comments
 (0)