fix operand numbers
[oota-llvm.git] / lib / Target / X86 / X86InstrInfo.cpp
1 //===- X86InstrInfo.cpp - X86 Instruction Information -----------*- C++ -*-===//
2 //
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 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the X86 implementation of the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "X86InstrInfo.h"
15 #include "X86.h"
16 #include "X86InstrBuilder.h"
17 #include "llvm/CodeGen/MachineInstrBuilder.h"
18 #include "X86GenInstrInfo.inc"
19 using namespace llvm;
20
21 X86InstrInfo::X86InstrInfo()
22   : TargetInstrInfo(X86Insts, sizeof(X86Insts)/sizeof(X86Insts[0])) {
23 }
24
25
26 bool X86InstrInfo::isMoveInstr(const MachineInstr& MI,
27                                unsigned& sourceReg,
28                                unsigned& destReg) const {
29   MachineOpCode oc = MI.getOpcode();
30   if (oc == X86::MOV8rr || oc == X86::MOV16rr || oc == X86::MOV32rr ||
31       oc == X86::FpMOV  || oc == X86::MOVSSrr || oc == X86::MOVSDrr ||
32       oc == X86::MOVAPSrr || oc == X86::MOVAPDrr) {
33       assert(MI.getNumOperands() == 2 &&
34              MI.getOperand(0).isRegister() &&
35              MI.getOperand(1).isRegister() &&
36              "invalid register-register move instruction");
37       sourceReg = MI.getOperand(1).getReg();
38       destReg = MI.getOperand(0).getReg();
39       return true;
40   }
41   return false;
42 }
43
44 unsigned X86InstrInfo::isLoadFromStackSlot(MachineInstr *MI, 
45                                            int &FrameIndex) const {
46   switch (MI->getOpcode()) {
47   default: break;
48   case X86::MOV8rm:
49   case X86::MOV16rm:
50   case X86::MOV32rm:
51   case X86::FpLD64m:
52   case X86::MOVSSrm:
53   case X86::MOVSDrm:
54     if (MI->getOperand(1).isFrameIndex() && MI->getOperand(2).isImmediate() &&
55         MI->getOperand(3).isRegister() && MI->getOperand(4).isImmediate() &&
56         MI->getOperand(2).getImmedValue() == 1 &&
57         MI->getOperand(3).getReg() == 0 &&
58         MI->getOperand(4).getImmedValue() == 0) {
59       FrameIndex = MI->getOperand(1).getFrameIndex();
60       return MI->getOperand(0).getReg();
61     }
62     break;
63   }
64   return 0;
65 }
66
67 unsigned X86InstrInfo::isStoreToStackSlot(MachineInstr *MI,
68                                           int &FrameIndex) const {
69   switch (MI->getOpcode()) {
70   default: break;
71   case X86::MOV8mr:
72   case X86::MOV16mr:
73   case X86::MOV32mr:
74   case X86::FpSTP64m:
75   case X86::MOVSSmr:
76   case X86::MOVSDmr:
77     if (MI->getOperand(0).isFrameIndex() && MI->getOperand(1).isImmediate() &&
78         MI->getOperand(2).isRegister() && MI->getOperand(3).isImmediate() &&
79         MI->getOperand(1).getImmedValue() == 1 &&
80         MI->getOperand(2).getReg() == 0 &&
81         MI->getOperand(3).getImmedValue() == 0) {
82       FrameIndex = MI->getOperand(0).getFrameIndex();
83       return MI->getOperand(4).getReg();
84     }
85     break;
86   }
87   return 0;
88 }
89
90
91
92 /// convertToThreeAddress - This method must be implemented by targets that
93 /// set the M_CONVERTIBLE_TO_3_ADDR flag.  When this flag is set, the target
94 /// may be able to convert a two-address instruction into a true
95 /// three-address instruction on demand.  This allows the X86 target (for
96 /// example) to convert ADD and SHL instructions into LEA instructions if they
97 /// would require register copies due to two-addressness.
98 ///
99 /// This method returns a null pointer if the transformation cannot be
100 /// performed, otherwise it returns the new instruction.
101 ///
102 MachineInstr *X86InstrInfo::convertToThreeAddress(MachineInstr *MI) const {
103   // All instructions input are two-addr instructions.  Get the known operands.
104   unsigned Dest = MI->getOperand(0).getReg();
105   unsigned Src = MI->getOperand(1).getReg();
106
107   // FIXME: None of these instructions are promotable to LEAs without
108   // additional information.  In particular, LEA doesn't set the flags that
109   // add and inc do.  :(
110   return 0;
111
112   // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's.  When
113   // we have subtarget support, enable the 16-bit LEA generation here.
114   bool DisableLEA16 = true;
115
116   switch (MI->getOpcode()) {
117   case X86::INC32r:
118     assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
119     return addRegOffset(BuildMI(X86::LEA32r, 5, Dest), Src, 1);
120   case X86::INC16r:
121     if (DisableLEA16) return 0;
122     assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
123     return addRegOffset(BuildMI(X86::LEA16r, 5, Dest), Src, 1);
124   case X86::DEC32r:
125     assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
126     return addRegOffset(BuildMI(X86::LEA32r, 5, Dest), Src, -1);
127   case X86::DEC16r:
128     if (DisableLEA16) return 0;
129     assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
130     return addRegOffset(BuildMI(X86::LEA16r, 5, Dest), Src, -1);
131   case X86::ADD32rr:
132     assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
133     return addRegReg(BuildMI(X86::LEA32r, 5, Dest), Src,
134                      MI->getOperand(2).getReg());
135   case X86::ADD16rr:
136     if (DisableLEA16) return 0;
137     assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
138     return addRegReg(BuildMI(X86::LEA16r, 5, Dest), Src,
139                      MI->getOperand(2).getReg());
140   case X86::ADD32ri:
141     assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
142     if (MI->getOperand(2).isImmediate())
143       return addRegOffset(BuildMI(X86::LEA32r, 5, Dest), Src,
144                           MI->getOperand(2).getImmedValue());
145     return 0;
146   case X86::ADD16ri:
147     if (DisableLEA16) return 0;
148     assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
149     if (MI->getOperand(2).isImmediate())
150       return addRegOffset(BuildMI(X86::LEA16r, 5, Dest), Src,
151                           MI->getOperand(2).getImmedValue());
152     break;
153
154   case X86::SHL16ri:
155     if (DisableLEA16) return 0;
156   case X86::SHL32ri:
157     assert(MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate() &&
158            "Unknown shl instruction!");
159     unsigned ShAmt = MI->getOperand(2).getImmedValue();
160     if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) {
161       X86AddressMode AM;
162       AM.Scale = 1 << ShAmt;
163       AM.IndexReg = Src;
164       unsigned Opc = MI->getOpcode() == X86::SHL32ri ? X86::LEA32r :X86::LEA16r;
165       return addFullAddress(BuildMI(Opc, 5, Dest), AM);
166     }
167     break;
168   }
169
170   return 0;
171 }
172
173 /// commuteInstruction - We have a few instructions that must be hacked on to
174 /// commute them.
175 ///
176 MachineInstr *X86InstrInfo::commuteInstruction(MachineInstr *MI) const {
177   switch (MI->getOpcode()) {
178   case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
179   case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
180   case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
181   case X86::SHLD32rri8:{// A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
182     unsigned Opc;
183     unsigned Size;
184     switch (MI->getOpcode()) {
185     default: assert(0 && "Unreachable!");
186     case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
187     case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
188     case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
189     case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
190     }
191     unsigned Amt = MI->getOperand(3).getImmedValue();
192     unsigned A = MI->getOperand(0).getReg();
193     unsigned B = MI->getOperand(1).getReg();
194     unsigned C = MI->getOperand(2).getReg();
195     return BuildMI(Opc, 3, A).addReg(C).addReg(B).addImm(Size-Amt);
196   }
197   default:
198     return TargetInstrInfo::commuteInstruction(MI);
199   }
200 }
201
202
203 void X86InstrInfo::insertGoto(MachineBasicBlock& MBB,
204                               MachineBasicBlock& TMBB) const {
205   BuildMI(MBB, MBB.end(), X86::JMP, 1).addMBB(&TMBB);
206 }
207
208 MachineBasicBlock::iterator
209 X86InstrInfo::reverseBranchCondition(MachineBasicBlock::iterator MI) const {
210   unsigned Opcode = MI->getOpcode();
211   assert(isBranch(Opcode) && "MachineInstr must be a branch");
212   unsigned ROpcode;
213   switch (Opcode) {
214   default: assert(0 && "Cannot reverse unconditional branches!");
215   case X86::JB:  ROpcode = X86::JAE; break;
216   case X86::JAE: ROpcode = X86::JB;  break;
217   case X86::JE:  ROpcode = X86::JNE; break;
218   case X86::JNE: ROpcode = X86::JE;  break;
219   case X86::JBE: ROpcode = X86::JA;  break;
220   case X86::JA:  ROpcode = X86::JBE; break;
221   case X86::JS:  ROpcode = X86::JNS; break;
222   case X86::JNS: ROpcode = X86::JS;  break;
223   case X86::JP:  ROpcode = X86::JNP; break;
224   case X86::JNP: ROpcode = X86::JP;  break;
225   case X86::JL:  ROpcode = X86::JGE; break;
226   case X86::JGE: ROpcode = X86::JL;  break;
227   case X86::JLE: ROpcode = X86::JG;  break;
228   case X86::JG:  ROpcode = X86::JLE; break;
229   }
230   MachineBasicBlock* MBB = MI->getParent();
231   MachineBasicBlock* TMBB = MI->getOperand(0).getMachineBasicBlock();
232   return BuildMI(*MBB, MBB->erase(MI), ROpcode, 1).addMBB(TMBB);
233 }
234