Skip to content

Commit

Permalink
[llvm][Mips] Bail on underaligned loads/stores in FastISel.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrp committed Aug 29, 2024
1 parent 2579b41 commit caa9db1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
28 changes: 20 additions & 8 deletions llvm/lib/Target/Mips/MipsFastISel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -881,38 +881,50 @@ bool MipsFastISel::selectLogicalOp(const Instruction *I) {
}

bool MipsFastISel::selectLoad(const Instruction *I) {
const LoadInst *LI = cast<LoadInst>(I);

// Atomic loads need special handling.
if (cast<LoadInst>(I)->isAtomic())
if (LI->isAtomic())
return false;

// Verify we have a legal type before going any further.
MVT VT;
if (!isLoadTypeLegal(I->getType(), VT))
if (!isLoadTypeLegal(LI->getType(), VT))
return false;

// Underaligned loads need special handling.
if (LI->getAlign() < VT.getFixedSizeInBits() / 8)
return false;

// See if we can handle this address.
Address Addr;
if (!computeAddress(I->getOperand(0), Addr))
if (!computeAddress(LI->getOperand(0), Addr))
return false;

unsigned ResultReg;
if (!emitLoad(VT, ResultReg, Addr))
return false;
updateValueMap(I, ResultReg);
updateValueMap(LI, ResultReg);
return true;
}

bool MipsFastISel::selectStore(const Instruction *I) {
Value *Op0 = I->getOperand(0);
const StoreInst *SI = cast<StoreInst>(I);

Value *Op0 = SI->getOperand(0);
unsigned SrcReg = 0;

// Atomic stores need special handling.
if (cast<StoreInst>(I)->isAtomic())
if (SI->isAtomic())
return false;

// Verify we have a legal type before going any further.
MVT VT;
if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
if (!isLoadTypeLegal(SI->getOperand(0)->getType(), VT))
return false;

// Underaligned stores need special handling.
if (SI->getAlign() < VT.getFixedSizeInBits() / 8)
return false;

// Get the value to be stored into a register.
Expand All @@ -922,7 +934,7 @@ bool MipsFastISel::selectStore(const Instruction *I) {

// See if we can handle this address.
Address Addr;
if (!computeAddress(I->getOperand(1), Addr))
if (!computeAddress(SI->getOperand(1), Addr))
return false;

if (!emitStore(VT, SrcReg, Addr))
Expand Down
23 changes: 23 additions & 0 deletions llvm/test/CodeGen/Mips/Fast-ISel/underaligned-load-store.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
; RUN: llc < %s -march mips -fast-isel -relocation-model pic | FileCheck %s -check-prefixes=MIPS

@var = external global i32, align 1

; FastISel should bail on the underaligned load and store.
define dso_local ccc i32 @__start() {
; MIPS-LABEL: __start:
; MIPS: # %bb.0:
; MIPS-NEXT: lui $2, %hi(_gp_disp)
; MIPS-NEXT: addiu $2, $2, %lo(_gp_disp)
; MIPS-NEXT: addu $1, $2, $25
; MIPS-NEXT: lw $1, %got(var)($1)
; MIPS-NEXT: lwl $2, 0($1)
; MIPS-NEXT: lwr $2, 3($1)
; MIPS-NEXT: addiu $3, $zero, 42
; MIPS-NEXT: swl $3, 0($1)
; MIPS-NEXT: jr $ra
; MIPS-NEXT: swr $3, 3($1)
%1 = load i32, ptr @var, align 1
store i32 42, ptr @var, align 1
ret i32 %1
}

0 comments on commit caa9db1

Please sign in to comment.