1 //===- X86InstrInfo.cpp - X86 Instruction Information -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file contains the X86 implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "X86InstrInfo.h"
16 #include "X86GenInstrInfo.inc"
17 #include "X86InstrBuilder.h"
18 #include "X86Subtarget.h"
19 #include "X86TargetMachine.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 X86InstrInfo::X86InstrInfo(X86TargetMachine &tm)
24 : TargetInstrInfo(X86Insts, sizeof(X86Insts)/sizeof(X86Insts[0])),
25 TM(tm), RI(tm, *this) {
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;
36 bool X86InstrInfo::isMoveInstr(const MachineInstr& MI,
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();
61 unsigned X86InstrInfo::isLoadFromStackSlot(MachineInstr *MI,
62 int &FrameIndex) const {
63 switch (MI->getOpcode()) {
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();
89 unsigned X86InstrInfo::isStoreToStackSlot(MachineInstr *MI,
90 int &FrameIndex) const {
91 switch (MI->getOpcode()) {
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();
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.
125 /// This method returns a null pointer if the transformation cannot be
126 /// performed, otherwise it returns the new instruction.
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();
133 switch (MI->getOpcode()) {
135 case X86::SHUFPSrri: {
136 assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!");
137 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
138 unsigned A = MI->getOperand(0).getReg();
139 unsigned B = MI->getOperand(1).getReg();
140 unsigned C = MI->getOperand(2).getReg();
141 unsigned M = MI->getOperand(3).getImmedValue();
142 if (!Subtarget->hasSSE2() || B != C) return 0;
143 return BuildMI(X86::PSHUFDri, 2, A).addReg(B).addImm(M);
147 // FIXME: None of these instructions are promotable to LEAs without
148 // additional information. In particular, LEA doesn't set the flags that
149 // add and inc do. :(
152 // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When
153 // we have subtarget support, enable the 16-bit LEA generation here.
154 bool DisableLEA16 = true;
156 switch (MI->getOpcode()) {
159 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
160 return addRegOffset(BuildMI(X86::LEA32r, 5, Dest), Src, 1);
163 if (DisableLEA16) return 0;
164 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
165 return addRegOffset(BuildMI(X86::LEA16r, 5, Dest), Src, 1);
168 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
169 return addRegOffset(BuildMI(X86::LEA32r, 5, Dest), Src, -1);
172 if (DisableLEA16) return 0;
173 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
174 return addRegOffset(BuildMI(X86::LEA16r, 5, Dest), Src, -1);
176 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
177 return addRegReg(BuildMI(X86::LEA32r, 5, Dest), Src,
178 MI->getOperand(2).getReg());
180 if (DisableLEA16) return 0;
181 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
182 return addRegReg(BuildMI(X86::LEA16r, 5, Dest), Src,
183 MI->getOperand(2).getReg());
186 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
187 if (MI->getOperand(2).isImmediate())
188 return addRegOffset(BuildMI(X86::LEA32r, 5, Dest), Src,
189 MI->getOperand(2).getImmedValue());
193 if (DisableLEA16) return 0;
194 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
195 if (MI->getOperand(2).isImmediate())
196 return addRegOffset(BuildMI(X86::LEA16r, 5, Dest), Src,
197 MI->getOperand(2).getImmedValue());
201 if (DisableLEA16) return 0;
203 assert(MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate() &&
204 "Unknown shl instruction!");
205 unsigned ShAmt = MI->getOperand(2).getImmedValue();
206 if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) {
208 AM.Scale = 1 << ShAmt;
210 unsigned Opc = MI->getOpcode() == X86::SHL32ri ? X86::LEA32r :X86::LEA16r;
211 return addFullAddress(BuildMI(Opc, 5, Dest), AM);
219 /// commuteInstruction - We have a few instructions that must be hacked on to
222 MachineInstr *X86InstrInfo::commuteInstruction(MachineInstr *MI) const {
223 // FIXME: Can commute cmoves by changing the condition!
224 switch (MI->getOpcode()) {
225 case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
226 case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
227 case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
228 case X86::SHLD32rri8:{// A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
231 switch (MI->getOpcode()) {
232 default: assert(0 && "Unreachable!");
233 case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
234 case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
235 case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
236 case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
238 unsigned Amt = MI->getOperand(3).getImmedValue();
239 unsigned A = MI->getOperand(0).getReg();
240 unsigned B = MI->getOperand(1).getReg();
241 unsigned C = MI->getOperand(2).getReg();
242 return BuildMI(Opc, 3, A).addReg(C).addReg(B).addImm(Size-Amt);
245 return TargetInstrInfo::commuteInstruction(MI);
249 static X86::CondCode GetCondFromBranchOpc(unsigned BrOpc) {
251 default: return X86::COND_INVALID;
252 case X86::JE: return X86::COND_E;
253 case X86::JNE: return X86::COND_NE;
254 case X86::JL: return X86::COND_L;
255 case X86::JLE: return X86::COND_LE;
256 case X86::JG: return X86::COND_G;
257 case X86::JGE: return X86::COND_GE;
258 case X86::JB: return X86::COND_B;
259 case X86::JBE: return X86::COND_BE;
260 case X86::JA: return X86::COND_A;
261 case X86::JAE: return X86::COND_AE;
262 case X86::JS: return X86::COND_S;
263 case X86::JNS: return X86::COND_NS;
264 case X86::JP: return X86::COND_P;
265 case X86::JNP: return X86::COND_NP;
266 case X86::JO: return X86::COND_O;
267 case X86::JNO: return X86::COND_NO;
271 unsigned X86::GetCondBranchFromCond(X86::CondCode CC) {
273 default: assert(0 && "Illegal condition code!");
274 case X86::COND_E: return X86::JE;
275 case X86::COND_NE: return X86::JNE;
276 case X86::COND_L: return X86::JL;
277 case X86::COND_LE: return X86::JLE;
278 case X86::COND_G: return X86::JG;
279 case X86::COND_GE: return X86::JGE;
280 case X86::COND_B: return X86::JB;
281 case X86::COND_BE: return X86::JBE;
282 case X86::COND_A: return X86::JA;
283 case X86::COND_AE: return X86::JAE;
284 case X86::COND_S: return X86::JS;
285 case X86::COND_NS: return X86::JNS;
286 case X86::COND_P: return X86::JP;
287 case X86::COND_NP: return X86::JNP;
288 case X86::COND_O: return X86::JO;
289 case X86::COND_NO: return X86::JNO;
293 /// GetOppositeBranchCondition - Return the inverse of the specified condition,
294 /// e.g. turning COND_E to COND_NE.
295 X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) {
297 default: assert(0 && "Illegal condition code!");
298 case X86::COND_E: return X86::COND_NE;
299 case X86::COND_NE: return X86::COND_E;
300 case X86::COND_L: return X86::COND_GE;
301 case X86::COND_LE: return X86::COND_G;
302 case X86::COND_G: return X86::COND_LE;
303 case X86::COND_GE: return X86::COND_L;
304 case X86::COND_B: return X86::COND_AE;
305 case X86::COND_BE: return X86::COND_A;
306 case X86::COND_A: return X86::COND_BE;
307 case X86::COND_AE: return X86::COND_B;
308 case X86::COND_S: return X86::COND_NS;
309 case X86::COND_NS: return X86::COND_S;
310 case X86::COND_P: return X86::COND_NP;
311 case X86::COND_NP: return X86::COND_P;
312 case X86::COND_O: return X86::COND_NO;
313 case X86::COND_NO: return X86::COND_O;
318 bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
319 MachineBasicBlock *&TBB,
320 MachineBasicBlock *&FBB,
321 std::vector<MachineOperand> &Cond) const {
322 // TODO: If FP_REG_KILL is around, ignore it.
324 // If the block has no terminators, it just falls into the block after it.
325 MachineBasicBlock::iterator I = MBB.end();
326 if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode()))
329 // Get the last instruction in the block.
330 MachineInstr *LastInst = I;
332 // If there is only one terminator instruction, process it.
333 if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode())) {
334 if (!isBranch(LastInst->getOpcode()))
337 // If the block ends with a branch there are 3 possibilities:
338 // it's an unconditional, conditional, or indirect branch.
340 if (LastInst->getOpcode() == X86::JMP) {
341 TBB = LastInst->getOperand(0).getMachineBasicBlock();
344 X86::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
345 if (BranchCode == X86::COND_INVALID)
346 return true; // Can't handle indirect branch.
348 // Otherwise, block ends with fall-through condbranch.
349 TBB = LastInst->getOperand(0).getMachineBasicBlock();
350 Cond.push_back(MachineOperand::CreateImm(BranchCode));
354 // Get the instruction before it if it's a terminator.
355 MachineInstr *SecondLastInst = I;
357 // If there are three terminators, we don't know what sort of block this is.
358 if (SecondLastInst && I != MBB.begin() &&
359 isTerminatorInstr((--I)->getOpcode()))
362 // If the block ends with X86::JMP and a conditional branch, handle it.
363 X86::CondCode BranchCode = GetCondFromBranchOpc(SecondLastInst->getOpcode());
364 if (BranchCode != X86::COND_INVALID && LastInst->getOpcode() == X86::JMP) {
365 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
366 Cond.push_back(MachineOperand::CreateImm(BranchCode));
367 FBB = LastInst->getOperand(0).getMachineBasicBlock();
371 // Otherwise, can't handle this.
375 void X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
376 MachineBasicBlock::iterator I = MBB.end();
377 if (I == MBB.begin()) return;
379 if (I->getOpcode() != X86::JMP &&
380 GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
383 // Remove the branch.
384 I->eraseFromParent();
388 if (I == MBB.begin()) return;
390 if (GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
393 // Remove the branch.
394 I->eraseFromParent();
397 void X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
398 MachineBasicBlock *FBB,
399 const std::vector<MachineOperand> &Cond) const {
400 // Shouldn't be a fall through.
401 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
402 assert((Cond.size() == 1 || Cond.size() == 0) &&
403 "X86 branch conditions have one component!");
405 if (FBB == 0) { // One way branch.
407 // Unconditional branch?
408 BuildMI(&MBB, X86::JMP, 1).addMBB(TBB);
410 // Conditional branch.
411 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
412 BuildMI(&MBB, Opc, 1).addMBB(TBB);
417 // Two-way Conditional branch.
418 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
419 BuildMI(&MBB, Opc, 1).addMBB(TBB);
420 BuildMI(&MBB, X86::JMP, 1).addMBB(FBB);
423 bool X86InstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
424 if (MBB.empty()) return false;
426 switch (MBB.back().getOpcode()) {
427 case X86::JMP: // Uncond branch.
428 case X86::JMP32r: // Indirect branch.
429 case X86::JMP32m: // Indirect branch through mem.
431 default: return false;
436 ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
437 assert(Cond.size() == 1 && "Invalid X86 branch condition!");
438 Cond[0].setImm(GetOppositeBranchCondition((X86::CondCode)Cond[0].getImm()));
442 const TargetRegisterClass *X86InstrInfo::getPointerRegClass() const {
443 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
444 if (Subtarget->is64Bit())
445 return &X86::GR64RegClass;
447 return &X86::GR32RegClass;