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"
21 #include "llvm/CodeGen/LiveVariables.h"
22 #include "llvm/CodeGen/SSARegMap.h"
25 X86InstrInfo::X86InstrInfo(X86TargetMachine &tm)
26 : TargetInstrInfo(X86Insts, sizeof(X86Insts)/sizeof(X86Insts[0])),
27 TM(tm), RI(tm, *this) {
30 bool X86InstrInfo::isMoveInstr(const MachineInstr& MI,
32 unsigned& destReg) const {
33 MachineOpCode oc = MI.getOpcode();
34 if (oc == X86::MOV8rr || oc == X86::MOV16rr ||
35 oc == X86::MOV32rr || oc == X86::MOV64rr ||
36 oc == X86::MOV16to16_ || oc == X86::MOV32to32_ ||
37 oc == X86::MOV_Fp3232 || oc == X86::MOVSSrr || oc == X86::MOVSDrr ||
38 oc == X86::MOV_Fp3264 || oc == X86::MOV_Fp6432 || oc == X86::MOV_Fp6464 ||
39 oc == X86::FsMOVAPSrr || oc == X86::FsMOVAPDrr ||
40 oc == X86::MOVAPSrr || oc == X86::MOVAPDrr ||
41 oc == X86::MOVSS2PSrr || oc == X86::MOVSD2PDrr ||
42 oc == X86::MOVPS2SSrr || oc == X86::MOVPD2SDrr ||
43 oc == X86::MMX_MOVD64rr || oc == X86::MMX_MOVQ64rr) {
44 assert(MI.getNumOperands() >= 2 &&
45 MI.getOperand(0).isRegister() &&
46 MI.getOperand(1).isRegister() &&
47 "invalid register-register move instruction");
48 sourceReg = MI.getOperand(1).getReg();
49 destReg = MI.getOperand(0).getReg();
55 unsigned X86InstrInfo::isLoadFromStackSlot(MachineInstr *MI,
56 int &FrameIndex) const {
57 switch (MI->getOpcode()) {
70 case X86::MMX_MOVD64rm:
71 case X86::MMX_MOVQ64rm:
72 if (MI->getOperand(1).isFrameIndex() && MI->getOperand(2).isImmediate() &&
73 MI->getOperand(3).isRegister() && MI->getOperand(4).isImmediate() &&
74 MI->getOperand(2).getImmedValue() == 1 &&
75 MI->getOperand(3).getReg() == 0 &&
76 MI->getOperand(4).getImmedValue() == 0) {
77 FrameIndex = MI->getOperand(1).getFrameIndex();
78 return MI->getOperand(0).getReg();
85 unsigned X86InstrInfo::isStoreToStackSlot(MachineInstr *MI,
86 int &FrameIndex) const {
87 switch (MI->getOpcode()) {
100 case X86::MMX_MOVD64mr:
101 case X86::MMX_MOVQ64mr:
102 case X86::MMX_MOVNTQmr:
103 if (MI->getOperand(0).isFrameIndex() && MI->getOperand(1).isImmediate() &&
104 MI->getOperand(2).isRegister() && MI->getOperand(3).isImmediate() &&
105 MI->getOperand(1).getImmedValue() == 1 &&
106 MI->getOperand(2).getReg() == 0 &&
107 MI->getOperand(3).getImmedValue() == 0) {
108 FrameIndex = MI->getOperand(0).getFrameIndex();
109 return MI->getOperand(4).getReg();
117 bool X86InstrInfo::isReallyTriviallyReMaterializable(MachineInstr *MI) const {
118 switch (MI->getOpcode()) {
131 case X86::MMX_MOVD64rm:
132 case X86::MMX_MOVQ64rm:
133 // Loads from constant pools are trivially rematerializable.
134 return MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate() &&
135 MI->getOperand(3).isRegister() && MI->getOperand(4).isConstantPoolIndex() &&
136 MI->getOperand(1).getReg() == 0 &&
137 MI->getOperand(2).getImmedValue() == 1 &&
138 MI->getOperand(3).getReg() == 0;
140 // All other instructions marked M_REMATERIALIZABLE are always trivially
145 /// convertToThreeAddress - This method must be implemented by targets that
146 /// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
147 /// may be able to convert a two-address instruction into a true
148 /// three-address instruction on demand. This allows the X86 target (for
149 /// example) to convert ADD and SHL instructions into LEA instructions if they
150 /// would require register copies due to two-addressness.
152 /// This method returns a null pointer if the transformation cannot be
153 /// performed, otherwise it returns the new instruction.
156 X86InstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
157 MachineBasicBlock::iterator &MBBI,
158 LiveVariables &LV) const {
159 MachineInstr *MI = MBBI;
160 // All instructions input are two-addr instructions. Get the known operands.
161 unsigned Dest = MI->getOperand(0).getReg();
162 unsigned Src = MI->getOperand(1).getReg();
164 MachineInstr *NewMI = NULL;
165 // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When
166 // we have better subtarget support, enable the 16-bit LEA generation here.
167 bool DisableLEA16 = true;
169 switch (MI->getOpcode()) {
171 case X86::SHUFPSrri: {
172 assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!");
173 if (!TM.getSubtarget<X86Subtarget>().hasSSE2()) return 0;
175 unsigned A = MI->getOperand(0).getReg();
176 unsigned B = MI->getOperand(1).getReg();
177 unsigned C = MI->getOperand(2).getReg();
178 unsigned M = MI->getOperand(3).getImm();
179 if (B != C) return 0;
180 NewMI = BuildMI(get(X86::PSHUFDri), A).addReg(B).addImm(M);
184 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
185 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
186 // the flags produced by a shift yet, so this is safe.
187 unsigned Dest = MI->getOperand(0).getReg();
188 unsigned Src = MI->getOperand(1).getReg();
189 unsigned ShAmt = MI->getOperand(2).getImm();
190 if (ShAmt == 0 || ShAmt >= 4) return 0;
192 NewMI = BuildMI(get(X86::LEA64r), Dest)
193 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
197 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
198 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
199 // the flags produced by a shift yet, so this is safe.
200 unsigned Dest = MI->getOperand(0).getReg();
201 unsigned Src = MI->getOperand(1).getReg();
202 unsigned ShAmt = MI->getOperand(2).getImm();
203 if (ShAmt == 0 || ShAmt >= 4) return 0;
205 unsigned Opc = TM.getSubtarget<X86Subtarget>().is64Bit() ?
206 X86::LEA64_32r : X86::LEA32r;
207 NewMI = BuildMI(get(Opc), Dest)
208 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
212 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
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;
221 // If 16-bit LEA is disabled, use 32-bit LEA via subregisters.
222 SSARegMap *RegMap = MFI->getParent()->getSSARegMap();
223 unsigned Opc, leaInReg, leaOutReg;
224 MVT::ValueType leaVT;
225 if (TM.getSubtarget<X86Subtarget>().is64Bit()) {
226 Opc = X86::LEA64_32r;
228 leaInReg = RegMap->createVirtualRegister(&X86::GR64RegClass);
229 leaOutReg = RegMap->createVirtualRegister(&X86::GR64RegClass);
233 leaInReg = RegMap->createVirtualRegister(&X86::GR32RegClass);
234 leaOutReg = RegMap->createVirtualRegister(&X86::GR32RegClass);
237 MachineInstr *Ins = NULL, *Ext = NULL;
239 Ins = BuildMI(get(X86::INSERT_SUBREG), leaInReg).addReg(Src).addImm(2);
240 Ins->copyKillDeadInfo(MI);
242 NewMI = BuildMI(get(Opc), leaOutReg)
243 .addReg(0).addImm(1 << ShAmt).addReg(leaInReg).addImm(0);
245 Ext = BuildMI(get(X86::EXTRACT_SUBREG), Dest).addReg(leaOutReg).addImm(2);
246 Ext->copyKillDeadInfo(MI);
248 MFI->insert(MBBI, Ins); // Insert the insert_subreg
249 LV.instructionChanged(MI, NewMI); // Update live variables
250 LV.addVirtualRegisterKilled(leaInReg, NewMI);
251 MFI->insert(MBBI, NewMI); // Insert the new inst
252 LV.addVirtualRegisterKilled(leaOutReg, Ext);
253 MFI->insert(MBBI, Ext); // Insert the extract_subreg
257 NewMI = BuildMI(get(X86::LEA16r), Dest)
258 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
264 // FIXME: None of these instructions are promotable to LEAs without
265 // additional information. In particular, LEA doesn't set the flags that
266 // add and inc do. :(
268 switch (MI->getOpcode()) {
271 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
272 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, 1);
276 if (DisableLEA16) return 0;
277 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
278 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, 1);
282 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
283 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, -1);
287 if (DisableLEA16) return 0;
288 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
289 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, -1);
292 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
293 NewMI = addRegReg(BuildMI(get(X86::LEA32r), Dest), Src,
294 MI->getOperand(2).getReg());
297 if (DisableLEA16) return 0;
298 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
299 NewMI = addRegReg(BuildMI(get(X86::LEA16r), Dest), Src,
300 MI->getOperand(2).getReg());
304 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
305 if (MI->getOperand(2).isImmediate())
306 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src,
307 MI->getOperand(2).getImmedValue());
311 if (DisableLEA16) return 0;
312 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
313 if (MI->getOperand(2).isImmediate())
314 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src,
315 MI->getOperand(2).getImmedValue());
318 if (DisableLEA16) return 0;
320 assert(MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate() &&
321 "Unknown shl instruction!");
322 unsigned ShAmt = MI->getOperand(2).getImmedValue();
323 if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) {
325 AM.Scale = 1 << ShAmt;
327 unsigned Opc = MI->getOpcode() == X86::SHL32ri ? X86::LEA32r :X86::LEA16r;
328 NewMI = addFullAddress(BuildMI(get(Opc), Dest), AM);
334 NewMI->copyKillDeadInfo(MI);
335 LV.instructionChanged(MI, NewMI); // Update live variables
336 MFI->insert(MBBI, NewMI); // Insert the new inst
341 /// commuteInstruction - We have a few instructions that must be hacked on to
344 MachineInstr *X86InstrInfo::commuteInstruction(MachineInstr *MI) const {
345 // FIXME: Can commute cmoves by changing the condition!
346 switch (MI->getOpcode()) {
347 case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
348 case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
349 case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
350 case X86::SHLD32rri8:{// A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
353 switch (MI->getOpcode()) {
354 default: assert(0 && "Unreachable!");
355 case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
356 case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
357 case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
358 case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
360 unsigned Amt = MI->getOperand(3).getImmedValue();
361 unsigned A = MI->getOperand(0).getReg();
362 unsigned B = MI->getOperand(1).getReg();
363 unsigned C = MI->getOperand(2).getReg();
364 bool BisKill = MI->getOperand(1).isKill();
365 bool CisKill = MI->getOperand(2).isKill();
366 return BuildMI(get(Opc), A).addReg(C, false, false, CisKill)
367 .addReg(B, false, false, BisKill).addImm(Size-Amt);
370 return TargetInstrInfo::commuteInstruction(MI);
374 static X86::CondCode GetCondFromBranchOpc(unsigned BrOpc) {
376 default: return X86::COND_INVALID;
377 case X86::JE: return X86::COND_E;
378 case X86::JNE: return X86::COND_NE;
379 case X86::JL: return X86::COND_L;
380 case X86::JLE: return X86::COND_LE;
381 case X86::JG: return X86::COND_G;
382 case X86::JGE: return X86::COND_GE;
383 case X86::JB: return X86::COND_B;
384 case X86::JBE: return X86::COND_BE;
385 case X86::JA: return X86::COND_A;
386 case X86::JAE: return X86::COND_AE;
387 case X86::JS: return X86::COND_S;
388 case X86::JNS: return X86::COND_NS;
389 case X86::JP: return X86::COND_P;
390 case X86::JNP: return X86::COND_NP;
391 case X86::JO: return X86::COND_O;
392 case X86::JNO: return X86::COND_NO;
396 unsigned X86::GetCondBranchFromCond(X86::CondCode CC) {
398 default: assert(0 && "Illegal condition code!");
399 case X86::COND_E: return X86::JE;
400 case X86::COND_NE: return X86::JNE;
401 case X86::COND_L: return X86::JL;
402 case X86::COND_LE: return X86::JLE;
403 case X86::COND_G: return X86::JG;
404 case X86::COND_GE: return X86::JGE;
405 case X86::COND_B: return X86::JB;
406 case X86::COND_BE: return X86::JBE;
407 case X86::COND_A: return X86::JA;
408 case X86::COND_AE: return X86::JAE;
409 case X86::COND_S: return X86::JS;
410 case X86::COND_NS: return X86::JNS;
411 case X86::COND_P: return X86::JP;
412 case X86::COND_NP: return X86::JNP;
413 case X86::COND_O: return X86::JO;
414 case X86::COND_NO: return X86::JNO;
418 /// GetOppositeBranchCondition - Return the inverse of the specified condition,
419 /// e.g. turning COND_E to COND_NE.
420 X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) {
422 default: assert(0 && "Illegal condition code!");
423 case X86::COND_E: return X86::COND_NE;
424 case X86::COND_NE: return X86::COND_E;
425 case X86::COND_L: return X86::COND_GE;
426 case X86::COND_LE: return X86::COND_G;
427 case X86::COND_G: return X86::COND_LE;
428 case X86::COND_GE: return X86::COND_L;
429 case X86::COND_B: return X86::COND_AE;
430 case X86::COND_BE: return X86::COND_A;
431 case X86::COND_A: return X86::COND_BE;
432 case X86::COND_AE: return X86::COND_B;
433 case X86::COND_S: return X86::COND_NS;
434 case X86::COND_NS: return X86::COND_S;
435 case X86::COND_P: return X86::COND_NP;
436 case X86::COND_NP: return X86::COND_P;
437 case X86::COND_O: return X86::COND_NO;
438 case X86::COND_NO: return X86::COND_O;
442 bool X86InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
443 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
444 if (TID->Flags & M_TERMINATOR_FLAG) {
445 // Conditional branch is a special case.
446 if ((TID->Flags & M_BRANCH_FLAG) != 0 && (TID->Flags & M_BARRIER_FLAG) == 0)
448 if ((TID->Flags & M_PREDICABLE) == 0)
450 return !isPredicated(MI);
455 // For purposes of branch analysis do not count FP_REG_KILL as a terminator.
456 static bool isBrAnalysisUnpredicatedTerminator(const MachineInstr *MI,
457 const X86InstrInfo &TII) {
458 if (MI->getOpcode() == X86::FP_REG_KILL)
460 return TII.isUnpredicatedTerminator(MI);
463 bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
464 MachineBasicBlock *&TBB,
465 MachineBasicBlock *&FBB,
466 std::vector<MachineOperand> &Cond) const {
467 // If the block has no terminators, it just falls into the block after it.
468 MachineBasicBlock::iterator I = MBB.end();
469 if (I == MBB.begin() || !isBrAnalysisUnpredicatedTerminator(--I, *this))
472 // Get the last instruction in the block.
473 MachineInstr *LastInst = I;
475 // If there is only one terminator instruction, process it.
476 if (I == MBB.begin() || !isBrAnalysisUnpredicatedTerminator(--I, *this)) {
477 if (!isBranch(LastInst->getOpcode()))
480 // If the block ends with a branch there are 3 possibilities:
481 // it's an unconditional, conditional, or indirect branch.
483 if (LastInst->getOpcode() == X86::JMP) {
484 TBB = LastInst->getOperand(0).getMachineBasicBlock();
487 X86::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
488 if (BranchCode == X86::COND_INVALID)
489 return true; // Can't handle indirect branch.
491 // Otherwise, block ends with fall-through condbranch.
492 TBB = LastInst->getOperand(0).getMachineBasicBlock();
493 Cond.push_back(MachineOperand::CreateImm(BranchCode));
497 // Get the instruction before it if it's a terminator.
498 MachineInstr *SecondLastInst = I;
500 // If there are three terminators, we don't know what sort of block this is.
501 if (SecondLastInst && I != MBB.begin() &&
502 isBrAnalysisUnpredicatedTerminator(--I, *this))
505 // If the block ends with X86::JMP and a conditional branch, handle it.
506 X86::CondCode BranchCode = GetCondFromBranchOpc(SecondLastInst->getOpcode());
507 if (BranchCode != X86::COND_INVALID && LastInst->getOpcode() == X86::JMP) {
508 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
509 Cond.push_back(MachineOperand::CreateImm(BranchCode));
510 FBB = LastInst->getOperand(0).getMachineBasicBlock();
514 // If the block ends with two X86::JMPs, handle it. The second one is not
515 // executed, so remove it.
516 if (SecondLastInst->getOpcode() == X86::JMP &&
517 LastInst->getOpcode() == X86::JMP) {
518 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
520 I->eraseFromParent();
524 // Otherwise, can't handle this.
528 unsigned X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
529 MachineBasicBlock::iterator I = MBB.end();
530 if (I == MBB.begin()) return 0;
532 if (I->getOpcode() != X86::JMP &&
533 GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
536 // Remove the branch.
537 I->eraseFromParent();
541 if (I == MBB.begin()) return 1;
543 if (GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
546 // Remove the branch.
547 I->eraseFromParent();
552 X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
553 MachineBasicBlock *FBB,
554 const std::vector<MachineOperand> &Cond) const {
555 // Shouldn't be a fall through.
556 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
557 assert((Cond.size() == 1 || Cond.size() == 0) &&
558 "X86 branch conditions have one component!");
560 if (FBB == 0) { // One way branch.
562 // Unconditional branch?
563 BuildMI(&MBB, get(X86::JMP)).addMBB(TBB);
565 // Conditional branch.
566 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
567 BuildMI(&MBB, get(Opc)).addMBB(TBB);
572 // Two-way Conditional branch.
573 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
574 BuildMI(&MBB, get(Opc)).addMBB(TBB);
575 BuildMI(&MBB, get(X86::JMP)).addMBB(FBB);
579 bool X86InstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
580 if (MBB.empty()) return false;
582 switch (MBB.back().getOpcode()) {
583 case X86::RET: // Return.
588 case X86::JMP: // Uncond branch.
589 case X86::JMP32r: // Indirect branch.
590 case X86::JMP32m: // Indirect branch through mem.
592 default: return false;
597 ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
598 assert(Cond.size() == 1 && "Invalid X86 branch condition!");
599 Cond[0].setImm(GetOppositeBranchCondition((X86::CondCode)Cond[0].getImm()));
603 const TargetRegisterClass *X86InstrInfo::getPointerRegClass() const {
604 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
605 if (Subtarget->is64Bit())
606 return &X86::GR64RegClass;
608 return &X86::GR32RegClass;