blob: b7595990de74c7229b9b37707571c8215f9ff904 [file] [log] [blame]
Chris Lattner64105522008-01-01 01:03:04 +00001//===-- TargetInstrInfoImpl.cpp - Target Instruction Information ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the TargetInstrInfoImpl class, it just provides default
11// implementations of various methods.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Target/TargetInstrInfo.h"
Owen Anderson44eb65c2008-08-14 22:49:33 +000016#include "llvm/ADT/SmallVector.h"
Dan Gohmanc54baa22008-12-03 18:43:12 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner64105522008-01-01 01:03:04 +000018#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng58dcb0e2008-06-16 07:33:11 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohmanc54baa22008-12-03 18:43:12 +000020#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner64105522008-01-01 01:03:04 +000021using namespace llvm;
22
23// commuteInstruction - The default implementation of this method just exchanges
24// operand 1 and 2.
Evan Cheng58dcb0e2008-06-16 07:33:11 +000025MachineInstr *TargetInstrInfoImpl::commuteInstruction(MachineInstr *MI,
26 bool NewMI) const {
Evan Cheng498c2902009-07-01 08:29:08 +000027 const TargetInstrDesc &TID = MI->getDesc();
28 bool HasDef = TID.getNumDefs();
29 unsigned Idx1 = HasDef ? 1 : 0;
30 unsigned Idx2 = HasDef ? 2 : 1;
31
32 assert(MI->getOperand(Idx1).isReg() && MI->getOperand(Idx2).isReg() &&
Chris Lattner64105522008-01-01 01:03:04 +000033 "This only knows how to commute register operands so far");
Evan Cheng498c2902009-07-01 08:29:08 +000034 unsigned Reg1 = MI->getOperand(Idx1).getReg();
35 unsigned Reg2 = MI->getOperand(Idx2).getReg();
36 bool Reg1IsKill = MI->getOperand(Idx1).isKill();
37 bool Reg2IsKill = MI->getOperand(Idx2).isKill();
Evan Cheng58dcb0e2008-06-16 07:33:11 +000038 bool ChangeReg0 = false;
Evan Cheng498c2902009-07-01 08:29:08 +000039 if (HasDef && MI->getOperand(0).getReg() == Reg1) {
Evan Chenga4d16a12008-02-13 02:46:49 +000040 // Must be two address instruction!
41 assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
42 "Expecting a two-address instruction!");
43 Reg2IsKill = false;
Evan Cheng58dcb0e2008-06-16 07:33:11 +000044 ChangeReg0 = true;
Evan Chenga4d16a12008-02-13 02:46:49 +000045 }
Evan Cheng58dcb0e2008-06-16 07:33:11 +000046
47 if (NewMI) {
48 // Create a new instruction.
Evan Cheng498c2902009-07-01 08:29:08 +000049 unsigned Reg0 = HasDef
50 ? (ChangeReg0 ? Reg2 : MI->getOperand(0).getReg()) : 0;
51 bool Reg0IsDead = HasDef ? MI->getOperand(0).isDead() : false;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000052 MachineFunction &MF = *MI->getParent()->getParent();
Evan Cheng498c2902009-07-01 08:29:08 +000053 if (HasDef)
54 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
55 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
56 .addReg(Reg2, getKillRegState(Reg2IsKill))
57 .addReg(Reg1, getKillRegState(Reg2IsKill));
58 else
59 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
60 .addReg(Reg2, getKillRegState(Reg2IsKill))
61 .addReg(Reg1, getKillRegState(Reg2IsKill));
Evan Cheng58dcb0e2008-06-16 07:33:11 +000062 }
63
64 if (ChangeReg0)
65 MI->getOperand(0).setReg(Reg2);
Evan Cheng498c2902009-07-01 08:29:08 +000066 MI->getOperand(Idx2).setReg(Reg1);
67 MI->getOperand(Idx1).setReg(Reg2);
68 MI->getOperand(Idx2).setIsKill(Reg1IsKill);
69 MI->getOperand(Idx1).setIsKill(Reg2IsKill);
Chris Lattner64105522008-01-01 01:03:04 +000070 return MI;
71}
72
Evan Chengf20db152008-02-15 18:21:33 +000073/// CommuteChangesDestination - Return true if commuting the specified
74/// instruction will also changes the destination operand. Also return the
75/// current operand index of the would be new destination register by
76/// reference. This can happen when the commutable instruction is also a
77/// two-address instruction.
78bool TargetInstrInfoImpl::CommuteChangesDestination(MachineInstr *MI,
79 unsigned &OpIdx) const{
Evan Cheng498c2902009-07-01 08:29:08 +000080 const TargetInstrDesc &TID = MI->getDesc();
81 if (!TID.getNumDefs())
82 return false;
Dan Gohmand735b802008-10-03 15:45:36 +000083 assert(MI->getOperand(1).isReg() && MI->getOperand(2).isReg() &&
Evan Chengf20db152008-02-15 18:21:33 +000084 "This only knows how to commute register operands so far");
85 if (MI->getOperand(0).getReg() == MI->getOperand(1).getReg()) {
86 // Must be two address instruction!
87 assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
88 "Expecting a two-address instruction!");
89 OpIdx = 2;
90 return true;
91 }
92 return false;
93}
94
95
Chris Lattner64105522008-01-01 01:03:04 +000096bool TargetInstrInfoImpl::PredicateInstruction(MachineInstr *MI,
Owen Anderson44eb65c2008-08-14 22:49:33 +000097 const SmallVectorImpl<MachineOperand> &Pred) const {
Chris Lattner64105522008-01-01 01:03:04 +000098 bool MadeChange = false;
Chris Lattner749c6f62008-01-07 07:27:27 +000099 const TargetInstrDesc &TID = MI->getDesc();
100 if (!TID.isPredicable())
101 return false;
102
103 for (unsigned j = 0, i = 0, e = MI->getNumOperands(); i != e; ++i) {
104 if (TID.OpInfo[i].isPredicate()) {
105 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000106 if (MO.isReg()) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000107 MO.setReg(Pred[j].getReg());
108 MadeChange = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000109 } else if (MO.isImm()) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000110 MO.setImm(Pred[j].getImm());
111 MadeChange = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000112 } else if (MO.isMBB()) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000113 MO.setMBB(Pred[j].getMBB());
114 MadeChange = true;
Chris Lattner64105522008-01-01 01:03:04 +0000115 }
Chris Lattner749c6f62008-01-07 07:27:27 +0000116 ++j;
Chris Lattner64105522008-01-01 01:03:04 +0000117 }
118 }
119 return MadeChange;
120}
Evan Chengca1267c2008-03-31 20:40:39 +0000121
122void TargetInstrInfoImpl::reMaterialize(MachineBasicBlock &MBB,
123 MachineBasicBlock::iterator I,
124 unsigned DestReg,
125 const MachineInstr *Orig) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000126 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Evan Chengca1267c2008-03-31 20:40:39 +0000127 MI->getOperand(0).setReg(DestReg);
128 MBB.insert(I, MI);
129}
130
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000131unsigned
132TargetInstrInfoImpl::GetFunctionSizeInBytes(const MachineFunction &MF) const {
133 unsigned FnSize = 0;
134 for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end();
135 MBBI != E; ++MBBI) {
136 const MachineBasicBlock &MBB = *MBBI;
Evan Cheng38855782008-09-11 05:58:06 +0000137 for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end();
138 I != E; ++I)
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000139 FnSize += GetInstSizeInBytes(I);
140 }
141 return FnSize;
142}
Dan Gohmanc54baa22008-12-03 18:43:12 +0000143
144/// foldMemoryOperand - Attempt to fold a load or store of the specified stack
145/// slot into the specified machine instruction for the specified operand(s).
146/// If this is possible, a new instruction is returned with the specified
147/// operand folded, otherwise NULL is returned. The client is responsible for
148/// removing the old instruction and adding the new one in the instruction
149/// stream.
150MachineInstr*
151TargetInstrInfo::foldMemoryOperand(MachineFunction &MF,
152 MachineInstr* MI,
153 const SmallVectorImpl<unsigned> &Ops,
154 int FrameIndex) const {
155 unsigned Flags = 0;
156 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
157 if (MI->getOperand(Ops[i]).isDef())
158 Flags |= MachineMemOperand::MOStore;
159 else
160 Flags |= MachineMemOperand::MOLoad;
161
162 // Ask the target to do the actual folding.
163 MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, FrameIndex);
164 if (!NewMI) return 0;
165
166 assert((!(Flags & MachineMemOperand::MOStore) ||
167 NewMI->getDesc().mayStore()) &&
168 "Folded a def to a non-store!");
169 assert((!(Flags & MachineMemOperand::MOLoad) ||
170 NewMI->getDesc().mayLoad()) &&
171 "Folded a use to a non-load!");
172 const MachineFrameInfo &MFI = *MF.getFrameInfo();
173 assert(MFI.getObjectOffset(FrameIndex) != -1);
174 MachineMemOperand MMO(PseudoSourceValue::getFixedStack(FrameIndex),
175 Flags,
176 MFI.getObjectOffset(FrameIndex),
177 MFI.getObjectSize(FrameIndex),
178 MFI.getObjectAlignment(FrameIndex));
179 NewMI->addMemOperand(MF, MMO);
180
181 return NewMI;
182}
183
184/// foldMemoryOperand - Same as the previous version except it allows folding
185/// of any load and store from / to any address, not just from a specific
186/// stack slot.
187MachineInstr*
188TargetInstrInfo::foldMemoryOperand(MachineFunction &MF,
189 MachineInstr* MI,
190 const SmallVectorImpl<unsigned> &Ops,
191 MachineInstr* LoadMI) const {
192 assert(LoadMI->getDesc().canFoldAsLoad() && "LoadMI isn't foldable!");
193#ifndef NDEBUG
194 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
195 assert(MI->getOperand(Ops[i]).isUse() && "Folding load into def!");
196#endif
197
198 // Ask the target to do the actual folding.
199 MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, LoadMI);
200 if (!NewMI) return 0;
201
202 // Copy the memoperands from the load to the folded instruction.
203 for (std::list<MachineMemOperand>::iterator I = LoadMI->memoperands_begin(),
204 E = LoadMI->memoperands_end(); I != E; ++I)
205 NewMI->addMemOperand(MF, *I);
206
207 return NewMI;
208}