2002-11-22 22:42:50 +00:00
|
|
|
//===- X86InstrInfo.cpp - X86 Instruction Information -----------*- C++ -*-===//
|
2005-04-21 23:38:14 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 23:38:14 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-10-25 22:55:53 +00:00
|
|
|
//
|
2003-01-14 22:00:31 +00:00
|
|
|
// This file contains the X86 implementation of the TargetInstrInfo class.
|
2002-10-25 22:55:53 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-10-29 21:05:24 +00:00
|
|
|
#include "X86InstrInfo.h"
|
2002-12-03 05:42:53 +00:00
|
|
|
#include "X86.h"
|
2005-01-02 02:37:07 +00:00
|
|
|
#include "X86InstrBuilder.h"
|
2003-05-24 00:09:50 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2003-08-03 21:55:55 +00:00
|
|
|
#include "X86GenInstrInfo.inc"
|
2003-11-11 22:41:34 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2002-10-29 21:05:24 +00:00
|
|
|
X86InstrInfo::X86InstrInfo()
|
2004-02-29 06:31:44 +00:00
|
|
|
: TargetInstrInfo(X86Insts, sizeof(X86Insts)/sizeof(X86Insts[0])) {
|
2002-10-25 22:55:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-28 17:35:08 +00:00
|
|
|
bool X86InstrInfo::isMoveInstr(const MachineInstr& MI,
|
|
|
|
unsigned& sourceReg,
|
|
|
|
unsigned& destReg) const {
|
|
|
|
MachineOpCode oc = MI.getOpcode();
|
A big X86 instruction rename. The instructions are renamed to make
their names more decriptive. A name consists of the base name, a
default operand size followed by a character per operand with an
optional special size. For example:
ADD8rr -> add, 8-bit register, 8-bit register
IMUL16rmi -> imul, 16-bit register, 16-bit memory, 16-bit immediate
IMUL16rmi8 -> imul, 16-bit register, 16-bit memory, 8-bit immediate
MOVSX32rm16 -> movsx, 32-bit register, 16-bit memory
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@11995 91177308-0d34-0410-b5e6-96231b3b80d8
2004-02-29 08:50:03 +00:00
|
|
|
if (oc == X86::MOV8rr || oc == X86::MOV16rr || oc == X86::MOV32rr ||
|
2005-10-14 22:06:00 +00:00
|
|
|
oc == X86::FpMOV || oc == X86::MOVSSrr || oc == X86::MOVSDrr) {
|
2003-12-28 17:35:08 +00:00
|
|
|
assert(MI.getNumOperands() == 2 &&
|
|
|
|
MI.getOperand(0).isRegister() &&
|
|
|
|
MI.getOperand(1).isRegister() &&
|
|
|
|
"invalid register-register move instruction");
|
2004-02-13 21:01:20 +00:00
|
|
|
sourceReg = MI.getOperand(1).getReg();
|
|
|
|
destReg = MI.getOperand(0).getReg();
|
2003-12-28 17:35:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2004-07-31 09:38:47 +00:00
|
|
|
|
2005-01-02 02:37:07 +00:00
|
|
|
/// convertToThreeAddress - This method must be implemented by targets that
|
|
|
|
/// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
|
|
|
|
/// may be able to convert a two-address instruction into a true
|
|
|
|
/// three-address instruction on demand. This allows the X86 target (for
|
|
|
|
/// example) to convert ADD and SHL instructions into LEA instructions if they
|
|
|
|
/// would require register copies due to two-addressness.
|
|
|
|
///
|
|
|
|
/// This method returns a null pointer if the transformation cannot be
|
|
|
|
/// performed, otherwise it returns the new instruction.
|
|
|
|
///
|
|
|
|
MachineInstr *X86InstrInfo::convertToThreeAddress(MachineInstr *MI) const {
|
|
|
|
// All instructions input are two-addr instructions. Get the known operands.
|
|
|
|
unsigned Dest = MI->getOperand(0).getReg();
|
|
|
|
unsigned Src = MI->getOperand(1).getReg();
|
|
|
|
|
2005-04-21 23:38:14 +00:00
|
|
|
// FIXME: None of these instructions are promotable to LEAs without
|
|
|
|
// additional information. In particular, LEA doesn't set the flags that
|
2005-01-02 04:18:17 +00:00
|
|
|
// add and inc do. :(
|
|
|
|
return 0;
|
|
|
|
|
2005-01-02 02:37:07 +00:00
|
|
|
// FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When
|
|
|
|
// we have subtarget support, enable the 16-bit LEA generation here.
|
|
|
|
bool DisableLEA16 = true;
|
|
|
|
|
|
|
|
switch (MI->getOpcode()) {
|
|
|
|
case X86::INC32r:
|
|
|
|
assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
|
|
|
|
return addRegOffset(BuildMI(X86::LEA32r, 5, Dest), Src, 1);
|
|
|
|
case X86::INC16r:
|
|
|
|
if (DisableLEA16) return 0;
|
|
|
|
assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
|
|
|
|
return addRegOffset(BuildMI(X86::LEA16r, 5, Dest), Src, 1);
|
|
|
|
case X86::DEC32r:
|
|
|
|
assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
|
|
|
|
return addRegOffset(BuildMI(X86::LEA32r, 5, Dest), Src, -1);
|
|
|
|
case X86::DEC16r:
|
|
|
|
if (DisableLEA16) return 0;
|
|
|
|
assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
|
|
|
|
return addRegOffset(BuildMI(X86::LEA16r, 5, Dest), Src, -1);
|
|
|
|
case X86::ADD32rr:
|
|
|
|
assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
|
|
|
|
return addRegReg(BuildMI(X86::LEA32r, 5, Dest), Src,
|
|
|
|
MI->getOperand(2).getReg());
|
|
|
|
case X86::ADD16rr:
|
|
|
|
if (DisableLEA16) return 0;
|
|
|
|
assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
|
|
|
|
return addRegReg(BuildMI(X86::LEA16r, 5, Dest), Src,
|
|
|
|
MI->getOperand(2).getReg());
|
|
|
|
case X86::ADD32ri:
|
|
|
|
assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
|
|
|
|
if (MI->getOperand(2).isImmediate())
|
|
|
|
return addRegOffset(BuildMI(X86::LEA32r, 5, Dest), Src,
|
|
|
|
MI->getOperand(2).getImmedValue());
|
|
|
|
return 0;
|
|
|
|
case X86::ADD16ri:
|
|
|
|
if (DisableLEA16) return 0;
|
|
|
|
assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
|
|
|
|
if (MI->getOperand(2).isImmediate())
|
|
|
|
return addRegOffset(BuildMI(X86::LEA16r, 5, Dest), Src,
|
|
|
|
MI->getOperand(2).getImmedValue());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case X86::SHL16ri:
|
|
|
|
if (DisableLEA16) return 0;
|
|
|
|
case X86::SHL32ri:
|
|
|
|
assert(MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate() &&
|
|
|
|
"Unknown shl instruction!");
|
|
|
|
unsigned ShAmt = MI->getOperand(2).getImmedValue();
|
|
|
|
if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) {
|
|
|
|
X86AddressMode AM;
|
|
|
|
AM.Scale = 1 << ShAmt;
|
|
|
|
AM.IndexReg = Src;
|
|
|
|
unsigned Opc = MI->getOpcode() == X86::SHL32ri ? X86::LEA32r :X86::LEA16r;
|
|
|
|
return addFullAddress(BuildMI(Opc, 5, Dest), AM);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
Teach the code generator that shrd/shld is commutable if it has an immediate.
This allows us to generate this:
foo:
mov %EAX, DWORD PTR [%ESP + 4]
mov %EDX, DWORD PTR [%ESP + 8]
shld %EDX, %EDX, 2
shl %EAX, 2
ret
instead of this:
foo:
mov %EAX, DWORD PTR [%ESP + 4]
mov %ECX, DWORD PTR [%ESP + 8]
mov %EDX, %EAX
shrd %EDX, %ECX, 30
shl %EAX, 2
ret
Note the magically transmogrifying immediate.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@19686 91177308-0d34-0410-b5e6-96231b3b80d8
2005-01-19 07:11:01 +00:00
|
|
|
/// commuteInstruction - We have a few instructions that must be hacked on to
|
|
|
|
/// commute them.
|
|
|
|
///
|
|
|
|
MachineInstr *X86InstrInfo::commuteInstruction(MachineInstr *MI) const {
|
|
|
|
switch (MI->getOpcode()) {
|
2005-01-19 07:31:24 +00:00
|
|
|
case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
|
|
|
|
case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
|
Teach the code generator that shrd/shld is commutable if it has an immediate.
This allows us to generate this:
foo:
mov %EAX, DWORD PTR [%ESP + 4]
mov %EDX, DWORD PTR [%ESP + 8]
shld %EDX, %EDX, 2
shl %EAX, 2
ret
instead of this:
foo:
mov %EAX, DWORD PTR [%ESP + 4]
mov %ECX, DWORD PTR [%ESP + 8]
mov %EDX, %EAX
shrd %EDX, %ECX, 30
shl %EAX, 2
ret
Note the magically transmogrifying immediate.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@19686 91177308-0d34-0410-b5e6-96231b3b80d8
2005-01-19 07:11:01 +00:00
|
|
|
case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
|
|
|
|
case X86::SHLD32rri8:{// A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
|
2005-01-19 07:31:24 +00:00
|
|
|
unsigned Opc;
|
|
|
|
unsigned Size;
|
|
|
|
switch (MI->getOpcode()) {
|
|
|
|
default: assert(0 && "Unreachable!");
|
|
|
|
case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
|
|
|
|
case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
|
|
|
|
case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
|
|
|
|
case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
|
|
|
|
}
|
Teach the code generator that shrd/shld is commutable if it has an immediate.
This allows us to generate this:
foo:
mov %EAX, DWORD PTR [%ESP + 4]
mov %EDX, DWORD PTR [%ESP + 8]
shld %EDX, %EDX, 2
shl %EAX, 2
ret
instead of this:
foo:
mov %EAX, DWORD PTR [%ESP + 4]
mov %ECX, DWORD PTR [%ESP + 8]
mov %EDX, %EAX
shrd %EDX, %ECX, 30
shl %EAX, 2
ret
Note the magically transmogrifying immediate.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@19686 91177308-0d34-0410-b5e6-96231b3b80d8
2005-01-19 07:11:01 +00:00
|
|
|
unsigned Amt = MI->getOperand(3).getImmedValue();
|
|
|
|
unsigned A = MI->getOperand(0).getReg();
|
|
|
|
unsigned B = MI->getOperand(1).getReg();
|
|
|
|
unsigned C = MI->getOperand(2).getReg();
|
2005-01-19 16:55:52 +00:00
|
|
|
return BuildMI(Opc, 3, A).addReg(C).addReg(B).addImm(Size-Amt);
|
Teach the code generator that shrd/shld is commutable if it has an immediate.
This allows us to generate this:
foo:
mov %EAX, DWORD PTR [%ESP + 4]
mov %EDX, DWORD PTR [%ESP + 8]
shld %EDX, %EDX, 2
shl %EAX, 2
ret
instead of this:
foo:
mov %EAX, DWORD PTR [%ESP + 4]
mov %ECX, DWORD PTR [%ESP + 8]
mov %EDX, %EAX
shrd %EDX, %ECX, 30
shl %EAX, 2
ret
Note the magically transmogrifying immediate.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@19686 91177308-0d34-0410-b5e6-96231b3b80d8
2005-01-19 07:11:01 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return TargetInstrInfo::commuteInstruction(MI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-02 02:37:07 +00:00
|
|
|
|
2004-07-31 09:38:47 +00:00
|
|
|
void X86InstrInfo::insertGoto(MachineBasicBlock& MBB,
|
|
|
|
MachineBasicBlock& TMBB) const {
|
|
|
|
BuildMI(MBB, MBB.end(), X86::JMP, 1).addMBB(&TMBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator
|
|
|
|
X86InstrInfo::reverseBranchCondition(MachineBasicBlock::iterator MI) const {
|
|
|
|
unsigned Opcode = MI->getOpcode();
|
|
|
|
assert(isBranch(Opcode) && "MachineInstr must be a branch");
|
|
|
|
unsigned ROpcode;
|
|
|
|
switch (Opcode) {
|
2004-08-01 19:31:30 +00:00
|
|
|
default: assert(0 && "Cannot reverse unconditional branches!");
|
2004-07-31 09:53:31 +00:00
|
|
|
case X86::JB: ROpcode = X86::JAE; break;
|
2004-07-31 10:05:44 +00:00
|
|
|
case X86::JAE: ROpcode = X86::JB; break;
|
2004-07-31 09:53:31 +00:00
|
|
|
case X86::JE: ROpcode = X86::JNE; break;
|
2004-07-31 10:05:44 +00:00
|
|
|
case X86::JNE: ROpcode = X86::JE; break;
|
|
|
|
case X86::JBE: ROpcode = X86::JA; break;
|
2004-07-31 09:53:31 +00:00
|
|
|
case X86::JA: ROpcode = X86::JBE; break;
|
|
|
|
case X86::JS: ROpcode = X86::JNS; break;
|
2004-07-31 10:05:44 +00:00
|
|
|
case X86::JNS: ROpcode = X86::JS; break;
|
2005-01-02 02:37:07 +00:00
|
|
|
case X86::JP: ROpcode = X86::JNP; break;
|
|
|
|
case X86::JNP: ROpcode = X86::JP; break;
|
2004-07-31 09:53:31 +00:00
|
|
|
case X86::JL: ROpcode = X86::JGE; break;
|
2004-07-31 10:05:44 +00:00
|
|
|
case X86::JGE: ROpcode = X86::JL; break;
|
|
|
|
case X86::JLE: ROpcode = X86::JG; break;
|
2004-07-31 09:53:31 +00:00
|
|
|
case X86::JG: ROpcode = X86::JLE; break;
|
2004-07-31 09:38:47 +00:00
|
|
|
}
|
|
|
|
MachineBasicBlock* MBB = MI->getParent();
|
|
|
|
MachineBasicBlock* TMBB = MI->getOperand(0).getMachineBasicBlock();
|
2004-07-31 09:44:32 +00:00
|
|
|
return BuildMI(*MBB, MBB->erase(MI), ROpcode, 1).addMBB(TMBB);
|
2004-07-31 09:38:47 +00:00
|
|
|
}
|
2005-01-02 02:37:07 +00:00
|
|
|
|