1 //===- X86InstrInfo.cpp - X86 Instruction Information -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file contains the X86 implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "X86InstrInfo.h"
16 #include "X86InstrBuilder.h"
17 #include "llvm/CodeGen/MachineInstrBuilder.h"
18 #include "X86GenInstrInfo.inc"
21 X86InstrInfo::X86InstrInfo()
22 : TargetInstrInfo(X86Insts, sizeof(X86Insts)/sizeof(X86Insts[0])) {
26 bool X86InstrInfo::isMoveInstr(const MachineInstr& MI,
28 unsigned& destReg) const {
29 MachineOpCode oc = MI.getOpcode();
30 if (oc == X86::MOV8rr || oc == X86::MOV16rr || oc == X86::MOV32rr ||
31 oc == X86::MOV16to16_ || oc == X86::MOV32to32_ ||
32 oc == X86::FpMOV || oc == X86::MOVSSrr || oc == X86::MOVSDrr ||
33 oc == X86::FsMOVAPSrr || oc == X86::FsMOVAPDrr ||
34 oc == X86::MOVAPSrr || oc == X86::MOVAPDrr ||
35 oc == X86::MOVSS2PSrr || oc == X86::MOVSD2PDrr ||
36 oc == X86::MOVPS2SSrr || oc == X86::MOVPD2SDrr ||
37 oc == X86::MOVDI2PDIrr || oc == X86::MOVQI2PQIrr ||
38 oc == X86::MOVPDI2DIrr) {
39 assert(MI.getNumOperands() == 2 &&
40 MI.getOperand(0).isRegister() &&
41 MI.getOperand(1).isRegister() &&
42 "invalid register-register move instruction");
43 sourceReg = MI.getOperand(1).getReg();
44 destReg = MI.getOperand(0).getReg();
50 unsigned X86InstrInfo::isLoadFromStackSlot(MachineInstr *MI,
51 int &FrameIndex) const {
52 switch (MI->getOpcode()) {
62 if (MI->getOperand(1).isFrameIndex() && MI->getOperand(2).isImmediate() &&
63 MI->getOperand(3).isRegister() && MI->getOperand(4).isImmediate() &&
64 MI->getOperand(2).getImmedValue() == 1 &&
65 MI->getOperand(3).getReg() == 0 &&
66 MI->getOperand(4).getImmedValue() == 0) {
67 FrameIndex = MI->getOperand(1).getFrameIndex();
68 return MI->getOperand(0).getReg();
75 unsigned X86InstrInfo::isStoreToStackSlot(MachineInstr *MI,
76 int &FrameIndex) const {
77 switch (MI->getOpcode()) {
87 if (MI->getOperand(0).isFrameIndex() && MI->getOperand(1).isImmediate() &&
88 MI->getOperand(2).isRegister() && MI->getOperand(3).isImmediate() &&
89 MI->getOperand(1).getImmedValue() == 1 &&
90 MI->getOperand(2).getReg() == 0 &&
91 MI->getOperand(3).getImmedValue() == 0) {
92 FrameIndex = MI->getOperand(0).getFrameIndex();
93 return MI->getOperand(4).getReg();
102 /// convertToThreeAddress - This method must be implemented by targets that
103 /// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
104 /// may be able to convert a two-address instruction into a true
105 /// three-address instruction on demand. This allows the X86 target (for
106 /// example) to convert ADD and SHL instructions into LEA instructions if they
107 /// would require register copies due to two-addressness.
109 /// This method returns a null pointer if the transformation cannot be
110 /// performed, otherwise it returns the new instruction.
112 MachineInstr *X86InstrInfo::convertToThreeAddress(MachineInstr *MI) const {
113 // All instructions input are two-addr instructions. Get the known operands.
114 unsigned Dest = MI->getOperand(0).getReg();
115 unsigned Src = MI->getOperand(1).getReg();
117 // FIXME: None of these instructions are promotable to LEAs without
118 // additional information. In particular, LEA doesn't set the flags that
119 // add and inc do. :(
122 // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When
123 // we have subtarget support, enable the 16-bit LEA generation here.
124 bool DisableLEA16 = true;
126 switch (MI->getOpcode()) {
128 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
129 return addRegOffset(BuildMI(X86::LEA32r, 5, Dest), Src, 1);
131 if (DisableLEA16) return 0;
132 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
133 return addRegOffset(BuildMI(X86::LEA16r, 5, Dest), Src, 1);
135 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
136 return addRegOffset(BuildMI(X86::LEA32r, 5, Dest), Src, -1);
138 if (DisableLEA16) return 0;
139 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
140 return addRegOffset(BuildMI(X86::LEA16r, 5, Dest), Src, -1);
142 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
143 return addRegReg(BuildMI(X86::LEA32r, 5, Dest), Src,
144 MI->getOperand(2).getReg());
146 if (DisableLEA16) return 0;
147 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
148 return addRegReg(BuildMI(X86::LEA16r, 5, Dest), Src,
149 MI->getOperand(2).getReg());
151 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
152 if (MI->getOperand(2).isImmediate())
153 return addRegOffset(BuildMI(X86::LEA32r, 5, Dest), Src,
154 MI->getOperand(2).getImmedValue());
157 if (DisableLEA16) return 0;
158 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
159 if (MI->getOperand(2).isImmediate())
160 return addRegOffset(BuildMI(X86::LEA16r, 5, Dest), Src,
161 MI->getOperand(2).getImmedValue());
165 if (DisableLEA16) return 0;
167 assert(MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate() &&
168 "Unknown shl instruction!");
169 unsigned ShAmt = MI->getOperand(2).getImmedValue();
170 if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) {
172 AM.Scale = 1 << ShAmt;
174 unsigned Opc = MI->getOpcode() == X86::SHL32ri ? X86::LEA32r :X86::LEA16r;
175 return addFullAddress(BuildMI(Opc, 5, Dest), AM);
183 /// commuteInstruction - We have a few instructions that must be hacked on to
186 MachineInstr *X86InstrInfo::commuteInstruction(MachineInstr *MI) const {
187 switch (MI->getOpcode()) {
188 case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
189 case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
190 case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
191 case X86::SHLD32rri8:{// A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
194 switch (MI->getOpcode()) {
195 default: assert(0 && "Unreachable!");
196 case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
197 case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
198 case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
199 case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
201 unsigned Amt = MI->getOperand(3).getImmedValue();
202 unsigned A = MI->getOperand(0).getReg();
203 unsigned B = MI->getOperand(1).getReg();
204 unsigned C = MI->getOperand(2).getReg();
205 return BuildMI(Opc, 3, A).addReg(C).addReg(B).addImm(Size-Amt);
208 return TargetInstrInfo::commuteInstruction(MI);
213 void X86InstrInfo::insertGoto(MachineBasicBlock& MBB,
214 MachineBasicBlock& TMBB) const {
215 BuildMI(MBB, MBB.end(), X86::JMP, 1).addMBB(&TMBB);
218 MachineBasicBlock::iterator
219 X86InstrInfo::reverseBranchCondition(MachineBasicBlock::iterator MI) const {
220 unsigned Opcode = MI->getOpcode();
221 assert(isBranch(Opcode) && "MachineInstr must be a branch");
224 default: assert(0 && "Cannot reverse unconditional branches!");
225 case X86::JB: ROpcode = X86::JAE; break;
226 case X86::JAE: ROpcode = X86::JB; break;
227 case X86::JE: ROpcode = X86::JNE; break;
228 case X86::JNE: ROpcode = X86::JE; break;
229 case X86::JBE: ROpcode = X86::JA; break;
230 case X86::JA: ROpcode = X86::JBE; break;
231 case X86::JS: ROpcode = X86::JNS; break;
232 case X86::JNS: ROpcode = X86::JS; break;
233 case X86::JP: ROpcode = X86::JNP; break;
234 case X86::JNP: ROpcode = X86::JP; break;
235 case X86::JL: ROpcode = X86::JGE; break;
236 case X86::JGE: ROpcode = X86::JL; break;
237 case X86::JLE: ROpcode = X86::JG; break;
238 case X86::JG: ROpcode = X86::JLE; break;
240 MachineBasicBlock* MBB = MI->getParent();
241 MachineBasicBlock* TMBB = MI->getOperand(0).getMachineBasicBlock();
242 return BuildMI(*MBB, MBB->erase(MI), ROpcode, 1).addMBB(TMBB);