blob: 65321616bf6d0f4277f4304bc7f80c7ece545967 [file] [log] [blame]
Misha Brukmana85d6bc2002-11-22 22:42:50 +00001//===- X86InstrInfo.cpp - X86 Instruction Information -----------*- C++ -*-===//
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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 Lattnerabf05b22003-08-03 21:55:55 +000016#include "X86GenInstrInfo.inc"
Evan Chengaa3c1412006-05-30 21:45:53 +000017#include "X86InstrBuilder.h"
18#include "X86Subtarget.h"
19#include "X86TargetMachine.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
Brian Gaeked0fde302003-11-11 22:41:34 +000021using namespace llvm;
22
Evan Chengaa3c1412006-05-30 21:45:53 +000023X86InstrInfo::X86InstrInfo(X86TargetMachine &tm)
24 : TargetInstrInfo(X86Insts, sizeof(X86Insts)/sizeof(X86Insts[0])),
Evan Cheng25ab6902006-09-08 06:48:29 +000025 TM(tm), RI(tm, *this) {
Chris Lattner72614082002-10-25 22:55:53 +000026}
27
Chris Lattnerae1dc402006-10-17 22:41:45 +000028/// getDWARF_LABELOpcode - Return the opcode of the target's DWARF_LABEL
29/// instruction if it has one. This is used by codegen passes that update
30/// DWARF line number info as they modify the code.
31unsigned X86InstrInfo::getDWARF_LABELOpcode() const {
32 return X86::DWARF_LABEL;
33}
34
Chris Lattner72614082002-10-25 22:55:53 +000035
Alkis Evlogimenos5e300022003-12-28 17:35:08 +000036bool X86InstrInfo::isMoveInstr(const MachineInstr& MI,
37 unsigned& sourceReg,
38 unsigned& destReg) const {
39 MachineOpCode oc = MI.getOpcode();
Evan Cheng25ab6902006-09-08 06:48:29 +000040 if (oc == X86::MOV8rr || oc == X86::MOV16rr ||
41 oc == X86::MOV32rr || oc == X86::MOV64rr ||
Evan Cheng403be7e2006-05-08 08:01:26 +000042 oc == X86::MOV16to16_ || oc == X86::MOV32to32_ ||
Evan Chengbda54cd2006-02-01 23:03:16 +000043 oc == X86::FpMOV || oc == X86::MOVSSrr || oc == X86::MOVSDrr ||
Evan Chengfe5cb192006-02-16 22:45:17 +000044 oc == X86::FsMOVAPSrr || oc == X86::FsMOVAPDrr ||
Evan Cheng82521dd2006-03-21 07:09:35 +000045 oc == X86::MOVAPSrr || oc == X86::MOVAPDrr ||
Evan Cheng11e15b32006-04-03 20:53:28 +000046 oc == X86::MOVSS2PSrr || oc == X86::MOVSD2PDrr ||
47 oc == X86::MOVPS2SSrr || oc == X86::MOVPD2SDrr ||
48 oc == X86::MOVDI2PDIrr || oc == X86::MOVQI2PQIrr ||
49 oc == X86::MOVPDI2DIrr) {
Alkis Evlogimenos5e300022003-12-28 17:35:08 +000050 assert(MI.getNumOperands() == 2 &&
51 MI.getOperand(0).isRegister() &&
52 MI.getOperand(1).isRegister() &&
53 "invalid register-register move instruction");
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +000054 sourceReg = MI.getOperand(1).getReg();
55 destReg = MI.getOperand(0).getReg();
Alkis Evlogimenos5e300022003-12-28 17:35:08 +000056 return true;
57 }
58 return false;
59}
Alkis Evlogimenos36f506e2004-07-31 09:38:47 +000060
Chris Lattner40839602006-02-02 20:12:32 +000061unsigned X86InstrInfo::isLoadFromStackSlot(MachineInstr *MI,
62 int &FrameIndex) const {
63 switch (MI->getOpcode()) {
64 default: break;
65 case X86::MOV8rm:
66 case X86::MOV16rm:
Evan Chengf4df6802006-05-11 07:33:49 +000067 case X86::MOV16_rm:
Chris Lattner40839602006-02-02 20:12:32 +000068 case X86::MOV32rm:
Evan Chengf4df6802006-05-11 07:33:49 +000069 case X86::MOV32_rm:
Evan Cheng25ab6902006-09-08 06:48:29 +000070 case X86::MOV64rm:
Chris Lattner40839602006-02-02 20:12:32 +000071 case X86::FpLD64m:
72 case X86::MOVSSrm:
73 case X86::MOVSDrm:
Chris Lattner993c8972006-04-18 16:44:51 +000074 case X86::MOVAPSrm:
75 case X86::MOVAPDrm:
Chris Lattner40839602006-02-02 20:12:32 +000076 if (MI->getOperand(1).isFrameIndex() && MI->getOperand(2).isImmediate() &&
77 MI->getOperand(3).isRegister() && MI->getOperand(4).isImmediate() &&
78 MI->getOperand(2).getImmedValue() == 1 &&
79 MI->getOperand(3).getReg() == 0 &&
80 MI->getOperand(4).getImmedValue() == 0) {
81 FrameIndex = MI->getOperand(1).getFrameIndex();
82 return MI->getOperand(0).getReg();
83 }
84 break;
85 }
86 return 0;
87}
88
89unsigned X86InstrInfo::isStoreToStackSlot(MachineInstr *MI,
90 int &FrameIndex) const {
91 switch (MI->getOpcode()) {
92 default: break;
93 case X86::MOV8mr:
94 case X86::MOV16mr:
Evan Chengf4df6802006-05-11 07:33:49 +000095 case X86::MOV16_mr:
Chris Lattner40839602006-02-02 20:12:32 +000096 case X86::MOV32mr:
Evan Chengf4df6802006-05-11 07:33:49 +000097 case X86::MOV32_mr:
Evan Cheng25ab6902006-09-08 06:48:29 +000098 case X86::MOV64mr:
Chris Lattner40839602006-02-02 20:12:32 +000099 case X86::FpSTP64m:
100 case X86::MOVSSmr:
101 case X86::MOVSDmr:
Chris Lattner993c8972006-04-18 16:44:51 +0000102 case X86::MOVAPSmr:
103 case X86::MOVAPDmr:
Chris Lattner40839602006-02-02 20:12:32 +0000104 if (MI->getOperand(0).isFrameIndex() && MI->getOperand(1).isImmediate() &&
105 MI->getOperand(2).isRegister() && MI->getOperand(3).isImmediate() &&
Chris Lattner1c07e722006-02-02 20:38:12 +0000106 MI->getOperand(1).getImmedValue() == 1 &&
107 MI->getOperand(2).getReg() == 0 &&
108 MI->getOperand(3).getImmedValue() == 0) {
109 FrameIndex = MI->getOperand(0).getFrameIndex();
Chris Lattner40839602006-02-02 20:12:32 +0000110 return MI->getOperand(4).getReg();
111 }
112 break;
113 }
114 return 0;
115}
116
117
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000118/// convertToThreeAddress - This method must be implemented by targets that
119/// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
120/// may be able to convert a two-address instruction into a true
121/// three-address instruction on demand. This allows the X86 target (for
122/// example) to convert ADD and SHL instructions into LEA instructions if they
123/// would require register copies due to two-addressness.
124///
125/// This method returns a null pointer if the transformation cannot be
126/// performed, otherwise it returns the new instruction.
127///
128MachineInstr *X86InstrInfo::convertToThreeAddress(MachineInstr *MI) const {
129 // All instructions input are two-addr instructions. Get the known operands.
130 unsigned Dest = MI->getOperand(0).getReg();
131 unsigned Src = MI->getOperand(1).getReg();
132
Evan Chengccba76b2006-05-30 20:26:50 +0000133 switch (MI->getOpcode()) {
134 default: break;
135 case X86::SHUFPSrri: {
136 assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!");
Evan Cheng51da42c2006-05-30 21:30:59 +0000137 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
Evan Chengaa3c1412006-05-30 21:45:53 +0000138 unsigned A = MI->getOperand(0).getReg();
139 unsigned B = MI->getOperand(1).getReg();
140 unsigned C = MI->getOperand(2).getReg();
141 unsigned M = MI->getOperand(3).getImmedValue();
Evan Chenga0eaf2d2006-05-30 22:13:36 +0000142 if (!Subtarget->hasSSE2() || B != C) return 0;
Evan Chengaa3c1412006-05-30 21:45:53 +0000143 return BuildMI(X86::PSHUFDri, 2, A).addReg(B).addImm(M);
Evan Chengccba76b2006-05-30 20:26:50 +0000144 }
145 }
146
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000147 // FIXME: None of these instructions are promotable to LEAs without
148 // additional information. In particular, LEA doesn't set the flags that
Chris Lattner5aee0b92005-01-02 04:18:17 +0000149 // add and inc do. :(
150 return 0;
151
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000152 // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When
153 // we have subtarget support, enable the 16-bit LEA generation here.
154 bool DisableLEA16 = true;
155
156 switch (MI->getOpcode()) {
157 case X86::INC32r:
Evan Cheng25ab6902006-09-08 06:48:29 +0000158 case X86::INC64_32r:
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000159 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
160 return addRegOffset(BuildMI(X86::LEA32r, 5, Dest), Src, 1);
161 case X86::INC16r:
Evan Cheng25ab6902006-09-08 06:48:29 +0000162 case X86::INC64_16r:
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000163 if (DisableLEA16) return 0;
164 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
165 return addRegOffset(BuildMI(X86::LEA16r, 5, Dest), Src, 1);
166 case X86::DEC32r:
Evan Cheng25ab6902006-09-08 06:48:29 +0000167 case X86::DEC64_32r:
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000168 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
169 return addRegOffset(BuildMI(X86::LEA32r, 5, Dest), Src, -1);
170 case X86::DEC16r:
Evan Cheng25ab6902006-09-08 06:48:29 +0000171 case X86::DEC64_16r:
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000172 if (DisableLEA16) return 0;
173 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
174 return addRegOffset(BuildMI(X86::LEA16r, 5, Dest), Src, -1);
175 case X86::ADD32rr:
176 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
177 return addRegReg(BuildMI(X86::LEA32r, 5, Dest), Src,
178 MI->getOperand(2).getReg());
179 case X86::ADD16rr:
180 if (DisableLEA16) return 0;
181 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
182 return addRegReg(BuildMI(X86::LEA16r, 5, Dest), Src,
183 MI->getOperand(2).getReg());
184 case X86::ADD32ri:
Evan Cheng6de01632006-05-19 18:43:41 +0000185 case X86::ADD32ri8:
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000186 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
187 if (MI->getOperand(2).isImmediate())
188 return addRegOffset(BuildMI(X86::LEA32r, 5, Dest), Src,
189 MI->getOperand(2).getImmedValue());
190 return 0;
191 case X86::ADD16ri:
Evan Cheng6de01632006-05-19 18:43:41 +0000192 case X86::ADD16ri8:
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000193 if (DisableLEA16) return 0;
194 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
195 if (MI->getOperand(2).isImmediate())
196 return addRegOffset(BuildMI(X86::LEA16r, 5, Dest), Src,
197 MI->getOperand(2).getImmedValue());
198 break;
199
200 case X86::SHL16ri:
201 if (DisableLEA16) return 0;
202 case X86::SHL32ri:
203 assert(MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate() &&
204 "Unknown shl instruction!");
205 unsigned ShAmt = MI->getOperand(2).getImmedValue();
206 if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) {
207 X86AddressMode AM;
208 AM.Scale = 1 << ShAmt;
209 AM.IndexReg = Src;
210 unsigned Opc = MI->getOpcode() == X86::SHL32ri ? X86::LEA32r :X86::LEA16r;
211 return addFullAddress(BuildMI(Opc, 5, Dest), AM);
212 }
213 break;
214 }
215
216 return 0;
217}
218
Chris Lattner41e431b2005-01-19 07:11:01 +0000219/// commuteInstruction - We have a few instructions that must be hacked on to
220/// commute them.
221///
222MachineInstr *X86InstrInfo::commuteInstruction(MachineInstr *MI) const {
Chris Lattner6458f182006-09-28 23:33:12 +0000223 // FIXME: Can commute cmoves by changing the condition!
Chris Lattner41e431b2005-01-19 07:11:01 +0000224 switch (MI->getOpcode()) {
Chris Lattner0df53d22005-01-19 07:31:24 +0000225 case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
226 case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
Chris Lattner41e431b2005-01-19 07:11:01 +0000227 case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
228 case X86::SHLD32rri8:{// A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
Chris Lattner0df53d22005-01-19 07:31:24 +0000229 unsigned Opc;
230 unsigned Size;
231 switch (MI->getOpcode()) {
232 default: assert(0 && "Unreachable!");
233 case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
234 case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
235 case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
236 case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
237 }
Chris Lattner41e431b2005-01-19 07:11:01 +0000238 unsigned Amt = MI->getOperand(3).getImmedValue();
239 unsigned A = MI->getOperand(0).getReg();
240 unsigned B = MI->getOperand(1).getReg();
241 unsigned C = MI->getOperand(2).getReg();
Chris Lattnera76f0482005-01-19 16:55:52 +0000242 return BuildMI(Opc, 3, A).addReg(C).addReg(B).addImm(Size-Amt);
Chris Lattner41e431b2005-01-19 07:11:01 +0000243 }
244 default:
245 return TargetInstrInfo::commuteInstruction(MI);
246 }
247}
248
Evan Cheng25ab6902006-09-08 06:48:29 +0000249const TargetRegisterClass *X86InstrInfo::getPointerRegClass() const {
250 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
251 if (Subtarget->is64Bit())
252 return &X86::GR64RegClass;
253 else
254 return &X86::GR32RegClass;
255}