blob: 393e315a3214b09c8f27c9ad5d72749fc24e7165 [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"
Dan Gohmana70dca12009-10-09 23:27:56 +000016#include "llvm/Target/TargetMachine.h"
17#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson44eb65c2008-08-14 22:49:33 +000018#include "llvm/ADT/SmallVector.h"
Dan Gohmanc54baa22008-12-03 18:43:12 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner64105522008-01-01 01:03:04 +000020#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng58dcb0e2008-06-16 07:33:11 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohmanc76909a2009-09-25 20:36:54 +000022#include "llvm/CodeGen/MachineMemOperand.h"
Dan Gohmana70dca12009-10-09 23:27:56 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanc54baa22008-12-03 18:43:12 +000024#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Cheng34c75092009-07-10 23:26:12 +000025#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/raw_ostream.h"
Chris Lattner64105522008-01-01 01:03:04 +000027using namespace llvm;
28
29// commuteInstruction - The default implementation of this method just exchanges
Evan Cheng34c75092009-07-10 23:26:12 +000030// the two operands returned by findCommutedOpIndices.
Evan Cheng58dcb0e2008-06-16 07:33:11 +000031MachineInstr *TargetInstrInfoImpl::commuteInstruction(MachineInstr *MI,
32 bool NewMI) const {
Evan Cheng498c2902009-07-01 08:29:08 +000033 const TargetInstrDesc &TID = MI->getDesc();
34 bool HasDef = TID.getNumDefs();
Evan Cheng34c75092009-07-10 23:26:12 +000035 if (HasDef && !MI->getOperand(0).isReg())
36 // No idea how to commute this instruction. Target should implement its own.
37 return 0;
38 unsigned Idx1, Idx2;
39 if (!findCommutedOpIndices(MI, Idx1, Idx2)) {
40 std::string msg;
41 raw_string_ostream Msg(msg);
42 Msg << "Don't know how to commute: " << *MI;
43 llvm_report_error(Msg.str());
44 }
Evan Cheng498c2902009-07-01 08:29:08 +000045
46 assert(MI->getOperand(Idx1).isReg() && MI->getOperand(Idx2).isReg() &&
Chris Lattner64105522008-01-01 01:03:04 +000047 "This only knows how to commute register operands so far");
Evan Cheng498c2902009-07-01 08:29:08 +000048 unsigned Reg1 = MI->getOperand(Idx1).getReg();
49 unsigned Reg2 = MI->getOperand(Idx2).getReg();
50 bool Reg1IsKill = MI->getOperand(Idx1).isKill();
51 bool Reg2IsKill = MI->getOperand(Idx2).isKill();
Evan Cheng58dcb0e2008-06-16 07:33:11 +000052 bool ChangeReg0 = false;
Evan Cheng498c2902009-07-01 08:29:08 +000053 if (HasDef && MI->getOperand(0).getReg() == Reg1) {
Evan Chenga4d16a12008-02-13 02:46:49 +000054 // Must be two address instruction!
55 assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
56 "Expecting a two-address instruction!");
57 Reg2IsKill = false;
Evan Cheng58dcb0e2008-06-16 07:33:11 +000058 ChangeReg0 = true;
Evan Chenga4d16a12008-02-13 02:46:49 +000059 }
Evan Cheng58dcb0e2008-06-16 07:33:11 +000060
61 if (NewMI) {
62 // Create a new instruction.
Evan Cheng498c2902009-07-01 08:29:08 +000063 unsigned Reg0 = HasDef
64 ? (ChangeReg0 ? Reg2 : MI->getOperand(0).getReg()) : 0;
65 bool Reg0IsDead = HasDef ? MI->getOperand(0).isDead() : false;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000066 MachineFunction &MF = *MI->getParent()->getParent();
Evan Cheng498c2902009-07-01 08:29:08 +000067 if (HasDef)
68 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
69 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
70 .addReg(Reg2, getKillRegState(Reg2IsKill))
71 .addReg(Reg1, getKillRegState(Reg2IsKill));
72 else
73 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
74 .addReg(Reg2, getKillRegState(Reg2IsKill))
75 .addReg(Reg1, getKillRegState(Reg2IsKill));
Evan Cheng58dcb0e2008-06-16 07:33:11 +000076 }
77
78 if (ChangeReg0)
79 MI->getOperand(0).setReg(Reg2);
Evan Cheng498c2902009-07-01 08:29:08 +000080 MI->getOperand(Idx2).setReg(Reg1);
81 MI->getOperand(Idx1).setReg(Reg2);
82 MI->getOperand(Idx2).setIsKill(Reg1IsKill);
83 MI->getOperand(Idx1).setIsKill(Reg2IsKill);
Chris Lattner64105522008-01-01 01:03:04 +000084 return MI;
85}
86
Evan Cheng261ce1d2009-07-10 19:15:51 +000087/// findCommutedOpIndices - If specified MI is commutable, return the two
88/// operand indices that would swap value. Return true if the instruction
89/// is not in a form which this routine understands.
90bool TargetInstrInfoImpl::findCommutedOpIndices(MachineInstr *MI,
91 unsigned &SrcOpIdx1,
92 unsigned &SrcOpIdx2) const {
Evan Cheng498c2902009-07-01 08:29:08 +000093 const TargetInstrDesc &TID = MI->getDesc();
Evan Cheng261ce1d2009-07-10 19:15:51 +000094 if (!TID.isCommutable())
Evan Cheng498c2902009-07-01 08:29:08 +000095 return false;
Evan Cheng261ce1d2009-07-10 19:15:51 +000096 // This assumes v0 = op v1, v2 and commuting would swap v1 and v2. If this
97 // is not true, then the target must implement this.
98 SrcOpIdx1 = TID.getNumDefs();
99 SrcOpIdx2 = SrcOpIdx1 + 1;
100 if (!MI->getOperand(SrcOpIdx1).isReg() ||
101 !MI->getOperand(SrcOpIdx2).isReg())
102 // No idea.
103 return false;
104 return true;
Evan Chengf20db152008-02-15 18:21:33 +0000105}
106
107
Chris Lattner64105522008-01-01 01:03:04 +0000108bool TargetInstrInfoImpl::PredicateInstruction(MachineInstr *MI,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000109 const SmallVectorImpl<MachineOperand> &Pred) const {
Chris Lattner64105522008-01-01 01:03:04 +0000110 bool MadeChange = false;
Chris Lattner749c6f62008-01-07 07:27:27 +0000111 const TargetInstrDesc &TID = MI->getDesc();
112 if (!TID.isPredicable())
113 return false;
114
115 for (unsigned j = 0, i = 0, e = MI->getNumOperands(); i != e; ++i) {
116 if (TID.OpInfo[i].isPredicate()) {
117 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000118 if (MO.isReg()) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000119 MO.setReg(Pred[j].getReg());
120 MadeChange = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000121 } else if (MO.isImm()) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000122 MO.setImm(Pred[j].getImm());
123 MadeChange = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000124 } else if (MO.isMBB()) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000125 MO.setMBB(Pred[j].getMBB());
126 MadeChange = true;
Chris Lattner64105522008-01-01 01:03:04 +0000127 }
Chris Lattner749c6f62008-01-07 07:27:27 +0000128 ++j;
Chris Lattner64105522008-01-01 01:03:04 +0000129 }
130 }
131 return MadeChange;
132}
Evan Chengca1267c2008-03-31 20:40:39 +0000133
134void TargetInstrInfoImpl::reMaterialize(MachineBasicBlock &MBB,
135 MachineBasicBlock::iterator I,
136 unsigned DestReg,
Evan Cheng37844532009-07-16 09:20:10 +0000137 unsigned SubIdx,
Evan Chengd57cdd52009-11-14 02:55:43 +0000138 const MachineInstr *Orig,
139 const TargetRegisterInfo *TRI) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000140 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Evan Cheng37844532009-07-16 09:20:10 +0000141 MachineOperand &MO = MI->getOperand(0);
Evan Chengd57cdd52009-11-14 02:55:43 +0000142 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
143 MO.setReg(DestReg);
144 MO.setSubReg(SubIdx);
Evan Cheng39aa7252009-11-16 06:31:49 +0000145 } else if (SubIdx) {
Evan Chengd57cdd52009-11-14 02:55:43 +0000146 MO.setReg(TRI->getSubReg(DestReg, SubIdx));
Evan Cheng39aa7252009-11-16 06:31:49 +0000147 } else {
148 MO.setReg(DestReg);
Evan Chengd57cdd52009-11-14 02:55:43 +0000149 }
Evan Chengca1267c2008-03-31 20:40:39 +0000150 MBB.insert(I, MI);
151}
152
Evan Cheng78e5c112009-11-07 03:52:02 +0000153bool
154TargetInstrInfoImpl::isIdentical(const MachineInstr *MI,
155 const MachineInstr *Other,
156 const MachineRegisterInfo *MRI) const {
157 if (MI->getOpcode() != Other->getOpcode() ||
158 MI->getNumOperands() != Other->getNumOperands())
159 return false;
160
161 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
162 const MachineOperand &MO = MI->getOperand(i);
163 const MachineOperand &OMO = Other->getOperand(i);
164 if (MO.isReg() && MO.isDef()) {
165 assert(OMO.isReg() && OMO.isDef());
166 unsigned Reg = MO.getReg();
167 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
168 if (Reg != OMO.getReg())
169 return false;
170 } else if (MRI->getRegClass(MO.getReg()) !=
171 MRI->getRegClass(OMO.getReg()))
172 return false;
173
174 continue;
175 }
176
177 if (!MO.isIdenticalTo(OMO))
178 return false;
179 }
180
181 return true;
182}
183
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000184unsigned
185TargetInstrInfoImpl::GetFunctionSizeInBytes(const MachineFunction &MF) const {
186 unsigned FnSize = 0;
187 for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end();
188 MBBI != E; ++MBBI) {
189 const MachineBasicBlock &MBB = *MBBI;
Evan Cheng38855782008-09-11 05:58:06 +0000190 for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end();
191 I != E; ++I)
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000192 FnSize += GetInstSizeInBytes(I);
193 }
194 return FnSize;
195}
Dan Gohmanc54baa22008-12-03 18:43:12 +0000196
197/// foldMemoryOperand - Attempt to fold a load or store of the specified stack
198/// slot into the specified machine instruction for the specified operand(s).
199/// If this is possible, a new instruction is returned with the specified
200/// operand folded, otherwise NULL is returned. The client is responsible for
201/// removing the old instruction and adding the new one in the instruction
202/// stream.
203MachineInstr*
204TargetInstrInfo::foldMemoryOperand(MachineFunction &MF,
205 MachineInstr* MI,
206 const SmallVectorImpl<unsigned> &Ops,
207 int FrameIndex) const {
208 unsigned Flags = 0;
209 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
210 if (MI->getOperand(Ops[i]).isDef())
211 Flags |= MachineMemOperand::MOStore;
212 else
213 Flags |= MachineMemOperand::MOLoad;
214
215 // Ask the target to do the actual folding.
216 MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, FrameIndex);
217 if (!NewMI) return 0;
218
219 assert((!(Flags & MachineMemOperand::MOStore) ||
220 NewMI->getDesc().mayStore()) &&
221 "Folded a def to a non-store!");
222 assert((!(Flags & MachineMemOperand::MOLoad) ||
223 NewMI->getDesc().mayLoad()) &&
224 "Folded a use to a non-load!");
225 const MachineFrameInfo &MFI = *MF.getFrameInfo();
226 assert(MFI.getObjectOffset(FrameIndex) != -1);
Dan Gohmanc76909a2009-09-25 20:36:54 +0000227 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +0000228 MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FrameIndex),
Dan Gohmanc76909a2009-09-25 20:36:54 +0000229 Flags, /*Offset=*/0,
230 MFI.getObjectSize(FrameIndex),
231 MFI.getObjectAlignment(FrameIndex));
Dan Gohmanc54baa22008-12-03 18:43:12 +0000232 NewMI->addMemOperand(MF, MMO);
233
234 return NewMI;
235}
236
237/// foldMemoryOperand - Same as the previous version except it allows folding
238/// of any load and store from / to any address, not just from a specific
239/// stack slot.
240MachineInstr*
241TargetInstrInfo::foldMemoryOperand(MachineFunction &MF,
242 MachineInstr* MI,
243 const SmallVectorImpl<unsigned> &Ops,
244 MachineInstr* LoadMI) const {
245 assert(LoadMI->getDesc().canFoldAsLoad() && "LoadMI isn't foldable!");
246#ifndef NDEBUG
247 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
248 assert(MI->getOperand(Ops[i]).isUse() && "Folding load into def!");
249#endif
250
251 // Ask the target to do the actual folding.
252 MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, LoadMI);
253 if (!NewMI) return 0;
254
255 // Copy the memoperands from the load to the folded instruction.
Dan Gohmanc76909a2009-09-25 20:36:54 +0000256 NewMI->setMemRefs(LoadMI->memoperands_begin(),
257 LoadMI->memoperands_end());
Dan Gohmanc54baa22008-12-03 18:43:12 +0000258
259 return NewMI;
260}
Dan Gohmana70dca12009-10-09 23:27:56 +0000261
262bool
263TargetInstrInfo::isReallyTriviallyReMaterializableGeneric(const MachineInstr *
264 MI,
265 AliasAnalysis *
266 AA) const {
267 const MachineFunction &MF = *MI->getParent()->getParent();
268 const MachineRegisterInfo &MRI = MF.getRegInfo();
269 const TargetMachine &TM = MF.getTarget();
270 const TargetInstrInfo &TII = *TM.getInstrInfo();
271 const TargetRegisterInfo &TRI = *TM.getRegisterInfo();
272
273 // A load from a fixed stack slot can be rematerialized. This may be
274 // redundant with subsequent checks, but it's target-independent,
275 // simple, and a common case.
276 int FrameIdx = 0;
277 if (TII.isLoadFromStackSlot(MI, FrameIdx) &&
278 MF.getFrameInfo()->isImmutableObjectIndex(FrameIdx))
279 return true;
280
281 const TargetInstrDesc &TID = MI->getDesc();
282
283 // Avoid instructions obviously unsafe for remat.
284 if (TID.hasUnmodeledSideEffects() || TID.isNotDuplicable() ||
285 TID.mayStore())
286 return false;
287
288 // Avoid instructions which load from potentially varying memory.
289 if (TID.mayLoad() && !MI->isInvariantLoad(AA))
290 return false;
291
292 // If any of the registers accessed are non-constant, conservatively assume
293 // the instruction is not rematerializable.
294 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
295 const MachineOperand &MO = MI->getOperand(i);
296 if (!MO.isReg()) continue;
297 unsigned Reg = MO.getReg();
298 if (Reg == 0)
299 continue;
300
301 // Check for a well-behaved physical register.
302 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
303 if (MO.isUse()) {
304 // If the physreg has no defs anywhere, it's just an ambient register
305 // and we can freely move its uses. Alternatively, if it's allocatable,
306 // it could get allocated to something with a def during allocation.
307 if (!MRI.def_empty(Reg))
308 return false;
309 BitVector AllocatableRegs = TRI.getAllocatableSet(MF, 0);
310 if (AllocatableRegs.test(Reg))
311 return false;
312 // Check for a def among the register's aliases too.
313 for (const unsigned *Alias = TRI.getAliasSet(Reg); *Alias; ++Alias) {
314 unsigned AliasReg = *Alias;
315 if (!MRI.def_empty(AliasReg))
316 return false;
317 if (AllocatableRegs.test(AliasReg))
318 return false;
319 }
320 } else {
321 // A physreg def. We can't remat it.
322 return false;
323 }
324 continue;
325 }
326
327 // Only allow one virtual-register def, and that in the first operand.
328 if (MO.isDef() != (i == 0))
329 return false;
330
331 // For the def, it should be the only def of that register.
Chris Lattner7896c9f2009-12-03 00:50:42 +0000332 if (MO.isDef() && (llvm::next(MRI.def_begin(Reg)) != MRI.def_end() ||
Dan Gohmana70dca12009-10-09 23:27:56 +0000333 MRI.isLiveIn(Reg)))
334 return false;
335
336 // Don't allow any virtual-register uses. Rematting an instruction with
337 // virtual register uses would length the live ranges of the uses, which
338 // is not necessarily a good idea, certainly not "trivial".
339 if (MO.isUse())
340 return false;
341 }
342
343 // Everything checked out.
344 return true;
345}