blob: 0ee891698f32294925555704f051eb1c111ded78 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- TargetInstrInfo.cpp - Target Instruction Information --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Target/TargetInstrInfo.h"
15#include "llvm/CodeGen/MachineInstr.h"
16#include "llvm/Constant.h"
17#include "llvm/DerivedTypes.h"
18using namespace llvm;
19
20/// findTiedToSrcOperand - Returns the operand that is tied to the specified
21/// dest operand. Returns -1 if there isn't one.
22int TargetInstrDescriptor::findTiedToSrcOperand(unsigned OpNum) const {
23 for (unsigned i = 0, e = numOperands; i != e; ++i) {
24 if (i == OpNum)
25 continue;
26 if (getOperandConstraint(i, TOI::TIED_TO) == (int)OpNum)
27 return i;
28 }
29 return -1;
30}
31
32
33TargetInstrInfo::TargetInstrInfo(const TargetInstrDescriptor* Desc,
34 unsigned numOpcodes)
35 : desc(Desc), NumOpcodes(numOpcodes) {
36}
37
38TargetInstrInfo::~TargetInstrInfo() {
39}
40
41// commuteInstruction - The default implementation of this method just exchanges
42// operand 1 and 2.
43MachineInstr *TargetInstrInfo::commuteInstruction(MachineInstr *MI) const {
44 assert(MI->getOperand(1).isRegister() && MI->getOperand(2).isRegister() &&
45 "This only knows how to commute register operands so far");
46 unsigned Reg1 = MI->getOperand(1).getReg();
47 unsigned Reg2 = MI->getOperand(2).getReg();
48 bool Reg1IsKill = MI->getOperand(1).isKill();
49 bool Reg2IsKill = MI->getOperand(2).isKill();
50 MI->getOperand(2).setReg(Reg1);
51 MI->getOperand(1).setReg(Reg2);
Chris Lattner7f2d3b82007-12-30 21:56:09 +000052 MI->getOperand(2).setIsKill(Reg1IsKill);
53 MI->getOperand(1).setIsKill(Reg2IsKill);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 return MI;
55}
56
57bool TargetInstrInfo::PredicateInstruction(MachineInstr *MI,
58 const std::vector<MachineOperand> &Pred) const {
59 bool MadeChange = false;
60 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
61 if (TID->Flags & M_PREDICABLE) {
62 for (unsigned j = 0, i = 0, e = MI->getNumOperands(); i != e; ++i) {
63 if ((TID->OpInfo[i].Flags & M_PREDICATE_OPERAND)) {
64 MachineOperand &MO = MI->getOperand(i);
Dan Gohman38a9a9f2007-09-14 20:33:02 +000065 if (MO.isRegister()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 MO.setReg(Pred[j].getReg());
67 MadeChange = true;
Dan Gohman38a9a9f2007-09-14 20:33:02 +000068 } else if (MO.isImmediate()) {
Chris Lattnera96056a2007-12-30 20:49:49 +000069 MO.setImm(Pred[j].getImm());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 MadeChange = true;
Dan Gohman38a9a9f2007-09-14 20:33:02 +000071 } else if (MO.isMachineBasicBlock()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 MO.setMachineBasicBlock(Pred[j].getMachineBasicBlock());
73 MadeChange = true;
74 }
75 ++j;
76 }
77 }
78 }
79 return MadeChange;
80}
81
82bool TargetInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
83 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
84 if (TID->Flags & M_TERMINATOR_FLAG) {
85 // Conditional branch is a special case.
86 if ((TID->Flags & M_BRANCH_FLAG) != 0 && (TID->Flags & M_BARRIER_FLAG) == 0)
87 return true;
88 if ((TID->Flags & M_PREDICABLE) == 0)
89 return true;
90 return !isPredicated(MI);
91 }
92 return false;
93}