Skip to content

Commit 0091cc3

Browse files
committed
[ARM] GlobalISel: Constrain callee register on indirect calls
When lowering calls, we generate instructions with machine opcodes rather than generic ones. Therefore, we need to constrain the register classes of the operands. Also enable the machine verifier on the arm-irtranslator.ll test, since that would've caught this issue. Fixes (part of) PR32146. llvm-svn: 304712
1 parent 60a0ea1 commit 0091cc3

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

llvm/lib/Target/ARM/ARMCallLowering.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "llvm/CodeGen/Analysis.h"
2323
#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
24+
#include "llvm/CodeGen/GlobalISel/Utils.h"
2425
#include "llvm/CodeGen/MachineRegisterInfo.h"
2526

2627
using namespace llvm;
@@ -461,7 +462,8 @@ bool ARMCallLowering::lowerCall(MachineIRBuilder &MIRBuilder,
461462
MachineFunction &MF = MIRBuilder.getMF();
462463
const auto &TLI = *getTLI<ARMTargetLowering>();
463464
const auto &DL = MF.getDataLayout();
464-
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
465+
const auto &STI = MF.getSubtarget();
466+
const TargetRegisterInfo *TRI = STI.getRegisterInfo();
465467
MachineRegisterInfo &MRI = MF.getRegInfo();
466468

467469
if (MF.getSubtarget<ARMSubtarget>().genLongCalls())
@@ -473,6 +475,13 @@ bool ARMCallLowering::lowerCall(MachineIRBuilder &MIRBuilder,
473475
// registers, but don't insert it yet.
474476
auto MIB = MIRBuilder.buildInstrNoInsert(ARM::BLX).add(Callee).addRegMask(
475477
TRI->getCallPreservedMask(MF, CallConv));
478+
if (Callee.isReg()) {
479+
auto CalleeReg = Callee.getReg();
480+
if (CalleeReg && !TRI->isPhysicalRegister(CalleeReg))
481+
MIB->getOperand(0).setReg(constrainOperandRegClass(
482+
MF, *TRI, MRI, *STI.getInstrInfo(), *STI.getRegBankInfo(),
483+
*MIB.getInstr(), MIB->getDesc(), CalleeReg, 0));
484+
}
476485

477486
SmallVector<ArgInfo, 8> ArgInfos;
478487
for (auto Arg : OrigArgs) {

llvm/test/CodeGen/ARM/GlobalISel/arm-irtranslator.ll

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
; RUN: llc -mtriple arm-unknown -mattr=+vfp2 -global-isel -stop-after=irtranslator %s -o - | FileCheck %s -check-prefix=CHECK -check-prefix=LITTLE
2-
; RUN: llc -mtriple armeb-unknown -mattr=+vfp2 -global-isel -stop-after=irtranslator %s -o - | FileCheck %s -check-prefix=CHECK -check-prefix=BIG
1+
; RUN: llc -mtriple arm-unknown -mattr=+vfp2 -global-isel -stop-after=irtranslator -verify-machineinstrs %s -o - | FileCheck %s -check-prefix=CHECK -check-prefix=LITTLE
2+
; RUN: llc -mtriple armeb-unknown -mattr=+vfp2 -global-isel -stop-after=irtranslator -verify-machineinstrs %s -o - | FileCheck %s -check-prefix=CHECK -check-prefix=BIG
33

44
define void @test_void_return() {
55
; CHECK-LABEL: name: test_void_return
@@ -420,9 +420,11 @@ entry:
420420

421421
define arm_aapcscc void @test_indirect_call(void() *%fptr) {
422422
; CHECK-LABEL: name: test_indirect_call
423-
; CHECK: [[FPTR:%[0-9]+]](p0) = COPY %r0
423+
; CHECK: registers:
424+
; CHECK-NEXT: id: [[FPTR:[0-9]+]], class: gpr
425+
; CHECK: %[[FPTR]](p0) = COPY %r0
424426
; CHECK: ADJCALLSTACKDOWN 0, 0, 14, _, implicit-def %sp, implicit %sp
425-
; CHECK: BLX [[FPTR]](p0), csr_aapcs, implicit-def %lr, implicit %sp
427+
; CHECK: BLX %[[FPTR]](p0), csr_aapcs, implicit-def %lr, implicit %sp
426428
; CHECK: ADJCALLSTACKUP 0, 0, 14, _, implicit-def %sp, implicit %sp
427429
entry:
428430
notail call arm_aapcscc void %fptr()

0 commit comments

Comments
 (0)