blob: 4439192fe2f4ebfa8abcbe6443e4dc97da756c5d [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"
Evan Cheng86050dc2010-06-18 23:09:54 +000016#include "llvm/Target/TargetLowering.h"
Dan Gohmana70dca12009-10-09 23:27:56 +000017#include "llvm/Target/TargetMachine.h"
18#include "llvm/Target/TargetRegisterInfo.h"
Owen Anderson44eb65c2008-08-14 22:49:33 +000019#include "llvm/ADT/SmallVector.h"
Dan Gohmanc54baa22008-12-03 18:43:12 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner64105522008-01-01 01:03:04 +000021#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng58dcb0e2008-06-16 07:33:11 +000022#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohmanc76909a2009-09-25 20:36:54 +000023#include "llvm/CodeGen/MachineMemOperand.h"
Dan Gohmana70dca12009-10-09 23:27:56 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Andrew Trick6b120722010-12-08 20:04:29 +000025#include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
Dan Gohmanc54baa22008-12-03 18:43:12 +000026#include "llvm/CodeGen/PseudoSourceValue.h"
Nick Lewycky028700f2011-12-15 22:58:58 +000027#include "llvm/MC/MCInstrItineraries.h"
Andrew Trickc8bfd1d2011-01-21 05:51:33 +000028#include "llvm/Support/CommandLine.h"
Jakob Stoklund Olesen9fac4152010-07-13 00:23:30 +000029#include "llvm/Support/Debug.h"
Evan Cheng34c75092009-07-10 23:26:12 +000030#include "llvm/Support/ErrorHandling.h"
31#include "llvm/Support/raw_ostream.h"
Chris Lattner64105522008-01-01 01:03:04 +000032using namespace llvm;
33
Andrew Trickc8bfd1d2011-01-21 05:51:33 +000034static cl::opt<bool> DisableHazardRecognizer(
35 "disable-sched-hazard", cl::Hidden, cl::init(false),
36 cl::desc("Disable hazard detection during preRA scheduling"));
37
Evan Cheng4d54e5b2010-06-22 01:18:16 +000038/// ReplaceTailWithBranchTo - Delete the instruction OldInst and everything
39/// after it, replacing it with an unconditional branch to NewDest.
Evan Cheng86050dc2010-06-18 23:09:54 +000040void
41TargetInstrInfoImpl::ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail,
42 MachineBasicBlock *NewDest) const {
43 MachineBasicBlock *MBB = Tail->getParent();
44
45 // Remove all the old successors of MBB from the CFG.
46 while (!MBB->succ_empty())
47 MBB->removeSuccessor(MBB->succ_begin());
48
49 // Remove all the dead instructions from the end of MBB.
50 MBB->erase(Tail, MBB->end());
51
52 // If MBB isn't immediately before MBB, insert a branch to it.
53 if (++MachineFunction::iterator(MBB) != MachineFunction::iterator(NewDest))
54 InsertBranch(*MBB, NewDest, 0, SmallVector<MachineOperand, 0>(),
55 Tail->getDebugLoc());
56 MBB->addSuccessor(NewDest);
57}
58
Chris Lattner64105522008-01-01 01:03:04 +000059// commuteInstruction - The default implementation of this method just exchanges
Evan Cheng34c75092009-07-10 23:26:12 +000060// the two operands returned by findCommutedOpIndices.
Evan Cheng58dcb0e2008-06-16 07:33:11 +000061MachineInstr *TargetInstrInfoImpl::commuteInstruction(MachineInstr *MI,
62 bool NewMI) const {
Evan Chenge837dea2011-06-28 19:10:37 +000063 const MCInstrDesc &MCID = MI->getDesc();
64 bool HasDef = MCID.getNumDefs();
Evan Cheng34c75092009-07-10 23:26:12 +000065 if (HasDef && !MI->getOperand(0).isReg())
66 // No idea how to commute this instruction. Target should implement its own.
67 return 0;
68 unsigned Idx1, Idx2;
69 if (!findCommutedOpIndices(MI, Idx1, Idx2)) {
70 std::string msg;
71 raw_string_ostream Msg(msg);
72 Msg << "Don't know how to commute: " << *MI;
Chris Lattner75361b62010-04-07 22:58:41 +000073 report_fatal_error(Msg.str());
Evan Cheng34c75092009-07-10 23:26:12 +000074 }
Evan Cheng498c2902009-07-01 08:29:08 +000075
76 assert(MI->getOperand(Idx1).isReg() && MI->getOperand(Idx2).isReg() &&
Chris Lattner64105522008-01-01 01:03:04 +000077 "This only knows how to commute register operands so far");
Evan Chengcb08f182011-08-22 23:04:56 +000078 unsigned Reg0 = HasDef ? MI->getOperand(0).getReg() : 0;
Evan Cheng498c2902009-07-01 08:29:08 +000079 unsigned Reg1 = MI->getOperand(Idx1).getReg();
80 unsigned Reg2 = MI->getOperand(Idx2).getReg();
Pete Cooper442ee9c2012-03-28 17:02:22 +000081 unsigned SubReg0 = HasDef ? MI->getOperand(0).getSubReg() : 0;
82 unsigned SubReg1 = MI->getOperand(Idx1).getSubReg();
83 unsigned SubReg2 = MI->getOperand(Idx2).getSubReg();
Evan Cheng498c2902009-07-01 08:29:08 +000084 bool Reg1IsKill = MI->getOperand(Idx1).isKill();
85 bool Reg2IsKill = MI->getOperand(Idx2).isKill();
Evan Chengcb08f182011-08-22 23:04:56 +000086 // If destination is tied to either of the commuted source register, then
87 // it must be updated.
88 if (HasDef && Reg0 == Reg1 &&
89 MI->getDesc().getOperandConstraint(Idx1, MCOI::TIED_TO) == 0) {
Evan Chenga4d16a12008-02-13 02:46:49 +000090 Reg2IsKill = false;
Evan Chengcb08f182011-08-22 23:04:56 +000091 Reg0 = Reg2;
Pete Cooper442ee9c2012-03-28 17:02:22 +000092 SubReg0 = SubReg2;
Evan Chengcb08f182011-08-22 23:04:56 +000093 } else if (HasDef && Reg0 == Reg2 &&
94 MI->getDesc().getOperandConstraint(Idx2, MCOI::TIED_TO) == 0) {
95 Reg1IsKill = false;
96 Reg0 = Reg1;
Pete Cooper442ee9c2012-03-28 17:02:22 +000097 SubReg0 = SubReg1;
Evan Chenga4d16a12008-02-13 02:46:49 +000098 }
Evan Cheng58dcb0e2008-06-16 07:33:11 +000099
100 if (NewMI) {
101 // Create a new instruction.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000102 MachineFunction &MF = *MI->getParent()->getParent();
Craig Topper89b2ff02012-08-31 16:30:05 +0000103 MI = MF.CloneMachineInstr(MI);
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000104 }
105
Pete Cooper442ee9c2012-03-28 17:02:22 +0000106 if (HasDef) {
Evan Chengcb08f182011-08-22 23:04:56 +0000107 MI->getOperand(0).setReg(Reg0);
Pete Cooper442ee9c2012-03-28 17:02:22 +0000108 MI->getOperand(0).setSubReg(SubReg0);
109 }
Evan Cheng498c2902009-07-01 08:29:08 +0000110 MI->getOperand(Idx2).setReg(Reg1);
111 MI->getOperand(Idx1).setReg(Reg2);
Pete Cooper442ee9c2012-03-28 17:02:22 +0000112 MI->getOperand(Idx2).setSubReg(SubReg1);
113 MI->getOperand(Idx1).setSubReg(SubReg2);
Evan Cheng498c2902009-07-01 08:29:08 +0000114 MI->getOperand(Idx2).setIsKill(Reg1IsKill);
115 MI->getOperand(Idx1).setIsKill(Reg2IsKill);
Chris Lattner64105522008-01-01 01:03:04 +0000116 return MI;
117}
118
Evan Cheng261ce1d2009-07-10 19:15:51 +0000119/// findCommutedOpIndices - If specified MI is commutable, return the two
120/// operand indices that would swap value. Return true if the instruction
121/// is not in a form which this routine understands.
122bool TargetInstrInfoImpl::findCommutedOpIndices(MachineInstr *MI,
123 unsigned &SrcOpIdx1,
124 unsigned &SrcOpIdx2) const {
Evan Chengddfd1372011-12-14 02:11:42 +0000125 assert(!MI->isBundle() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000126 "TargetInstrInfoImpl::findCommutedOpIndices() can't handle bundles");
127
Evan Chenge837dea2011-06-28 19:10:37 +0000128 const MCInstrDesc &MCID = MI->getDesc();
129 if (!MCID.isCommutable())
Evan Cheng498c2902009-07-01 08:29:08 +0000130 return false;
Evan Cheng261ce1d2009-07-10 19:15:51 +0000131 // This assumes v0 = op v1, v2 and commuting would swap v1 and v2. If this
132 // is not true, then the target must implement this.
Evan Chenge837dea2011-06-28 19:10:37 +0000133 SrcOpIdx1 = MCID.getNumDefs();
Evan Cheng261ce1d2009-07-10 19:15:51 +0000134 SrcOpIdx2 = SrcOpIdx1 + 1;
135 if (!MI->getOperand(SrcOpIdx1).isReg() ||
136 !MI->getOperand(SrcOpIdx2).isReg())
137 // No idea.
138 return false;
139 return true;
Evan Chengf20db152008-02-15 18:21:33 +0000140}
141
142
Evan Cheng32f97632011-12-09 06:41:08 +0000143bool
144TargetInstrInfoImpl::isUnpredicatedTerminator(const MachineInstr *MI) const {
145 if (!MI->isTerminator()) return false;
146
147 // Conditional branch is a special case.
148 if (MI->isBranch() && !MI->isBarrier())
149 return true;
150 if (!MI->isPredicable())
151 return true;
152 return !isPredicated(MI);
153}
154
155
Chris Lattner64105522008-01-01 01:03:04 +0000156bool TargetInstrInfoImpl::PredicateInstruction(MachineInstr *MI,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000157 const SmallVectorImpl<MachineOperand> &Pred) const {
Chris Lattner64105522008-01-01 01:03:04 +0000158 bool MadeChange = false;
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000159
Evan Chengddfd1372011-12-14 02:11:42 +0000160 assert(!MI->isBundle() &&
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000161 "TargetInstrInfoImpl::PredicateInstruction() can't handle bundles");
162
Evan Chenge837dea2011-06-28 19:10:37 +0000163 const MCInstrDesc &MCID = MI->getDesc();
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000164 if (!MI->isPredicable())
Chris Lattner749c6f62008-01-07 07:27:27 +0000165 return false;
Andrew Trick6b120722010-12-08 20:04:29 +0000166
Chris Lattner749c6f62008-01-07 07:27:27 +0000167 for (unsigned j = 0, i = 0, e = MI->getNumOperands(); i != e; ++i) {
Evan Chenge837dea2011-06-28 19:10:37 +0000168 if (MCID.OpInfo[i].isPredicate()) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000169 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000170 if (MO.isReg()) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000171 MO.setReg(Pred[j].getReg());
172 MadeChange = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000173 } else if (MO.isImm()) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000174 MO.setImm(Pred[j].getImm());
175 MadeChange = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000176 } else if (MO.isMBB()) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000177 MO.setMBB(Pred[j].getMBB());
178 MadeChange = true;
Chris Lattner64105522008-01-01 01:03:04 +0000179 }
Chris Lattner749c6f62008-01-07 07:27:27 +0000180 ++j;
Chris Lattner64105522008-01-01 01:03:04 +0000181 }
182 }
183 return MadeChange;
184}
Evan Chengca1267c2008-03-31 20:40:39 +0000185
Jakob Stoklund Olesen2df3f582011-08-08 20:53:24 +0000186bool TargetInstrInfoImpl::hasLoadFromStackSlot(const MachineInstr *MI,
187 const MachineMemOperand *&MMO,
188 int &FrameIndex) const {
189 for (MachineInstr::mmo_iterator o = MI->memoperands_begin(),
190 oe = MI->memoperands_end();
191 o != oe;
192 ++o) {
193 if ((*o)->isLoad() && (*o)->getValue())
194 if (const FixedStackPseudoSourceValue *Value =
195 dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) {
196 FrameIndex = Value->getFrameIndex();
197 MMO = *o;
198 return true;
199 }
200 }
201 return false;
202}
203
204bool TargetInstrInfoImpl::hasStoreToStackSlot(const MachineInstr *MI,
205 const MachineMemOperand *&MMO,
206 int &FrameIndex) const {
207 for (MachineInstr::mmo_iterator o = MI->memoperands_begin(),
208 oe = MI->memoperands_end();
209 o != oe;
210 ++o) {
211 if ((*o)->isStore() && (*o)->getValue())
212 if (const FixedStackPseudoSourceValue *Value =
213 dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) {
214 FrameIndex = Value->getFrameIndex();
215 MMO = *o;
216 return true;
217 }
218 }
219 return false;
220}
221
Evan Chengca1267c2008-03-31 20:40:39 +0000222void TargetInstrInfoImpl::reMaterialize(MachineBasicBlock &MBB,
223 MachineBasicBlock::iterator I,
224 unsigned DestReg,
Evan Cheng37844532009-07-16 09:20:10 +0000225 unsigned SubIdx,
Evan Chengd57cdd52009-11-14 02:55:43 +0000226 const MachineInstr *Orig,
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +0000227 const TargetRegisterInfo &TRI) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000228 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +0000229 MI->substituteRegister(MI->getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengca1267c2008-03-31 20:40:39 +0000230 MBB.insert(I, MI);
231}
232
Evan Cheng9fe20092011-01-20 08:34:58 +0000233bool
234TargetInstrInfoImpl::produceSameValue(const MachineInstr *MI0,
235 const MachineInstr *MI1,
236 const MachineRegisterInfo *MRI) const {
Evan Cheng506049f2010-03-03 01:44:33 +0000237 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
238}
239
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000240MachineInstr *TargetInstrInfoImpl::duplicate(MachineInstr *Orig,
241 MachineFunction &MF) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000242 assert(!Orig->isNotDuplicable() &&
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000243 "Instruction cannot be duplicated");
244 return MF.CloneMachineInstr(Orig);
245}
246
Jakob Stoklund Olesen1f323402010-07-09 20:43:13 +0000247// If the COPY instruction in MI can be folded to a stack operation, return
248// the register class to use.
249static const TargetRegisterClass *canFoldCopy(const MachineInstr *MI,
250 unsigned FoldIdx) {
251 assert(MI->isCopy() && "MI must be a COPY instruction");
252 if (MI->getNumOperands() != 2)
253 return 0;
254 assert(FoldIdx<2 && "FoldIdx refers no nonexistent operand");
255
256 const MachineOperand &FoldOp = MI->getOperand(FoldIdx);
257 const MachineOperand &LiveOp = MI->getOperand(1-FoldIdx);
258
259 if (FoldOp.getSubReg() || LiveOp.getSubReg())
260 return 0;
261
262 unsigned FoldReg = FoldOp.getReg();
263 unsigned LiveReg = LiveOp.getReg();
264
265 assert(TargetRegisterInfo::isVirtualRegister(FoldReg) &&
266 "Cannot fold physregs");
267
268 const MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
269 const TargetRegisterClass *RC = MRI.getRegClass(FoldReg);
270
271 if (TargetRegisterInfo::isPhysicalRegister(LiveOp.getReg()))
272 return RC->contains(LiveOp.getReg()) ? RC : 0;
273
Jakob Stoklund Olesenfa226bc2011-06-02 05:43:46 +0000274 if (RC->hasSubClassEq(MRI.getRegClass(LiveReg)))
Jakob Stoklund Olesen1f323402010-07-09 20:43:13 +0000275 return RC;
276
277 // FIXME: Allow folding when register classes are memory compatible.
278 return 0;
279}
280
281bool TargetInstrInfoImpl::
282canFoldMemoryOperand(const MachineInstr *MI,
283 const SmallVectorImpl<unsigned> &Ops) const {
284 return MI->isCopy() && Ops.size() == 1 && canFoldCopy(MI, Ops[0]);
285}
286
Dan Gohmanc54baa22008-12-03 18:43:12 +0000287/// foldMemoryOperand - Attempt to fold a load or store of the specified stack
288/// slot into the specified machine instruction for the specified operand(s).
289/// If this is possible, a new instruction is returned with the specified
290/// operand folded, otherwise NULL is returned. The client is responsible for
291/// removing the old instruction and adding the new one in the instruction
292/// stream.
293MachineInstr*
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000294TargetInstrInfo::foldMemoryOperand(MachineBasicBlock::iterator MI,
Dan Gohmanc54baa22008-12-03 18:43:12 +0000295 const SmallVectorImpl<unsigned> &Ops,
Jakob Stoklund Olesen1f323402010-07-09 20:43:13 +0000296 int FI) const {
Dan Gohmanc54baa22008-12-03 18:43:12 +0000297 unsigned Flags = 0;
298 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
299 if (MI->getOperand(Ops[i]).isDef())
300 Flags |= MachineMemOperand::MOStore;
301 else
302 Flags |= MachineMemOperand::MOLoad;
303
Jakob Stoklund Olesen1f323402010-07-09 20:43:13 +0000304 MachineBasicBlock *MBB = MI->getParent();
305 assert(MBB && "foldMemoryOperand needs an inserted instruction");
306 MachineFunction &MF = *MBB->getParent();
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000307
Dan Gohmanc54baa22008-12-03 18:43:12 +0000308 // Ask the target to do the actual folding.
Jakob Stoklund Olesen9fac4152010-07-13 00:23:30 +0000309 if (MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, FI)) {
310 // Add a memory operand, foldMemoryOperandImpl doesn't do that.
311 assert((!(Flags & MachineMemOperand::MOStore) ||
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000312 NewMI->mayStore()) &&
Jakob Stoklund Olesen9fac4152010-07-13 00:23:30 +0000313 "Folded a def to a non-store!");
314 assert((!(Flags & MachineMemOperand::MOLoad) ||
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000315 NewMI->mayLoad()) &&
Jakob Stoklund Olesen9fac4152010-07-13 00:23:30 +0000316 "Folded a use to a non-load!");
317 const MachineFrameInfo &MFI = *MF.getFrameInfo();
318 assert(MFI.getObjectOffset(FI) != -1);
319 MachineMemOperand *MMO =
Jay Foadf4a50842011-11-15 07:51:13 +0000320 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Chris Lattner93a95ae2010-09-21 04:46:39 +0000321 Flags, MFI.getObjectSize(FI),
Jakob Stoklund Olesen9fac4152010-07-13 00:23:30 +0000322 MFI.getObjectAlignment(FI));
323 NewMI->addMemOperand(MF, MMO);
Jakob Stoklund Olesen1f323402010-07-09 20:43:13 +0000324
Jakob Stoklund Olesen1f323402010-07-09 20:43:13 +0000325 // FIXME: change foldMemoryOperandImpl semantics to also insert NewMI.
Jakob Stoklund Olesen9fac4152010-07-13 00:23:30 +0000326 return MBB->insert(MI, NewMI);
Jakob Stoklund Olesen1f323402010-07-09 20:43:13 +0000327 }
328
Jakob Stoklund Olesen9fac4152010-07-13 00:23:30 +0000329 // Straight COPY may fold as load/store.
330 if (!MI->isCopy() || Ops.size() != 1)
331 return 0;
Dan Gohmanc54baa22008-12-03 18:43:12 +0000332
Jakob Stoklund Olesen9fac4152010-07-13 00:23:30 +0000333 const TargetRegisterClass *RC = canFoldCopy(MI, Ops[0]);
334 if (!RC)
335 return 0;
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000336
Jakob Stoklund Olesen9fac4152010-07-13 00:23:30 +0000337 const MachineOperand &MO = MI->getOperand(1-Ops[0]);
338 MachineBasicBlock::iterator Pos = MI;
339 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
Dan Gohmanc54baa22008-12-03 18:43:12 +0000340
Jakob Stoklund Olesen9fac4152010-07-13 00:23:30 +0000341 if (Flags == MachineMemOperand::MOStore)
342 storeRegToStackSlot(*MBB, Pos, MO.getReg(), MO.isKill(), FI, RC, TRI);
343 else
344 loadRegFromStackSlot(*MBB, Pos, MO.getReg(), FI, RC, TRI);
345 return --Pos;
Dan Gohmanc54baa22008-12-03 18:43:12 +0000346}
347
348/// foldMemoryOperand - Same as the previous version except it allows folding
349/// of any load and store from / to any address, not just from a specific
350/// stack slot.
351MachineInstr*
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000352TargetInstrInfo::foldMemoryOperand(MachineBasicBlock::iterator MI,
Dan Gohmanc54baa22008-12-03 18:43:12 +0000353 const SmallVectorImpl<unsigned> &Ops,
354 MachineInstr* LoadMI) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000355 assert(LoadMI->canFoldAsLoad() && "LoadMI isn't foldable!");
Dan Gohmanc54baa22008-12-03 18:43:12 +0000356#ifndef NDEBUG
357 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
358 assert(MI->getOperand(Ops[i]).isUse() && "Folding load into def!");
359#endif
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000360 MachineBasicBlock &MBB = *MI->getParent();
361 MachineFunction &MF = *MBB.getParent();
Dan Gohmanc54baa22008-12-03 18:43:12 +0000362
363 // Ask the target to do the actual folding.
364 MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, LoadMI);
365 if (!NewMI) return 0;
366
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000367 NewMI = MBB.insert(MI, NewMI);
368
Dan Gohmanc54baa22008-12-03 18:43:12 +0000369 // Copy the memoperands from the load to the folded instruction.
Dan Gohmanc76909a2009-09-25 20:36:54 +0000370 NewMI->setMemRefs(LoadMI->memoperands_begin(),
371 LoadMI->memoperands_end());
Dan Gohmanc54baa22008-12-03 18:43:12 +0000372
373 return NewMI;
374}
Dan Gohmana70dca12009-10-09 23:27:56 +0000375
Evan Cheng44acc242010-06-12 00:11:53 +0000376bool TargetInstrInfo::
377isReallyTriviallyReMaterializableGeneric(const MachineInstr *MI,
378 AliasAnalysis *AA) const {
Dan Gohmana70dca12009-10-09 23:27:56 +0000379 const MachineFunction &MF = *MI->getParent()->getParent();
380 const MachineRegisterInfo &MRI = MF.getRegInfo();
381 const TargetMachine &TM = MF.getTarget();
382 const TargetInstrInfo &TII = *TM.getInstrInfo();
Dan Gohmana70dca12009-10-09 23:27:56 +0000383
Jakob Stoklund Olesen4a0a18a2011-09-01 18:27:51 +0000384 // Remat clients assume operand 0 is the defined register.
385 if (!MI->getNumOperands() || !MI->getOperand(0).isReg())
386 return false;
387 unsigned DefReg = MI->getOperand(0).getReg();
388
Jakob Stoklund Olesen9d548d02011-09-01 17:18:50 +0000389 // A sub-register definition can only be rematerialized if the instruction
390 // doesn't read the other parts of the register. Otherwise it is really a
391 // read-modify-write operation on the full virtual register which cannot be
392 // moved safely.
Jakob Stoklund Olesen4a0a18a2011-09-01 18:27:51 +0000393 if (TargetRegisterInfo::isVirtualRegister(DefReg) &&
394 MI->getOperand(0).getSubReg() && MI->readsVirtualRegister(DefReg))
Jakob Stoklund Olesen9d548d02011-09-01 17:18:50 +0000395 return false;
396
Dan Gohmana70dca12009-10-09 23:27:56 +0000397 // A load from a fixed stack slot can be rematerialized. This may be
398 // redundant with subsequent checks, but it's target-independent,
399 // simple, and a common case.
400 int FrameIdx = 0;
401 if (TII.isLoadFromStackSlot(MI, FrameIdx) &&
402 MF.getFrameInfo()->isImmutableObjectIndex(FrameIdx))
403 return true;
404
Dan Gohmana70dca12009-10-09 23:27:56 +0000405 // Avoid instructions obviously unsafe for remat.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000406 if (MI->isNotDuplicable() || MI->mayStore() ||
Evan Chengc36b7062011-01-07 23:50:32 +0000407 MI->hasUnmodeledSideEffects())
408 return false;
409
410 // Don't remat inline asm. We have no idea how expensive it is
411 // even if it's side effect free.
412 if (MI->isInlineAsm())
Dan Gohmana70dca12009-10-09 23:27:56 +0000413 return false;
414
415 // Avoid instructions which load from potentially varying memory.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000416 if (MI->mayLoad() && !MI->isInvariantLoad(AA))
Dan Gohmana70dca12009-10-09 23:27:56 +0000417 return false;
418
419 // If any of the registers accessed are non-constant, conservatively assume
420 // the instruction is not rematerializable.
421 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
422 const MachineOperand &MO = MI->getOperand(i);
423 if (!MO.isReg()) continue;
424 unsigned Reg = MO.getReg();
425 if (Reg == 0)
426 continue;
427
428 // Check for a well-behaved physical register.
429 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
430 if (MO.isUse()) {
431 // If the physreg has no defs anywhere, it's just an ambient register
432 // and we can freely move its uses. Alternatively, if it's allocatable,
433 // it could get allocated to something with a def during allocation.
Jakob Stoklund Olesenc035c942012-01-16 22:34:08 +0000434 if (!MRI.isConstantPhysReg(Reg, MF))
Dan Gohmana70dca12009-10-09 23:27:56 +0000435 return false;
Dan Gohmana70dca12009-10-09 23:27:56 +0000436 } else {
437 // A physreg def. We can't remat it.
438 return false;
439 }
440 continue;
441 }
442
Jakob Stoklund Olesen4a0a18a2011-09-01 18:27:51 +0000443 // Only allow one virtual-register def. There may be multiple defs of the
444 // same virtual register, though.
445 if (MO.isDef() && Reg != DefReg)
Dan Gohmana70dca12009-10-09 23:27:56 +0000446 return false;
447
Dan Gohmana70dca12009-10-09 23:27:56 +0000448 // Don't allow any virtual-register uses. Rematting an instruction with
449 // virtual register uses would length the live ranges of the uses, which
450 // is not necessarily a good idea, certainly not "trivial".
451 if (MO.isUse())
452 return false;
453 }
454
455 // Everything checked out.
456 return true;
457}
Evan Cheng774bc882010-06-14 21:06:53 +0000458
Evan Cheng86050dc2010-06-18 23:09:54 +0000459/// isSchedulingBoundary - Test if the given instruction should be
460/// considered a scheduling boundary. This primarily includes labels
461/// and terminators.
462bool TargetInstrInfoImpl::isSchedulingBoundary(const MachineInstr *MI,
463 const MachineBasicBlock *MBB,
464 const MachineFunction &MF) const{
465 // Terminators and labels can't be scheduled around.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000466 if (MI->isTerminator() || MI->isLabel())
Evan Cheng86050dc2010-06-18 23:09:54 +0000467 return true;
468
469 // Don't attempt to schedule around any instruction that defines
470 // a stack-oriented pointer, as it's unlikely to be profitable. This
471 // saves compile time, because it doesn't require every single
472 // stack slot reference to depend on the instruction that does the
473 // modification.
474 const TargetLowering &TLI = *MF.getTarget().getTargetLowering();
475 if (MI->definesRegister(TLI.getStackPointerRegisterToSaveRestore()))
476 return true;
477
478 return false;
479}
480
Andrew Trickc8bfd1d2011-01-21 05:51:33 +0000481// Provide a global flag for disabling the PreRA hazard recognizer that targets
482// may choose to honor.
483bool TargetInstrInfoImpl::usePreRAHazardRecognizer() const {
484 return !DisableHazardRecognizer;
485}
486
487// Default implementation of CreateTargetRAHazardRecognizer.
Andrew Trick2da8bc82010-12-24 05:03:26 +0000488ScheduleHazardRecognizer *TargetInstrInfoImpl::
489CreateTargetHazardRecognizer(const TargetMachine *TM,
490 const ScheduleDAG *DAG) const {
491 // Dummy hazard recognizer allows all instructions to issue.
492 return new ScheduleHazardRecognizer();
493}
494
Andrew Trick0a39d4e2012-05-24 22:11:09 +0000495// Default implementation of CreateTargetMIHazardRecognizer.
496ScheduleHazardRecognizer *TargetInstrInfoImpl::
497CreateTargetMIHazardRecognizer(const InstrItineraryData *II,
498 const ScheduleDAG *DAG) const {
499 return (ScheduleHazardRecognizer *)
500 new ScoreboardHazardRecognizer(II, DAG, "misched");
501}
502
Evan Cheng774bc882010-06-14 21:06:53 +0000503// Default implementation of CreateTargetPostRAHazardRecognizer.
504ScheduleHazardRecognizer *TargetInstrInfoImpl::
Andrew Trick2da8bc82010-12-24 05:03:26 +0000505CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
506 const ScheduleDAG *DAG) const {
507 return (ScheduleHazardRecognizer *)
508 new ScoreboardHazardRecognizer(II, DAG, "post-RA-sched");
Evan Cheng774bc882010-06-14 21:06:53 +0000509}
Nick Lewycky028700f2011-12-15 22:58:58 +0000510
Andrew Trickc36d0332012-06-08 17:23:27 +0000511//===----------------------------------------------------------------------===//
512// SelectionDAG latency interface.
513//===----------------------------------------------------------------------===//
514
Nick Lewycky028700f2011-12-15 22:58:58 +0000515int
Eli Friedman1e2ec6a2011-12-19 20:06:03 +0000516TargetInstrInfoImpl::getOperandLatency(const InstrItineraryData *ItinData,
517 SDNode *DefNode, unsigned DefIdx,
518 SDNode *UseNode, unsigned UseIdx) const {
Nick Lewycky028700f2011-12-15 22:58:58 +0000519 if (!ItinData || ItinData->isEmpty())
520 return -1;
521
522 if (!DefNode->isMachineOpcode())
523 return -1;
524
525 unsigned DefClass = get(DefNode->getMachineOpcode()).getSchedClass();
526 if (!UseNode->isMachineOpcode())
527 return ItinData->getOperandCycle(DefClass, DefIdx);
528 unsigned UseClass = get(UseNode->getMachineOpcode()).getSchedClass();
529 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
530}
531
Eli Friedman1e2ec6a2011-12-19 20:06:03 +0000532int TargetInstrInfoImpl::getInstrLatency(const InstrItineraryData *ItinData,
533 SDNode *N) const {
Nick Lewycky028700f2011-12-15 22:58:58 +0000534 if (!ItinData || ItinData->isEmpty())
535 return 1;
536
537 if (!N->isMachineOpcode())
538 return 1;
539
540 return ItinData->getStageLatency(get(N->getMachineOpcode()).getSchedClass());
541}
542
Andrew Trickc36d0332012-06-08 17:23:27 +0000543//===----------------------------------------------------------------------===//
544// MachineInstr latency interface.
545//===----------------------------------------------------------------------===//
546
547unsigned
Andrew Trickeb81df72012-06-08 21:52:38 +0000548TargetInstrInfoImpl::getNumMicroOps(const InstrItineraryData *ItinData,
549 const MachineInstr *MI) const {
Andrew Trickc36d0332012-06-08 17:23:27 +0000550 if (!ItinData || ItinData->isEmpty())
551 return 1;
552
553 unsigned Class = MI->getDesc().getSchedClass();
Andrew Trick218ee742012-07-02 18:10:42 +0000554 int UOps = ItinData->Itineraries[Class].NumMicroOps;
555 if (UOps >= 0)
Andrew Trickc36d0332012-06-08 17:23:27 +0000556 return UOps;
557
558 // The # of u-ops is dynamically determined. The specific target should
559 // override this function to return the right number.
560 return 1;
561}
562
563/// Return the default expected latency for a def based on it's opcode.
Andrew Trick3c417552012-08-08 02:44:11 +0000564unsigned TargetInstrInfo::defaultDefLatency(const MCSchedModel *SchedModel,
Andrew Trickc36d0332012-06-08 17:23:27 +0000565 const MachineInstr *DefMI) const {
Andrew Trick4903c152012-10-11 05:37:06 +0000566 if (DefMI->isTransient())
567 return 0;
Andrew Trickc36d0332012-06-08 17:23:27 +0000568 if (DefMI->mayLoad())
Andrew Trick3c417552012-08-08 02:44:11 +0000569 return SchedModel->LoadLatency;
Andrew Trickc36d0332012-06-08 17:23:27 +0000570 if (isHighLatencyDef(DefMI->getOpcode()))
Andrew Trick3c417552012-08-08 02:44:11 +0000571 return SchedModel->HighLatency;
Andrew Trickc36d0332012-06-08 17:23:27 +0000572 return 1;
573}
574
Andrew Trickeb81df72012-06-08 21:52:38 +0000575unsigned TargetInstrInfoImpl::
576getInstrLatency(const InstrItineraryData *ItinData,
577 const MachineInstr *MI,
578 unsigned *PredCost) const {
Andrew Trickc36d0332012-06-08 17:23:27 +0000579 // Default to one cycle for no itinerary. However, an "empty" itinerary may
580 // still have a MinLatency property, which getStageLatency checks.
581 if (!ItinData)
582 return MI->mayLoad() ? 2 : 1;
583
584 return ItinData->getStageLatency(MI->getDesc().getSchedClass());
585}
586
Andrew Trickeb81df72012-06-08 21:52:38 +0000587bool TargetInstrInfoImpl::hasLowDefLatency(const InstrItineraryData *ItinData,
588 const MachineInstr *DefMI,
589 unsigned DefIdx) const {
Andrew Trickc36d0332012-06-08 17:23:27 +0000590 if (!ItinData || ItinData->isEmpty())
591 return false;
592
593 unsigned DefClass = DefMI->getDesc().getSchedClass();
594 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
595 return (DefCycle != -1 && DefCycle <= 1);
596}
597
598/// Both DefMI and UseMI must be valid. By default, call directly to the
599/// itinerary. This may be overriden by the target.
Andrew Trickeb81df72012-06-08 21:52:38 +0000600int TargetInstrInfoImpl::
601getOperandLatency(const InstrItineraryData *ItinData,
602 const MachineInstr *DefMI, unsigned DefIdx,
603 const MachineInstr *UseMI, unsigned UseIdx) const {
Andrew Trickc36d0332012-06-08 17:23:27 +0000604 unsigned DefClass = DefMI->getDesc().getSchedClass();
605 unsigned UseClass = UseMI->getDesc().getSchedClass();
606 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
607}
608
609/// If we can determine the operand latency from the def only, without itinerary
610/// lookup, do so. Otherwise return -1.
Andrew Trick34301ce2012-09-18 04:03:34 +0000611int TargetInstrInfo::computeDefOperandLatency(
612 const InstrItineraryData *ItinData,
613 const MachineInstr *DefMI, bool FindMin) const {
Andrew Trickc36d0332012-06-08 17:23:27 +0000614
615 // Let the target hook getInstrLatency handle missing itineraries.
616 if (!ItinData)
Andrew Trick34301ce2012-09-18 04:03:34 +0000617 return getInstrLatency(ItinData, DefMI);
Andrew Trickc36d0332012-06-08 17:23:27 +0000618
619 // Return a latency based on the itinerary properties and defining instruction
620 // if possible. Some common subtargets don't require per-operand latency,
621 // especially for minimum latencies.
622 if (FindMin) {
623 // If MinLatency is valid, call getInstrLatency. This uses Stage latency if
624 // it exists before defaulting to MinLatency.
Andrew Trick2661b412012-07-07 04:00:00 +0000625 if (ItinData->SchedModel->MinLatency >= 0)
Andrew Trick34301ce2012-09-18 04:03:34 +0000626 return getInstrLatency(ItinData, DefMI);
Andrew Trickc36d0332012-06-08 17:23:27 +0000627
628 // If MinLatency is invalid, OperandLatency is interpreted as MinLatency.
629 // For empty itineraries, short-cirtuit the check and default to one cycle.
630 if (ItinData->isEmpty())
631 return 1;
632 }
633 else if(ItinData->isEmpty())
Andrew Trick34301ce2012-09-18 04:03:34 +0000634 return defaultDefLatency(ItinData->SchedModel, DefMI);
Andrew Trickc36d0332012-06-08 17:23:27 +0000635
636 // ...operand lookup required
Andrew Trick70cb1772012-07-09 20:43:01 +0000637 return -1;
Andrew Trickc36d0332012-06-08 17:23:27 +0000638}
639
640/// computeOperandLatency - Compute and return the latency of the given data
Andrew Trickffd25262012-08-23 00:39:43 +0000641/// dependent def and use when the operand indices are already known. UseMI may
642/// be NULL for an unknown use.
Andrew Trickc36d0332012-06-08 17:23:27 +0000643///
Andrew Trickffd25262012-08-23 00:39:43 +0000644/// FindMin may be set to get the minimum vs. expected latency. Minimum
645/// latency is used for scheduling groups, while expected latency is for
646/// instruction cost and critical path.
647///
648/// Depending on the subtarget's itinerary properties, this may or may not need
649/// to call getOperandLatency(). For most subtargets, we don't need DefIdx or
650/// UseIdx to compute min latency.
Andrew Trickc36d0332012-06-08 17:23:27 +0000651unsigned TargetInstrInfo::
652computeOperandLatency(const InstrItineraryData *ItinData,
653 const MachineInstr *DefMI, unsigned DefIdx,
654 const MachineInstr *UseMI, unsigned UseIdx,
655 bool FindMin) const {
656
Andrew Trick34301ce2012-09-18 04:03:34 +0000657 int DefLatency = computeDefOperandLatency(ItinData, DefMI, FindMin);
Andrew Trickc36d0332012-06-08 17:23:27 +0000658 if (DefLatency >= 0)
659 return DefLatency;
660
661 assert(ItinData && !ItinData->isEmpty() && "computeDefOperandLatency fail");
662
Andrew Trickffd25262012-08-23 00:39:43 +0000663 int OperLatency = 0;
664 if (UseMI)
665 OperLatency = getOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx);
666 else {
667 unsigned DefClass = DefMI->getDesc().getSchedClass();
668 OperLatency = ItinData->getOperandCycle(DefClass, DefIdx);
669 }
Andrew Trickc36d0332012-06-08 17:23:27 +0000670 if (OperLatency >= 0)
671 return OperLatency;
672
673 // No operand latency was found.
674 unsigned InstrLatency = getInstrLatency(ItinData, DefMI);
675
676 // Expected latency is the max of the stage latency and itinerary props.
677 if (!FindMin)
Andrew Trick3c417552012-08-08 02:44:11 +0000678 InstrLatency = std::max(InstrLatency,
679 defaultDefLatency(ItinData->SchedModel, DefMI));
Andrew Trickc36d0332012-06-08 17:23:27 +0000680 return InstrLatency;
681}