blob: 99594906a68d59e88a8af926c3e9800b1433238f [file] [log] [blame]
Misha Brukman1a72c632002-11-22 22:42:50 +00001//===- X86InstrInfo.cpp - X86 Instruction Information -----------*- C++ -*-===//
Misha Brukmanc88330a2005-04-21 23:38:14 +00002//
John Criswell482202a2003-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 Brukmanc88330a2005-04-21 23:38:14 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerd92fb002002-10-25 22:55:53 +00009//
Chris Lattnerb4d58d72003-01-14 22:00:31 +000010// This file contains the X86 implementation of the TargetInstrInfo class.
Chris Lattnerd92fb002002-10-25 22:55:53 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner27d24792002-10-29 21:05:24 +000014#include "X86InstrInfo.h"
Chris Lattner0d808742002-12-03 05:42:53 +000015#include "X86.h"
Chris Lattner05e2f382003-08-03 21:55:55 +000016#include "X86GenInstrInfo.inc"
Evan Chengc8c172e2006-05-30 21:45:53 +000017#include "X86InstrBuilder.h"
18#include "X86Subtarget.h"
19#include "X86TargetMachine.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Cheng07fc1072006-12-01 21:52:41 +000021#include "llvm/CodeGen/LiveVariables.h"
Brian Gaeke960707c2003-11-11 22:41:34 +000022using namespace llvm;
23
Evan Chengc8c172e2006-05-30 21:45:53 +000024X86InstrInfo::X86InstrInfo(X86TargetMachine &tm)
25 : TargetInstrInfo(X86Insts, sizeof(X86Insts)/sizeof(X86Insts[0])),
Evan Cheng11b0a5d2006-09-08 06:48:29 +000026 TM(tm), RI(tm, *this) {
Chris Lattnerd92fb002002-10-25 22:55:53 +000027}
28
Alkis Evlogimenos52564b22003-12-28 17:35:08 +000029bool X86InstrInfo::isMoveInstr(const MachineInstr& MI,
30 unsigned& sourceReg,
31 unsigned& destReg) const {
32 MachineOpCode oc = MI.getOpcode();
Evan Cheng11b0a5d2006-09-08 06:48:29 +000033 if (oc == X86::MOV8rr || oc == X86::MOV16rr ||
34 oc == X86::MOV32rr || oc == X86::MOV64rr ||
Evan Cheng9733bde2006-05-08 08:01:26 +000035 oc == X86::MOV16to16_ || oc == X86::MOV32to32_ ||
Evan Chengb3ea2672006-02-01 23:03:16 +000036 oc == X86::FpMOV || oc == X86::MOVSSrr || oc == X86::MOVSDrr ||
Evan Cheng24c461b2006-02-16 22:45:17 +000037 oc == X86::FsMOVAPSrr || oc == X86::FsMOVAPDrr ||
Evan Cheng1208d9172006-03-21 07:09:35 +000038 oc == X86::MOVAPSrr || oc == X86::MOVAPDrr ||
Evan Chengebf10062006-04-03 20:53:28 +000039 oc == X86::MOVSS2PSrr || oc == X86::MOVSD2PDrr ||
Bill Wendling6092ce22007-03-08 22:09:11 +000040 oc == X86::MOVPS2SSrr || oc == X86::MOVPD2SDrr ||
Bill Wendlinge2324ca2007-04-24 21:17:46 +000041 oc == X86::MMX_MOVD64rr || oc == X86::MMX_MOVQ64rr) {
Evan Cheng8cd224e2007-04-25 07:12:14 +000042 assert(MI.getNumOperands() >= 2 &&
Alkis Evlogimenos52564b22003-12-28 17:35:08 +000043 MI.getOperand(0).isRegister() &&
44 MI.getOperand(1).isRegister() &&
45 "invalid register-register move instruction");
Alkis Evlogimenos8cdd0212004-02-13 21:01:20 +000046 sourceReg = MI.getOperand(1).getReg();
47 destReg = MI.getOperand(0).getReg();
Alkis Evlogimenos52564b22003-12-28 17:35:08 +000048 return true;
49 }
50 return false;
51}
Alkis Evlogimenosf57d78a2004-07-31 09:38:47 +000052
Chris Lattnerbb53acd2006-02-02 20:12:32 +000053unsigned X86InstrInfo::isLoadFromStackSlot(MachineInstr *MI,
54 int &FrameIndex) const {
55 switch (MI->getOpcode()) {
56 default: break;
57 case X86::MOV8rm:
58 case X86::MOV16rm:
Evan Chengdd7230c2006-05-11 07:33:49 +000059 case X86::MOV16_rm:
Chris Lattnerbb53acd2006-02-02 20:12:32 +000060 case X86::MOV32rm:
Evan Chengdd7230c2006-05-11 07:33:49 +000061 case X86::MOV32_rm:
Evan Cheng11b0a5d2006-09-08 06:48:29 +000062 case X86::MOV64rm:
Chris Lattnerbb53acd2006-02-02 20:12:32 +000063 case X86::FpLD64m:
64 case X86::MOVSSrm:
65 case X86::MOVSDrm:
Chris Lattnerbfc2c682006-04-18 16:44:51 +000066 case X86::MOVAPSrm:
67 case X86::MOVAPDrm:
Bill Wendlinge7b2a862007-04-03 06:00:37 +000068 case X86::MMX_MOVD64rm:
69 case X86::MMX_MOVQ64rm:
Chris Lattnerbb53acd2006-02-02 20:12:32 +000070 if (MI->getOperand(1).isFrameIndex() && MI->getOperand(2).isImmediate() &&
71 MI->getOperand(3).isRegister() && MI->getOperand(4).isImmediate() &&
72 MI->getOperand(2).getImmedValue() == 1 &&
73 MI->getOperand(3).getReg() == 0 &&
74 MI->getOperand(4).getImmedValue() == 0) {
75 FrameIndex = MI->getOperand(1).getFrameIndex();
76 return MI->getOperand(0).getReg();
77 }
78 break;
79 }
80 return 0;
81}
82
83unsigned X86InstrInfo::isStoreToStackSlot(MachineInstr *MI,
84 int &FrameIndex) const {
85 switch (MI->getOpcode()) {
86 default: break;
87 case X86::MOV8mr:
88 case X86::MOV16mr:
Evan Chengdd7230c2006-05-11 07:33:49 +000089 case X86::MOV16_mr:
Chris Lattnerbb53acd2006-02-02 20:12:32 +000090 case X86::MOV32mr:
Evan Chengdd7230c2006-05-11 07:33:49 +000091 case X86::MOV32_mr:
Evan Cheng11b0a5d2006-09-08 06:48:29 +000092 case X86::MOV64mr:
Chris Lattnerbb53acd2006-02-02 20:12:32 +000093 case X86::FpSTP64m:
94 case X86::MOVSSmr:
95 case X86::MOVSDmr:
Chris Lattnerbfc2c682006-04-18 16:44:51 +000096 case X86::MOVAPSmr:
97 case X86::MOVAPDmr:
Bill Wendlinge7b2a862007-04-03 06:00:37 +000098 case X86::MMX_MOVD64mr:
99 case X86::MMX_MOVQ64mr:
Bill Wendlingac5b6502007-04-03 23:48:32 +0000100 case X86::MMX_MOVNTQmr:
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000101 if (MI->getOperand(0).isFrameIndex() && MI->getOperand(1).isImmediate() &&
102 MI->getOperand(2).isRegister() && MI->getOperand(3).isImmediate() &&
Chris Lattner512334672006-02-02 20:38:12 +0000103 MI->getOperand(1).getImmedValue() == 1 &&
104 MI->getOperand(2).getReg() == 0 &&
105 MI->getOperand(3).getImmedValue() == 0) {
106 FrameIndex = MI->getOperand(0).getFrameIndex();
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000107 return MI->getOperand(4).getReg();
108 }
109 break;
110 }
111 return 0;
112}
113
114
Dan Gohmane8c1e422007-06-26 00:48:07 +0000115bool X86InstrInfo::isReallyTriviallyReMaterializable(MachineInstr *MI) const {
Dan Gohman4a4a8eb2007-06-14 20:50:44 +0000116 switch (MI->getOpcode()) {
117 default: break;
118 case X86::MOV8rm:
119 case X86::MOV16rm:
120 case X86::MOV16_rm:
121 case X86::MOV32rm:
122 case X86::MOV32_rm:
123 case X86::MOV64rm:
124 case X86::FpLD64m:
125 case X86::MOVSSrm:
126 case X86::MOVSDrm:
127 case X86::MOVAPSrm:
128 case X86::MOVAPDrm:
129 case X86::MMX_MOVD64rm:
130 case X86::MMX_MOVQ64rm:
Dan Gohman9e820642007-06-19 01:48:05 +0000131 // Loads from constant pools are trivially rematerializable.
Dan Gohman4a4a8eb2007-06-14 20:50:44 +0000132 return MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate() &&
133 MI->getOperand(3).isRegister() && MI->getOperand(4).isConstantPoolIndex() &&
134 MI->getOperand(1).getReg() == 0 &&
135 MI->getOperand(2).getImmedValue() == 1 &&
136 MI->getOperand(3).getReg() == 0;
137 }
Dan Gohmane8c1e422007-06-26 00:48:07 +0000138 // All other instructions marked M_REMATERIALIZABLE are always trivially
139 // rematerializable.
140 return true;
Dan Gohman4a4a8eb2007-06-14 20:50:44 +0000141}
142
Chris Lattnerb7782d72005-01-02 02:37:07 +0000143/// convertToThreeAddress - This method must be implemented by targets that
144/// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
145/// may be able to convert a two-address instruction into a true
146/// three-address instruction on demand. This allows the X86 target (for
147/// example) to convert ADD and SHL instructions into LEA instructions if they
148/// would require register copies due to two-addressness.
149///
150/// This method returns a null pointer if the transformation cannot be
151/// performed, otherwise it returns the new instruction.
152///
Evan Cheng07fc1072006-12-01 21:52:41 +0000153MachineInstr *
154X86InstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
155 MachineBasicBlock::iterator &MBBI,
156 LiveVariables &LV) const {
157 MachineInstr *MI = MBBI;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000158 // All instructions input are two-addr instructions. Get the known operands.
159 unsigned Dest = MI->getOperand(0).getReg();
160 unsigned Src = MI->getOperand(1).getReg();
161
Evan Chengdc2c8742006-11-15 20:58:11 +0000162 MachineInstr *NewMI = NULL;
Evan Cheng07fc1072006-12-01 21:52:41 +0000163 // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When
Chris Lattner3e1d9172007-03-20 06:08:29 +0000164 // we have better subtarget support, enable the 16-bit LEA generation here.
Evan Cheng07fc1072006-12-01 21:52:41 +0000165 bool DisableLEA16 = true;
166
Evan Cheng66f849b2006-05-30 20:26:50 +0000167 switch (MI->getOpcode()) {
Chris Lattner3e1d9172007-03-20 06:08:29 +0000168 default: return 0;
Evan Cheng66f849b2006-05-30 20:26:50 +0000169 case X86::SHUFPSrri: {
170 assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!");
Chris Lattner3e1d9172007-03-20 06:08:29 +0000171 if (!TM.getSubtarget<X86Subtarget>().hasSSE2()) return 0;
172
Evan Chengc8c172e2006-05-30 21:45:53 +0000173 unsigned A = MI->getOperand(0).getReg();
174 unsigned B = MI->getOperand(1).getReg();
175 unsigned C = MI->getOperand(2).getReg();
Chris Lattner3e1d9172007-03-20 06:08:29 +0000176 unsigned M = MI->getOperand(3).getImm();
177 if (B != C) return 0;
Evan Cheng20350c42006-11-27 23:37:22 +0000178 NewMI = BuildMI(get(X86::PSHUFDri), A).addReg(B).addImm(M);
Chris Lattner3e1d9172007-03-20 06:08:29 +0000179 break;
180 }
Chris Lattnerbcd38852007-03-28 18:12:31 +0000181 case X86::SHL64ri: {
182 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
183 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
184 // the flags produced by a shift yet, so this is safe.
185 unsigned Dest = MI->getOperand(0).getReg();
186 unsigned Src = MI->getOperand(1).getReg();
187 unsigned ShAmt = MI->getOperand(2).getImm();
188 if (ShAmt == 0 || ShAmt >= 4) return 0;
189
190 NewMI = BuildMI(get(X86::LEA64r), Dest)
191 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
192 break;
193 }
Chris Lattner3e1d9172007-03-20 06:08:29 +0000194 case X86::SHL32ri: {
195 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
196 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
197 // the flags produced by a shift yet, so this is safe.
198 unsigned Dest = MI->getOperand(0).getReg();
199 unsigned Src = MI->getOperand(1).getReg();
200 unsigned ShAmt = MI->getOperand(2).getImm();
201 if (ShAmt == 0 || ShAmt >= 4) return 0;
202
Chris Lattnerc56e4922007-03-28 00:58:40 +0000203 unsigned Opc = TM.getSubtarget<X86Subtarget>().is64Bit() ?
204 X86::LEA64_32r : X86::LEA32r;
205 NewMI = BuildMI(get(Opc), Dest)
Chris Lattner3e1d9172007-03-20 06:08:29 +0000206 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
207 break;
208 }
209 case X86::SHL16ri: {
210 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
211 if (DisableLEA16) return 0;
212
213 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
214 // the flags produced by a shift yet, so this is safe.
215 unsigned Dest = MI->getOperand(0).getReg();
216 unsigned Src = MI->getOperand(1).getReg();
217 unsigned ShAmt = MI->getOperand(2).getImm();
218 if (ShAmt == 0 || ShAmt >= 4) return 0;
219
220 NewMI = BuildMI(get(X86::LEA16r), Dest)
221 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
222 break;
Evan Cheng66f849b2006-05-30 20:26:50 +0000223 }
224 }
225
Misha Brukmanc88330a2005-04-21 23:38:14 +0000226 // FIXME: None of these instructions are promotable to LEAs without
227 // additional information. In particular, LEA doesn't set the flags that
Chris Lattner733aac12005-01-02 04:18:17 +0000228 // add and inc do. :(
Chris Lattner3e1d9172007-03-20 06:08:29 +0000229 if (0)
Chris Lattnerb7782d72005-01-02 02:37:07 +0000230 switch (MI->getOpcode()) {
231 case X86::INC32r:
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000232 case X86::INC64_32r:
Chris Lattnerb7782d72005-01-02 02:37:07 +0000233 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
Evan Cheng20350c42006-11-27 23:37:22 +0000234 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, 1);
Evan Chengdc2c8742006-11-15 20:58:11 +0000235 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000236 case X86::INC16r:
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000237 case X86::INC64_16r:
Chris Lattnerb7782d72005-01-02 02:37:07 +0000238 if (DisableLEA16) return 0;
239 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
Evan Cheng20350c42006-11-27 23:37:22 +0000240 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, 1);
Evan Chengdc2c8742006-11-15 20:58:11 +0000241 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000242 case X86::DEC32r:
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000243 case X86::DEC64_32r:
Chris Lattnerb7782d72005-01-02 02:37:07 +0000244 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
Evan Cheng20350c42006-11-27 23:37:22 +0000245 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, -1);
Evan Chengdc2c8742006-11-15 20:58:11 +0000246 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000247 case X86::DEC16r:
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000248 case X86::DEC64_16r:
Chris Lattnerb7782d72005-01-02 02:37:07 +0000249 if (DisableLEA16) return 0;
250 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
Evan Cheng20350c42006-11-27 23:37:22 +0000251 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, -1);
Evan Chengdc2c8742006-11-15 20:58:11 +0000252 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000253 case X86::ADD32rr:
254 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
Evan Cheng20350c42006-11-27 23:37:22 +0000255 NewMI = addRegReg(BuildMI(get(X86::LEA32r), Dest), Src,
Chris Lattnerb7782d72005-01-02 02:37:07 +0000256 MI->getOperand(2).getReg());
Evan Chengdc2c8742006-11-15 20:58:11 +0000257 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000258 case X86::ADD16rr:
259 if (DisableLEA16) return 0;
260 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
Evan Cheng20350c42006-11-27 23:37:22 +0000261 NewMI = addRegReg(BuildMI(get(X86::LEA16r), Dest), Src,
Chris Lattnerb7782d72005-01-02 02:37:07 +0000262 MI->getOperand(2).getReg());
Evan Chengdc2c8742006-11-15 20:58:11 +0000263 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000264 case X86::ADD32ri:
Evan Chengfeca91a2006-05-19 18:43:41 +0000265 case X86::ADD32ri8:
Chris Lattnerb7782d72005-01-02 02:37:07 +0000266 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
267 if (MI->getOperand(2).isImmediate())
Evan Cheng20350c42006-11-27 23:37:22 +0000268 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src,
Chris Lattnerb7782d72005-01-02 02:37:07 +0000269 MI->getOperand(2).getImmedValue());
Evan Chengdc2c8742006-11-15 20:58:11 +0000270 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000271 case X86::ADD16ri:
Evan Chengfeca91a2006-05-19 18:43:41 +0000272 case X86::ADD16ri8:
Chris Lattnerb7782d72005-01-02 02:37:07 +0000273 if (DisableLEA16) return 0;
274 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
275 if (MI->getOperand(2).isImmediate())
Evan Cheng20350c42006-11-27 23:37:22 +0000276 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src,
Chris Lattnerb7782d72005-01-02 02:37:07 +0000277 MI->getOperand(2).getImmedValue());
278 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000279 case X86::SHL16ri:
280 if (DisableLEA16) return 0;
281 case X86::SHL32ri:
282 assert(MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate() &&
283 "Unknown shl instruction!");
284 unsigned ShAmt = MI->getOperand(2).getImmedValue();
285 if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) {
286 X86AddressMode AM;
287 AM.Scale = 1 << ShAmt;
288 AM.IndexReg = Src;
289 unsigned Opc = MI->getOpcode() == X86::SHL32ri ? X86::LEA32r :X86::LEA16r;
Evan Cheng20350c42006-11-27 23:37:22 +0000290 NewMI = addFullAddress(BuildMI(get(Opc), Dest), AM);
Chris Lattnerb7782d72005-01-02 02:37:07 +0000291 }
292 break;
293 }
294
Evan Cheng07fc1072006-12-01 21:52:41 +0000295 if (NewMI) {
Evan Chengdc2c8742006-11-15 20:58:11 +0000296 NewMI->copyKillDeadInfo(MI);
Evan Cheng07fc1072006-12-01 21:52:41 +0000297 LV.instructionChanged(MI, NewMI); // Update live variables
298 MFI->insert(MBBI, NewMI); // Insert the new inst
299 }
Evan Chengdc2c8742006-11-15 20:58:11 +0000300 return NewMI;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000301}
302
Chris Lattner29478012005-01-19 07:11:01 +0000303/// commuteInstruction - We have a few instructions that must be hacked on to
304/// commute them.
305///
306MachineInstr *X86InstrInfo::commuteInstruction(MachineInstr *MI) const {
Chris Lattnerd9e4bf52006-09-28 23:33:12 +0000307 // FIXME: Can commute cmoves by changing the condition!
Chris Lattner29478012005-01-19 07:11:01 +0000308 switch (MI->getOpcode()) {
Chris Lattnerd54845f2005-01-19 07:31:24 +0000309 case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
310 case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
Chris Lattner29478012005-01-19 07:11:01 +0000311 case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
312 case X86::SHLD32rri8:{// A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
Chris Lattnerd54845f2005-01-19 07:31:24 +0000313 unsigned Opc;
314 unsigned Size;
315 switch (MI->getOpcode()) {
316 default: assert(0 && "Unreachable!");
317 case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
318 case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
319 case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
320 case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
321 }
Chris Lattner29478012005-01-19 07:11:01 +0000322 unsigned Amt = MI->getOperand(3).getImmedValue();
323 unsigned A = MI->getOperand(0).getReg();
324 unsigned B = MI->getOperand(1).getReg();
325 unsigned C = MI->getOperand(2).getReg();
Evan Chengdc2c8742006-11-15 20:58:11 +0000326 bool BisKill = MI->getOperand(1).isKill();
327 bool CisKill = MI->getOperand(2).isKill();
Evan Cheng20350c42006-11-27 23:37:22 +0000328 return BuildMI(get(Opc), A).addReg(C, false, false, CisKill)
Evan Chengdc2c8742006-11-15 20:58:11 +0000329 .addReg(B, false, false, BisKill).addImm(Size-Amt);
Chris Lattner29478012005-01-19 07:11:01 +0000330 }
331 default:
332 return TargetInstrInfo::commuteInstruction(MI);
333 }
334}
335
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000336static X86::CondCode GetCondFromBranchOpc(unsigned BrOpc) {
337 switch (BrOpc) {
338 default: return X86::COND_INVALID;
339 case X86::JE: return X86::COND_E;
340 case X86::JNE: return X86::COND_NE;
341 case X86::JL: return X86::COND_L;
342 case X86::JLE: return X86::COND_LE;
343 case X86::JG: return X86::COND_G;
344 case X86::JGE: return X86::COND_GE;
345 case X86::JB: return X86::COND_B;
346 case X86::JBE: return X86::COND_BE;
347 case X86::JA: return X86::COND_A;
348 case X86::JAE: return X86::COND_AE;
349 case X86::JS: return X86::COND_S;
350 case X86::JNS: return X86::COND_NS;
351 case X86::JP: return X86::COND_P;
352 case X86::JNP: return X86::COND_NP;
353 case X86::JO: return X86::COND_O;
354 case X86::JNO: return X86::COND_NO;
355 }
356}
357
358unsigned X86::GetCondBranchFromCond(X86::CondCode CC) {
359 switch (CC) {
360 default: assert(0 && "Illegal condition code!");
361 case X86::COND_E: return X86::JE;
362 case X86::COND_NE: return X86::JNE;
363 case X86::COND_L: return X86::JL;
364 case X86::COND_LE: return X86::JLE;
365 case X86::COND_G: return X86::JG;
366 case X86::COND_GE: return X86::JGE;
367 case X86::COND_B: return X86::JB;
368 case X86::COND_BE: return X86::JBE;
369 case X86::COND_A: return X86::JA;
370 case X86::COND_AE: return X86::JAE;
371 case X86::COND_S: return X86::JS;
372 case X86::COND_NS: return X86::JNS;
373 case X86::COND_P: return X86::JP;
374 case X86::COND_NP: return X86::JNP;
375 case X86::COND_O: return X86::JO;
376 case X86::COND_NO: return X86::JNO;
377 }
378}
379
Chris Lattner3a897f32006-10-21 05:52:40 +0000380/// GetOppositeBranchCondition - Return the inverse of the specified condition,
381/// e.g. turning COND_E to COND_NE.
382X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) {
383 switch (CC) {
384 default: assert(0 && "Illegal condition code!");
385 case X86::COND_E: return X86::COND_NE;
386 case X86::COND_NE: return X86::COND_E;
387 case X86::COND_L: return X86::COND_GE;
388 case X86::COND_LE: return X86::COND_G;
389 case X86::COND_G: return X86::COND_LE;
390 case X86::COND_GE: return X86::COND_L;
391 case X86::COND_B: return X86::COND_AE;
392 case X86::COND_BE: return X86::COND_A;
393 case X86::COND_A: return X86::COND_BE;
394 case X86::COND_AE: return X86::COND_B;
395 case X86::COND_S: return X86::COND_NS;
396 case X86::COND_NS: return X86::COND_S;
397 case X86::COND_P: return X86::COND_NP;
398 case X86::COND_NP: return X86::COND_P;
399 case X86::COND_O: return X86::COND_NO;
400 case X86::COND_NO: return X86::COND_O;
401 }
402}
403
Dale Johannesen616627b2007-06-14 22:03:45 +0000404// For purposes of branch analysis do not count FP_REG_KILL as a terminator.
405bool X86InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
406 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
407 if (MI->getOpcode() == X86::FP_REG_KILL)
408 return false;
409 if (TID->Flags & M_TERMINATOR_FLAG)
410 return !isPredicated(MI);
411 return false;
412}
Chris Lattner3a897f32006-10-21 05:52:40 +0000413
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000414bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
415 MachineBasicBlock *&TBB,
416 MachineBasicBlock *&FBB,
417 std::vector<MachineOperand> &Cond) const {
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000418 // If the block has no terminators, it just falls into the block after it.
419 MachineBasicBlock::iterator I = MBB.end();
Evan Cheng5514bbe2007-06-08 21:59:56 +0000420 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000421 return false;
422
423 // Get the last instruction in the block.
424 MachineInstr *LastInst = I;
425
426 // If there is only one terminator instruction, process it.
Evan Cheng5514bbe2007-06-08 21:59:56 +0000427 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000428 if (!isBranch(LastInst->getOpcode()))
429 return true;
430
431 // If the block ends with a branch there are 3 possibilities:
432 // it's an unconditional, conditional, or indirect branch.
433
434 if (LastInst->getOpcode() == X86::JMP) {
435 TBB = LastInst->getOperand(0).getMachineBasicBlock();
436 return false;
437 }
438 X86::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
439 if (BranchCode == X86::COND_INVALID)
440 return true; // Can't handle indirect branch.
441
442 // Otherwise, block ends with fall-through condbranch.
443 TBB = LastInst->getOperand(0).getMachineBasicBlock();
444 Cond.push_back(MachineOperand::CreateImm(BranchCode));
445 return false;
446 }
447
448 // Get the instruction before it if it's a terminator.
449 MachineInstr *SecondLastInst = I;
450
451 // If there are three terminators, we don't know what sort of block this is.
Dale Johannesen616627b2007-06-14 22:03:45 +0000452 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000453 return true;
454
Chris Lattner74436002006-10-30 22:27:23 +0000455 // If the block ends with X86::JMP and a conditional branch, handle it.
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000456 X86::CondCode BranchCode = GetCondFromBranchOpc(SecondLastInst->getOpcode());
457 if (BranchCode != X86::COND_INVALID && LastInst->getOpcode() == X86::JMP) {
Chris Lattner74436002006-10-30 22:27:23 +0000458 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
459 Cond.push_back(MachineOperand::CreateImm(BranchCode));
460 FBB = LastInst->getOperand(0).getMachineBasicBlock();
461 return false;
462 }
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000463
Dale Johannesenc6855462007-06-13 17:59:52 +0000464 // If the block ends with two X86::JMPs, handle it. The second one is not
465 // executed, so remove it.
466 if (SecondLastInst->getOpcode() == X86::JMP &&
467 LastInst->getOpcode() == X86::JMP) {
468 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
469 I = LastInst;
470 I->eraseFromParent();
471 return false;
472 }
473
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000474 // Otherwise, can't handle this.
475 return true;
476}
477
Evan Chenge20dd922007-05-18 00:18:17 +0000478unsigned X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000479 MachineBasicBlock::iterator I = MBB.end();
Evan Chenge20dd922007-05-18 00:18:17 +0000480 if (I == MBB.begin()) return 0;
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000481 --I;
482 if (I->getOpcode() != X86::JMP &&
483 GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
Evan Chenge20dd922007-05-18 00:18:17 +0000484 return 0;
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000485
486 // Remove the branch.
487 I->eraseFromParent();
488
489 I = MBB.end();
490
Evan Chenge20dd922007-05-18 00:18:17 +0000491 if (I == MBB.begin()) return 1;
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000492 --I;
493 if (GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
Evan Chenge20dd922007-05-18 00:18:17 +0000494 return 1;
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000495
496 // Remove the branch.
497 I->eraseFromParent();
Evan Chenge20dd922007-05-18 00:18:17 +0000498 return 2;
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000499}
500
Evan Chenge20dd922007-05-18 00:18:17 +0000501unsigned
502X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
503 MachineBasicBlock *FBB,
504 const std::vector<MachineOperand> &Cond) const {
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000505 // Shouldn't be a fall through.
506 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Chris Lattner6fca75e2006-10-21 05:34:23 +0000507 assert((Cond.size() == 1 || Cond.size() == 0) &&
508 "X86 branch conditions have one component!");
509
510 if (FBB == 0) { // One way branch.
511 if (Cond.empty()) {
512 // Unconditional branch?
Evan Cheng20350c42006-11-27 23:37:22 +0000513 BuildMI(&MBB, get(X86::JMP)).addMBB(TBB);
Chris Lattner6fca75e2006-10-21 05:34:23 +0000514 } else {
515 // Conditional branch.
516 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
Evan Cheng20350c42006-11-27 23:37:22 +0000517 BuildMI(&MBB, get(Opc)).addMBB(TBB);
Chris Lattner6fca75e2006-10-21 05:34:23 +0000518 }
Evan Chenge20dd922007-05-18 00:18:17 +0000519 return 1;
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000520 }
521
Chris Lattnerd8816602006-10-21 05:42:09 +0000522 // Two-way Conditional branch.
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000523 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
Evan Cheng20350c42006-11-27 23:37:22 +0000524 BuildMI(&MBB, get(Opc)).addMBB(TBB);
525 BuildMI(&MBB, get(X86::JMP)).addMBB(FBB);
Evan Chenge20dd922007-05-18 00:18:17 +0000526 return 2;
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000527}
528
Chris Lattner53ebf202006-10-28 17:29:57 +0000529bool X86InstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
530 if (MBB.empty()) return false;
531
532 switch (MBB.back().getOpcode()) {
Evan Chengfc94eb62007-05-21 18:44:17 +0000533 case X86::RET: // Return.
534 case X86::RETI:
535 case X86::TAILJMPd:
536 case X86::TAILJMPr:
537 case X86::TAILJMPm:
Chris Lattner53ebf202006-10-28 17:29:57 +0000538 case X86::JMP: // Uncond branch.
539 case X86::JMP32r: // Indirect branch.
540 case X86::JMP32m: // Indirect branch through mem.
541 return true;
542 default: return false;
543 }
544}
545
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000546bool X86InstrInfo::
547ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
Chris Lattner3a897f32006-10-21 05:52:40 +0000548 assert(Cond.size() == 1 && "Invalid X86 branch condition!");
549 Cond[0].setImm(GetOppositeBranchCondition((X86::CondCode)Cond[0].getImm()));
550 return false;
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000551}
552
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000553const TargetRegisterClass *X86InstrInfo::getPointerRegClass() const {
554 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
555 if (Subtarget->is64Bit())
556 return &X86::GR64RegClass;
557 else
558 return &X86::GR32RegClass;
559}