blob: d588afe28af5dd445c766b3f4fd2a23e1b4815ab [file] [log] [blame]
Misha Brukmana85d6bc2002-11-22 22:42:50 +00001//===- X86InstrInfo.cpp - X86 Instruction Information -----------*- C++ -*-===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner72614082002-10-25 22:55:53 +00009//
Chris Lattner3501fea2003-01-14 22:00:31 +000010// This file contains the X86 implementation of the TargetInstrInfo class.
Chris Lattner72614082002-10-25 22:55:53 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner055c9652002-10-29 21:05:24 +000014#include "X86InstrInfo.h"
Chris Lattner4ce42a72002-12-03 05:42:53 +000015#include "X86.h"
Chris Lattnerbcea4d62005-01-02 02:37:07 +000016#include "X86InstrBuilder.h"
Misha Brukmane9d88382003-05-24 00:09:50 +000017#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerabf05b22003-08-03 21:55:55 +000018#include "X86GenInstrInfo.inc"
Brian Gaeked0fde302003-11-11 22:41:34 +000019using namespace llvm;
20
Chris Lattner055c9652002-10-29 21:05:24 +000021X86InstrInfo::X86InstrInfo()
Chris Lattnerdce363d2004-02-29 06:31:44 +000022 : TargetInstrInfo(X86Insts, sizeof(X86Insts)/sizeof(X86Insts[0])) {
Chris Lattner72614082002-10-25 22:55:53 +000023}
24
25
Alkis Evlogimenos5e300022003-12-28 17:35:08 +000026bool X86InstrInfo::isMoveInstr(const MachineInstr& MI,
27 unsigned& sourceReg,
28 unsigned& destReg) const {
29 MachineOpCode oc = MI.getOpcode();
Alkis Evlogimenos8295f202004-02-29 08:50:03 +000030 if (oc == X86::MOV8rr || oc == X86::MOV16rr || oc == X86::MOV32rr ||
Alkis Evlogimenosa1b6f952004-02-01 08:22:16 +000031 oc == X86::FpMOV) {
Alkis Evlogimenos5e300022003-12-28 17:35:08 +000032 assert(MI.getNumOperands() == 2 &&
33 MI.getOperand(0).isRegister() &&
34 MI.getOperand(1).isRegister() &&
35 "invalid register-register move instruction");
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +000036 sourceReg = MI.getOperand(1).getReg();
37 destReg = MI.getOperand(0).getReg();
Alkis Evlogimenos5e300022003-12-28 17:35:08 +000038 return true;
39 }
40 return false;
41}
Alkis Evlogimenos36f506e2004-07-31 09:38:47 +000042
Chris Lattnerbcea4d62005-01-02 02:37:07 +000043/// convertToThreeAddress - This method must be implemented by targets that
44/// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
45/// may be able to convert a two-address instruction into a true
46/// three-address instruction on demand. This allows the X86 target (for
47/// example) to convert ADD and SHL instructions into LEA instructions if they
48/// would require register copies due to two-addressness.
49///
50/// This method returns a null pointer if the transformation cannot be
51/// performed, otherwise it returns the new instruction.
52///
53MachineInstr *X86InstrInfo::convertToThreeAddress(MachineInstr *MI) const {
54 // All instructions input are two-addr instructions. Get the known operands.
55 unsigned Dest = MI->getOperand(0).getReg();
56 unsigned Src = MI->getOperand(1).getReg();
57
58 // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When
59 // we have subtarget support, enable the 16-bit LEA generation here.
60 bool DisableLEA16 = true;
61
62 switch (MI->getOpcode()) {
63 case X86::INC32r:
64 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
65 return addRegOffset(BuildMI(X86::LEA32r, 5, Dest), Src, 1);
66 case X86::INC16r:
67 if (DisableLEA16) return 0;
68 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
69 return addRegOffset(BuildMI(X86::LEA16r, 5, Dest), Src, 1);
70 case X86::DEC32r:
71 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
72 return addRegOffset(BuildMI(X86::LEA32r, 5, Dest), Src, -1);
73 case X86::DEC16r:
74 if (DisableLEA16) return 0;
75 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
76 return addRegOffset(BuildMI(X86::LEA16r, 5, Dest), Src, -1);
77 case X86::ADD32rr:
78 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
79 return addRegReg(BuildMI(X86::LEA32r, 5, Dest), Src,
80 MI->getOperand(2).getReg());
81 case X86::ADD16rr:
82 if (DisableLEA16) return 0;
83 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
84 return addRegReg(BuildMI(X86::LEA16r, 5, Dest), Src,
85 MI->getOperand(2).getReg());
86 case X86::ADD32ri:
87 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
88 if (MI->getOperand(2).isImmediate())
89 return addRegOffset(BuildMI(X86::LEA32r, 5, Dest), Src,
90 MI->getOperand(2).getImmedValue());
91 return 0;
92 case X86::ADD16ri:
93 if (DisableLEA16) return 0;
94 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
95 if (MI->getOperand(2).isImmediate())
96 return addRegOffset(BuildMI(X86::LEA16r, 5, Dest), Src,
97 MI->getOperand(2).getImmedValue());
98 break;
99
100 case X86::SHL16ri:
101 if (DisableLEA16) return 0;
102 case X86::SHL32ri:
103 assert(MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate() &&
104 "Unknown shl instruction!");
105 unsigned ShAmt = MI->getOperand(2).getImmedValue();
106 if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) {
107 X86AddressMode AM;
108 AM.Scale = 1 << ShAmt;
109 AM.IndexReg = Src;
110 unsigned Opc = MI->getOpcode() == X86::SHL32ri ? X86::LEA32r :X86::LEA16r;
111 return addFullAddress(BuildMI(Opc, 5, Dest), AM);
112 }
113 break;
114 }
115
116 return 0;
117}
118
119
Alkis Evlogimenos36f506e2004-07-31 09:38:47 +0000120void X86InstrInfo::insertGoto(MachineBasicBlock& MBB,
121 MachineBasicBlock& TMBB) const {
122 BuildMI(MBB, MBB.end(), X86::JMP, 1).addMBB(&TMBB);
123}
124
125MachineBasicBlock::iterator
126X86InstrInfo::reverseBranchCondition(MachineBasicBlock::iterator MI) const {
127 unsigned Opcode = MI->getOpcode();
128 assert(isBranch(Opcode) && "MachineInstr must be a branch");
129 unsigned ROpcode;
130 switch (Opcode) {
Chris Lattnerbcdda012004-08-01 19:31:30 +0000131 default: assert(0 && "Cannot reverse unconditional branches!");
Chris Lattner167cf332004-07-31 09:53:31 +0000132 case X86::JB: ROpcode = X86::JAE; break;
Alkis Evlogimenos31e155e2004-07-31 10:05:44 +0000133 case X86::JAE: ROpcode = X86::JB; break;
Chris Lattner167cf332004-07-31 09:53:31 +0000134 case X86::JE: ROpcode = X86::JNE; break;
Alkis Evlogimenos31e155e2004-07-31 10:05:44 +0000135 case X86::JNE: ROpcode = X86::JE; break;
136 case X86::JBE: ROpcode = X86::JA; break;
Chris Lattner167cf332004-07-31 09:53:31 +0000137 case X86::JA: ROpcode = X86::JBE; break;
138 case X86::JS: ROpcode = X86::JNS; break;
Alkis Evlogimenos31e155e2004-07-31 10:05:44 +0000139 case X86::JNS: ROpcode = X86::JS; break;
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000140 case X86::JP: ROpcode = X86::JNP; break;
141 case X86::JNP: ROpcode = X86::JP; break;
Chris Lattner167cf332004-07-31 09:53:31 +0000142 case X86::JL: ROpcode = X86::JGE; break;
Alkis Evlogimenos31e155e2004-07-31 10:05:44 +0000143 case X86::JGE: ROpcode = X86::JL; break;
144 case X86::JLE: ROpcode = X86::JG; break;
Chris Lattner167cf332004-07-31 09:53:31 +0000145 case X86::JG: ROpcode = X86::JLE; break;
Alkis Evlogimenos36f506e2004-07-31 09:38:47 +0000146 }
147 MachineBasicBlock* MBB = MI->getParent();
148 MachineBasicBlock* TMBB = MI->getOperand(0).getMachineBasicBlock();
Alkis Evlogimenos6103c172004-07-31 09:44:32 +0000149 return BuildMI(*MBB, MBB->erase(MI), ROpcode, 1).addMBB(TMBB);
Alkis Evlogimenos36f506e2004-07-31 09:38:47 +0000150}
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000151