blob: a13498e3ae4b9561dedc1629225345fd3febd138 [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
124
Alkis Evlogimenos36f506e2004-07-31 09:38:47 +0000125void X86InstrInfo::insertGoto(MachineBasicBlock& MBB,
126 MachineBasicBlock& TMBB) const {
127 BuildMI(MBB, MBB.end(), X86::JMP, 1).addMBB(&TMBB);
128}
129
130MachineBasicBlock::iterator
131X86InstrInfo::reverseBranchCondition(MachineBasicBlock::iterator MI) const {
132 unsigned Opcode = MI->getOpcode();
133 assert(isBranch(Opcode) && "MachineInstr must be a branch");
134 unsigned ROpcode;
135 switch (Opcode) {
Chris Lattnerbcdda012004-08-01 19:31:30 +0000136 default: assert(0 && "Cannot reverse unconditional branches!");
Chris Lattner167cf332004-07-31 09:53:31 +0000137 case X86::JB: ROpcode = X86::JAE; break;
Alkis Evlogimenos31e155e2004-07-31 10:05:44 +0000138 case X86::JAE: ROpcode = X86::JB; break;
Chris Lattner167cf332004-07-31 09:53:31 +0000139 case X86::JE: ROpcode = X86::JNE; break;
Alkis Evlogimenos31e155e2004-07-31 10:05:44 +0000140 case X86::JNE: ROpcode = X86::JE; break;
141 case X86::JBE: ROpcode = X86::JA; break;
Chris Lattner167cf332004-07-31 09:53:31 +0000142 case X86::JA: ROpcode = X86::JBE; break;
143 case X86::JS: ROpcode = X86::JNS; break;
Alkis Evlogimenos31e155e2004-07-31 10:05:44 +0000144 case X86::JNS: ROpcode = X86::JS; break;
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000145 case X86::JP: ROpcode = X86::JNP; break;
146 case X86::JNP: ROpcode = X86::JP; break;
Chris Lattner167cf332004-07-31 09:53:31 +0000147 case X86::JL: ROpcode = X86::JGE; break;
Alkis Evlogimenos31e155e2004-07-31 10:05:44 +0000148 case X86::JGE: ROpcode = X86::JL; break;
149 case X86::JLE: ROpcode = X86::JG; break;
Chris Lattner167cf332004-07-31 09:53:31 +0000150 case X86::JG: ROpcode = X86::JLE; break;
Alkis Evlogimenos36f506e2004-07-31 09:38:47 +0000151 }
152 MachineBasicBlock* MBB = MI->getParent();
153 MachineBasicBlock* TMBB = MI->getOperand(0).getMachineBasicBlock();
Alkis Evlogimenos6103c172004-07-31 09:44:32 +0000154 return BuildMI(*MBB, MBB->erase(MI), ROpcode, 1).addMBB(TMBB);
Alkis Evlogimenos36f506e2004-07-31 09:38:47 +0000155}
Chris Lattnerbcea4d62005-01-02 02:37:07 +0000156