2006-05-14 22:18:28 +00:00
|
|
|
//===- ARMRegisterInfo.cpp - ARM Register Information -----------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
2006-05-14 22:18:28 +00:00
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-02-10 18:45:23 +00:00
|
|
|
// This file contains the ARM implementation of the TargetRegisterInfo class.
|
2006-05-14 22:18:28 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ARM.h"
|
2007-01-19 07:51:42 +00:00
|
|
|
#include "ARMAddressingModes.h"
|
|
|
|
#include "ARMInstrInfo.h"
|
|
|
|
#include "ARMMachineFunctionInfo.h"
|
2006-05-14 22:18:28 +00:00
|
|
|
#include "ARMRegisterInfo.h"
|
2007-01-19 07:51:42 +00:00
|
|
|
#include "ARMSubtarget.h"
|
2007-01-30 23:01:46 +00:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
2006-05-14 22:18:28 +00:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2007-01-30 23:01:46 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2006-05-14 22:18:28 +00:00
|
|
|
#include "llvm/CodeGen/MachineLocation.h"
|
2007-12-31 04:13:23 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2007-02-27 21:12:35 +00:00
|
|
|
#include "llvm/CodeGen/RegisterScavenging.h"
|
2006-10-31 13:03:26 +00:00
|
|
|
#include "llvm/Target/TargetFrameInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2006-10-26 13:31:26 +00:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2007-02-19 21:49:54 +00:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
2007-01-19 07:51:42 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2006-05-14 22:18:28 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-06-27 12:16:40 +00:00
|
|
|
ARMRegisterInfo::ARMRegisterInfo(const TargetInstrInfo &tii,
|
|
|
|
const ARMSubtarget &sti)
|
|
|
|
: ARMBaseRegisterInfo(tii, sti) {
|
|
|
|
}
|
|
|
|
|
2007-10-05 01:32:41 +00:00
|
|
|
static inline
|
|
|
|
const MachineInstrBuilder &AddDefaultPred(const MachineInstrBuilder &MIB) {
|
|
|
|
return MIB.addImm((int64_t)ARMCC::AL).addReg(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline
|
|
|
|
const MachineInstrBuilder &AddDefaultCC(const MachineInstrBuilder &MIB) {
|
|
|
|
return MIB.addReg(0);
|
|
|
|
}
|
|
|
|
|
2007-03-20 08:09:38 +00:00
|
|
|
/// emitLoadConstPool - Emits a load from constpool to materialize the
|
|
|
|
/// specified immediate.
|
2008-03-31 20:40:39 +00:00
|
|
|
void ARMRegisterInfo::emitLoadConstPool(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator &MBBI,
|
2009-06-27 12:59:03 +00:00
|
|
|
const TargetInstrInfo *TII, DebugLoc dl,
|
2008-03-31 20:40:39 +00:00
|
|
|
unsigned DestReg, int Val,
|
2009-06-27 12:59:03 +00:00
|
|
|
ARMCC::CondCodes Pred,
|
|
|
|
unsigned PredReg) const {
|
2007-03-20 08:09:38 +00:00
|
|
|
MachineFunction &MF = *MBB.getParent();
|
|
|
|
MachineConstantPool *ConstantPool = MF.getConstantPool();
|
|
|
|
Constant *C = ConstantInt::get(Type::Int32Ty, Val);
|
2009-03-13 07:51:59 +00:00
|
|
|
unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
|
2009-06-27 12:16:40 +00:00
|
|
|
|
|
|
|
BuildMI(MBB, MBBI, dl, TII->get(ARM::LDRcp), DestReg)
|
|
|
|
.addConstantPoolIndex(Idx)
|
|
|
|
.addReg(0).addImm(0).addImm(Pred).addReg(PredReg);
|
2007-03-20 08:09:38 +00:00
|
|
|
}
|
|
|
|
|
2007-02-28 00:59:19 +00:00
|
|
|
bool
|
|
|
|
ARMRegisterInfo::requiresRegisterScavenging(const MachineFunction &MF) const {
|
2009-06-27 12:16:40 +00:00
|
|
|
return true;
|
2007-02-28 00:21:58 +00:00
|
|
|
}
|
|
|
|
|
2007-05-01 00:52:08 +00:00
|
|
|
// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
|
|
|
|
// not required, we reserve argument space for call sites in the function
|
|
|
|
// immediately on entry to the current function. This eliminates the need for
|
|
|
|
// add/sub sp brackets around call sites. Returns true if the call frame is
|
|
|
|
// included as part of the stack frame.
|
|
|
|
bool ARMRegisterInfo::hasReservedCallFrame(MachineFunction &MF) const {
|
|
|
|
const MachineFrameInfo *FFI = MF.getFrameInfo();
|
|
|
|
unsigned CFSize = FFI->getMaxCallFrameSize();
|
|
|
|
// It's not always a good idea to include the call frame as part of the
|
|
|
|
// stack frame. ARM (especially Thumb) has small immediate offset to
|
|
|
|
// address the stack frame. So a large call frame can cause poor codegen
|
|
|
|
// and may even makes it impossible to scavenge a register.
|
2009-06-27 12:16:40 +00:00
|
|
|
if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
|
|
|
|
return false;
|
|
|
|
|
2007-07-19 00:42:58 +00:00
|
|
|
return !MF.getFrameInfo()->hasVarSizedObjects();
|
2007-05-01 00:52:08 +00:00
|
|
|
}
|
|
|
|
|
2007-01-30 23:01:46 +00:00
|
|
|
/// emitARMRegPlusImmediate - Emits a series of instructions to materialize
|
2007-01-19 07:51:42 +00:00
|
|
|
/// a destreg = basereg + immediate in ARM code.
|
|
|
|
static
|
|
|
|
void emitARMRegPlusImmediate(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator &MBBI,
|
2007-07-05 07:11:03 +00:00
|
|
|
unsigned DestReg, unsigned BaseReg, int NumBytes,
|
|
|
|
ARMCC::CondCodes Pred, unsigned PredReg,
|
2009-02-13 02:26:21 +00:00
|
|
|
const TargetInstrInfo &TII,
|
|
|
|
DebugLoc dl) {
|
2007-01-19 07:51:42 +00:00
|
|
|
bool isSub = NumBytes < 0;
|
|
|
|
if (isSub) NumBytes = -NumBytes;
|
|
|
|
|
|
|
|
while (NumBytes) {
|
|
|
|
unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
|
|
|
|
unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
|
|
|
|
assert(ThisVal && "Didn't extract field correctly");
|
2009-06-27 12:16:40 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// We will handle these bits from offset, clear them.
|
|
|
|
NumBytes &= ~ThisVal;
|
2009-06-27 12:16:40 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// Get the properly encoded SOImmVal field.
|
|
|
|
int SOImmVal = ARM_AM::getSOImmVal(ThisVal);
|
|
|
|
assert(SOImmVal != -1 && "Bit extraction didn't work?");
|
2009-06-27 12:16:40 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// Build the new ADD / SUB.
|
2009-02-13 02:26:21 +00:00
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(isSub ? ARM::SUBri : ARM::ADDri), DestReg)
|
2009-05-13 21:33:08 +00:00
|
|
|
.addReg(BaseReg, RegState::Kill).addImm(SOImmVal)
|
2007-07-10 18:08:01 +00:00
|
|
|
.addImm((unsigned)Pred).addReg(PredReg).addReg(0);
|
2007-01-19 07:51:42 +00:00
|
|
|
BaseReg = DestReg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-27 12:59:03 +00:00
|
|
|
static void
|
2009-06-27 12:16:40 +00:00
|
|
|
emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
|
2009-06-27 12:59:03 +00:00
|
|
|
const TargetInstrInfo &TII, DebugLoc dl,
|
|
|
|
int NumBytes,
|
|
|
|
ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
|
2009-06-27 12:16:40 +00:00
|
|
|
emitARMRegPlusImmediate(MBB, MBBI, ARM::SP, ARM::SP, NumBytes,
|
|
|
|
Pred, PredReg, TII, dl);
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
|
|
|
|
2006-05-14 22:18:28 +00:00
|
|
|
void ARMRegisterInfo::
|
|
|
|
eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I) const {
|
2007-05-01 00:52:08 +00:00
|
|
|
if (!hasReservedCallFrame(MF)) {
|
2007-01-19 07:51:42 +00:00
|
|
|
// If we have alloca, convert as follows:
|
|
|
|
// ADJCALLSTACKDOWN -> sub, sp, sp, amount
|
|
|
|
// ADJCALLSTACKUP -> add, sp, sp, amount
|
2006-10-31 13:03:26 +00:00
|
|
|
MachineInstr *Old = I;
|
2009-02-13 02:26:21 +00:00
|
|
|
DebugLoc dl = Old->getDebugLoc();
|
2007-12-30 20:49:49 +00:00
|
|
|
unsigned Amount = Old->getOperand(0).getImm();
|
2006-10-31 13:03:26 +00:00
|
|
|
if (Amount != 0) {
|
2007-01-19 07:51:42 +00:00
|
|
|
// We need to keep the stack aligned properly. To do this, we round the
|
|
|
|
// amount of space needed for the outgoing arguments up to the next
|
|
|
|
// alignment boundary.
|
2006-10-31 13:03:26 +00:00
|
|
|
unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
|
|
|
|
Amount = (Amount+Align-1)/Align*Align;
|
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// Replace the pseudo instruction with a new instruction...
|
2007-05-15 01:29:07 +00:00
|
|
|
unsigned Opc = Old->getOpcode();
|
2009-06-27 12:16:40 +00:00
|
|
|
ARMCC::CondCodes Pred = (ARMCC::CondCodes)Old->getOperand(1).getImm();
|
2007-05-15 01:29:07 +00:00
|
|
|
if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
|
2007-11-13 00:44:25 +00:00
|
|
|
// Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
|
2009-06-27 12:16:40 +00:00
|
|
|
unsigned PredReg = Old->getOperand(2).getReg();
|
2009-06-27 12:59:03 +00:00
|
|
|
emitSPUpdate(MBB, I, TII, dl, -Amount, Pred, PredReg);
|
2006-10-31 13:03:26 +00:00
|
|
|
} else {
|
2007-11-13 00:44:25 +00:00
|
|
|
// Note: PredReg is operand 3 for ADJCALLSTACKUP.
|
2009-06-27 12:16:40 +00:00
|
|
|
unsigned PredReg = Old->getOperand(3).getReg();
|
2007-05-15 01:29:07 +00:00
|
|
|
assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
|
2009-06-27 12:59:03 +00:00
|
|
|
emitSPUpdate(MBB, I, TII, dl, Amount, Pred, PredReg);
|
2006-10-31 13:03:26 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-26 13:31:26 +00:00
|
|
|
}
|
2006-05-14 22:18:28 +00:00
|
|
|
MBB.erase(I);
|
|
|
|
}
|
|
|
|
|
2007-03-01 08:57:52 +00:00
|
|
|
/// findScratchRegister - Find a 'free' ARM register. If register scavenger
|
|
|
|
/// is not being used, R12 is available. Otherwise, try for a call-clobbered
|
|
|
|
/// register first and then a spilled callee-saved register if that fails.
|
|
|
|
static
|
|
|
|
unsigned findScratchRegister(RegScavenger *RS, const TargetRegisterClass *RC,
|
|
|
|
ARMFunctionInfo *AFI) {
|
|
|
|
unsigned Reg = RS ? RS->FindUnusedReg(RC, true) : (unsigned) ARM::R12;
|
2009-04-07 20:34:09 +00:00
|
|
|
assert (!AFI->isThumbFunction());
|
2007-03-01 08:57:52 +00:00
|
|
|
if (Reg == 0)
|
|
|
|
// Try a already spilled CS register.
|
|
|
|
Reg = RS->FindUnusedReg(RC, AFI->getSpilledCSRegisters());
|
|
|
|
|
|
|
|
return Reg;
|
|
|
|
}
|
|
|
|
|
2007-02-28 00:21:58 +00:00
|
|
|
void ARMRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
|
2007-05-01 09:13:03 +00:00
|
|
|
int SPAdj, RegScavenger *RS) const{
|
2007-01-19 07:51:42 +00:00
|
|
|
unsigned i = 0;
|
2006-06-18 00:08:07 +00:00
|
|
|
MachineInstr &MI = *II;
|
|
|
|
MachineBasicBlock &MBB = *MI.getParent();
|
|
|
|
MachineFunction &MF = *MBB.getParent();
|
2007-01-19 07:51:42 +00:00
|
|
|
ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
|
2009-02-13 02:26:21 +00:00
|
|
|
DebugLoc dl = MI.getDebugLoc();
|
2006-06-18 00:08:07 +00:00
|
|
|
|
2008-10-03 15:45:36 +00:00
|
|
|
while (!MI.getOperand(i).isFI()) {
|
2007-01-19 07:51:42 +00:00
|
|
|
++i;
|
|
|
|
assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
|
|
|
|
}
|
2009-06-27 12:16:40 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
unsigned FrameReg = ARM::SP;
|
2007-12-30 23:10:15 +00:00
|
|
|
int FrameIndex = MI.getOperand(i).getIndex();
|
2009-06-27 12:16:40 +00:00
|
|
|
int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
|
2007-05-01 09:13:03 +00:00
|
|
|
MF.getFrameInfo()->getStackSize() + SPAdj;
|
2006-06-18 00:08:07 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
if (AFI->isGPRCalleeSavedArea1Frame(FrameIndex))
|
|
|
|
Offset -= AFI->getGPRCalleeSavedArea1Offset();
|
|
|
|
else if (AFI->isGPRCalleeSavedArea2Frame(FrameIndex))
|
|
|
|
Offset -= AFI->getGPRCalleeSavedArea2Offset();
|
|
|
|
else if (AFI->isDPRCalleeSavedAreaFrame(FrameIndex))
|
|
|
|
Offset -= AFI->getDPRCalleeSavedAreaOffset();
|
2007-01-20 02:09:25 +00:00
|
|
|
else if (hasFP(MF)) {
|
2007-05-01 09:13:03 +00:00
|
|
|
assert(SPAdj == 0 && "Unexpected");
|
2007-01-19 07:51:42 +00:00
|
|
|
// There is alloca()'s in this function, must reference off the frame
|
|
|
|
// pointer instead.
|
|
|
|
FrameReg = getFrameRegister(MF);
|
2007-01-31 13:12:46 +00:00
|
|
|
Offset -= AFI->getFramePtrSpillOffset();
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Opcode = MI.getOpcode();
|
2008-01-07 07:27:27 +00:00
|
|
|
const TargetInstrDesc &Desc = MI.getDesc();
|
2007-01-19 07:51:42 +00:00
|
|
|
unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
|
|
|
|
bool isSub = false;
|
2007-03-07 00:12:18 +00:00
|
|
|
|
2009-05-19 18:33:02 +00:00
|
|
|
// Memory operands in inline assembly always use AddrMode2.
|
|
|
|
if (Opcode == ARM::INLINEASM)
|
|
|
|
AddrMode = ARMII::AddrMode2;
|
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
if (Opcode == ARM::ADDri) {
|
|
|
|
Offset += MI.getOperand(i+1).getImm();
|
|
|
|
if (Offset == 0) {
|
|
|
|
// Turn it into a move.
|
2008-01-11 18:10:50 +00:00
|
|
|
MI.setDesc(TII.get(ARM::MOVr));
|
2007-01-19 07:51:42 +00:00
|
|
|
MI.getOperand(i).ChangeToRegister(FrameReg, false);
|
|
|
|
MI.RemoveOperand(i+1);
|
|
|
|
return;
|
|
|
|
} else if (Offset < 0) {
|
|
|
|
Offset = -Offset;
|
|
|
|
isSub = true;
|
2008-01-11 18:10:50 +00:00
|
|
|
MI.setDesc(TII.get(ARM::SUBri));
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
2006-06-18 00:08:07 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// Common case: small offset, fits into instruction.
|
|
|
|
int ImmedOffset = ARM_AM::getSOImmVal(Offset);
|
|
|
|
if (ImmedOffset != -1) {
|
|
|
|
// Replace the FrameIndex with sp / fp
|
|
|
|
MI.getOperand(i).ChangeToRegister(FrameReg, false);
|
|
|
|
MI.getOperand(i+1).ChangeToImmediate(ImmedOffset);
|
|
|
|
return;
|
|
|
|
}
|
2009-06-27 12:16:40 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// Otherwise, we fallback to common code below to form the imm offset with
|
|
|
|
// a sequence of ADDri instructions. First though, pull as much of the imm
|
|
|
|
// into this ADDri as possible.
|
|
|
|
unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
|
2007-04-03 21:31:21 +00:00
|
|
|
unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
|
2009-06-27 12:16:40 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// We will handle these bits from offset, clear them.
|
|
|
|
Offset &= ~ThisImmVal;
|
2009-06-27 12:16:40 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// Get the properly encoded SOImmVal field.
|
|
|
|
int ThisSOImmVal = ARM_AM::getSOImmVal(ThisImmVal);
|
2009-06-27 12:16:40 +00:00
|
|
|
assert(ThisSOImmVal != -1 && "Bit extraction didn't work?");
|
2007-01-19 07:51:42 +00:00
|
|
|
MI.getOperand(i+1).ChangeToImmediate(ThisSOImmVal);
|
|
|
|
} else {
|
|
|
|
unsigned ImmIdx = 0;
|
|
|
|
int InstrOffs = 0;
|
|
|
|
unsigned NumBits = 0;
|
|
|
|
unsigned Scale = 1;
|
|
|
|
switch (AddrMode) {
|
|
|
|
case ARMII::AddrMode2: {
|
|
|
|
ImmIdx = i+2;
|
|
|
|
InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
|
|
|
|
if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
|
|
|
|
InstrOffs *= -1;
|
|
|
|
NumBits = 12;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ARMII::AddrMode3: {
|
|
|
|
ImmIdx = i+2;
|
|
|
|
InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
|
|
|
|
if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
|
|
|
|
InstrOffs *= -1;
|
|
|
|
NumBits = 8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ARMII::AddrMode5: {
|
|
|
|
ImmIdx = i+1;
|
|
|
|
InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
|
|
|
|
if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
|
|
|
|
InstrOffs *= -1;
|
|
|
|
NumBits = 8;
|
|
|
|
Scale = 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2007-02-07 21:18:32 +00:00
|
|
|
assert(0 && "Unsupported addressing mode!");
|
2007-01-19 07:51:42 +00:00
|
|
|
abort();
|
|
|
|
break;
|
|
|
|
}
|
2006-06-18 00:08:07 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
Offset += InstrOffs * Scale;
|
2007-02-01 02:18:36 +00:00
|
|
|
assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
|
2009-06-27 12:16:40 +00:00
|
|
|
if (Offset < 0) {
|
2007-01-19 07:51:42 +00:00
|
|
|
Offset = -Offset;
|
|
|
|
isSub = true;
|
|
|
|
}
|
2006-06-18 00:08:07 +00:00
|
|
|
|
2007-02-07 02:44:23 +00:00
|
|
|
// Common case: small offset, fits into instruction.
|
2007-02-07 00:06:56 +00:00
|
|
|
MachineOperand &ImmOp = MI.getOperand(ImmIdx);
|
|
|
|
int ImmedOffset = Offset / Scale;
|
|
|
|
unsigned Mask = (1 << NumBits) - 1;
|
|
|
|
if ((unsigned)Offset <= Mask * Scale) {
|
|
|
|
// Replace the FrameIndex with sp
|
|
|
|
MI.getOperand(i).ChangeToRegister(FrameReg, false);
|
|
|
|
if (isSub)
|
|
|
|
ImmedOffset |= 1 << NumBits;
|
|
|
|
ImmOp.ChangeToImmediate(ImmedOffset);
|
|
|
|
return;
|
|
|
|
}
|
2007-02-06 00:23:31 +00:00
|
|
|
|
2009-06-27 12:16:40 +00:00
|
|
|
// Otherwise, it didn't fit. Pull in what we can to simplify the immed.
|
|
|
|
ImmedOffset = ImmedOffset & Mask;
|
|
|
|
if (isSub)
|
|
|
|
ImmedOffset |= 1 << NumBits;
|
|
|
|
ImmOp.ChangeToImmediate(ImmedOffset);
|
|
|
|
Offset &= ~(Mask*Scale);
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
2009-06-27 12:16:40 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// If we get here, the immediate doesn't fit into the instruction. We folded
|
|
|
|
// as much as possible above, handle the rest, providing a register that is
|
|
|
|
// SP+LargeImm.
|
|
|
|
assert(Offset && "This code isn't needed if offset already handled!");
|
2006-06-18 00:08:07 +00:00
|
|
|
|
2009-06-27 12:16:40 +00:00
|
|
|
// Insert a set of r12 with the full address: r12 = sp + offset
|
|
|
|
// If the offset we have is too large to fit into the instruction, we need
|
|
|
|
// to form it with a series of ADDri's. Do this by taking 8-bit chunks
|
|
|
|
// out of 'Offset'.
|
|
|
|
unsigned ScratchReg = findScratchRegister(RS, &ARM::GPRRegClass, AFI);
|
|
|
|
if (ScratchReg == 0)
|
|
|
|
// No register is "free". Scavenge a register.
|
|
|
|
ScratchReg = RS->scavengeRegister(&ARM::GPRRegClass, II, SPAdj);
|
|
|
|
int PIdx = MI.findFirstPredOperandIdx();
|
|
|
|
ARMCC::CondCodes Pred = (PIdx == -1)
|
|
|
|
? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
|
|
|
|
unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg();
|
|
|
|
emitARMRegPlusImmediate(MBB, II, ScratchReg, FrameReg,
|
|
|
|
isSub ? -Offset : Offset, Pred, PredReg, TII, dl);
|
|
|
|
MI.getOperand(i).ChangeToRegister(ScratchReg, false, false, true);
|
2006-05-14 22:18:28 +00:00
|
|
|
}
|
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
/// Move iterator pass the next bunch of callee save load / store ops for
|
|
|
|
/// the particular spill area (1: integer area 1, 2: integer area 2,
|
|
|
|
/// 3: fp area, 0: don't care).
|
|
|
|
static void movePastCSLoadStoreOps(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator &MBBI,
|
|
|
|
int Opc, unsigned Area,
|
|
|
|
const ARMSubtarget &STI) {
|
|
|
|
while (MBBI != MBB.end() &&
|
2008-10-03 15:45:36 +00:00
|
|
|
MBBI->getOpcode() == Opc && MBBI->getOperand(1).isFI()) {
|
2007-01-19 07:51:42 +00:00
|
|
|
if (Area != 0) {
|
|
|
|
bool Done = false;
|
|
|
|
unsigned Category = 0;
|
|
|
|
switch (MBBI->getOperand(0).getReg()) {
|
2007-01-20 02:09:25 +00:00
|
|
|
case ARM::R4: case ARM::R5: case ARM::R6: case ARM::R7:
|
2007-01-19 07:51:42 +00:00
|
|
|
case ARM::LR:
|
|
|
|
Category = 1;
|
|
|
|
break;
|
2007-01-20 02:09:25 +00:00
|
|
|
case ARM::R8: case ARM::R9: case ARM::R10: case ARM::R11:
|
2007-01-19 19:28:01 +00:00
|
|
|
Category = STI.isTargetDarwin() ? 2 : 1;
|
2007-01-19 07:51:42 +00:00
|
|
|
break;
|
2007-01-20 02:09:25 +00:00
|
|
|
case ARM::D8: case ARM::D9: case ARM::D10: case ARM::D11:
|
|
|
|
case ARM::D12: case ARM::D13: case ARM::D14: case ARM::D15:
|
2007-01-19 07:51:42 +00:00
|
|
|
Category = 3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Done = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (Done || Category != Area)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
++MBBI;
|
|
|
|
}
|
|
|
|
}
|
2006-05-14 22:18:28 +00:00
|
|
|
|
|
|
|
void ARMRegisterInfo::emitPrologue(MachineFunction &MF) const {
|
2006-07-18 17:00:30 +00:00
|
|
|
MachineBasicBlock &MBB = MF.front();
|
2006-07-21 12:26:16 +00:00
|
|
|
MachineBasicBlock::iterator MBBI = MBB.begin();
|
2006-07-18 17:00:30 +00:00
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
2007-01-19 07:51:42 +00:00
|
|
|
ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
|
|
|
|
unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
|
|
|
|
unsigned NumBytes = MFI->getStackSize();
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
|
2009-02-23 00:42:30 +00:00
|
|
|
DebugLoc dl = (MBBI != MBB.end() ?
|
|
|
|
MBBI->getDebugLoc() : DebugLoc::getUnknownLoc());
|
2006-07-18 17:00:30 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// Determine the sizes of each callee-save spill areas and record which frame
|
|
|
|
// belongs to which callee-save spill areas.
|
|
|
|
unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
|
|
|
|
int FramePtrSpillFI = 0;
|
2007-02-23 21:53:48 +00:00
|
|
|
|
|
|
|
if (VARegSaveSize)
|
2009-06-27 12:59:03 +00:00
|
|
|
emitSPUpdate(MBB, MBBI, TII, dl, -VARegSaveSize);
|
2007-02-23 21:53:48 +00:00
|
|
|
|
2007-01-30 02:57:02 +00:00
|
|
|
if (!AFI->hasStackFrame()) {
|
|
|
|
if (NumBytes != 0)
|
2009-06-27 12:59:03 +00:00
|
|
|
emitSPUpdate(MBB, MBBI, TII, dl, -NumBytes);
|
2007-01-30 02:57:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
|
|
|
|
unsigned Reg = CSI[i].getReg();
|
|
|
|
int FI = CSI[i].getFrameIdx();
|
|
|
|
switch (Reg) {
|
|
|
|
case ARM::R4:
|
|
|
|
case ARM::R5:
|
|
|
|
case ARM::R6:
|
|
|
|
case ARM::R7:
|
|
|
|
case ARM::LR:
|
|
|
|
if (Reg == FramePtr)
|
|
|
|
FramePtrSpillFI = FI;
|
|
|
|
AFI->addGPRCalleeSavedArea1Frame(FI);
|
|
|
|
GPRCS1Size += 4;
|
|
|
|
break;
|
|
|
|
case ARM::R8:
|
|
|
|
case ARM::R9:
|
|
|
|
case ARM::R10:
|
|
|
|
case ARM::R11:
|
|
|
|
if (Reg == FramePtr)
|
|
|
|
FramePtrSpillFI = FI;
|
|
|
|
if (STI.isTargetDarwin()) {
|
|
|
|
AFI->addGPRCalleeSavedArea2Frame(FI);
|
|
|
|
GPRCS2Size += 4;
|
|
|
|
} else {
|
2007-01-19 07:51:42 +00:00
|
|
|
AFI->addGPRCalleeSavedArea1Frame(FI);
|
|
|
|
GPRCS1Size += 4;
|
|
|
|
}
|
2007-01-30 02:57:02 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
AFI->addDPRCalleeSavedAreaFrame(FI);
|
|
|
|
DPRCSSize += 8;
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
2007-01-30 02:57:02 +00:00
|
|
|
}
|
2007-01-19 07:51:42 +00:00
|
|
|
|
2009-06-27 12:16:40 +00:00
|
|
|
// Build the new SUBri to adjust SP for integer callee-save spill area 1.
|
2009-06-27 12:59:03 +00:00
|
|
|
emitSPUpdate(MBB, MBBI, TII, dl, -GPRCS1Size);
|
2009-06-27 12:16:40 +00:00
|
|
|
movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, 1, STI);
|
2007-01-19 07:51:42 +00:00
|
|
|
|
2007-01-31 22:25:33 +00:00
|
|
|
// Darwin ABI requires FP to point to the stack slot that contains the
|
|
|
|
// previous FP.
|
2007-05-15 01:29:07 +00:00
|
|
|
if (STI.isTargetDarwin() || hasFP(MF)) {
|
|
|
|
MachineInstrBuilder MIB =
|
2009-06-27 12:16:40 +00:00
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(ARM::ADDri), FramePtr)
|
2007-01-30 02:57:02 +00:00
|
|
|
.addFrameIndex(FramePtrSpillFI).addImm(0);
|
2009-06-27 12:16:40 +00:00
|
|
|
AddDefaultCC(AddDefaultPred(MIB));
|
2007-05-15 01:29:07 +00:00
|
|
|
}
|
2007-01-19 07:51:42 +00:00
|
|
|
|
2009-06-27 12:16:40 +00:00
|
|
|
// Build the new SUBri to adjust SP for integer callee-save spill area 2.
|
2009-06-27 12:59:03 +00:00
|
|
|
emitSPUpdate(MBB, MBBI, TII, dl, -GPRCS2Size);
|
2007-01-19 07:51:42 +00:00
|
|
|
|
2009-06-27 12:16:40 +00:00
|
|
|
// Build the new SUBri to adjust SP for FP callee-save spill area.
|
|
|
|
movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, 2, STI);
|
2009-06-27 12:59:03 +00:00
|
|
|
emitSPUpdate(MBB, MBBI, TII, dl, -DPRCSSize);
|
2006-10-26 13:31:26 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// Determine starting offsets of spill areas.
|
2007-01-30 02:57:02 +00:00
|
|
|
unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
|
|
|
|
unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
|
|
|
|
unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
|
|
|
|
AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + NumBytes);
|
|
|
|
AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
|
|
|
|
AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
|
|
|
|
AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
|
2009-06-27 12:16:40 +00:00
|
|
|
|
2007-01-30 02:57:02 +00:00
|
|
|
NumBytes = DPRCSOffset;
|
|
|
|
if (NumBytes) {
|
|
|
|
// Insert it after all the callee-save spills.
|
2009-06-27 12:16:40 +00:00
|
|
|
movePastCSLoadStoreOps(MBB, MBBI, ARM::FSTD, 3, STI);
|
2009-06-27 12:59:03 +00:00
|
|
|
emitSPUpdate(MBB, MBBI, TII, dl, -NumBytes);
|
2007-01-30 02:57:02 +00:00
|
|
|
}
|
2006-12-14 13:31:27 +00:00
|
|
|
|
2009-06-27 12:16:40 +00:00
|
|
|
if (STI.isTargetELF() && hasFP(MF)) {
|
2007-05-03 20:28:35 +00:00
|
|
|
MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
|
|
|
|
AFI->getFramePtrSpillOffset());
|
|
|
|
}
|
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
|
|
|
|
AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
|
|
|
|
AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
|
|
|
|
}
|
2006-10-26 13:31:26 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
|
|
|
|
for (unsigned i = 0; CSRegs[i]; ++i)
|
|
|
|
if (Reg == CSRegs[i])
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isCSRestore(MachineInstr *MI, const unsigned *CSRegs) {
|
|
|
|
return ((MI->getOpcode() == ARM::FLDD ||
|
2009-06-27 12:16:40 +00:00
|
|
|
MI->getOpcode() == ARM::LDR) &&
|
2008-10-03 15:45:36 +00:00
|
|
|
MI->getOperand(1).isFI() &&
|
2007-01-19 07:51:42 +00:00
|
|
|
isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs));
|
2006-05-14 22:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ARMRegisterInfo::emitEpilogue(MachineFunction &MF,
|
2007-04-16 18:10:23 +00:00
|
|
|
MachineBasicBlock &MBB) const {
|
2006-07-18 17:00:30 +00:00
|
|
|
MachineBasicBlock::iterator MBBI = prior(MBB.end());
|
2009-06-27 12:16:40 +00:00
|
|
|
assert(MBBI->getOpcode() == ARM::BX_RET &&
|
2006-07-18 17:00:30 +00:00
|
|
|
"Can only insert epilog into returning blocks");
|
2009-02-23 00:42:30 +00:00
|
|
|
DebugLoc dl = MBBI->getDebugLoc();
|
2006-07-18 17:00:30 +00:00
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
2007-01-19 07:51:42 +00:00
|
|
|
ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
|
|
|
|
unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
|
|
|
|
int NumBytes = (int)MFI->getStackSize();
|
2009-02-23 00:42:30 +00:00
|
|
|
|
2007-01-30 02:57:02 +00:00
|
|
|
if (!AFI->hasStackFrame()) {
|
|
|
|
if (NumBytes != 0)
|
2009-06-27 12:59:03 +00:00
|
|
|
emitSPUpdate(MBB, MBBI, TII, dl, NumBytes);
|
2007-02-01 01:49:46 +00:00
|
|
|
} else {
|
2007-02-23 21:53:48 +00:00
|
|
|
// Unwind MBBI to point to first LDR / FLDD.
|
|
|
|
const unsigned *CSRegs = getCalleeSavedRegs();
|
|
|
|
if (MBBI != MBB.begin()) {
|
|
|
|
do
|
|
|
|
--MBBI;
|
|
|
|
while (MBBI != MBB.begin() && isCSRestore(MBBI, CSRegs));
|
|
|
|
if (!isCSRestore(MBBI, CSRegs))
|
|
|
|
++MBBI;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move SP to start of FP callee save spill area.
|
|
|
|
NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
|
|
|
|
AFI->getGPRCalleeSavedArea2Size() +
|
|
|
|
AFI->getDPRCalleeSavedAreaSize());
|
2009-06-27 12:16:40 +00:00
|
|
|
|
|
|
|
// Darwin ABI requires FP to point to the stack slot that contains the
|
|
|
|
// previous FP.
|
|
|
|
if ((STI.isTargetDarwin() && NumBytes) || hasFP(MF)) {
|
|
|
|
NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
|
|
|
|
// Reset SP based on frame pointer only if the stack frame extends beyond
|
|
|
|
// frame pointer stack slot or target is ELF and the function has FP.
|
|
|
|
if (AFI->getGPRCalleeSavedArea2Size() ||
|
|
|
|
AFI->getDPRCalleeSavedAreaSize() ||
|
|
|
|
AFI->getDPRCalleeSavedAreaOffset()||
|
|
|
|
hasFP(MF)) {
|
2007-01-30 02:57:02 +00:00
|
|
|
if (NumBytes)
|
2009-06-27 12:16:40 +00:00
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(ARM::SUBri), ARM::SP).addReg(FramePtr)
|
|
|
|
.addImm(NumBytes)
|
|
|
|
.addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
|
2007-01-30 02:57:02 +00:00
|
|
|
else
|
2009-06-27 12:16:40 +00:00
|
|
|
BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP).addReg(FramePtr)
|
|
|
|
.addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
|
2007-02-23 21:53:48 +00:00
|
|
|
}
|
2009-06-27 12:16:40 +00:00
|
|
|
} else if (NumBytes) {
|
2009-06-27 12:59:03 +00:00
|
|
|
emitSPUpdate(MBB, MBBI, TII, dl, NumBytes);
|
2009-06-27 12:16:40 +00:00
|
|
|
}
|
2007-01-31 22:25:33 +00:00
|
|
|
|
2009-06-27 12:16:40 +00:00
|
|
|
// Move SP to start of integer callee save spill area 2.
|
|
|
|
movePastCSLoadStoreOps(MBB, MBBI, ARM::FLDD, 3, STI);
|
2009-06-27 12:59:03 +00:00
|
|
|
emitSPUpdate(MBB, MBBI, TII, dl, AFI->getDPRCalleeSavedAreaSize());
|
2007-01-30 02:57:02 +00:00
|
|
|
|
2009-06-27 12:16:40 +00:00
|
|
|
// Move SP to start of integer callee save spill area 1.
|
|
|
|
movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, 2, STI);
|
2009-06-27 12:59:03 +00:00
|
|
|
emitSPUpdate(MBB, MBBI, TII, dl, AFI->getGPRCalleeSavedArea2Size());
|
2007-01-30 02:57:02 +00:00
|
|
|
|
2009-06-27 12:16:40 +00:00
|
|
|
// Move SP to SP upon entry to the function.
|
|
|
|
movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, 1, STI);
|
2009-06-27 12:59:03 +00:00
|
|
|
emitSPUpdate(MBB, MBBI, TII, dl, AFI->getGPRCalleeSavedArea1Size());
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
2007-01-30 02:57:02 +00:00
|
|
|
|
2009-06-27 12:16:40 +00:00
|
|
|
if (VARegSaveSize)
|
2009-06-27 12:59:03 +00:00
|
|
|
emitSPUpdate(MBB, MBBI, TII, dl, VARegSaveSize);
|
2007-02-02 08:58:48 +00:00
|
|
|
|
2006-05-14 22:18:28 +00:00
|
|
|
}
|
|
|
|
|