rv64: Use JAL for known calls in range
7a78da402ab033d7dca5c470e6e76a5358a94085aac1385701cff9324db5af38
Emit one JAL instruction when a call target is known and its offset fits the J-type immediate. This saves one instruction and four bytes for each eligible call without a program layout pass. Forward and distant calls use the two-instruction relocation path. Assisted-by: Codex:gpt-6 Change-Id: I99d6235e257269e01812b47f06301f1e94ba5d48
1 parent
3ee3621a
lib/std/arch/rv64/emit.rad
+9 -3
| 353 | 353 | if long { |
|
| 354 | 354 | try emit(e, encode::nop()); |
|
| 355 | 355 | } |
|
| 356 | 356 | } |
|
| 357 | 357 | ||
| 358 | - | /// Record a function call needing later patching. |
|
| 359 | - | /// Emits placeholder instructions that will be patched later. |
|
| 360 | - | /// Uses two slots to support long-distance calls. |
|
| 358 | + | /// Emit a function call. Known targets in J-type range use one instruction. |
|
| 359 | + | /// Other calls use two placeholder instructions for later patching. |
|
| 361 | 360 | export fn recordCall(e: &mut Emitter, target: *[u8]) throws (super::Error) { |
|
| 362 | 361 | let index = e.code.len; |
|
| 362 | + | if dict::get(&e.labels.funcs, target) <> nil { |
|
| 363 | + | let offset = try branchOffsetToFunc(e, index, target); |
|
| 364 | + | if encode::isJumpImm(offset) { |
|
| 365 | + | try emit(e, encode::jal(super::RA, offset)); |
|
| 366 | + | return; |
|
| 367 | + | } |
|
| 368 | + | } |
|
| 363 | 369 | try recordCallAt(e, target, super::RA, super::SCRATCH1, index); |
|
| 364 | 370 | ||
| 365 | 371 | try emit(e, encode::nop()); // Placeholder for AUIPC. |
|
| 366 | 372 | try emit(e, encode::nop()); // Placeholder for JALR. |
|
| 367 | 373 | } |
lib/std/arch/rv64/tests.rad
+5 -2
| 265 | 265 | try expectEncoding(code[site], encode::addi(super::SP, super::SP, -16)); |
|
| 266 | 266 | try expectEncoding(code[site + 1], encode::addi(super::SCRATCH1, super::ZERO, 108)); |
|
| 267 | 267 | try expectEncoding(code[site + 2], encode::sd(super::SCRATCH1, super::SP, 0)); |
|
| 268 | 268 | try expectEncoding(code[site + 3], encode::addi(super::SCRATCH1, super::ZERO, 109)); |
|
| 269 | 269 | try expectEncoding(code[site + 4], encode::sd(super::SCRATCH1, super::SP, 8)); |
|
| 270 | - | // Eight argument register loads, the two-instruction call, then the release. |
|
| 271 | - | try expectEncoding(code[site + 5 + 8 + 2], encode::addi(super::SP, super::SP, 16)); |
|
| 270 | + | // Eight argument register loads, the one-instruction call, then the release. |
|
| 271 | + | let callSite = site + 5 + 8; |
|
| 272 | + | let offset = (calleeStart as i32 - callSite as i32) * super::INSTR_SIZE; |
|
| 273 | + | try expectEncoding(code[callSite], encode::jal(super::RA, offset)); |
|
| 274 | + | try expectEncoding(code[callSite + 1], encode::addi(super::SP, super::SP, 16)); |
|
| 272 | 275 | } |
|
| 273 | 276 | ||
| 274 | 277 | /// Narrow operations preserve the full value of a source that is still live. |
|
| 275 | 278 | @test unsafe fn testNarrowLiveSource() throws (testing::TestError) { |
|
| 276 | 279 | for typ in [il::Type::W8, il::Type::W16, il::Type::W32] { |