blob: 200ed45bb48cc370646fce25eb5c9bb0b691c016 [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_ ||
Dale Johannesen3d7008c2007-07-04 21:07:47 +000036 oc == X86::MOV_Fp3232 || oc == X86::MOVSSrr || oc == X86::MOVSDrr ||
37 oc == X86::MOV_Fp3264 || oc == X86::MOV_Fp6432 || oc == X86::MOV_Fp6464 ||
Evan Cheng24c461b2006-02-16 22:45:17 +000038 oc == X86::FsMOVAPSrr || oc == X86::FsMOVAPDrr ||
Evan Cheng1208d9172006-03-21 07:09:35 +000039 oc == X86::MOVAPSrr || oc == X86::MOVAPDrr ||
Evan Chengebf10062006-04-03 20:53:28 +000040 oc == X86::MOVSS2PSrr || oc == X86::MOVSD2PDrr ||
Bill Wendling6092ce22007-03-08 22:09:11 +000041 oc == X86::MOVPS2SSrr || oc == X86::MOVPD2SDrr ||
Bill Wendlinge2324ca2007-04-24 21:17:46 +000042 oc == X86::MMX_MOVD64rr || oc == X86::MMX_MOVQ64rr) {
Evan Cheng8cd224e2007-04-25 07:12:14 +000043 assert(MI.getNumOperands() >= 2 &&
Alkis Evlogimenos52564b22003-12-28 17:35:08 +000044 MI.getOperand(0).isRegister() &&
45 MI.getOperand(1).isRegister() &&
46 "invalid register-register move instruction");
Alkis Evlogimenos8cdd0212004-02-13 21:01:20 +000047 sourceReg = MI.getOperand(1).getReg();
48 destReg = MI.getOperand(0).getReg();
Alkis Evlogimenos52564b22003-12-28 17:35:08 +000049 return true;
50 }
51 return false;
52}
Alkis Evlogimenosf57d78a2004-07-31 09:38:47 +000053
Chris Lattnerbb53acd2006-02-02 20:12:32 +000054unsigned X86InstrInfo::isLoadFromStackSlot(MachineInstr *MI,
55 int &FrameIndex) const {
56 switch (MI->getOpcode()) {
57 default: break;
58 case X86::MOV8rm:
59 case X86::MOV16rm:
Evan Chengdd7230c2006-05-11 07:33:49 +000060 case X86::MOV16_rm:
Chris Lattnerbb53acd2006-02-02 20:12:32 +000061 case X86::MOV32rm:
Evan Chengdd7230c2006-05-11 07:33:49 +000062 case X86::MOV32_rm:
Evan Cheng11b0a5d2006-09-08 06:48:29 +000063 case X86::MOV64rm:
Dale Johannesen3d7008c2007-07-04 21:07:47 +000064 case X86::LD_Fp64m:
Chris Lattnerbb53acd2006-02-02 20:12:32 +000065 case X86::MOVSSrm:
66 case X86::MOVSDrm:
Chris Lattnerbfc2c682006-04-18 16:44:51 +000067 case X86::MOVAPSrm:
68 case X86::MOVAPDrm:
Bill Wendlinge7b2a862007-04-03 06:00:37 +000069 case X86::MMX_MOVD64rm:
70 case X86::MMX_MOVQ64rm:
Chris Lattnerbb53acd2006-02-02 20:12:32 +000071 if (MI->getOperand(1).isFrameIndex() && MI->getOperand(2).isImmediate() &&
72 MI->getOperand(3).isRegister() && MI->getOperand(4).isImmediate() &&
73 MI->getOperand(2).getImmedValue() == 1 &&
74 MI->getOperand(3).getReg() == 0 &&
75 MI->getOperand(4).getImmedValue() == 0) {
76 FrameIndex = MI->getOperand(1).getFrameIndex();
77 return MI->getOperand(0).getReg();
78 }
79 break;
80 }
81 return 0;
82}
83
84unsigned X86InstrInfo::isStoreToStackSlot(MachineInstr *MI,
85 int &FrameIndex) const {
86 switch (MI->getOpcode()) {
87 default: break;
88 case X86::MOV8mr:
89 case X86::MOV16mr:
Evan Chengdd7230c2006-05-11 07:33:49 +000090 case X86::MOV16_mr:
Chris Lattnerbb53acd2006-02-02 20:12:32 +000091 case X86::MOV32mr:
Evan Chengdd7230c2006-05-11 07:33:49 +000092 case X86::MOV32_mr:
Evan Cheng11b0a5d2006-09-08 06:48:29 +000093 case X86::MOV64mr:
Dale Johannesen3d7008c2007-07-04 21:07:47 +000094 case X86::ST_FpP64m:
Chris Lattnerbb53acd2006-02-02 20:12:32 +000095 case X86::MOVSSmr:
96 case X86::MOVSDmr:
Chris Lattnerbfc2c682006-04-18 16:44:51 +000097 case X86::MOVAPSmr:
98 case X86::MOVAPDmr:
Bill Wendlinge7b2a862007-04-03 06:00:37 +000099 case X86::MMX_MOVD64mr:
100 case X86::MMX_MOVQ64mr:
Bill Wendlingac5b6502007-04-03 23:48:32 +0000101 case X86::MMX_MOVNTQmr:
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000102 if (MI->getOperand(0).isFrameIndex() && MI->getOperand(1).isImmediate() &&
103 MI->getOperand(2).isRegister() && MI->getOperand(3).isImmediate() &&
Chris Lattner512334672006-02-02 20:38:12 +0000104 MI->getOperand(1).getImmedValue() == 1 &&
105 MI->getOperand(2).getReg() == 0 &&
106 MI->getOperand(3).getImmedValue() == 0) {
107 FrameIndex = MI->getOperand(0).getFrameIndex();
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000108 return MI->getOperand(4).getReg();
109 }
110 break;
111 }
112 return 0;
113}
114
115
Dan Gohmane8c1e422007-06-26 00:48:07 +0000116bool X86InstrInfo::isReallyTriviallyReMaterializable(MachineInstr *MI) const {
Dan Gohman4a4a8eb2007-06-14 20:50:44 +0000117 switch (MI->getOpcode()) {
118 default: break;
119 case X86::MOV8rm:
120 case X86::MOV16rm:
121 case X86::MOV16_rm:
122 case X86::MOV32rm:
123 case X86::MOV32_rm:
124 case X86::MOV64rm:
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000125 case X86::LD_Fp64m:
Dan Gohman4a4a8eb2007-06-14 20:50:44 +0000126 case X86::MOVSSrm:
127 case X86::MOVSDrm:
128 case X86::MOVAPSrm:
129 case X86::MOVAPDrm:
130 case X86::MMX_MOVD64rm:
131 case X86::MMX_MOVQ64rm:
Dan Gohman9e820642007-06-19 01:48:05 +0000132 // Loads from constant pools are trivially rematerializable.
Dan Gohman4a4a8eb2007-06-14 20:50:44 +0000133 return MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate() &&
134 MI->getOperand(3).isRegister() && MI->getOperand(4).isConstantPoolIndex() &&
135 MI->getOperand(1).getReg() == 0 &&
136 MI->getOperand(2).getImmedValue() == 1 &&
137 MI->getOperand(3).getReg() == 0;
138 }
Dan Gohmane8c1e422007-06-26 00:48:07 +0000139 // All other instructions marked M_REMATERIALIZABLE are always trivially
140 // rematerializable.
141 return true;
Dan Gohman4a4a8eb2007-06-14 20:50:44 +0000142}
143
Chris Lattnerb7782d72005-01-02 02:37:07 +0000144/// convertToThreeAddress - This method must be implemented by targets that
145/// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
146/// may be able to convert a two-address instruction into a true
147/// three-address instruction on demand. This allows the X86 target (for
148/// example) to convert ADD and SHL instructions into LEA instructions if they
149/// would require register copies due to two-addressness.
150///
151/// This method returns a null pointer if the transformation cannot be
152/// performed, otherwise it returns the new instruction.
153///
Evan Cheng07fc1072006-12-01 21:52:41 +0000154MachineInstr *
155X86InstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
156 MachineBasicBlock::iterator &MBBI,
157 LiveVariables &LV) const {
158 MachineInstr *MI = MBBI;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000159 // All instructions input are two-addr instructions. Get the known operands.
160 unsigned Dest = MI->getOperand(0).getReg();
161 unsigned Src = MI->getOperand(1).getReg();
162
Evan Chengdc2c8742006-11-15 20:58:11 +0000163 MachineInstr *NewMI = NULL;
Evan Cheng07fc1072006-12-01 21:52:41 +0000164 // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When
Chris Lattner3e1d9172007-03-20 06:08:29 +0000165 // we have better subtarget support, enable the 16-bit LEA generation here.
Evan Cheng07fc1072006-12-01 21:52:41 +0000166 bool DisableLEA16 = true;
167
Evan Cheng66f849b2006-05-30 20:26:50 +0000168 switch (MI->getOpcode()) {
Chris Lattner3e1d9172007-03-20 06:08:29 +0000169 default: return 0;
Evan Cheng66f849b2006-05-30 20:26:50 +0000170 case X86::SHUFPSrri: {
171 assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!");
Chris Lattner3e1d9172007-03-20 06:08:29 +0000172 if (!TM.getSubtarget<X86Subtarget>().hasSSE2()) return 0;
173
Evan Chengc8c172e2006-05-30 21:45:53 +0000174 unsigned A = MI->getOperand(0).getReg();
175 unsigned B = MI->getOperand(1).getReg();
176 unsigned C = MI->getOperand(2).getReg();
Chris Lattner3e1d9172007-03-20 06:08:29 +0000177 unsigned M = MI->getOperand(3).getImm();
178 if (B != C) return 0;
Evan Cheng20350c42006-11-27 23:37:22 +0000179 NewMI = BuildMI(get(X86::PSHUFDri), A).addReg(B).addImm(M);
Chris Lattner3e1d9172007-03-20 06:08:29 +0000180 break;
181 }
Chris Lattnerbcd38852007-03-28 18:12:31 +0000182 case X86::SHL64ri: {
183 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
184 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
185 // the flags produced by a shift yet, so this is safe.
186 unsigned Dest = MI->getOperand(0).getReg();
187 unsigned Src = MI->getOperand(1).getReg();
188 unsigned ShAmt = MI->getOperand(2).getImm();
189 if (ShAmt == 0 || ShAmt >= 4) return 0;
190
191 NewMI = BuildMI(get(X86::LEA64r), Dest)
192 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
193 break;
194 }
Chris Lattner3e1d9172007-03-20 06:08:29 +0000195 case X86::SHL32ri: {
196 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
197 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
198 // the flags produced by a shift yet, so this is safe.
199 unsigned Dest = MI->getOperand(0).getReg();
200 unsigned Src = MI->getOperand(1).getReg();
201 unsigned ShAmt = MI->getOperand(2).getImm();
202 if (ShAmt == 0 || ShAmt >= 4) return 0;
203
Chris Lattnerc56e4922007-03-28 00:58:40 +0000204 unsigned Opc = TM.getSubtarget<X86Subtarget>().is64Bit() ?
205 X86::LEA64_32r : X86::LEA32r;
206 NewMI = BuildMI(get(Opc), Dest)
Chris Lattner3e1d9172007-03-20 06:08:29 +0000207 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
208 break;
209 }
210 case X86::SHL16ri: {
211 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
212 if (DisableLEA16) return 0;
213
214 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
215 // the flags produced by a shift yet, so this is safe.
216 unsigned Dest = MI->getOperand(0).getReg();
217 unsigned Src = MI->getOperand(1).getReg();
218 unsigned ShAmt = MI->getOperand(2).getImm();
219 if (ShAmt == 0 || ShAmt >= 4) return 0;
220
221 NewMI = BuildMI(get(X86::LEA16r), Dest)
222 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
223 break;
Evan Cheng66f849b2006-05-30 20:26:50 +0000224 }
225 }
226
Misha Brukmanc88330a2005-04-21 23:38:14 +0000227 // FIXME: None of these instructions are promotable to LEAs without
228 // additional information. In particular, LEA doesn't set the flags that
Chris Lattner733aac12005-01-02 04:18:17 +0000229 // add and inc do. :(
Chris Lattner3e1d9172007-03-20 06:08:29 +0000230 if (0)
Chris Lattnerb7782d72005-01-02 02:37:07 +0000231 switch (MI->getOpcode()) {
232 case X86::INC32r:
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000233 case X86::INC64_32r:
Chris Lattnerb7782d72005-01-02 02:37:07 +0000234 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
Evan Cheng20350c42006-11-27 23:37:22 +0000235 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, 1);
Evan Chengdc2c8742006-11-15 20:58:11 +0000236 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000237 case X86::INC16r:
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000238 case X86::INC64_16r:
Chris Lattnerb7782d72005-01-02 02:37:07 +0000239 if (DisableLEA16) return 0;
240 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
Evan Cheng20350c42006-11-27 23:37:22 +0000241 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, 1);
Evan Chengdc2c8742006-11-15 20:58:11 +0000242 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000243 case X86::DEC32r:
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000244 case X86::DEC64_32r:
Chris Lattnerb7782d72005-01-02 02:37:07 +0000245 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
Evan Cheng20350c42006-11-27 23:37:22 +0000246 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, -1);
Evan Chengdc2c8742006-11-15 20:58:11 +0000247 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000248 case X86::DEC16r:
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000249 case X86::DEC64_16r:
Chris Lattnerb7782d72005-01-02 02:37:07 +0000250 if (DisableLEA16) return 0;
251 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
Evan Cheng20350c42006-11-27 23:37:22 +0000252 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, -1);
Evan Chengdc2c8742006-11-15 20:58:11 +0000253 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000254 case X86::ADD32rr:
255 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
Evan Cheng20350c42006-11-27 23:37:22 +0000256 NewMI = addRegReg(BuildMI(get(X86::LEA32r), Dest), Src,
Chris Lattnerb7782d72005-01-02 02:37:07 +0000257 MI->getOperand(2).getReg());
Evan Chengdc2c8742006-11-15 20:58:11 +0000258 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000259 case X86::ADD16rr:
260 if (DisableLEA16) return 0;
261 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
Evan Cheng20350c42006-11-27 23:37:22 +0000262 NewMI = addRegReg(BuildMI(get(X86::LEA16r), Dest), Src,
Chris Lattnerb7782d72005-01-02 02:37:07 +0000263 MI->getOperand(2).getReg());
Evan Chengdc2c8742006-11-15 20:58:11 +0000264 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000265 case X86::ADD32ri:
Evan Chengfeca91a2006-05-19 18:43:41 +0000266 case X86::ADD32ri8:
Chris Lattnerb7782d72005-01-02 02:37:07 +0000267 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
268 if (MI->getOperand(2).isImmediate())
Evan Cheng20350c42006-11-27 23:37:22 +0000269 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src,
Chris Lattnerb7782d72005-01-02 02:37:07 +0000270 MI->getOperand(2).getImmedValue());
Evan Chengdc2c8742006-11-15 20:58:11 +0000271 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000272 case X86::ADD16ri:
Evan Chengfeca91a2006-05-19 18:43:41 +0000273 case X86::ADD16ri8:
Chris Lattnerb7782d72005-01-02 02:37:07 +0000274 if (DisableLEA16) return 0;
275 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
276 if (MI->getOperand(2).isImmediate())
Evan Cheng20350c42006-11-27 23:37:22 +0000277 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src,
Chris Lattnerb7782d72005-01-02 02:37:07 +0000278 MI->getOperand(2).getImmedValue());
279 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000280 case X86::SHL16ri:
281 if (DisableLEA16) return 0;
282 case X86::SHL32ri:
283 assert(MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate() &&
284 "Unknown shl instruction!");
285 unsigned ShAmt = MI->getOperand(2).getImmedValue();
286 if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) {
287 X86AddressMode AM;
288 AM.Scale = 1 << ShAmt;
289 AM.IndexReg = Src;
290 unsigned Opc = MI->getOpcode() == X86::SHL32ri ? X86::LEA32r :X86::LEA16r;
Evan Cheng20350c42006-11-27 23:37:22 +0000291 NewMI = addFullAddress(BuildMI(get(Opc), Dest), AM);
Chris Lattnerb7782d72005-01-02 02:37:07 +0000292 }
293 break;
294 }
295
Evan Cheng07fc1072006-12-01 21:52:41 +0000296 if (NewMI) {
Evan Chengdc2c8742006-11-15 20:58:11 +0000297 NewMI->copyKillDeadInfo(MI);
Evan Cheng07fc1072006-12-01 21:52:41 +0000298 LV.instructionChanged(MI, NewMI); // Update live variables
299 MFI->insert(MBBI, NewMI); // Insert the new inst
300 }
Evan Chengdc2c8742006-11-15 20:58:11 +0000301 return NewMI;
Chris Lattnerb7782d72005-01-02 02:37:07 +0000302}
303
Chris Lattner29478012005-01-19 07:11:01 +0000304/// commuteInstruction - We have a few instructions that must be hacked on to
305/// commute them.
306///
307MachineInstr *X86InstrInfo::commuteInstruction(MachineInstr *MI) const {
Chris Lattnerd9e4bf52006-09-28 23:33:12 +0000308 // FIXME: Can commute cmoves by changing the condition!
Chris Lattner29478012005-01-19 07:11:01 +0000309 switch (MI->getOpcode()) {
Chris Lattnerd54845f2005-01-19 07:31:24 +0000310 case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
311 case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
Chris Lattner29478012005-01-19 07:11:01 +0000312 case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
313 case X86::SHLD32rri8:{// A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
Chris Lattnerd54845f2005-01-19 07:31:24 +0000314 unsigned Opc;
315 unsigned Size;
316 switch (MI->getOpcode()) {
317 default: assert(0 && "Unreachable!");
318 case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
319 case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
320 case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
321 case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
322 }
Chris Lattner29478012005-01-19 07:11:01 +0000323 unsigned Amt = MI->getOperand(3).getImmedValue();
324 unsigned A = MI->getOperand(0).getReg();
325 unsigned B = MI->getOperand(1).getReg();
326 unsigned C = MI->getOperand(2).getReg();
Evan Chengdc2c8742006-11-15 20:58:11 +0000327 bool BisKill = MI->getOperand(1).isKill();
328 bool CisKill = MI->getOperand(2).isKill();
Evan Cheng20350c42006-11-27 23:37:22 +0000329 return BuildMI(get(Opc), A).addReg(C, false, false, CisKill)
Evan Chengdc2c8742006-11-15 20:58:11 +0000330 .addReg(B, false, false, BisKill).addImm(Size-Amt);
Chris Lattner29478012005-01-19 07:11:01 +0000331 }
332 default:
333 return TargetInstrInfo::commuteInstruction(MI);
334 }
335}
336
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000337static X86::CondCode GetCondFromBranchOpc(unsigned BrOpc) {
338 switch (BrOpc) {
339 default: return X86::COND_INVALID;
340 case X86::JE: return X86::COND_E;
341 case X86::JNE: return X86::COND_NE;
342 case X86::JL: return X86::COND_L;
343 case X86::JLE: return X86::COND_LE;
344 case X86::JG: return X86::COND_G;
345 case X86::JGE: return X86::COND_GE;
346 case X86::JB: return X86::COND_B;
347 case X86::JBE: return X86::COND_BE;
348 case X86::JA: return X86::COND_A;
349 case X86::JAE: return X86::COND_AE;
350 case X86::JS: return X86::COND_S;
351 case X86::JNS: return X86::COND_NS;
352 case X86::JP: return X86::COND_P;
353 case X86::JNP: return X86::COND_NP;
354 case X86::JO: return X86::COND_O;
355 case X86::JNO: return X86::COND_NO;
356 }
357}
358
359unsigned X86::GetCondBranchFromCond(X86::CondCode CC) {
360 switch (CC) {
361 default: assert(0 && "Illegal condition code!");
362 case X86::COND_E: return X86::JE;
363 case X86::COND_NE: return X86::JNE;
364 case X86::COND_L: return X86::JL;
365 case X86::COND_LE: return X86::JLE;
366 case X86::COND_G: return X86::JG;
367 case X86::COND_GE: return X86::JGE;
368 case X86::COND_B: return X86::JB;
369 case X86::COND_BE: return X86::JBE;
370 case X86::COND_A: return X86::JA;
371 case X86::COND_AE: return X86::JAE;
372 case X86::COND_S: return X86::JS;
373 case X86::COND_NS: return X86::JNS;
374 case X86::COND_P: return X86::JP;
375 case X86::COND_NP: return X86::JNP;
376 case X86::COND_O: return X86::JO;
377 case X86::COND_NO: return X86::JNO;
378 }
379}
380
Chris Lattner3a897f32006-10-21 05:52:40 +0000381/// GetOppositeBranchCondition - Return the inverse of the specified condition,
382/// e.g. turning COND_E to COND_NE.
383X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) {
384 switch (CC) {
385 default: assert(0 && "Illegal condition code!");
386 case X86::COND_E: return X86::COND_NE;
387 case X86::COND_NE: return X86::COND_E;
388 case X86::COND_L: return X86::COND_GE;
389 case X86::COND_LE: return X86::COND_G;
390 case X86::COND_G: return X86::COND_LE;
391 case X86::COND_GE: return X86::COND_L;
392 case X86::COND_B: return X86::COND_AE;
393 case X86::COND_BE: return X86::COND_A;
394 case X86::COND_A: return X86::COND_BE;
395 case X86::COND_AE: return X86::COND_B;
396 case X86::COND_S: return X86::COND_NS;
397 case X86::COND_NS: return X86::COND_S;
398 case X86::COND_P: return X86::COND_NP;
399 case X86::COND_NP: return X86::COND_P;
400 case X86::COND_O: return X86::COND_NO;
401 case X86::COND_NO: return X86::COND_O;
402 }
403}
404
Dale Johannesen616627b2007-06-14 22:03:45 +0000405// For purposes of branch analysis do not count FP_REG_KILL as a terminator.
406bool X86InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
407 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
408 if (MI->getOpcode() == X86::FP_REG_KILL)
409 return false;
410 if (TID->Flags & M_TERMINATOR_FLAG)
411 return !isPredicated(MI);
412 return false;
413}
Chris Lattner3a897f32006-10-21 05:52:40 +0000414
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000415bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
416 MachineBasicBlock *&TBB,
417 MachineBasicBlock *&FBB,
418 std::vector<MachineOperand> &Cond) const {
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000419 // If the block has no terminators, it just falls into the block after it.
420 MachineBasicBlock::iterator I = MBB.end();
Evan Cheng5514bbe2007-06-08 21:59:56 +0000421 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000422 return false;
423
424 // Get the last instruction in the block.
425 MachineInstr *LastInst = I;
426
427 // If there is only one terminator instruction, process it.
Evan Cheng5514bbe2007-06-08 21:59:56 +0000428 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000429 if (!isBranch(LastInst->getOpcode()))
430 return true;
431
432 // If the block ends with a branch there are 3 possibilities:
433 // it's an unconditional, conditional, or indirect branch.
434
435 if (LastInst->getOpcode() == X86::JMP) {
436 TBB = LastInst->getOperand(0).getMachineBasicBlock();
437 return false;
438 }
439 X86::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
440 if (BranchCode == X86::COND_INVALID)
441 return true; // Can't handle indirect branch.
442
443 // Otherwise, block ends with fall-through condbranch.
444 TBB = LastInst->getOperand(0).getMachineBasicBlock();
445 Cond.push_back(MachineOperand::CreateImm(BranchCode));
446 return false;
447 }
448
449 // Get the instruction before it if it's a terminator.
450 MachineInstr *SecondLastInst = I;
451
452 // If there are three terminators, we don't know what sort of block this is.
Dale Johannesen616627b2007-06-14 22:03:45 +0000453 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000454 return true;
455
Chris Lattner74436002006-10-30 22:27:23 +0000456 // If the block ends with X86::JMP and a conditional branch, handle it.
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000457 X86::CondCode BranchCode = GetCondFromBranchOpc(SecondLastInst->getOpcode());
458 if (BranchCode != X86::COND_INVALID && LastInst->getOpcode() == X86::JMP) {
Chris Lattner74436002006-10-30 22:27:23 +0000459 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
460 Cond.push_back(MachineOperand::CreateImm(BranchCode));
461 FBB = LastInst->getOperand(0).getMachineBasicBlock();
462 return false;
463 }
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000464
Dale Johannesenc6855462007-06-13 17:59:52 +0000465 // If the block ends with two X86::JMPs, handle it. The second one is not
466 // executed, so remove it.
467 if (SecondLastInst->getOpcode() == X86::JMP &&
468 LastInst->getOpcode() == X86::JMP) {
469 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
470 I = LastInst;
471 I->eraseFromParent();
472 return false;
473 }
474
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000475 // Otherwise, can't handle this.
476 return true;
477}
478
Evan Chenge20dd922007-05-18 00:18:17 +0000479unsigned X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000480 MachineBasicBlock::iterator I = MBB.end();
Evan Chenge20dd922007-05-18 00:18:17 +0000481 if (I == MBB.begin()) return 0;
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000482 --I;
483 if (I->getOpcode() != X86::JMP &&
484 GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
Evan Chenge20dd922007-05-18 00:18:17 +0000485 return 0;
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000486
487 // Remove the branch.
488 I->eraseFromParent();
489
490 I = MBB.end();
491
Evan Chenge20dd922007-05-18 00:18:17 +0000492 if (I == MBB.begin()) return 1;
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000493 --I;
494 if (GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
Evan Chenge20dd922007-05-18 00:18:17 +0000495 return 1;
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000496
497 // Remove the branch.
498 I->eraseFromParent();
Evan Chenge20dd922007-05-18 00:18:17 +0000499 return 2;
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000500}
501
Evan Chenge20dd922007-05-18 00:18:17 +0000502unsigned
503X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
504 MachineBasicBlock *FBB,
505 const std::vector<MachineOperand> &Cond) const {
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000506 // Shouldn't be a fall through.
507 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Chris Lattner6fca75e2006-10-21 05:34:23 +0000508 assert((Cond.size() == 1 || Cond.size() == 0) &&
509 "X86 branch conditions have one component!");
510
511 if (FBB == 0) { // One way branch.
512 if (Cond.empty()) {
513 // Unconditional branch?
Evan Cheng20350c42006-11-27 23:37:22 +0000514 BuildMI(&MBB, get(X86::JMP)).addMBB(TBB);
Chris Lattner6fca75e2006-10-21 05:34:23 +0000515 } else {
516 // Conditional branch.
517 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
Evan Cheng20350c42006-11-27 23:37:22 +0000518 BuildMI(&MBB, get(Opc)).addMBB(TBB);
Chris Lattner6fca75e2006-10-21 05:34:23 +0000519 }
Evan Chenge20dd922007-05-18 00:18:17 +0000520 return 1;
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000521 }
522
Chris Lattnerd8816602006-10-21 05:42:09 +0000523 // Two-way Conditional branch.
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000524 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
Evan Cheng20350c42006-11-27 23:37:22 +0000525 BuildMI(&MBB, get(Opc)).addMBB(TBB);
526 BuildMI(&MBB, get(X86::JMP)).addMBB(FBB);
Evan Chenge20dd922007-05-18 00:18:17 +0000527 return 2;
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000528}
529
Chris Lattner53ebf202006-10-28 17:29:57 +0000530bool X86InstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
531 if (MBB.empty()) return false;
532
533 switch (MBB.back().getOpcode()) {
Evan Chengfc94eb62007-05-21 18:44:17 +0000534 case X86::RET: // Return.
535 case X86::RETI:
536 case X86::TAILJMPd:
537 case X86::TAILJMPr:
538 case X86::TAILJMPm:
Chris Lattner53ebf202006-10-28 17:29:57 +0000539 case X86::JMP: // Uncond branch.
540 case X86::JMP32r: // Indirect branch.
541 case X86::JMP32m: // Indirect branch through mem.
542 return true;
543 default: return false;
544 }
545}
546
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000547bool X86InstrInfo::
548ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
Chris Lattner3a897f32006-10-21 05:52:40 +0000549 assert(Cond.size() == 1 && "Invalid X86 branch condition!");
550 Cond[0].setImm(GetOppositeBranchCondition((X86::CondCode)Cond[0].getImm()));
551 return false;
Chris Lattnerc0fb5672006-10-20 17:42:20 +0000552}
553
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000554const TargetRegisterClass *X86InstrInfo::getPointerRegClass() const {
555 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
556 if (Subtarget->is64Bit())
557 return &X86::GR64RegClass;
558 else
559 return &X86::GR32RegClass;
560}