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"
24 X86InstrInfo::X86InstrInfo(X86TargetMachine &tm)
25 : TargetInstrInfo(X86Insts, sizeof(X86Insts)/sizeof(X86Insts[0])),
26 TM(tm), RI(tm, *this) {
29 bool X86InstrInfo::isMoveInstr(const MachineInstr& MI,
31 unsigned& destReg) const {
32 MachineOpCode oc = MI.getOpcode();
33 if (oc == X86::MOV8rr || oc == X86::MOV16rr ||
34 oc == X86::MOV32rr || oc == X86::MOV64rr ||
35 oc == X86::MOV16to16_ || oc == X86::MOV32to32_ ||
36 oc == X86::FpMOV || oc == X86::MOVSSrr || oc == X86::MOVSDrr ||
37 oc == X86::FsMOVAPSrr || oc == X86::FsMOVAPDrr ||
38 oc == X86::MOVAPSrr || oc == X86::MOVAPDrr ||
39 oc == X86::MOVSS2PSrr || oc == X86::MOVSD2PDrr ||
40 oc == X86::MOVPS2SSrr || oc == X86::MOVPD2SDrr ||
41 oc == X86::MMX_MOVD64rr || oc == X86::MMX_MOVQ64rr) {
42 assert(MI.getNumOperands() >= 2 &&
43 MI.getOperand(0).isRegister() &&
44 MI.getOperand(1).isRegister() &&
45 "invalid register-register move instruction");
46 sourceReg = MI.getOperand(1).getReg();
47 destReg = MI.getOperand(0).getReg();
53 unsigned X86InstrInfo::isLoadFromStackSlot(MachineInstr *MI,
54 int &FrameIndex) const {
55 switch (MI->getOpcode()) {
68 case X86::MMX_MOVD64rm:
69 case X86::MMX_MOVQ64rm:
70 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();
83 unsigned X86InstrInfo::isStoreToStackSlot(MachineInstr *MI,
84 int &FrameIndex) const {
85 switch (MI->getOpcode()) {
98 case X86::MMX_MOVD64mr:
99 case X86::MMX_MOVQ64mr:
100 case X86::MMX_MOVNTQmr:
101 if (MI->getOperand(0).isFrameIndex() && MI->getOperand(1).isImmediate() &&
102 MI->getOperand(2).isRegister() && MI->getOperand(3).isImmediate() &&
103 MI->getOperand(1).getImmedValue() == 1 &&
104 MI->getOperand(2).getReg() == 0 &&
105 MI->getOperand(3).getImmedValue() == 0) {
106 FrameIndex = MI->getOperand(0).getFrameIndex();
107 return MI->getOperand(4).getReg();
115 bool X86InstrInfo::isReallyTriviallyReMaterializable(MachineInstr *MI) const {
116 switch (MI->getOpcode()) {
129 case X86::MMX_MOVD64rm:
130 case X86::MMX_MOVQ64rm:
131 // Loads from constant pools are trivially rematerializable.
132 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;
138 // All other instructions marked M_REMATERIALIZABLE are always trivially
143 /// 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.
150 /// This method returns a null pointer if the transformation cannot be
151 /// performed, otherwise it returns the new instruction.
154 X86InstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
155 MachineBasicBlock::iterator &MBBI,
156 LiveVariables &LV) const {
157 MachineInstr *MI = MBBI;
158 // 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();
162 MachineInstr *NewMI = NULL;
163 // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When
164 // we have better subtarget support, enable the 16-bit LEA generation here.
165 bool DisableLEA16 = true;
167 switch (MI->getOpcode()) {
169 case X86::SHUFPSrri: {
170 assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!");
171 if (!TM.getSubtarget<X86Subtarget>().hasSSE2()) return 0;
173 unsigned A = MI->getOperand(0).getReg();
174 unsigned B = MI->getOperand(1).getReg();
175 unsigned C = MI->getOperand(2).getReg();
176 unsigned M = MI->getOperand(3).getImm();
177 if (B != C) return 0;
178 NewMI = BuildMI(get(X86::PSHUFDri), A).addReg(B).addImm(M);
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;
190 NewMI = BuildMI(get(X86::LEA64r), Dest)
191 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
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;
203 unsigned Opc = TM.getSubtarget<X86Subtarget>().is64Bit() ?
204 X86::LEA64_32r : X86::LEA32r;
205 NewMI = BuildMI(get(Opc), Dest)
206 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
210 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
211 if (DisableLEA16) return 0;
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;
220 NewMI = BuildMI(get(X86::LEA16r), Dest)
221 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
226 // FIXME: None of these instructions are promotable to LEAs without
227 // additional information. In particular, LEA doesn't set the flags that
228 // add and inc do. :(
230 switch (MI->getOpcode()) {
233 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
234 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, 1);
238 if (DisableLEA16) return 0;
239 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
240 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, 1);
244 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
245 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, -1);
249 if (DisableLEA16) return 0;
250 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
251 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, -1);
254 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
255 NewMI = addRegReg(BuildMI(get(X86::LEA32r), Dest), Src,
256 MI->getOperand(2).getReg());
259 if (DisableLEA16) return 0;
260 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
261 NewMI = addRegReg(BuildMI(get(X86::LEA16r), Dest), Src,
262 MI->getOperand(2).getReg());
266 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
267 if (MI->getOperand(2).isImmediate())
268 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src,
269 MI->getOperand(2).getImmedValue());
273 if (DisableLEA16) return 0;
274 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
275 if (MI->getOperand(2).isImmediate())
276 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src,
277 MI->getOperand(2).getImmedValue());
280 if (DisableLEA16) return 0;
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) {
287 AM.Scale = 1 << ShAmt;
289 unsigned Opc = MI->getOpcode() == X86::SHL32ri ? X86::LEA32r :X86::LEA16r;
290 NewMI = addFullAddress(BuildMI(get(Opc), Dest), AM);
296 NewMI->copyKillDeadInfo(MI);
297 LV.instructionChanged(MI, NewMI); // Update live variables
298 MFI->insert(MBBI, NewMI); // Insert the new inst
303 /// commuteInstruction - We have a few instructions that must be hacked on to
306 MachineInstr *X86InstrInfo::commuteInstruction(MachineInstr *MI) const {
307 // FIXME: Can commute cmoves by changing the condition!
308 switch (MI->getOpcode()) {
309 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)
311 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)
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;
322 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();
326 bool BisKill = MI->getOperand(1).isKill();
327 bool CisKill = MI->getOperand(2).isKill();
328 return BuildMI(get(Opc), A).addReg(C, false, false, CisKill)
329 .addReg(B, false, false, BisKill).addImm(Size-Amt);
332 return TargetInstrInfo::commuteInstruction(MI);
336 static X86::CondCode GetCondFromBranchOpc(unsigned 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;
358 unsigned X86::GetCondBranchFromCond(X86::CondCode 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;
380 /// GetOppositeBranchCondition - Return the inverse of the specified condition,
381 /// e.g. turning COND_E to COND_NE.
382 X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode 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;
404 // For purposes of branch analysis do not count FP_REG_KILL as a terminator.
405 bool X86InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
406 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
407 if (MI->getOpcode() == X86::FP_REG_KILL)
409 if (TID->Flags & M_TERMINATOR_FLAG)
410 return !isPredicated(MI);
414 bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
415 MachineBasicBlock *&TBB,
416 MachineBasicBlock *&FBB,
417 std::vector<MachineOperand> &Cond) const {
418 // If the block has no terminators, it just falls into the block after it.
419 MachineBasicBlock::iterator I = MBB.end();
420 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
423 // Get the last instruction in the block.
424 MachineInstr *LastInst = I;
426 // If there is only one terminator instruction, process it.
427 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
428 if (!isBranch(LastInst->getOpcode()))
431 // If the block ends with a branch there are 3 possibilities:
432 // it's an unconditional, conditional, or indirect branch.
434 if (LastInst->getOpcode() == X86::JMP) {
435 TBB = LastInst->getOperand(0).getMachineBasicBlock();
438 X86::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
439 if (BranchCode == X86::COND_INVALID)
440 return true; // Can't handle indirect branch.
442 // Otherwise, block ends with fall-through condbranch.
443 TBB = LastInst->getOperand(0).getMachineBasicBlock();
444 Cond.push_back(MachineOperand::CreateImm(BranchCode));
448 // Get the instruction before it if it's a terminator.
449 MachineInstr *SecondLastInst = I;
451 // If there are three terminators, we don't know what sort of block this is.
452 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
455 // If the block ends with X86::JMP and a conditional branch, handle it.
456 X86::CondCode BranchCode = GetCondFromBranchOpc(SecondLastInst->getOpcode());
457 if (BranchCode != X86::COND_INVALID && LastInst->getOpcode() == X86::JMP) {
458 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
459 Cond.push_back(MachineOperand::CreateImm(BranchCode));
460 FBB = LastInst->getOperand(0).getMachineBasicBlock();
464 // 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();
470 I->eraseFromParent();
474 // Otherwise, can't handle this.
478 unsigned X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
479 MachineBasicBlock::iterator I = MBB.end();
480 if (I == MBB.begin()) return 0;
482 if (I->getOpcode() != X86::JMP &&
483 GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
486 // Remove the branch.
487 I->eraseFromParent();
491 if (I == MBB.begin()) return 1;
493 if (GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
496 // Remove the branch.
497 I->eraseFromParent();
502 X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
503 MachineBasicBlock *FBB,
504 const std::vector<MachineOperand> &Cond) const {
505 // Shouldn't be a fall through.
506 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
507 assert((Cond.size() == 1 || Cond.size() == 0) &&
508 "X86 branch conditions have one component!");
510 if (FBB == 0) { // One way branch.
512 // Unconditional branch?
513 BuildMI(&MBB, get(X86::JMP)).addMBB(TBB);
515 // Conditional branch.
516 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
517 BuildMI(&MBB, get(Opc)).addMBB(TBB);
522 // Two-way Conditional branch.
523 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
524 BuildMI(&MBB, get(Opc)).addMBB(TBB);
525 BuildMI(&MBB, get(X86::JMP)).addMBB(FBB);
529 bool X86InstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
530 if (MBB.empty()) return false;
532 switch (MBB.back().getOpcode()) {
533 case X86::RET: // Return.
538 case X86::JMP: // Uncond branch.
539 case X86::JMP32r: // Indirect branch.
540 case X86::JMP32m: // Indirect branch through mem.
542 default: return false;
547 ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
548 assert(Cond.size() == 1 && "Invalid X86 branch condition!");
549 Cond[0].setImm(GetOppositeBranchCondition((X86::CondCode)Cond[0].getImm()));
553 const TargetRegisterClass *X86InstrInfo::getPointerRegClass() const {
554 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
555 if (Subtarget->is64Bit())
556 return &X86::GR64RegClass;
558 return &X86::GR32RegClass;