8a1cb36290a5de7a8ba89e7f5d98bb6bc3c46f86
[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 "X86GenInstrInfo.inc"
17 #include "X86InstrBuilder.h"
18 #include "X86Subtarget.h"
19 #include "X86TargetMachine.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 using namespace llvm;
22
23 X86InstrInfo::X86InstrInfo(X86TargetMachine &tm)
24   : TargetInstrInfo(X86Insts, sizeof(X86Insts)/sizeof(X86Insts[0])),
25     TM(tm), RI(tm, *this) {
26 }
27
28 /// 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.
31 unsigned X86InstrInfo::getDWARF_LABELOpcode() const {
32   return X86::DWARF_LABEL;
33 }
34
35
36 bool X86InstrInfo::isMoveInstr(const MachineInstr& MI,
37                                unsigned& sourceReg,
38                                unsigned& destReg) const {
39   MachineOpCode oc = MI.getOpcode();
40   if (oc == X86::MOV8rr || oc == X86::MOV16rr ||
41       oc == X86::MOV32rr || oc == X86::MOV64rr ||
42       oc == X86::MOV16to16_ || oc == X86::MOV32to32_ ||
43       oc == X86::FpMOV  || oc == X86::MOVSSrr || oc == X86::MOVSDrr ||
44       oc == X86::FsMOVAPSrr || oc == X86::FsMOVAPDrr ||
45       oc == X86::MOVAPSrr || oc == X86::MOVAPDrr ||
46       oc == X86::MOVSS2PSrr || oc == X86::MOVSD2PDrr ||
47       oc == X86::MOVPS2SSrr || oc == X86::MOVPD2SDrr ||
48       oc == X86::MOVDI2PDIrr || oc == X86::MOVQI2PQIrr ||
49       oc == X86::MOVPDI2DIrr) {
50       assert(MI.getNumOperands() == 2 &&
51              MI.getOperand(0).isRegister() &&
52              MI.getOperand(1).isRegister() &&
53              "invalid register-register move instruction");
54       sourceReg = MI.getOperand(1).getReg();
55       destReg = MI.getOperand(0).getReg();
56       return true;
57   }
58   return false;
59 }
60
61 unsigned X86InstrInfo::isLoadFromStackSlot(MachineInstr *MI, 
62                                            int &FrameIndex) const {
63   switch (MI->getOpcode()) {
64   default: break;
65   case X86::MOV8rm:
66   case X86::MOV16rm:
67   case X86::MOV16_rm:
68   case X86::MOV32rm:
69   case X86::MOV32_rm:
70   case X86::MOV64rm:
71   case X86::FpLD64m:
72   case X86::MOVSSrm:
73   case X86::MOVSDrm:
74   case X86::MOVAPSrm:
75   case X86::MOVAPDrm:
76     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
89 unsigned X86InstrInfo::isStoreToStackSlot(MachineInstr *MI,
90                                           int &FrameIndex) const {
91   switch (MI->getOpcode()) {
92   default: break;
93   case X86::MOV8mr:
94   case X86::MOV16mr:
95   case X86::MOV16_mr:
96   case X86::MOV32mr:
97   case X86::MOV32_mr:
98   case X86::MOV64mr:
99   case X86::FpSTP64m:
100   case X86::MOVSSmr:
101   case X86::MOVSDmr:
102   case X86::MOVAPSmr:
103   case X86::MOVAPDmr:
104     if (MI->getOperand(0).isFrameIndex() && MI->getOperand(1).isImmediate() &&
105         MI->getOperand(2).isRegister() && MI->getOperand(3).isImmediate() &&
106         MI->getOperand(1).getImmedValue() == 1 &&
107         MI->getOperand(2).getReg() == 0 &&
108         MI->getOperand(3).getImmedValue() == 0) {
109       FrameIndex = MI->getOperand(0).getFrameIndex();
110       return MI->getOperand(4).getReg();
111     }
112     break;
113   }
114   return 0;
115 }
116
117
118 /// 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 ///
128 MachineInstr *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
133   MachineInstr *NewMI = NULL;
134   switch (MI->getOpcode()) {
135   default: break;
136   case X86::SHUFPSrri: {
137     assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!");
138     const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
139     unsigned A = MI->getOperand(0).getReg();
140     unsigned B = MI->getOperand(1).getReg();
141     unsigned C = MI->getOperand(2).getReg();
142     unsigned M = MI->getOperand(3).getImmedValue();
143     if (!Subtarget->hasSSE2() || B != C) return 0;
144     NewMI = BuildMI(*this, X86::PSHUFDri, 2, A).addReg(B).addImm(M);
145     NewMI->copyKillDeadInfo(MI);
146     return NewMI;
147   }
148   }
149
150   // FIXME: None of these instructions are promotable to LEAs without
151   // additional information.  In particular, LEA doesn't set the flags that
152   // add and inc do.  :(
153   return 0;
154
155   // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's.  When
156   // we have subtarget support, enable the 16-bit LEA generation here.
157   bool DisableLEA16 = true;
158
159   switch (MI->getOpcode()) {
160   case X86::INC32r:
161   case X86::INC64_32r:
162     assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
163     NewMI = addRegOffset(BuildMI(*this, X86::LEA32r, 5, Dest), Src, 1);
164     break;
165   case X86::INC16r:
166   case X86::INC64_16r:
167     if (DisableLEA16) return 0;
168     assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
169     NewMI = addRegOffset(BuildMI(*this, X86::LEA16r, 5, Dest), Src, 1);
170     break;
171   case X86::DEC32r:
172   case X86::DEC64_32r:
173     assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
174     NewMI = addRegOffset(BuildMI(*this, X86::LEA32r, 5, Dest), Src, -1);
175     break;
176   case X86::DEC16r:
177   case X86::DEC64_16r:
178     if (DisableLEA16) return 0;
179     assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
180     NewMI = addRegOffset(BuildMI(*this, X86::LEA16r, 5, Dest), Src, -1);
181     break;
182   case X86::ADD32rr:
183     assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
184     NewMI = addRegReg(BuildMI(*this, X86::LEA32r, 5, Dest), Src,
185                      MI->getOperand(2).getReg());
186     break;
187   case X86::ADD16rr:
188     if (DisableLEA16) return 0;
189     assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
190     NewMI = addRegReg(BuildMI(*this, X86::LEA16r, 5, Dest), Src,
191                      MI->getOperand(2).getReg());
192     break;
193   case X86::ADD32ri:
194   case X86::ADD32ri8:
195     assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
196     if (MI->getOperand(2).isImmediate())
197       NewMI = addRegOffset(BuildMI(*this, X86::LEA32r, 5, Dest), Src,
198                           MI->getOperand(2).getImmedValue());
199     break;
200   case X86::ADD16ri:
201   case X86::ADD16ri8:
202     if (DisableLEA16) return 0;
203     assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
204     if (MI->getOperand(2).isImmediate())
205       NewMI = addRegOffset(BuildMI(*this, X86::LEA16r, 5, Dest), Src,
206                           MI->getOperand(2).getImmedValue());
207     break;
208   case X86::SHL16ri:
209     if (DisableLEA16) return 0;
210   case X86::SHL32ri:
211     assert(MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate() &&
212            "Unknown shl instruction!");
213     unsigned ShAmt = MI->getOperand(2).getImmedValue();
214     if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) {
215       X86AddressMode AM;
216       AM.Scale = 1 << ShAmt;
217       AM.IndexReg = Src;
218       unsigned Opc = MI->getOpcode() == X86::SHL32ri ? X86::LEA32r :X86::LEA16r;
219       NewMI = addFullAddress(BuildMI(*this, Opc, 5, Dest), AM);
220     }
221     break;
222   }
223
224   if (NewMI)
225     NewMI->copyKillDeadInfo(MI);
226   return NewMI;
227 }
228
229 /// commuteInstruction - We have a few instructions that must be hacked on to
230 /// commute them.
231 ///
232 MachineInstr *X86InstrInfo::commuteInstruction(MachineInstr *MI) const {
233   // FIXME: Can commute cmoves by changing the condition!
234   switch (MI->getOpcode()) {
235   case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
236   case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
237   case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
238   case X86::SHLD32rri8:{// A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
239     unsigned Opc;
240     unsigned Size;
241     switch (MI->getOpcode()) {
242     default: assert(0 && "Unreachable!");
243     case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
244     case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
245     case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
246     case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
247     }
248     unsigned Amt = MI->getOperand(3).getImmedValue();
249     unsigned A = MI->getOperand(0).getReg();
250     unsigned B = MI->getOperand(1).getReg();
251     unsigned C = MI->getOperand(2).getReg();
252     bool BisKill = MI->getOperand(1).isKill();
253     bool CisKill = MI->getOperand(2).isKill();
254     return BuildMI(*this, Opc, 3, A).addReg(C, false, false, CisKill)
255       .addReg(B, false, false, BisKill).addImm(Size-Amt);
256   }
257   default:
258     return TargetInstrInfo::commuteInstruction(MI);
259   }
260 }
261
262 static X86::CondCode GetCondFromBranchOpc(unsigned BrOpc) {
263   switch (BrOpc) {
264   default: return X86::COND_INVALID;
265   case X86::JE:  return X86::COND_E;
266   case X86::JNE: return X86::COND_NE;
267   case X86::JL:  return X86::COND_L;
268   case X86::JLE: return X86::COND_LE;
269   case X86::JG:  return X86::COND_G;
270   case X86::JGE: return X86::COND_GE;
271   case X86::JB:  return X86::COND_B;
272   case X86::JBE: return X86::COND_BE;
273   case X86::JA:  return X86::COND_A;
274   case X86::JAE: return X86::COND_AE;
275   case X86::JS:  return X86::COND_S;
276   case X86::JNS: return X86::COND_NS;
277   case X86::JP:  return X86::COND_P;
278   case X86::JNP: return X86::COND_NP;
279   case X86::JO:  return X86::COND_O;
280   case X86::JNO: return X86::COND_NO;
281   }
282 }
283
284 unsigned X86::GetCondBranchFromCond(X86::CondCode CC) {
285   switch (CC) {
286   default: assert(0 && "Illegal condition code!");
287   case X86::COND_E:  return X86::JE;
288   case X86::COND_NE: return X86::JNE;
289   case X86::COND_L:  return X86::JL;
290   case X86::COND_LE: return X86::JLE;
291   case X86::COND_G:  return X86::JG;
292   case X86::COND_GE: return X86::JGE;
293   case X86::COND_B:  return X86::JB;
294   case X86::COND_BE: return X86::JBE;
295   case X86::COND_A:  return X86::JA;
296   case X86::COND_AE: return X86::JAE;
297   case X86::COND_S:  return X86::JS;
298   case X86::COND_NS: return X86::JNS;
299   case X86::COND_P:  return X86::JP;
300   case X86::COND_NP: return X86::JNP;
301   case X86::COND_O:  return X86::JO;
302   case X86::COND_NO: return X86::JNO;
303   }
304 }
305
306 /// GetOppositeBranchCondition - Return the inverse of the specified condition,
307 /// e.g. turning COND_E to COND_NE.
308 X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) {
309   switch (CC) {
310   default: assert(0 && "Illegal condition code!");
311   case X86::COND_E:  return X86::COND_NE;
312   case X86::COND_NE: return X86::COND_E;
313   case X86::COND_L:  return X86::COND_GE;
314   case X86::COND_LE: return X86::COND_G;
315   case X86::COND_G:  return X86::COND_LE;
316   case X86::COND_GE: return X86::COND_L;
317   case X86::COND_B:  return X86::COND_AE;
318   case X86::COND_BE: return X86::COND_A;
319   case X86::COND_A:  return X86::COND_BE;
320   case X86::COND_AE: return X86::COND_B;
321   case X86::COND_S:  return X86::COND_NS;
322   case X86::COND_NS: return X86::COND_S;
323   case X86::COND_P:  return X86::COND_NP;
324   case X86::COND_NP: return X86::COND_P;
325   case X86::COND_O:  return X86::COND_NO;
326   case X86::COND_NO: return X86::COND_O;
327   }
328 }
329
330
331 bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, 
332                                  MachineBasicBlock *&TBB,
333                                  MachineBasicBlock *&FBB,
334                                  std::vector<MachineOperand> &Cond) const {
335   // TODO: If FP_REG_KILL is around, ignore it.
336                                    
337   // If the block has no terminators, it just falls into the block after it.
338   MachineBasicBlock::iterator I = MBB.end();
339   if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode()))
340     return false;
341
342   // Get the last instruction in the block.
343   MachineInstr *LastInst = I;
344   
345   // If there is only one terminator instruction, process it.
346   if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode())) {
347     if (!isBranch(LastInst->getOpcode()))
348       return true;
349     
350     // If the block ends with a branch there are 3 possibilities:
351     // it's an unconditional, conditional, or indirect branch.
352     
353     if (LastInst->getOpcode() == X86::JMP) {
354       TBB = LastInst->getOperand(0).getMachineBasicBlock();
355       return false;
356     }
357     X86::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
358     if (BranchCode == X86::COND_INVALID)
359       return true;  // Can't handle indirect branch.
360
361     // Otherwise, block ends with fall-through condbranch.
362     TBB = LastInst->getOperand(0).getMachineBasicBlock();
363     Cond.push_back(MachineOperand::CreateImm(BranchCode));
364     return false;
365   }
366   
367   // Get the instruction before it if it's a terminator.
368   MachineInstr *SecondLastInst = I;
369   
370   // If there are three terminators, we don't know what sort of block this is.
371   if (SecondLastInst && I != MBB.begin() &&
372       isTerminatorInstr((--I)->getOpcode()))
373     return true;
374
375   // If the block ends with X86::JMP and a conditional branch, handle it.
376   X86::CondCode BranchCode = GetCondFromBranchOpc(SecondLastInst->getOpcode());
377   if (BranchCode != X86::COND_INVALID && LastInst->getOpcode() == X86::JMP) {
378     TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
379     Cond.push_back(MachineOperand::CreateImm(BranchCode));
380     FBB = LastInst->getOperand(0).getMachineBasicBlock();
381     return false;
382   }
383
384   // Otherwise, can't handle this.
385   return true;
386 }
387
388 void X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
389   MachineBasicBlock::iterator I = MBB.end();
390   if (I == MBB.begin()) return;
391   --I;
392   if (I->getOpcode() != X86::JMP && 
393       GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
394     return;
395   
396   // Remove the branch.
397   I->eraseFromParent();
398   
399   I = MBB.end();
400   
401   if (I == MBB.begin()) return;
402   --I;
403   if (GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
404     return;
405   
406   // Remove the branch.
407   I->eraseFromParent();
408 }
409
410 void X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
411                                 MachineBasicBlock *FBB,
412                                 const std::vector<MachineOperand> &Cond) const {
413   // Shouldn't be a fall through.
414   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
415   assert((Cond.size() == 1 || Cond.size() == 0) &&
416          "X86 branch conditions have one component!");
417
418   if (FBB == 0) { // One way branch.
419     if (Cond.empty()) {
420       // Unconditional branch?
421       BuildMI(&MBB, X86::JMP, 1).addMBB(TBB);
422     } else {
423       // Conditional branch.
424       unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
425       BuildMI(&MBB, Opc, 1).addMBB(TBB);
426     }
427     return;
428   }
429   
430   // Two-way Conditional branch.
431   unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
432   BuildMI(&MBB, Opc, 1).addMBB(TBB);
433   BuildMI(&MBB, X86::JMP, 1).addMBB(FBB);
434 }
435
436 bool X86InstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
437   if (MBB.empty()) return false;
438   
439   switch (MBB.back().getOpcode()) {
440   case X86::JMP:     // Uncond branch.
441   case X86::JMP32r:  // Indirect branch.
442   case X86::JMP32m:  // Indirect branch through mem.
443     return true;
444   default: return false;
445   }
446 }
447
448 bool X86InstrInfo::
449 ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
450   assert(Cond.size() == 1 && "Invalid X86 branch condition!");
451   Cond[0].setImm(GetOppositeBranchCondition((X86::CondCode)Cond[0].getImm()));
452   return false;
453 }
454
455 const TargetRegisterClass *X86InstrInfo::getPointerRegClass() const {
456   const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
457   if (Subtarget->is64Bit())
458     return &X86::GR64RegClass;
459   else
460     return &X86::GR32RegClass;
461 }