blob: 0b791e4a71f5ae605278ce46ece740f9e8c789e5 [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"
Evan Cheng34c75092009-07-10 23:26:12 +000021#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/raw_ostream.h"
Chris Lattner64105522008-01-01 01:03:04 +000023using namespace llvm;
24
25// commuteInstruction - The default implementation of this method just exchanges
Evan Cheng34c75092009-07-10 23:26:12 +000026// the two operands returned by findCommutedOpIndices.
Evan Cheng58dcb0e2008-06-16 07:33:11 +000027MachineInstr *TargetInstrInfoImpl::commuteInstruction(MachineInstr *MI,
28 bool NewMI) const {
Evan Cheng498c2902009-07-01 08:29:08 +000029 const TargetInstrDesc &TID = MI->getDesc();
30 bool HasDef = TID.getNumDefs();
Evan Cheng34c75092009-07-10 23:26:12 +000031 if (HasDef && !MI->getOperand(0).isReg())
32 // No idea how to commute this instruction. Target should implement its own.
33 return 0;
34 unsigned Idx1, Idx2;
35 if (!findCommutedOpIndices(MI, Idx1, Idx2)) {
36 std::string msg;
37 raw_string_ostream Msg(msg);
38 Msg << "Don't know how to commute: " << *MI;
39 llvm_report_error(Msg.str());
40 }
Evan Cheng498c2902009-07-01 08:29:08 +000041
42 assert(MI->getOperand(Idx1).isReg() && MI->getOperand(Idx2).isReg() &&
Chris Lattner64105522008-01-01 01:03:04 +000043 "This only knows how to commute register operands so far");
Evan Cheng498c2902009-07-01 08:29:08 +000044 unsigned Reg1 = MI->getOperand(Idx1).getReg();
45 unsigned Reg2 = MI->getOperand(Idx2).getReg();
46 bool Reg1IsKill = MI->getOperand(Idx1).isKill();
47 bool Reg2IsKill = MI->getOperand(Idx2).isKill();
Evan Cheng58dcb0e2008-06-16 07:33:11 +000048 bool ChangeReg0 = false;
Evan Cheng498c2902009-07-01 08:29:08 +000049 if (HasDef && MI->getOperand(0).getReg() == Reg1) {
Evan Chenga4d16a12008-02-13 02:46:49 +000050 // Must be two address instruction!
51 assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
52 "Expecting a two-address instruction!");
53 Reg2IsKill = false;
Evan Cheng58dcb0e2008-06-16 07:33:11 +000054 ChangeReg0 = true;
Evan Chenga4d16a12008-02-13 02:46:49 +000055 }
Evan Cheng58dcb0e2008-06-16 07:33:11 +000056
57 if (NewMI) {
58 // Create a new instruction.
Evan Cheng498c2902009-07-01 08:29:08 +000059 unsigned Reg0 = HasDef
60 ? (ChangeReg0 ? Reg2 : MI->getOperand(0).getReg()) : 0;
61 bool Reg0IsDead = HasDef ? MI->getOperand(0).isDead() : false;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000062 MachineFunction &MF = *MI->getParent()->getParent();
Evan Cheng498c2902009-07-01 08:29:08 +000063 if (HasDef)
64 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
65 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
66 .addReg(Reg2, getKillRegState(Reg2IsKill))
67 .addReg(Reg1, getKillRegState(Reg2IsKill));
68 else
69 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
70 .addReg(Reg2, getKillRegState(Reg2IsKill))
71 .addReg(Reg1, getKillRegState(Reg2IsKill));
Evan Cheng58dcb0e2008-06-16 07:33:11 +000072 }
73
74 if (ChangeReg0)
75 MI->getOperand(0).setReg(Reg2);
Evan Cheng498c2902009-07-01 08:29:08 +000076 MI->getOperand(Idx2).setReg(Reg1);
77 MI->getOperand(Idx1).setReg(Reg2);
78 MI->getOperand(Idx2).setIsKill(Reg1IsKill);
79 MI->getOperand(Idx1).setIsKill(Reg2IsKill);
Chris Lattner64105522008-01-01 01:03:04 +000080 return MI;
81}
82
Evan Cheng261ce1d2009-07-10 19:15:51 +000083/// findCommutedOpIndices - If specified MI is commutable, return the two
84/// operand indices that would swap value. Return true if the instruction
85/// is not in a form which this routine understands.
86bool TargetInstrInfoImpl::findCommutedOpIndices(MachineInstr *MI,
87 unsigned &SrcOpIdx1,
88 unsigned &SrcOpIdx2) const {
Evan Cheng498c2902009-07-01 08:29:08 +000089 const TargetInstrDesc &TID = MI->getDesc();
Evan Cheng261ce1d2009-07-10 19:15:51 +000090 if (!TID.isCommutable())
Evan Cheng498c2902009-07-01 08:29:08 +000091 return false;
Evan Cheng261ce1d2009-07-10 19:15:51 +000092 // This assumes v0 = op v1, v2 and commuting would swap v1 and v2. If this
93 // is not true, then the target must implement this.
94 SrcOpIdx1 = TID.getNumDefs();
95 SrcOpIdx2 = SrcOpIdx1 + 1;
96 if (!MI->getOperand(SrcOpIdx1).isReg() ||
97 !MI->getOperand(SrcOpIdx2).isReg())
98 // No idea.
99 return false;
100 return true;
Evan Chengf20db152008-02-15 18:21:33 +0000101}
102
103
Chris Lattner64105522008-01-01 01:03:04 +0000104bool TargetInstrInfoImpl::PredicateInstruction(MachineInstr *MI,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000105 const SmallVectorImpl<MachineOperand> &Pred) const {
Chris Lattner64105522008-01-01 01:03:04 +0000106 bool MadeChange = false;
Chris Lattner749c6f62008-01-07 07:27:27 +0000107 const TargetInstrDesc &TID = MI->getDesc();
108 if (!TID.isPredicable())
109 return false;
110
111 for (unsigned j = 0, i = 0, e = MI->getNumOperands(); i != e; ++i) {
112 if (TID.OpInfo[i].isPredicate()) {
113 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000114 if (MO.isReg()) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000115 MO.setReg(Pred[j].getReg());
116 MadeChange = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000117 } else if (MO.isImm()) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000118 MO.setImm(Pred[j].getImm());
119 MadeChange = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000120 } else if (MO.isMBB()) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000121 MO.setMBB(Pred[j].getMBB());
122 MadeChange = true;
Chris Lattner64105522008-01-01 01:03:04 +0000123 }
Chris Lattner749c6f62008-01-07 07:27:27 +0000124 ++j;
Chris Lattner64105522008-01-01 01:03:04 +0000125 }
126 }
127 return MadeChange;
128}
Evan Chengca1267c2008-03-31 20:40:39 +0000129
130void TargetInstrInfoImpl::reMaterialize(MachineBasicBlock &MBB,
131 MachineBasicBlock::iterator I,
132 unsigned DestReg,
Evan Cheng37844532009-07-16 09:20:10 +0000133 unsigned SubIdx,
Evan Chengca1267c2008-03-31 20:40:39 +0000134 const MachineInstr *Orig) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000135 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Evan Cheng37844532009-07-16 09:20:10 +0000136 MachineOperand &MO = MI->getOperand(0);
137 MO.setReg(DestReg);
138 MO.setSubReg(SubIdx);
Evan Chengca1267c2008-03-31 20:40:39 +0000139 MBB.insert(I, MI);
140}
141
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000142unsigned
143TargetInstrInfoImpl::GetFunctionSizeInBytes(const MachineFunction &MF) const {
144 unsigned FnSize = 0;
145 for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end();
146 MBBI != E; ++MBBI) {
147 const MachineBasicBlock &MBB = *MBBI;
Evan Cheng38855782008-09-11 05:58:06 +0000148 for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end();
149 I != E; ++I)
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000150 FnSize += GetInstSizeInBytes(I);
151 }
152 return FnSize;
153}
Dan Gohmanc54baa22008-12-03 18:43:12 +0000154
155/// foldMemoryOperand - Attempt to fold a load or store of the specified stack
156/// slot into the specified machine instruction for the specified operand(s).
157/// If this is possible, a new instruction is returned with the specified
158/// operand folded, otherwise NULL is returned. The client is responsible for
159/// removing the old instruction and adding the new one in the instruction
160/// stream.
161MachineInstr*
162TargetInstrInfo::foldMemoryOperand(MachineFunction &MF,
163 MachineInstr* MI,
164 const SmallVectorImpl<unsigned> &Ops,
165 int FrameIndex) const {
166 unsigned Flags = 0;
167 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
168 if (MI->getOperand(Ops[i]).isDef())
169 Flags |= MachineMemOperand::MOStore;
170 else
171 Flags |= MachineMemOperand::MOLoad;
172
173 // Ask the target to do the actual folding.
174 MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, FrameIndex);
175 if (!NewMI) return 0;
176
177 assert((!(Flags & MachineMemOperand::MOStore) ||
178 NewMI->getDesc().mayStore()) &&
179 "Folded a def to a non-store!");
180 assert((!(Flags & MachineMemOperand::MOLoad) ||
181 NewMI->getDesc().mayLoad()) &&
182 "Folded a use to a non-load!");
183 const MachineFrameInfo &MFI = *MF.getFrameInfo();
184 assert(MFI.getObjectOffset(FrameIndex) != -1);
185 MachineMemOperand MMO(PseudoSourceValue::getFixedStack(FrameIndex),
186 Flags,
187 MFI.getObjectOffset(FrameIndex),
188 MFI.getObjectSize(FrameIndex),
189 MFI.getObjectAlignment(FrameIndex));
190 NewMI->addMemOperand(MF, MMO);
191
192 return NewMI;
193}
194
195/// foldMemoryOperand - Same as the previous version except it allows folding
196/// of any load and store from / to any address, not just from a specific
197/// stack slot.
198MachineInstr*
199TargetInstrInfo::foldMemoryOperand(MachineFunction &MF,
200 MachineInstr* MI,
201 const SmallVectorImpl<unsigned> &Ops,
202 MachineInstr* LoadMI) const {
203 assert(LoadMI->getDesc().canFoldAsLoad() && "LoadMI isn't foldable!");
204#ifndef NDEBUG
205 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
206 assert(MI->getOperand(Ops[i]).isUse() && "Folding load into def!");
207#endif
208
209 // Ask the target to do the actual folding.
210 MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, LoadMI);
211 if (!NewMI) return 0;
212
213 // Copy the memoperands from the load to the folded instruction.
214 for (std::list<MachineMemOperand>::iterator I = LoadMI->memoperands_begin(),
215 E = LoadMI->memoperands_end(); I != E; ++I)
216 NewMI->addMemOperand(MF, *I);
217
218 return NewMI;
219}