Skip to content

Commit

Permalink
[RISCV] LUI used for address computation should not isAsCheapAsAMove
Browse files Browse the repository at this point in the history
A LUI instruction with flag RISCVII::MO_HI is usually used in conjunction
with ADDI, and jointly complete address computation. To bind the cost
evaluation of address computation, the LUI should not be regarded as a cheap
 move separately, which is consistent with ADDI.

In this test case, it improves the unroll-loop code that the rematerialization
of array's base address miss MachineCSE with Heuristics #1 at isProfitableToCSE.

Reviewed By: asb, frasercrmck

Differential Revision: https://reviews.llvm.org/D118216
  • Loading branch information
Haocong.Lu authored and benshi001 committed Feb 12, 2022
1 parent 2e57156 commit 23a5073
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
2 changes: 2 additions & 0 deletions llvm/lib/Target/RISCV/RISCVInstrInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,8 @@ bool RISCVInstrInfo::isAsCheapAsAMove(const MachineInstr &MI) const {
return (MI.getOperand(1).isReg() &&
MI.getOperand(1).getReg() == RISCV::X0) ||
(MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0);
case RISCV::LUI:
return MI.getOperand(1).getTargetFlags() != RISCVII::MO_HI;
}
return MI.isAsCheapAsAMove();
}
Expand Down
20 changes: 6 additions & 14 deletions llvm/test/CodeGen/RISCV/unroll-loop-cse.ll
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,20 @@ define signext i32 @unroll_loop_cse() {
; CHECK-NEXT: bne a3, a4, .LBB0_6
; CHECK-NEXT: # %bb.1:
; CHECK-NEXT: addi a1, a1, %lo(x)
; CHECK-NEXT: lw a1, 4(a1)
; CHECK-NEXT: lw a3, 4(a1)
; CHECK-NEXT: addi a2, a2, %lo(check)
; CHECK-NEXT: lw a2, 4(a2)
; CHECK-NEXT: bne a1, a2, .LBB0_6
; CHECK-NEXT: lw a4, 4(a2)
; CHECK-NEXT: bne a3, a4, .LBB0_6
; CHECK-NEXT: # %bb.2:
; CHECK-NEXT: lui a1, %hi(x)
; CHECK-NEXT: addi a1, a1, %lo(x)
; CHECK-NEXT: lw a3, 8(a1)
; CHECK-NEXT: lui a2, %hi(check)
; CHECK-NEXT: addi a2, a2, %lo(check)
; CHECK-NEXT: lw a4, 8(a2)
; CHECK-NEXT: bne a3, a4, .LBB0_6
; CHECK-NEXT: # %bb.3:
; CHECK-NEXT: lw a1, 12(a1)
; CHECK-NEXT: lw a2, 12(a2)
; CHECK-NEXT: bne a1, a2, .LBB0_6
; CHECK-NEXT: lw a3, 12(a1)
; CHECK-NEXT: lw a4, 12(a2)
; CHECK-NEXT: bne a3, a4, .LBB0_6
; CHECK-NEXT: # %bb.4:
; CHECK-NEXT: lui a1, %hi(x)
; CHECK-NEXT: addi a1, a1, %lo(x)
; CHECK-NEXT: lw a3, 16(a1)
; CHECK-NEXT: lui a2, %hi(check)
; CHECK-NEXT: addi a2, a2, %lo(check)
; CHECK-NEXT: lw a4, 16(a2)
; CHECK-NEXT: bne a3, a4, .LBB0_6
; CHECK-NEXT: # %bb.5:
Expand Down

0 comments on commit 23a5073

Please sign in to comment.