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::isTriviallyReMaterializable(MachineInstr *MI) const {
116 switch (MI->getOpcode()) {
123 case X86::MMX_V_SET0:
124 case X86::MMX_V_SETALLONES:
126 case X86::V_SETALLONES:
127 // These instructions are always trivially rematerializable.
140 case X86::MMX_MOVD64rm:
141 case X86::MMX_MOVQ64rm:
142 // Loads from constant pools are trivially rematerializable.
143 return MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate() &&
144 MI->getOperand(3).isRegister() && MI->getOperand(4).isConstantPoolIndex() &&
145 MI->getOperand(1).getReg() == 0 &&
146 MI->getOperand(2).getImmedValue() == 1 &&
147 MI->getOperand(3).getReg() == 0;
152 /// convertToThreeAddress - This method must be implemented by targets that
153 /// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
154 /// may be able to convert a two-address instruction into a true
155 /// three-address instruction on demand. This allows the X86 target (for
156 /// example) to convert ADD and SHL instructions into LEA instructions if they
157 /// would require register copies due to two-addressness.
159 /// This method returns a null pointer if the transformation cannot be
160 /// performed, otherwise it returns the new instruction.
163 X86InstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
164 MachineBasicBlock::iterator &MBBI,
165 LiveVariables &LV) const {
166 MachineInstr *MI = MBBI;
167 // All instructions input are two-addr instructions. Get the known operands.
168 unsigned Dest = MI->getOperand(0).getReg();
169 unsigned Src = MI->getOperand(1).getReg();
171 MachineInstr *NewMI = NULL;
172 // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When
173 // we have better subtarget support, enable the 16-bit LEA generation here.
174 bool DisableLEA16 = true;
176 switch (MI->getOpcode()) {
178 case X86::SHUFPSrri: {
179 assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!");
180 if (!TM.getSubtarget<X86Subtarget>().hasSSE2()) return 0;
182 unsigned A = MI->getOperand(0).getReg();
183 unsigned B = MI->getOperand(1).getReg();
184 unsigned C = MI->getOperand(2).getReg();
185 unsigned M = MI->getOperand(3).getImm();
186 if (B != C) return 0;
187 NewMI = BuildMI(get(X86::PSHUFDri), A).addReg(B).addImm(M);
191 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
192 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
193 // the flags produced by a shift yet, so this is safe.
194 unsigned Dest = MI->getOperand(0).getReg();
195 unsigned Src = MI->getOperand(1).getReg();
196 unsigned ShAmt = MI->getOperand(2).getImm();
197 if (ShAmt == 0 || ShAmt >= 4) return 0;
199 NewMI = BuildMI(get(X86::LEA64r), Dest)
200 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
204 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
205 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
206 // the flags produced by a shift yet, so this is safe.
207 unsigned Dest = MI->getOperand(0).getReg();
208 unsigned Src = MI->getOperand(1).getReg();
209 unsigned ShAmt = MI->getOperand(2).getImm();
210 if (ShAmt == 0 || ShAmt >= 4) return 0;
212 unsigned Opc = TM.getSubtarget<X86Subtarget>().is64Bit() ?
213 X86::LEA64_32r : X86::LEA32r;
214 NewMI = BuildMI(get(Opc), Dest)
215 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
219 assert(MI->getNumOperands() == 3 && "Unknown shift instruction!");
220 if (DisableLEA16) return 0;
222 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
223 // the flags produced by a shift yet, so this is safe.
224 unsigned Dest = MI->getOperand(0).getReg();
225 unsigned Src = MI->getOperand(1).getReg();
226 unsigned ShAmt = MI->getOperand(2).getImm();
227 if (ShAmt == 0 || ShAmt >= 4) return 0;
229 NewMI = BuildMI(get(X86::LEA16r), Dest)
230 .addReg(0).addImm(1 << ShAmt).addReg(Src).addImm(0);
235 // FIXME: None of these instructions are promotable to LEAs without
236 // additional information. In particular, LEA doesn't set the flags that
237 // add and inc do. :(
239 switch (MI->getOpcode()) {
242 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
243 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, 1);
247 if (DisableLEA16) return 0;
248 assert(MI->getNumOperands() == 2 && "Unknown inc instruction!");
249 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, 1);
253 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
254 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src, -1);
258 if (DisableLEA16) return 0;
259 assert(MI->getNumOperands() == 2 && "Unknown dec instruction!");
260 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src, -1);
263 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
264 NewMI = addRegReg(BuildMI(get(X86::LEA32r), Dest), Src,
265 MI->getOperand(2).getReg());
268 if (DisableLEA16) return 0;
269 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
270 NewMI = addRegReg(BuildMI(get(X86::LEA16r), Dest), Src,
271 MI->getOperand(2).getReg());
275 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
276 if (MI->getOperand(2).isImmediate())
277 NewMI = addRegOffset(BuildMI(get(X86::LEA32r), Dest), Src,
278 MI->getOperand(2).getImmedValue());
282 if (DisableLEA16) return 0;
283 assert(MI->getNumOperands() == 3 && "Unknown add instruction!");
284 if (MI->getOperand(2).isImmediate())
285 NewMI = addRegOffset(BuildMI(get(X86::LEA16r), Dest), Src,
286 MI->getOperand(2).getImmedValue());
289 if (DisableLEA16) return 0;
291 assert(MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate() &&
292 "Unknown shl instruction!");
293 unsigned ShAmt = MI->getOperand(2).getImmedValue();
294 if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) {
296 AM.Scale = 1 << ShAmt;
298 unsigned Opc = MI->getOpcode() == X86::SHL32ri ? X86::LEA32r :X86::LEA16r;
299 NewMI = addFullAddress(BuildMI(get(Opc), Dest), AM);
305 NewMI->copyKillDeadInfo(MI);
306 LV.instructionChanged(MI, NewMI); // Update live variables
307 MFI->insert(MBBI, NewMI); // Insert the new inst
312 /// commuteInstruction - We have a few instructions that must be hacked on to
315 MachineInstr *X86InstrInfo::commuteInstruction(MachineInstr *MI) const {
316 // FIXME: Can commute cmoves by changing the condition!
317 switch (MI->getOpcode()) {
318 case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
319 case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
320 case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
321 case X86::SHLD32rri8:{// A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
324 switch (MI->getOpcode()) {
325 default: assert(0 && "Unreachable!");
326 case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
327 case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
328 case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
329 case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
331 unsigned Amt = MI->getOperand(3).getImmedValue();
332 unsigned A = MI->getOperand(0).getReg();
333 unsigned B = MI->getOperand(1).getReg();
334 unsigned C = MI->getOperand(2).getReg();
335 bool BisKill = MI->getOperand(1).isKill();
336 bool CisKill = MI->getOperand(2).isKill();
337 return BuildMI(get(Opc), A).addReg(C, false, false, CisKill)
338 .addReg(B, false, false, BisKill).addImm(Size-Amt);
341 return TargetInstrInfo::commuteInstruction(MI);
345 static X86::CondCode GetCondFromBranchOpc(unsigned BrOpc) {
347 default: return X86::COND_INVALID;
348 case X86::JE: return X86::COND_E;
349 case X86::JNE: return X86::COND_NE;
350 case X86::JL: return X86::COND_L;
351 case X86::JLE: return X86::COND_LE;
352 case X86::JG: return X86::COND_G;
353 case X86::JGE: return X86::COND_GE;
354 case X86::JB: return X86::COND_B;
355 case X86::JBE: return X86::COND_BE;
356 case X86::JA: return X86::COND_A;
357 case X86::JAE: return X86::COND_AE;
358 case X86::JS: return X86::COND_S;
359 case X86::JNS: return X86::COND_NS;
360 case X86::JP: return X86::COND_P;
361 case X86::JNP: return X86::COND_NP;
362 case X86::JO: return X86::COND_O;
363 case X86::JNO: return X86::COND_NO;
367 unsigned X86::GetCondBranchFromCond(X86::CondCode CC) {
369 default: assert(0 && "Illegal condition code!");
370 case X86::COND_E: return X86::JE;
371 case X86::COND_NE: return X86::JNE;
372 case X86::COND_L: return X86::JL;
373 case X86::COND_LE: return X86::JLE;
374 case X86::COND_G: return X86::JG;
375 case X86::COND_GE: return X86::JGE;
376 case X86::COND_B: return X86::JB;
377 case X86::COND_BE: return X86::JBE;
378 case X86::COND_A: return X86::JA;
379 case X86::COND_AE: return X86::JAE;
380 case X86::COND_S: return X86::JS;
381 case X86::COND_NS: return X86::JNS;
382 case X86::COND_P: return X86::JP;
383 case X86::COND_NP: return X86::JNP;
384 case X86::COND_O: return X86::JO;
385 case X86::COND_NO: return X86::JNO;
389 /// GetOppositeBranchCondition - Return the inverse of the specified condition,
390 /// e.g. turning COND_E to COND_NE.
391 X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) {
393 default: assert(0 && "Illegal condition code!");
394 case X86::COND_E: return X86::COND_NE;
395 case X86::COND_NE: return X86::COND_E;
396 case X86::COND_L: return X86::COND_GE;
397 case X86::COND_LE: return X86::COND_G;
398 case X86::COND_G: return X86::COND_LE;
399 case X86::COND_GE: return X86::COND_L;
400 case X86::COND_B: return X86::COND_AE;
401 case X86::COND_BE: return X86::COND_A;
402 case X86::COND_A: return X86::COND_BE;
403 case X86::COND_AE: return X86::COND_B;
404 case X86::COND_S: return X86::COND_NS;
405 case X86::COND_NS: return X86::COND_S;
406 case X86::COND_P: return X86::COND_NP;
407 case X86::COND_NP: return X86::COND_P;
408 case X86::COND_O: return X86::COND_NO;
409 case X86::COND_NO: return X86::COND_O;
413 // For purposes of branch analysis do not count FP_REG_KILL as a terminator.
414 bool X86InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
415 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
416 if (MI->getOpcode() == X86::FP_REG_KILL)
418 if (TID->Flags & M_TERMINATOR_FLAG)
419 return !isPredicated(MI);
423 bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
424 MachineBasicBlock *&TBB,
425 MachineBasicBlock *&FBB,
426 std::vector<MachineOperand> &Cond) const {
427 // If the block has no terminators, it just falls into the block after it.
428 MachineBasicBlock::iterator I = MBB.end();
429 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
432 // Get the last instruction in the block.
433 MachineInstr *LastInst = I;
435 // If there is only one terminator instruction, process it.
436 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
437 if (!isBranch(LastInst->getOpcode()))
440 // If the block ends with a branch there are 3 possibilities:
441 // it's an unconditional, conditional, or indirect branch.
443 if (LastInst->getOpcode() == X86::JMP) {
444 TBB = LastInst->getOperand(0).getMachineBasicBlock();
447 X86::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
448 if (BranchCode == X86::COND_INVALID)
449 return true; // Can't handle indirect branch.
451 // Otherwise, block ends with fall-through condbranch.
452 TBB = LastInst->getOperand(0).getMachineBasicBlock();
453 Cond.push_back(MachineOperand::CreateImm(BranchCode));
457 // Get the instruction before it if it's a terminator.
458 MachineInstr *SecondLastInst = I;
460 // If there are three terminators, we don't know what sort of block this is.
461 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
464 // If the block ends with X86::JMP and a conditional branch, handle it.
465 X86::CondCode BranchCode = GetCondFromBranchOpc(SecondLastInst->getOpcode());
466 if (BranchCode != X86::COND_INVALID && LastInst->getOpcode() == X86::JMP) {
467 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
468 Cond.push_back(MachineOperand::CreateImm(BranchCode));
469 FBB = LastInst->getOperand(0).getMachineBasicBlock();
473 // If the block ends with two X86::JMPs, handle it. The second one is not
474 // executed, so remove it.
475 if (SecondLastInst->getOpcode() == X86::JMP &&
476 LastInst->getOpcode() == X86::JMP) {
477 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
479 I->eraseFromParent();
483 // Otherwise, can't handle this.
487 unsigned X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
488 MachineBasicBlock::iterator I = MBB.end();
489 if (I == MBB.begin()) return 0;
491 if (I->getOpcode() != X86::JMP &&
492 GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
495 // Remove the branch.
496 I->eraseFromParent();
500 if (I == MBB.begin()) return 1;
502 if (GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
505 // Remove the branch.
506 I->eraseFromParent();
511 X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
512 MachineBasicBlock *FBB,
513 const std::vector<MachineOperand> &Cond) const {
514 // Shouldn't be a fall through.
515 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
516 assert((Cond.size() == 1 || Cond.size() == 0) &&
517 "X86 branch conditions have one component!");
519 if (FBB == 0) { // One way branch.
521 // Unconditional branch?
522 BuildMI(&MBB, get(X86::JMP)).addMBB(TBB);
524 // Conditional branch.
525 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
526 BuildMI(&MBB, get(Opc)).addMBB(TBB);
531 // Two-way Conditional branch.
532 unsigned Opc = GetCondBranchFromCond((X86::CondCode)Cond[0].getImm());
533 BuildMI(&MBB, get(Opc)).addMBB(TBB);
534 BuildMI(&MBB, get(X86::JMP)).addMBB(FBB);
538 bool X86InstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
539 if (MBB.empty()) return false;
541 switch (MBB.back().getOpcode()) {
542 case X86::RET: // Return.
547 case X86::JMP: // Uncond branch.
548 case X86::JMP32r: // Indirect branch.
549 case X86::JMP32m: // Indirect branch through mem.
551 default: return false;
556 ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
557 assert(Cond.size() == 1 && "Invalid X86 branch condition!");
558 Cond[0].setImm(GetOppositeBranchCondition((X86::CondCode)Cond[0].getImm()));
562 const TargetRegisterClass *X86InstrInfo::getPointerRegClass() const {
563 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
564 if (Subtarget->is64Bit())
565 return &X86::GR64RegClass;
567 return &X86::GR32RegClass;