2007-07-26 08:18:32 +00:00
|
|
|
//===-- LowerSubregs.cpp - Subregister Lowering instruction pass ----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-07-26 08:18:32 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-09-24 23:44:12 +00:00
|
|
|
//
|
|
|
|
// This file defines a MachineFunction pass which runs after register
|
|
|
|
// allocation that turns subreg insert/extract instructions into register
|
|
|
|
// copies, as needed. This ensures correct codegen even if the coalescer
|
|
|
|
// isn't able to remove all subreg instructions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-07-26 08:18:32 +00:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "lowersubregs"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2009-08-03 20:08:18 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2007-12-31 04:13:23 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2008-02-10 18:45:23 +00:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2007-07-26 08:18:32 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-25 00:23:56 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2007-07-26 08:18:32 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2009-10-25 06:33:48 +00:00
|
|
|
struct LowerSubregsInstructionPass : public MachineFunctionPass {
|
2009-10-25 07:49:57 +00:00
|
|
|
private:
|
|
|
|
const TargetRegisterInfo *TRI;
|
|
|
|
const TargetInstrInfo *TII;
|
|
|
|
|
|
|
|
public:
|
2007-07-26 08:18:32 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2008-09-04 17:05:41 +00:00
|
|
|
LowerSubregsInstructionPass() : MachineFunctionPass(&ID) {}
|
2007-07-26 08:18:32 +00:00
|
|
|
|
|
|
|
const char *getPassName() const {
|
|
|
|
return "Subregister lowering instruction pass";
|
|
|
|
}
|
|
|
|
|
2008-09-22 20:58:04 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2009-07-31 23:37:33 +00:00
|
|
|
AU.setPreservesCFG();
|
2008-09-22 22:21:38 +00:00
|
|
|
AU.addPreservedID(MachineLoopInfoID);
|
|
|
|
AU.addPreservedID(MachineDominatorsID);
|
2008-09-22 20:58:04 +00:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2007-07-26 08:18:32 +00:00
|
|
|
/// runOnMachineFunction - pass entry point
|
|
|
|
bool runOnMachineFunction(MachineFunction&);
|
2009-10-25 07:49:57 +00:00
|
|
|
|
|
|
|
private:
|
2007-08-06 16:33:56 +00:00
|
|
|
bool LowerExtract(MachineInstr *MI);
|
|
|
|
bool LowerInsert(MachineInstr *MI);
|
2008-03-16 03:12:01 +00:00
|
|
|
bool LowerSubregToReg(MachineInstr *MI);
|
2008-12-18 22:14:08 +00:00
|
|
|
|
|
|
|
void TransferDeadFlag(MachineInstr *MI, unsigned DstReg,
|
2009-10-25 07:49:57 +00:00
|
|
|
const TargetRegisterInfo *TRI);
|
2008-12-18 22:14:08 +00:00
|
|
|
void TransferKillFlag(MachineInstr *MI, unsigned SrcReg,
|
2009-10-25 07:49:57 +00:00
|
|
|
const TargetRegisterInfo *TRI,
|
2009-08-05 02:25:11 +00:00
|
|
|
bool AddIfNotFound = false);
|
2007-07-26 08:18:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
char LowerSubregsInstructionPass::ID = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *llvm::createLowerSubregsPass() {
|
|
|
|
return new LowerSubregsInstructionPass();
|
|
|
|
}
|
|
|
|
|
2008-12-18 22:14:08 +00:00
|
|
|
/// TransferDeadFlag - MI is a pseudo-instruction with DstReg dead,
|
|
|
|
/// and the lowered replacement instructions immediately precede it.
|
|
|
|
/// Mark the replacement instructions with the dead flag.
|
|
|
|
void
|
|
|
|
LowerSubregsInstructionPass::TransferDeadFlag(MachineInstr *MI,
|
|
|
|
unsigned DstReg,
|
2009-10-25 07:49:57 +00:00
|
|
|
const TargetRegisterInfo *TRI) {
|
2008-12-18 22:14:08 +00:00
|
|
|
for (MachineBasicBlock::iterator MII =
|
|
|
|
prior(MachineBasicBlock::iterator(MI)); ; --MII) {
|
2009-10-25 07:49:57 +00:00
|
|
|
if (MII->addRegisterDead(DstReg, TRI))
|
2008-12-18 22:14:08 +00:00
|
|
|
break;
|
|
|
|
assert(MII != MI->getParent()->begin() &&
|
|
|
|
"copyRegToReg output doesn't reference destination register!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// TransferKillFlag - MI is a pseudo-instruction with SrcReg killed,
|
|
|
|
/// and the lowered replacement instructions immediately precede it.
|
|
|
|
/// Mark the replacement instructions with the kill flag.
|
|
|
|
void
|
|
|
|
LowerSubregsInstructionPass::TransferKillFlag(MachineInstr *MI,
|
|
|
|
unsigned SrcReg,
|
2009-10-25 07:49:57 +00:00
|
|
|
const TargetRegisterInfo *TRI,
|
2009-08-05 02:25:11 +00:00
|
|
|
bool AddIfNotFound) {
|
2008-12-18 22:14:08 +00:00
|
|
|
for (MachineBasicBlock::iterator MII =
|
|
|
|
prior(MachineBasicBlock::iterator(MI)); ; --MII) {
|
2009-10-25 07:49:57 +00:00
|
|
|
if (MII->addRegisterKilled(SrcReg, TRI, AddIfNotFound))
|
2008-12-18 22:14:08 +00:00
|
|
|
break;
|
|
|
|
assert(MII != MI->getParent()->begin() &&
|
|
|
|
"copyRegToReg output doesn't reference source register!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-06 16:33:56 +00:00
|
|
|
bool LowerSubregsInstructionPass::LowerExtract(MachineInstr *MI) {
|
2008-12-18 22:06:01 +00:00
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
LowerSubregsInstructionPass::LowerExtract should not extend the live range of registers.
When LowerExtract eliminates an EXTRACT_SUBREG with a kill flag, it moves the
kill flag to the place where the sub-register is killed. This can accidentally
overlap with the use of a sibling sub-register, and we have trouble.
In the test case we have this code:
Live Ins: %R0 %R1 %R2
%R2L<def> = EXTRACT_SUBREG %R2<kill>, 1
%R2H<def> = LOAD16fi <fi#-1>, 0, Mem:LD(2,4) [FixedStack-1 + 0]
%R1L<def> = EXTRACT_SUBREG %R1<kill>, 1
%R0L<def> = EXTRACT_SUBREG %R0<kill>, 1
%R0H<def> = ADD16 %R2H<kill>, %R2L<kill>, %AZ<imp-def>, %AN<imp-def>, %AC0<imp-def>, %V<imp-def>, %VS<imp-def>
subreg: CONVERTING: %R2L<def> = EXTRACT_SUBREG %R2<kill>, 1
subreg: eliminated!
subreg: killed here: %R0H<def> = ADD16 %R2H, %R2L, %R2<imp-use,kill>, %AZ<imp-def>, %AN<imp-def>, %AC0<imp-def>, %V<imp-def>, %VS<imp-def>
The kill flag on %R2 is moved to the last instruction, and the live range overlaps with the definition of %R2H:
*** Bad machine code: Redefining a live physical register ***
- function: f
- basic block: 0x18358c0 (#0)
- instruction: %R2H<def> = LOAD16fi <fi#-1>, 0, Mem:LD(2,4) [FixedStack-1 + 0]
Register R2H was defined but already live.
The fix is to replace EXTRACT_SUBREG with IMPLICIT_DEF instead of eliminating
it completely:
subreg: CONVERTING: %R2L<def> = EXTRACT_SUBREG %R2<kill>, 1
subreg: replace by: %R2L<def> = IMPLICIT_DEF %R2<kill>
Note that these IMPLICIT_DEF instructions survive to the asm output. It is
necessary to fix the stack-color-with-reg test case because of that.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78093 91177308-0d34-0410-b5e6-96231b3b80d8
2009-08-04 20:01:11 +00:00
|
|
|
|
2008-12-18 22:06:01 +00:00
|
|
|
assert(MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
|
|
|
|
MI->getOperand(1).isReg() && MI->getOperand(1).isUse() &&
|
|
|
|
MI->getOperand(2).isImm() && "Malformed extract_subreg");
|
2007-08-06 16:33:56 +00:00
|
|
|
|
2008-12-18 22:06:01 +00:00
|
|
|
unsigned DstReg = MI->getOperand(0).getReg();
|
|
|
|
unsigned SuperReg = MI->getOperand(1).getReg();
|
|
|
|
unsigned SubIdx = MI->getOperand(2).getImm();
|
2009-10-25 07:49:57 +00:00
|
|
|
unsigned SrcReg = TRI->getSubReg(SuperReg, SubIdx);
|
2007-08-06 16:33:56 +00:00
|
|
|
|
2008-12-18 22:06:01 +00:00
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(SuperReg) &&
|
|
|
|
"Extract supperg source must be a physical register");
|
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(DstReg) &&
|
2008-12-18 22:07:25 +00:00
|
|
|
"Extract destination must be in a physical register");
|
2009-08-05 03:53:14 +00:00
|
|
|
assert(SrcReg && "invalid subregister index for register");
|
LowerSubregsInstructionPass::LowerExtract should not extend the live range of registers.
When LowerExtract eliminates an EXTRACT_SUBREG with a kill flag, it moves the
kill flag to the place where the sub-register is killed. This can accidentally
overlap with the use of a sibling sub-register, and we have trouble.
In the test case we have this code:
Live Ins: %R0 %R1 %R2
%R2L<def> = EXTRACT_SUBREG %R2<kill>, 1
%R2H<def> = LOAD16fi <fi#-1>, 0, Mem:LD(2,4) [FixedStack-1 + 0]
%R1L<def> = EXTRACT_SUBREG %R1<kill>, 1
%R0L<def> = EXTRACT_SUBREG %R0<kill>, 1
%R0H<def> = ADD16 %R2H<kill>, %R2L<kill>, %AZ<imp-def>, %AN<imp-def>, %AC0<imp-def>, %V<imp-def>, %VS<imp-def>
subreg: CONVERTING: %R2L<def> = EXTRACT_SUBREG %R2<kill>, 1
subreg: eliminated!
subreg: killed here: %R0H<def> = ADD16 %R2H, %R2L, %R2<imp-use,kill>, %AZ<imp-def>, %AN<imp-def>, %AC0<imp-def>, %V<imp-def>, %VS<imp-def>
The kill flag on %R2 is moved to the last instruction, and the live range overlaps with the definition of %R2H:
*** Bad machine code: Redefining a live physical register ***
- function: f
- basic block: 0x18358c0 (#0)
- instruction: %R2H<def> = LOAD16fi <fi#-1>, 0, Mem:LD(2,4) [FixedStack-1 + 0]
Register R2H was defined but already live.
The fix is to replace EXTRACT_SUBREG with IMPLICIT_DEF instead of eliminating
it completely:
subreg: CONVERTING: %R2L<def> = EXTRACT_SUBREG %R2<kill>, 1
subreg: replace by: %R2L<def> = IMPLICIT_DEF %R2<kill>
Note that these IMPLICIT_DEF instructions survive to the asm output. It is
necessary to fix the stack-color-with-reg test case because of that.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78093 91177308-0d34-0410-b5e6-96231b3b80d8
2009-08-04 20:01:11 +00:00
|
|
|
|
2010-01-04 23:06:47 +00:00
|
|
|
DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
|
2007-08-06 16:33:56 +00:00
|
|
|
|
2008-12-18 22:11:34 +00:00
|
|
|
if (SrcReg == DstReg) {
|
LowerSubregsInstructionPass::LowerExtract should not extend the live range of registers.
When LowerExtract eliminates an EXTRACT_SUBREG with a kill flag, it moves the
kill flag to the place where the sub-register is killed. This can accidentally
overlap with the use of a sibling sub-register, and we have trouble.
In the test case we have this code:
Live Ins: %R0 %R1 %R2
%R2L<def> = EXTRACT_SUBREG %R2<kill>, 1
%R2H<def> = LOAD16fi <fi#-1>, 0, Mem:LD(2,4) [FixedStack-1 + 0]
%R1L<def> = EXTRACT_SUBREG %R1<kill>, 1
%R0L<def> = EXTRACT_SUBREG %R0<kill>, 1
%R0H<def> = ADD16 %R2H<kill>, %R2L<kill>, %AZ<imp-def>, %AN<imp-def>, %AC0<imp-def>, %V<imp-def>, %VS<imp-def>
subreg: CONVERTING: %R2L<def> = EXTRACT_SUBREG %R2<kill>, 1
subreg: eliminated!
subreg: killed here: %R0H<def> = ADD16 %R2H, %R2L, %R2<imp-use,kill>, %AZ<imp-def>, %AN<imp-def>, %AC0<imp-def>, %V<imp-def>, %VS<imp-def>
The kill flag on %R2 is moved to the last instruction, and the live range overlaps with the definition of %R2H:
*** Bad machine code: Redefining a live physical register ***
- function: f
- basic block: 0x18358c0 (#0)
- instruction: %R2H<def> = LOAD16fi <fi#-1>, 0, Mem:LD(2,4) [FixedStack-1 + 0]
Register R2H was defined but already live.
The fix is to replace EXTRACT_SUBREG with IMPLICIT_DEF instead of eliminating
it completely:
subreg: CONVERTING: %R2L<def> = EXTRACT_SUBREG %R2<kill>, 1
subreg: replace by: %R2L<def> = IMPLICIT_DEF %R2<kill>
Note that these IMPLICIT_DEF instructions survive to the asm output. It is
necessary to fix the stack-color-with-reg test case because of that.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78093 91177308-0d34-0410-b5e6-96231b3b80d8
2009-08-04 20:01:11 +00:00
|
|
|
// No need to insert an identity copy instruction.
|
|
|
|
if (MI->getOperand(1).isKill()) {
|
2009-09-28 20:32:46 +00:00
|
|
|
// We must make sure the super-register gets killed. Replace the
|
|
|
|
// instruction with KILL.
|
2010-02-09 19:54:29 +00:00
|
|
|
MI->setDesc(TII->get(TargetOpcode::KILL));
|
LowerSubregsInstructionPass::LowerExtract should not extend the live range of registers.
When LowerExtract eliminates an EXTRACT_SUBREG with a kill flag, it moves the
kill flag to the place where the sub-register is killed. This can accidentally
overlap with the use of a sibling sub-register, and we have trouble.
In the test case we have this code:
Live Ins: %R0 %R1 %R2
%R2L<def> = EXTRACT_SUBREG %R2<kill>, 1
%R2H<def> = LOAD16fi <fi#-1>, 0, Mem:LD(2,4) [FixedStack-1 + 0]
%R1L<def> = EXTRACT_SUBREG %R1<kill>, 1
%R0L<def> = EXTRACT_SUBREG %R0<kill>, 1
%R0H<def> = ADD16 %R2H<kill>, %R2L<kill>, %AZ<imp-def>, %AN<imp-def>, %AC0<imp-def>, %V<imp-def>, %VS<imp-def>
subreg: CONVERTING: %R2L<def> = EXTRACT_SUBREG %R2<kill>, 1
subreg: eliminated!
subreg: killed here: %R0H<def> = ADD16 %R2H, %R2L, %R2<imp-use,kill>, %AZ<imp-def>, %AN<imp-def>, %AC0<imp-def>, %V<imp-def>, %VS<imp-def>
The kill flag on %R2 is moved to the last instruction, and the live range overlaps with the definition of %R2H:
*** Bad machine code: Redefining a live physical register ***
- function: f
- basic block: 0x18358c0 (#0)
- instruction: %R2H<def> = LOAD16fi <fi#-1>, 0, Mem:LD(2,4) [FixedStack-1 + 0]
Register R2H was defined but already live.
The fix is to replace EXTRACT_SUBREG with IMPLICIT_DEF instead of eliminating
it completely:
subreg: CONVERTING: %R2L<def> = EXTRACT_SUBREG %R2<kill>, 1
subreg: replace by: %R2L<def> = IMPLICIT_DEF %R2<kill>
Note that these IMPLICIT_DEF instructions survive to the asm output. It is
necessary to fix the stack-color-with-reg test case because of that.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78093 91177308-0d34-0410-b5e6-96231b3b80d8
2009-08-04 20:01:11 +00:00
|
|
|
MI->RemoveOperand(2); // SubIdx
|
2010-01-04 23:06:47 +00:00
|
|
|
DEBUG(dbgs() << "subreg: replace by: " << *MI);
|
LowerSubregsInstructionPass::LowerExtract should not extend the live range of registers.
When LowerExtract eliminates an EXTRACT_SUBREG with a kill flag, it moves the
kill flag to the place where the sub-register is killed. This can accidentally
overlap with the use of a sibling sub-register, and we have trouble.
In the test case we have this code:
Live Ins: %R0 %R1 %R2
%R2L<def> = EXTRACT_SUBREG %R2<kill>, 1
%R2H<def> = LOAD16fi <fi#-1>, 0, Mem:LD(2,4) [FixedStack-1 + 0]
%R1L<def> = EXTRACT_SUBREG %R1<kill>, 1
%R0L<def> = EXTRACT_SUBREG %R0<kill>, 1
%R0H<def> = ADD16 %R2H<kill>, %R2L<kill>, %AZ<imp-def>, %AN<imp-def>, %AC0<imp-def>, %V<imp-def>, %VS<imp-def>
subreg: CONVERTING: %R2L<def> = EXTRACT_SUBREG %R2<kill>, 1
subreg: eliminated!
subreg: killed here: %R0H<def> = ADD16 %R2H, %R2L, %R2<imp-use,kill>, %AZ<imp-def>, %AN<imp-def>, %AC0<imp-def>, %V<imp-def>, %VS<imp-def>
The kill flag on %R2 is moved to the last instruction, and the live range overlaps with the definition of %R2H:
*** Bad machine code: Redefining a live physical register ***
- function: f
- basic block: 0x18358c0 (#0)
- instruction: %R2H<def> = LOAD16fi <fi#-1>, 0, Mem:LD(2,4) [FixedStack-1 + 0]
Register R2H was defined but already live.
The fix is to replace EXTRACT_SUBREG with IMPLICIT_DEF instead of eliminating
it completely:
subreg: CONVERTING: %R2L<def> = EXTRACT_SUBREG %R2<kill>, 1
subreg: replace by: %R2L<def> = IMPLICIT_DEF %R2<kill>
Note that these IMPLICIT_DEF instructions survive to the asm output. It is
necessary to fix the stack-color-with-reg test case because of that.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78093 91177308-0d34-0410-b5e6-96231b3b80d8
2009-08-04 20:01:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-08-22 20:23:49 +00:00
|
|
|
|
2010-01-04 23:06:47 +00:00
|
|
|
DEBUG(dbgs() << "subreg: eliminated!");
|
2008-12-18 22:11:34 +00:00
|
|
|
} else {
|
|
|
|
// Insert copy
|
2009-10-25 07:49:57 +00:00
|
|
|
const TargetRegisterClass *TRCS = TRI->getPhysicalRegisterRegClass(DstReg);
|
|
|
|
const TargetRegisterClass *TRCD = TRI->getPhysicalRegisterRegClass(SrcReg);
|
|
|
|
bool Emitted = TII->copyRegToReg(*MBB, MI, DstReg, SrcReg, TRCD, TRCS);
|
2009-07-16 13:55:26 +00:00
|
|
|
(void)Emitted;
|
|
|
|
assert(Emitted && "Subreg and Dst must be of compatible register class");
|
2008-12-18 22:14:08 +00:00
|
|
|
// Transfer the kill/dead flags, if needed.
|
|
|
|
if (MI->getOperand(0).isDead())
|
|
|
|
TransferDeadFlag(MI, DstReg, TRI);
|
|
|
|
if (MI->getOperand(1).isKill())
|
2009-08-05 02:25:11 +00:00
|
|
|
TransferKillFlag(MI, SuperReg, TRI, true);
|
2009-08-22 20:23:49 +00:00
|
|
|
DEBUG({
|
|
|
|
MachineBasicBlock::iterator dMI = MI;
|
2010-01-04 23:06:47 +00:00
|
|
|
dbgs() << "subreg: " << *(--dMI);
|
2009-08-22 20:23:49 +00:00
|
|
|
});
|
2008-12-18 22:06:01 +00:00
|
|
|
}
|
2007-08-06 16:33:56 +00:00
|
|
|
|
2010-01-04 23:06:47 +00:00
|
|
|
DEBUG(dbgs() << '\n');
|
2008-12-18 22:06:01 +00:00
|
|
|
MBB->erase(MI);
|
|
|
|
return true;
|
2007-08-06 16:33:56 +00:00
|
|
|
}
|
|
|
|
|
2008-03-16 03:12:01 +00:00
|
|
|
bool LowerSubregsInstructionPass::LowerSubregToReg(MachineInstr *MI) {
|
2007-08-06 16:33:56 +00:00
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
2008-10-03 15:45:36 +00:00
|
|
|
assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) &&
|
|
|
|
MI->getOperand(1).isImm() &&
|
|
|
|
(MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) &&
|
|
|
|
MI->getOperand(3).isImm() && "Invalid subreg_to_reg");
|
2008-03-11 10:09:17 +00:00
|
|
|
|
2008-03-16 03:12:01 +00:00
|
|
|
unsigned DstReg = MI->getOperand(0).getReg();
|
|
|
|
unsigned InsReg = MI->getOperand(2).getReg();
|
2009-03-23 07:19:58 +00:00
|
|
|
unsigned InsSIdx = MI->getOperand(2).getSubReg();
|
|
|
|
unsigned SubIdx = MI->getOperand(3).getImm();
|
2007-08-06 16:33:56 +00:00
|
|
|
|
2008-03-16 03:12:01 +00:00
|
|
|
assert(SubIdx != 0 && "Invalid index for insert_subreg");
|
2009-10-25 07:49:57 +00:00
|
|
|
unsigned DstSubReg = TRI->getSubReg(DstReg, SubIdx);
|
2009-03-23 07:19:58 +00:00
|
|
|
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(DstReg) &&
|
2007-08-06 16:33:56 +00:00
|
|
|
"Insert destination must be in a physical register");
|
2008-02-10 18:45:23 +00:00
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(InsReg) &&
|
2007-08-06 16:33:56 +00:00
|
|
|
"Inserted value must be in a physical register");
|
|
|
|
|
2010-01-04 23:06:47 +00:00
|
|
|
DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
|
2008-03-16 03:12:01 +00:00
|
|
|
|
2009-03-23 07:19:58 +00:00
|
|
|
if (DstSubReg == InsReg && InsSIdx == 0) {
|
2008-08-07 02:54:50 +00:00
|
|
|
// No need to insert an identify copy instruction.
|
2009-03-23 07:19:58 +00:00
|
|
|
// Watch out for case like this:
|
|
|
|
// %RAX<def> = ...
|
|
|
|
// %RAX<def> = SUBREG_TO_REG 0, %EAX:3<kill>, 3
|
|
|
|
// The first def is defining RAX, not EAX so the top bits were not
|
|
|
|
// zero extended.
|
2010-01-04 23:06:47 +00:00
|
|
|
DEBUG(dbgs() << "subreg: eliminated!");
|
2008-08-07 02:54:50 +00:00
|
|
|
} else {
|
|
|
|
// Insert sub-register copy
|
2009-10-25 07:49:57 +00:00
|
|
|
const TargetRegisterClass *TRC0= TRI->getPhysicalRegisterRegClass(DstSubReg);
|
|
|
|
const TargetRegisterClass *TRC1= TRI->getPhysicalRegisterRegClass(InsReg);
|
|
|
|
bool Emitted = TII->copyRegToReg(*MBB, MI, DstSubReg, InsReg, TRC0, TRC1);
|
2009-10-24 00:27:00 +00:00
|
|
|
(void)Emitted;
|
|
|
|
assert(Emitted && "Subreg and Dst must be of compatible register class");
|
2008-12-18 22:14:08 +00:00
|
|
|
// Transfer the kill/dead flags, if needed.
|
|
|
|
if (MI->getOperand(0).isDead())
|
|
|
|
TransferDeadFlag(MI, DstSubReg, TRI);
|
|
|
|
if (MI->getOperand(2).isKill())
|
|
|
|
TransferKillFlag(MI, InsReg, TRI);
|
2009-08-22 20:23:49 +00:00
|
|
|
DEBUG({
|
|
|
|
MachineBasicBlock::iterator dMI = MI;
|
2010-01-04 23:06:47 +00:00
|
|
|
dbgs() << "subreg: " << *(--dMI);
|
2009-08-22 20:23:49 +00:00
|
|
|
});
|
2008-08-07 02:54:50 +00:00
|
|
|
}
|
2007-08-06 16:33:56 +00:00
|
|
|
|
2010-01-04 23:06:47 +00:00
|
|
|
DEBUG(dbgs() << '\n');
|
2008-07-17 23:49:46 +00:00
|
|
|
MBB->erase(MI);
|
2009-10-24 00:27:00 +00:00
|
|
|
return true;
|
2008-03-16 03:12:01 +00:00
|
|
|
}
|
2007-08-06 16:33:56 +00:00
|
|
|
|
2008-03-16 03:12:01 +00:00
|
|
|
bool LowerSubregsInstructionPass::LowerInsert(MachineInstr *MI) {
|
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
2008-10-03 15:45:36 +00:00
|
|
|
assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) &&
|
|
|
|
(MI->getOperand(1).isReg() && MI->getOperand(1).isUse()) &&
|
|
|
|
(MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) &&
|
|
|
|
MI->getOperand(3).isImm() && "Invalid insert_subreg");
|
2008-03-16 03:12:01 +00:00
|
|
|
|
|
|
|
unsigned DstReg = MI->getOperand(0).getReg();
|
2008-11-21 20:00:59 +00:00
|
|
|
#ifndef NDEBUG
|
2008-03-16 03:12:01 +00:00
|
|
|
unsigned SrcReg = MI->getOperand(1).getReg();
|
2008-11-21 20:00:59 +00:00
|
|
|
#endif
|
2008-03-16 03:12:01 +00:00
|
|
|
unsigned InsReg = MI->getOperand(2).getReg();
|
|
|
|
unsigned SubIdx = MI->getOperand(3).getImm();
|
2007-08-10 21:11:55 +00:00
|
|
|
|
2008-03-16 03:12:01 +00:00
|
|
|
assert(DstReg == SrcReg && "insert_subreg not a two-address instruction?");
|
|
|
|
assert(SubIdx != 0 && "Invalid index for insert_subreg");
|
2009-10-25 07:49:57 +00:00
|
|
|
unsigned DstSubReg = TRI->getSubReg(DstReg, SubIdx);
|
2009-08-03 20:08:18 +00:00
|
|
|
assert(DstSubReg && "invalid subregister index for register");
|
2008-03-16 03:12:01 +00:00
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
|
|
|
|
"Insert superreg source must be in a physical register");
|
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(InsReg) &&
|
|
|
|
"Inserted value must be in a physical register");
|
2007-08-06 16:33:56 +00:00
|
|
|
|
2010-01-04 23:06:47 +00:00
|
|
|
DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
|
2008-03-16 03:12:01 +00:00
|
|
|
|
2008-06-16 22:52:53 +00:00
|
|
|
if (DstSubReg == InsReg) {
|
2009-08-03 20:08:18 +00:00
|
|
|
// No need to insert an identity copy instruction. If the SrcReg was
|
2009-09-28 20:32:46 +00:00
|
|
|
// <undef>, we need to make sure it is alive by inserting a KILL
|
2009-08-03 20:08:18 +00:00
|
|
|
if (MI->getOperand(1).isUndef() && !MI->getOperand(0).isDead()) {
|
2009-08-05 01:57:22 +00:00
|
|
|
MachineInstrBuilder MIB = BuildMI(*MBB, MI, MI->getDebugLoc(),
|
2010-02-09 19:54:29 +00:00
|
|
|
TII->get(TargetOpcode::KILL), DstReg);
|
2009-08-05 01:57:22 +00:00
|
|
|
if (MI->getOperand(2).isUndef())
|
2009-09-28 20:32:46 +00:00
|
|
|
MIB.addReg(InsReg, RegState::Undef);
|
2009-08-05 01:57:22 +00:00
|
|
|
else
|
2009-09-28 20:32:46 +00:00
|
|
|
MIB.addReg(InsReg, RegState::Kill);
|
2009-08-03 20:08:18 +00:00
|
|
|
} else {
|
2010-01-04 23:06:47 +00:00
|
|
|
DEBUG(dbgs() << "subreg: eliminated!\n");
|
2009-08-03 20:08:18 +00:00
|
|
|
MBB->erase(MI);
|
|
|
|
return true;
|
|
|
|
}
|
2008-06-16 22:52:53 +00:00
|
|
|
} else {
|
|
|
|
// Insert sub-register copy
|
2009-10-25 07:49:57 +00:00
|
|
|
const TargetRegisterClass *TRC0= TRI->getPhysicalRegisterRegClass(DstSubReg);
|
|
|
|
const TargetRegisterClass *TRC1= TRI->getPhysicalRegisterRegClass(InsReg);
|
2009-08-05 01:29:24 +00:00
|
|
|
if (MI->getOperand(2).isUndef())
|
2009-09-28 20:32:46 +00:00
|
|
|
// If the source register being inserted is undef, then this becomes a
|
|
|
|
// KILL.
|
2009-08-05 01:29:24 +00:00
|
|
|
BuildMI(*MBB, MI, MI->getDebugLoc(),
|
2010-02-09 19:54:29 +00:00
|
|
|
TII->get(TargetOpcode::KILL), DstSubReg);
|
2009-10-24 00:27:00 +00:00
|
|
|
else {
|
2009-10-25 07:49:57 +00:00
|
|
|
bool Emitted = TII->copyRegToReg(*MBB, MI, DstSubReg, InsReg, TRC0, TRC1);
|
2009-10-24 00:27:00 +00:00
|
|
|
(void)Emitted;
|
|
|
|
assert(Emitted && "Subreg and Dst must be of compatible register class");
|
|
|
|
}
|
2009-08-03 20:08:18 +00:00
|
|
|
MachineBasicBlock::iterator CopyMI = MI;
|
|
|
|
--CopyMI;
|
|
|
|
|
Remove RegisterScavenger::isSuperRegUsed(). This completely reverses the mistaken commit r77904.
Now there is no special treatment of instructions that redefine part of a
super-register. Instead, the super-register is marked with <imp-use,kill> and
<imp-def>. For instance, from LowerSubregs on ARM:
subreg: CONVERTING: %Q1<def> = INSERT_SUBREG %Q1<undef>, %D1<kill>, 5
subreg: %D2<def> = FCPYD %D1<kill>, 14, %reg0, %Q1<imp-def>
subreg: CONVERTING: %Q1<def> = INSERT_SUBREG %Q1, %D0<kill>, 6
subreg: %D3<def> = FCPYD %D0<kill>, 14, %reg0, %Q1<imp-use,kill>, %Q1<imp-def>
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78466 91177308-0d34-0410-b5e6-96231b3b80d8
2009-08-08 13:19:10 +00:00
|
|
|
// INSERT_SUBREG is a two-address instruction so it implicitly kills SrcReg.
|
|
|
|
if (!MI->getOperand(1).isUndef())
|
|
|
|
CopyMI->addOperand(MachineOperand::CreateReg(DstReg, false, true, true));
|
|
|
|
|
2008-12-18 22:14:08 +00:00
|
|
|
// Transfer the kill/dead flags, if needed.
|
2009-08-03 20:08:18 +00:00
|
|
|
if (MI->getOperand(0).isDead()) {
|
2008-12-18 22:14:08 +00:00
|
|
|
TransferDeadFlag(MI, DstSubReg, TRI);
|
Remove RegisterScavenger::isSuperRegUsed(). This completely reverses the mistaken commit r77904.
Now there is no special treatment of instructions that redefine part of a
super-register. Instead, the super-register is marked with <imp-use,kill> and
<imp-def>. For instance, from LowerSubregs on ARM:
subreg: CONVERTING: %Q1<def> = INSERT_SUBREG %Q1<undef>, %D1<kill>, 5
subreg: %D2<def> = FCPYD %D1<kill>, 14, %reg0, %Q1<imp-def>
subreg: CONVERTING: %Q1<def> = INSERT_SUBREG %Q1, %D0<kill>, 6
subreg: %D3<def> = FCPYD %D0<kill>, 14, %reg0, %Q1<imp-use,kill>, %Q1<imp-def>
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78466 91177308-0d34-0410-b5e6-96231b3b80d8
2009-08-08 13:19:10 +00:00
|
|
|
} else {
|
|
|
|
// Make sure the full DstReg is live after this replacement.
|
2009-08-03 20:08:18 +00:00
|
|
|
CopyMI->addOperand(MachineOperand::CreateReg(DstReg, true, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the inserted register gets killed
|
2009-08-05 01:29:24 +00:00
|
|
|
if (MI->getOperand(2).isKill() && !MI->getOperand(2).isUndef())
|
2008-12-18 22:14:08 +00:00
|
|
|
TransferKillFlag(MI, InsReg, TRI);
|
2009-08-03 20:08:18 +00:00
|
|
|
}
|
2008-12-18 22:11:34 +00:00
|
|
|
|
2009-08-22 20:23:49 +00:00
|
|
|
DEBUG({
|
|
|
|
MachineBasicBlock::iterator dMI = MI;
|
2010-01-04 23:06:47 +00:00
|
|
|
dbgs() << "subreg: " << *(--dMI) << "\n";
|
2009-08-22 20:23:49 +00:00
|
|
|
});
|
2007-08-06 16:33:56 +00:00
|
|
|
|
2008-07-17 23:49:46 +00:00
|
|
|
MBB->erase(MI);
|
Remove RegisterScavenger::isSuperRegUsed(). This completely reverses the mistaken commit r77904.
Now there is no special treatment of instructions that redefine part of a
super-register. Instead, the super-register is marked with <imp-use,kill> and
<imp-def>. For instance, from LowerSubregs on ARM:
subreg: CONVERTING: %Q1<def> = INSERT_SUBREG %Q1<undef>, %D1<kill>, 5
subreg: %D2<def> = FCPYD %D1<kill>, 14, %reg0, %Q1<imp-def>
subreg: CONVERTING: %Q1<def> = INSERT_SUBREG %Q1, %D0<kill>, 6
subreg: %D3<def> = FCPYD %D0<kill>, 14, %reg0, %Q1<imp-use,kill>, %Q1<imp-def>
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78466 91177308-0d34-0410-b5e6-96231b3b80d8
2009-08-08 13:19:10 +00:00
|
|
|
return true;
|
2007-08-06 16:33:56 +00:00
|
|
|
}
|
2007-07-26 08:18:32 +00:00
|
|
|
|
|
|
|
/// runOnMachineFunction - Reduce subregister inserts and extracts to register
|
|
|
|
/// copies.
|
|
|
|
///
|
|
|
|
bool LowerSubregsInstructionPass::runOnMachineFunction(MachineFunction &MF) {
|
2010-01-04 23:06:47 +00:00
|
|
|
DEBUG(dbgs() << "Machine Function\n"
|
2009-08-22 20:23:49 +00:00
|
|
|
<< "********** LOWERING SUBREG INSTRS **********\n"
|
|
|
|
<< "********** Function: "
|
|
|
|
<< MF.getFunction()->getName() << '\n');
|
2009-10-25 07:49:57 +00:00
|
|
|
TRI = MF.getTarget().getRegisterInfo();
|
|
|
|
TII = MF.getTarget().getInstrInfo();
|
2007-07-26 08:18:32 +00:00
|
|
|
|
2009-08-22 20:23:49 +00:00
|
|
|
bool MadeChange = false;
|
2007-07-26 08:18:32 +00:00
|
|
|
|
|
|
|
for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
|
|
|
|
mbbi != mbbe; ++mbbi) {
|
|
|
|
for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
|
2007-08-06 16:33:56 +00:00
|
|
|
mi != me;) {
|
2009-12-03 00:50:42 +00:00
|
|
|
MachineBasicBlock::iterator nmi = llvm::next(mi);
|
2009-10-25 07:49:57 +00:00
|
|
|
MachineInstr *MI = mi;
|
2010-02-09 19:54:29 +00:00
|
|
|
if (MI->isExtractSubreg()) {
|
2007-08-06 16:33:56 +00:00
|
|
|
MadeChange |= LowerExtract(MI);
|
2010-02-09 19:54:29 +00:00
|
|
|
} else if (MI->isInsertSubreg()) {
|
2007-08-06 16:33:56 +00:00
|
|
|
MadeChange |= LowerInsert(MI);
|
2010-02-09 19:54:29 +00:00
|
|
|
} else if (MI->isSubregToReg()) {
|
2008-03-16 03:12:01 +00:00
|
|
|
MadeChange |= LowerSubregToReg(MI);
|
2007-07-26 08:18:32 +00:00
|
|
|
}
|
2009-10-25 07:49:57 +00:00
|
|
|
mi = nmi;
|
2007-07-26 08:18:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|