blob: 35782a48f03a468425e6637180e958cfbf035679 [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
Chris Lattner5aee0b92005-01-02 04:18:17 +000058 // FIXME: None of these instructions are promotable to LEAs without
59 // additional information. In particular, LEA doesn't set the flags that
60 // add and inc do. :(
61 return 0;
62
Chris Lattnerbcea4d62005-01-02 02:37:07 +000063 // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When
64 // we have subtarget support, enable the 16-bit LEA generation here.
65 bool DisableLEA16 = true;
66
67 switch (MI->getOpcode()) {
68 case X86::INC32r:
69 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
70 return addRegOffset(BuildMI(X86::LEA32r, 5, Dest), Src, 1);
71 case X86::INC16r:
72 if (DisableLEA16) return 0;
73 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
74 return addRegOffset(BuildMI(X86::LEA16r, 5, Dest), Src, 1);
75 case X86::DEC32r:
76 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
77 return addRegOffset(BuildMI(X86::LEA32r, 5, Dest), Src, -1);
78 case X86::DEC16r:
79 if (DisableLEA16) return 0;
80 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
81 return addRegOffset(BuildMI(X86::LEA16r, 5, Dest), Src, -1);
82 case X86::ADD32rr:
83 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
84 return addRegReg(BuildMI(X86::LEA32r, 5, Dest), Src,
85 MI->getOperand(2).getReg());
86 case X86::ADD16rr:
87 if (DisableLEA16) return 0;
88 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
89 return addRegReg(BuildMI(X86::LEA16r, 5, Dest), Src,
90 MI->getOperand(2).getReg());
91 case X86::ADD32ri:
92 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
93 if (MI->getOperand(2).isImmediate())
94 return addRegOffset(BuildMI(X86::LEA32r, 5, Dest), Src,
95 MI->getOperand(2).getImmedValue());
96 return 0;
97 case X86::ADD16ri:
98 if (DisableLEA16) return 0;
99 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
100 if (MI->getOperand(2).isImmediate())
101 return addRegOffset(BuildMI(X86::LEA16r, 5, Dest), Src,
102 MI->getOperand(2).getImmedValue());
103 break;
104
105 case X86::SHL16ri:
106 if (DisableLEA16) return 0;
107 case X86::SHL32ri:
108 assert(MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate() &&
109 "Unknown shl instruction!");
110 unsigned ShAmt = MI->getOperand(2).getImmedValue();
111 if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) {
112 X86AddressMode AM;
113 AM.Scale = 1 << ShAmt;
114 AM.IndexReg = Src;
115 unsigned Opc = MI->getOpcode() == X86::SHL32ri ? X86::LEA32r :X86::LEA16r;
116 return addFullAddress(BuildMI(Opc, 5, Dest), AM);
117 }
118 break;
119 }
120
121 return 0;
122}
123
Chris Lattner41e431b2005-01-19 07:11:01 +0000124/// commuteInstruction - We have a few instructions that must be hacked on to
125/// commute them.
126///
127MachineInstr *X86InstrInfo::commuteInstruction(MachineInstr *MI) const {
128 switch (MI->getOpcode()) {
Chris Lattner0df53d22005-01-19 07:31:24 +0000129 case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
130 case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
Chris Lattner41e431b2005-01-19 07:11:01 +0000131 case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
132 case X86::SHLD32rri8:{// A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
Chris Lattner0df53d22005-01-19 07:31:24 +0000133 unsigned Opc;
134 unsigned Size;
135 switch (MI->getOpcode()) {
136 default: assert(0 && "Unreachable!");
137 case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
138 case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
139 case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
140 case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
141 }
Chris Lattner41e431b2005-01-19 07:11:01 +0000142 unsigned Amt = MI->getOperand(3).getImmedValue();
143 unsigned A = MI->getOperand(0).getReg();
144 unsigned B = MI->getOperand(1).getReg();
145 unsigned C = MI->getOperand(2).getReg();
Chris Lattnera76f0482005-01-19 16:55:52 +0000146 return BuildMI(Opc, 3, A).addReg(C).addReg(B).addImm(Size-Amt);
Chris Lattner41e431b2005-01-19 07:11:01 +0000147 }
148 default:
149 return TargetInstrInfo::commuteInstruction(MI);
150 }
151}
152
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000153
Alkis Evlogimenos36f506e2004-07-31 09:38:47 +0000154void X86InstrInfo::insertGoto(MachineBasicBlock& MBB,
155 MachineBasicBlock& TMBB) const {
156 BuildMI(MBB, MBB.end(), X86::JMP, 1).addMBB(&TMBB);
157}
158
159MachineBasicBlock::iterator
160X86InstrInfo::reverseBranchCondition(MachineBasicBlock::iterator MI) const {
161 unsigned Opcode = MI->getOpcode();
162 assert(isBranch(Opcode) && "MachineInstr must be a branch");
163 unsigned ROpcode;
164 switch (Opcode) {
Chris Lattnerbcdda012004-08-01 19:31:30 +0000165 default: assert(0 && "Cannot reverse unconditional branches!");
Chris Lattner167cf332004-07-31 09:53:31 +0000166 case X86::JB: ROpcode = X86::JAE; break;
Alkis Evlogimenos31e155e2004-07-31 10:05:44 +0000167 case X86::JAE: ROpcode = X86::JB; break;
Chris Lattner167cf332004-07-31 09:53:31 +0000168 case X86::JE: ROpcode = X86::JNE; break;
Alkis Evlogimenos31e155e2004-07-31 10:05:44 +0000169 case X86::JNE: ROpcode = X86::JE; break;
170 case X86::JBE: ROpcode = X86::JA; break;
Chris Lattner167cf332004-07-31 09:53:31 +0000171 case X86::JA: ROpcode = X86::JBE; break;
172 case X86::JS: ROpcode = X86::JNS; break;
Alkis Evlogimenos31e155e2004-07-31 10:05:44 +0000173 case X86::JNS: ROpcode = X86::JS; break;
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000174 case X86::JP: ROpcode = X86::JNP; break;
175 case X86::JNP: ROpcode = X86::JP; break;
Chris Lattner167cf332004-07-31 09:53:31 +0000176 case X86::JL: ROpcode = X86::JGE; break;
Alkis Evlogimenos31e155e2004-07-31 10:05:44 +0000177 case X86::JGE: ROpcode = X86::JL; break;
178 case X86::JLE: ROpcode = X86::JG; break;
Chris Lattner167cf332004-07-31 09:53:31 +0000179 case X86::JG: ROpcode = X86::JLE; break;
Alkis Evlogimenos36f506e2004-07-31 09:38:47 +0000180 }
181 MachineBasicBlock* MBB = MI->getParent();
182 MachineBasicBlock* TMBB = MI->getOperand(0).getMachineBasicBlock();
Alkis Evlogimenos6103c172004-07-31 09:44:32 +0000183 return BuildMI(*MBB, MBB->erase(MI), ROpcode, 1).addMBB(TMBB);
Alkis Evlogimenos36f506e2004-07-31 09:38:47 +0000184}
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000185