blob: 5115d5bd07a39652dbdefbfe267fb3a1b13d8695 [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"
Andrew Trickc8bfd1d2011-01-21 05:51:33 +000027#include "llvm/Support/CommandLine.h"
Jakob Stoklund Olesen9fac4152010-07-13 00:23:30 +000028#include "llvm/Support/Debug.h"
Evan Cheng34c75092009-07-10 23:26:12 +000029#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/raw_ostream.h"
Chris Lattner64105522008-01-01 01:03:04 +000031using namespace llvm;
32
Andrew Trickc8bfd1d2011-01-21 05:51:33 +000033static cl::opt<bool> DisableHazardRecognizer(
34 "disable-sched-hazard", cl::Hidden, cl::init(false),
35 cl::desc("Disable hazard detection during preRA scheduling"));
36
Evan Cheng4d54e5b2010-06-22 01:18:16 +000037/// ReplaceTailWithBranchTo - Delete the instruction OldInst and everything
38/// after it, replacing it with an unconditional branch to NewDest.
Evan Cheng86050dc2010-06-18 23:09:54 +000039void
40TargetInstrInfoImpl::ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail,
41 MachineBasicBlock *NewDest) const {
42 MachineBasicBlock *MBB = Tail->getParent();
43
44 // Remove all the old successors of MBB from the CFG.
45 while (!MBB->succ_empty())
46 MBB->removeSuccessor(MBB->succ_begin());
47
48 // Remove all the dead instructions from the end of MBB.
49 MBB->erase(Tail, MBB->end());
50
51 // If MBB isn't immediately before MBB, insert a branch to it.
52 if (++MachineFunction::iterator(MBB) != MachineFunction::iterator(NewDest))
53 InsertBranch(*MBB, NewDest, 0, SmallVector<MachineOperand, 0>(),
54 Tail->getDebugLoc());
55 MBB->addSuccessor(NewDest);
56}
57
Chris Lattner64105522008-01-01 01:03:04 +000058// commuteInstruction - The default implementation of this method just exchanges
Evan Cheng34c75092009-07-10 23:26:12 +000059// the two operands returned by findCommutedOpIndices.
Evan Cheng58dcb0e2008-06-16 07:33:11 +000060MachineInstr *TargetInstrInfoImpl::commuteInstruction(MachineInstr *MI,
61 bool NewMI) const {
Evan Chenge837dea2011-06-28 19:10:37 +000062 const MCInstrDesc &MCID = MI->getDesc();
63 bool HasDef = MCID.getNumDefs();
Evan Cheng34c75092009-07-10 23:26:12 +000064 if (HasDef && !MI->getOperand(0).isReg())
65 // No idea how to commute this instruction. Target should implement its own.
66 return 0;
67 unsigned Idx1, Idx2;
68 if (!findCommutedOpIndices(MI, Idx1, Idx2)) {
69 std::string msg;
70 raw_string_ostream Msg(msg);
71 Msg << "Don't know how to commute: " << *MI;
Chris Lattner75361b62010-04-07 22:58:41 +000072 report_fatal_error(Msg.str());
Evan Cheng34c75092009-07-10 23:26:12 +000073 }
Evan Cheng498c2902009-07-01 08:29:08 +000074
75 assert(MI->getOperand(Idx1).isReg() && MI->getOperand(Idx2).isReg() &&
Chris Lattner64105522008-01-01 01:03:04 +000076 "This only knows how to commute register operands so far");
Evan Chengcb08f182011-08-22 23:04:56 +000077 unsigned Reg0 = HasDef ? MI->getOperand(0).getReg() : 0;
Evan Cheng498c2902009-07-01 08:29:08 +000078 unsigned Reg1 = MI->getOperand(Idx1).getReg();
79 unsigned Reg2 = MI->getOperand(Idx2).getReg();
80 bool Reg1IsKill = MI->getOperand(Idx1).isKill();
81 bool Reg2IsKill = MI->getOperand(Idx2).isKill();
Evan Chengcb08f182011-08-22 23:04:56 +000082 // If destination is tied to either of the commuted source register, then
83 // it must be updated.
84 if (HasDef && Reg0 == Reg1 &&
85 MI->getDesc().getOperandConstraint(Idx1, MCOI::TIED_TO) == 0) {
Evan Chenga4d16a12008-02-13 02:46:49 +000086 Reg2IsKill = false;
Evan Chengcb08f182011-08-22 23:04:56 +000087 Reg0 = Reg2;
88 } else if (HasDef && Reg0 == Reg2 &&
89 MI->getDesc().getOperandConstraint(Idx2, MCOI::TIED_TO) == 0) {
90 Reg1IsKill = false;
91 Reg0 = Reg1;
Evan Chenga4d16a12008-02-13 02:46:49 +000092 }
Evan Cheng58dcb0e2008-06-16 07:33:11 +000093
94 if (NewMI) {
95 // Create a new instruction.
Evan Cheng498c2902009-07-01 08:29:08 +000096 bool Reg0IsDead = HasDef ? MI->getOperand(0).isDead() : false;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000097 MachineFunction &MF = *MI->getParent()->getParent();
Evan Cheng498c2902009-07-01 08:29:08 +000098 if (HasDef)
99 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
100 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
101 .addReg(Reg2, getKillRegState(Reg2IsKill))
102 .addReg(Reg1, getKillRegState(Reg2IsKill));
103 else
104 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
105 .addReg(Reg2, getKillRegState(Reg2IsKill))
106 .addReg(Reg1, getKillRegState(Reg2IsKill));
Evan Cheng58dcb0e2008-06-16 07:33:11 +0000107 }
108
Evan Chengcb08f182011-08-22 23:04:56 +0000109 if (HasDef)
110 MI->getOperand(0).setReg(Reg0);
Evan Cheng498c2902009-07-01 08:29:08 +0000111 MI->getOperand(Idx2).setReg(Reg1);
112 MI->getOperand(Idx1).setReg(Reg2);
113 MI->getOperand(Idx2).setIsKill(Reg1IsKill);
114 MI->getOperand(Idx1).setIsKill(Reg2IsKill);
Chris Lattner64105522008-01-01 01:03:04 +0000115 return MI;
116}
117
Evan Cheng261ce1d2009-07-10 19:15:51 +0000118/// findCommutedOpIndices - If specified MI is commutable, return the two
119/// operand indices that would swap value. Return true if the instruction
120/// is not in a form which this routine understands.
121bool TargetInstrInfoImpl::findCommutedOpIndices(MachineInstr *MI,
122 unsigned &SrcOpIdx1,
123 unsigned &SrcOpIdx2) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000124 assert(MI->getOpcode() != TargetOpcode::BUNDLE &&
125 "TargetInstrInfoImpl::findCommutedOpIndices() can't handle bundles");
126
Evan Chenge837dea2011-06-28 19:10:37 +0000127 const MCInstrDesc &MCID = MI->getDesc();
128 if (!MCID.isCommutable())
Evan Cheng498c2902009-07-01 08:29:08 +0000129 return false;
Evan Cheng261ce1d2009-07-10 19:15:51 +0000130 // This assumes v0 = op v1, v2 and commuting would swap v1 and v2. If this
131 // is not true, then the target must implement this.
Evan Chenge837dea2011-06-28 19:10:37 +0000132 SrcOpIdx1 = MCID.getNumDefs();
Evan Cheng261ce1d2009-07-10 19:15:51 +0000133 SrcOpIdx2 = SrcOpIdx1 + 1;
134 if (!MI->getOperand(SrcOpIdx1).isReg() ||
135 !MI->getOperand(SrcOpIdx2).isReg())
136 // No idea.
137 return false;
138 return true;
Evan Chengf20db152008-02-15 18:21:33 +0000139}
140
141
Chris Lattner64105522008-01-01 01:03:04 +0000142bool TargetInstrInfoImpl::PredicateInstruction(MachineInstr *MI,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000143 const SmallVectorImpl<MachineOperand> &Pred) const {
Chris Lattner64105522008-01-01 01:03:04 +0000144 bool MadeChange = false;
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000145
146 assert(MI->getOpcode() != TargetOpcode::BUNDLE &&
147 "TargetInstrInfoImpl::PredicateInstruction() can't handle bundles");
148
Evan Chenge837dea2011-06-28 19:10:37 +0000149 const MCInstrDesc &MCID = MI->getDesc();
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000150 if (!MI->isPredicable())
Chris Lattner749c6f62008-01-07 07:27:27 +0000151 return false;
Andrew Trick6b120722010-12-08 20:04:29 +0000152
Chris Lattner749c6f62008-01-07 07:27:27 +0000153 for (unsigned j = 0, i = 0, e = MI->getNumOperands(); i != e; ++i) {
Evan Chenge837dea2011-06-28 19:10:37 +0000154 if (MCID.OpInfo[i].isPredicate()) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000155 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000156 if (MO.isReg()) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000157 MO.setReg(Pred[j].getReg());
158 MadeChange = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000159 } else if (MO.isImm()) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000160 MO.setImm(Pred[j].getImm());
161 MadeChange = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000162 } else if (MO.isMBB()) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000163 MO.setMBB(Pred[j].getMBB());
164 MadeChange = true;
Chris Lattner64105522008-01-01 01:03:04 +0000165 }
Chris Lattner749c6f62008-01-07 07:27:27 +0000166 ++j;
Chris Lattner64105522008-01-01 01:03:04 +0000167 }
168 }
169 return MadeChange;
170}
Evan Chengca1267c2008-03-31 20:40:39 +0000171
Jakob Stoklund Olesen2df3f582011-08-08 20:53:24 +0000172bool TargetInstrInfoImpl::hasLoadFromStackSlot(const MachineInstr *MI,
173 const MachineMemOperand *&MMO,
174 int &FrameIndex) const {
175 for (MachineInstr::mmo_iterator o = MI->memoperands_begin(),
176 oe = MI->memoperands_end();
177 o != oe;
178 ++o) {
179 if ((*o)->isLoad() && (*o)->getValue())
180 if (const FixedStackPseudoSourceValue *Value =
181 dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) {
182 FrameIndex = Value->getFrameIndex();
183 MMO = *o;
184 return true;
185 }
186 }
187 return false;
188}
189
190bool TargetInstrInfoImpl::hasStoreToStackSlot(const MachineInstr *MI,
191 const MachineMemOperand *&MMO,
192 int &FrameIndex) const {
193 for (MachineInstr::mmo_iterator o = MI->memoperands_begin(),
194 oe = MI->memoperands_end();
195 o != oe;
196 ++o) {
197 if ((*o)->isStore() && (*o)->getValue())
198 if (const FixedStackPseudoSourceValue *Value =
199 dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) {
200 FrameIndex = Value->getFrameIndex();
201 MMO = *o;
202 return true;
203 }
204 }
205 return false;
206}
207
Evan Chengca1267c2008-03-31 20:40:39 +0000208void TargetInstrInfoImpl::reMaterialize(MachineBasicBlock &MBB,
209 MachineBasicBlock::iterator I,
210 unsigned DestReg,
Evan Cheng37844532009-07-16 09:20:10 +0000211 unsigned SubIdx,
Evan Chengd57cdd52009-11-14 02:55:43 +0000212 const MachineInstr *Orig,
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +0000213 const TargetRegisterInfo &TRI) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000214 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +0000215 MI->substituteRegister(MI->getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengca1267c2008-03-31 20:40:39 +0000216 MBB.insert(I, MI);
217}
218
Evan Cheng9fe20092011-01-20 08:34:58 +0000219bool
220TargetInstrInfoImpl::produceSameValue(const MachineInstr *MI0,
221 const MachineInstr *MI1,
222 const MachineRegisterInfo *MRI) const {
Evan Cheng506049f2010-03-03 01:44:33 +0000223 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
224}
225
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000226MachineInstr *TargetInstrInfoImpl::duplicate(MachineInstr *Orig,
227 MachineFunction &MF) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000228 assert(!Orig->isNotDuplicable() &&
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000229 "Instruction cannot be duplicated");
230 return MF.CloneMachineInstr(Orig);
231}
232
Jakob Stoklund Olesen1f323402010-07-09 20:43:13 +0000233// If the COPY instruction in MI can be folded to a stack operation, return
234// the register class to use.
235static const TargetRegisterClass *canFoldCopy(const MachineInstr *MI,
236 unsigned FoldIdx) {
237 assert(MI->isCopy() && "MI must be a COPY instruction");
238 if (MI->getNumOperands() != 2)
239 return 0;
240 assert(FoldIdx<2 && "FoldIdx refers no nonexistent operand");
241
242 const MachineOperand &FoldOp = MI->getOperand(FoldIdx);
243 const MachineOperand &LiveOp = MI->getOperand(1-FoldIdx);
244
245 if (FoldOp.getSubReg() || LiveOp.getSubReg())
246 return 0;
247
248 unsigned FoldReg = FoldOp.getReg();
249 unsigned LiveReg = LiveOp.getReg();
250
251 assert(TargetRegisterInfo::isVirtualRegister(FoldReg) &&
252 "Cannot fold physregs");
253
254 const MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
255 const TargetRegisterClass *RC = MRI.getRegClass(FoldReg);
256
257 if (TargetRegisterInfo::isPhysicalRegister(LiveOp.getReg()))
258 return RC->contains(LiveOp.getReg()) ? RC : 0;
259
Jakob Stoklund Olesenfa226bc2011-06-02 05:43:46 +0000260 if (RC->hasSubClassEq(MRI.getRegClass(LiveReg)))
Jakob Stoklund Olesen1f323402010-07-09 20:43:13 +0000261 return RC;
262
263 // FIXME: Allow folding when register classes are memory compatible.
264 return 0;
265}
266
267bool TargetInstrInfoImpl::
268canFoldMemoryOperand(const MachineInstr *MI,
269 const SmallVectorImpl<unsigned> &Ops) const {
270 return MI->isCopy() && Ops.size() == 1 && canFoldCopy(MI, Ops[0]);
271}
272
Dan Gohmanc54baa22008-12-03 18:43:12 +0000273/// foldMemoryOperand - Attempt to fold a load or store of the specified stack
274/// slot into the specified machine instruction for the specified operand(s).
275/// If this is possible, a new instruction is returned with the specified
276/// operand folded, otherwise NULL is returned. The client is responsible for
277/// removing the old instruction and adding the new one in the instruction
278/// stream.
279MachineInstr*
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000280TargetInstrInfo::foldMemoryOperand(MachineBasicBlock::iterator MI,
Dan Gohmanc54baa22008-12-03 18:43:12 +0000281 const SmallVectorImpl<unsigned> &Ops,
Jakob Stoklund Olesen1f323402010-07-09 20:43:13 +0000282 int FI) const {
Dan Gohmanc54baa22008-12-03 18:43:12 +0000283 unsigned Flags = 0;
284 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
285 if (MI->getOperand(Ops[i]).isDef())
286 Flags |= MachineMemOperand::MOStore;
287 else
288 Flags |= MachineMemOperand::MOLoad;
289
Jakob Stoklund Olesen1f323402010-07-09 20:43:13 +0000290 MachineBasicBlock *MBB = MI->getParent();
291 assert(MBB && "foldMemoryOperand needs an inserted instruction");
292 MachineFunction &MF = *MBB->getParent();
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000293
Dan Gohmanc54baa22008-12-03 18:43:12 +0000294 // Ask the target to do the actual folding.
Jakob Stoklund Olesen9fac4152010-07-13 00:23:30 +0000295 if (MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, FI)) {
296 // Add a memory operand, foldMemoryOperandImpl doesn't do that.
297 assert((!(Flags & MachineMemOperand::MOStore) ||
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000298 NewMI->mayStore()) &&
Jakob Stoklund Olesen9fac4152010-07-13 00:23:30 +0000299 "Folded a def to a non-store!");
300 assert((!(Flags & MachineMemOperand::MOLoad) ||
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000301 NewMI->mayLoad()) &&
Jakob Stoklund Olesen9fac4152010-07-13 00:23:30 +0000302 "Folded a use to a non-load!");
303 const MachineFrameInfo &MFI = *MF.getFrameInfo();
304 assert(MFI.getObjectOffset(FI) != -1);
305 MachineMemOperand *MMO =
Jay Foadf4a50842011-11-15 07:51:13 +0000306 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Chris Lattner93a95ae2010-09-21 04:46:39 +0000307 Flags, MFI.getObjectSize(FI),
Jakob Stoklund Olesen9fac4152010-07-13 00:23:30 +0000308 MFI.getObjectAlignment(FI));
309 NewMI->addMemOperand(MF, MMO);
Jakob Stoklund Olesen1f323402010-07-09 20:43:13 +0000310
Jakob Stoklund Olesen1f323402010-07-09 20:43:13 +0000311 // FIXME: change foldMemoryOperandImpl semantics to also insert NewMI.
Jakob Stoklund Olesen9fac4152010-07-13 00:23:30 +0000312 return MBB->insert(MI, NewMI);
Jakob Stoklund Olesen1f323402010-07-09 20:43:13 +0000313 }
314
Jakob Stoklund Olesen9fac4152010-07-13 00:23:30 +0000315 // Straight COPY may fold as load/store.
316 if (!MI->isCopy() || Ops.size() != 1)
317 return 0;
Dan Gohmanc54baa22008-12-03 18:43:12 +0000318
Jakob Stoklund Olesen9fac4152010-07-13 00:23:30 +0000319 const TargetRegisterClass *RC = canFoldCopy(MI, Ops[0]);
320 if (!RC)
321 return 0;
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000322
Jakob Stoklund Olesen9fac4152010-07-13 00:23:30 +0000323 const MachineOperand &MO = MI->getOperand(1-Ops[0]);
324 MachineBasicBlock::iterator Pos = MI;
325 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
Dan Gohmanc54baa22008-12-03 18:43:12 +0000326
Jakob Stoklund Olesen9fac4152010-07-13 00:23:30 +0000327 if (Flags == MachineMemOperand::MOStore)
328 storeRegToStackSlot(*MBB, Pos, MO.getReg(), MO.isKill(), FI, RC, TRI);
329 else
330 loadRegFromStackSlot(*MBB, Pos, MO.getReg(), FI, RC, TRI);
331 return --Pos;
Dan Gohmanc54baa22008-12-03 18:43:12 +0000332}
333
334/// foldMemoryOperand - Same as the previous version except it allows folding
335/// of any load and store from / to any address, not just from a specific
336/// stack slot.
337MachineInstr*
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000338TargetInstrInfo::foldMemoryOperand(MachineBasicBlock::iterator MI,
Dan Gohmanc54baa22008-12-03 18:43:12 +0000339 const SmallVectorImpl<unsigned> &Ops,
340 MachineInstr* LoadMI) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000341 assert(LoadMI->canFoldAsLoad() && "LoadMI isn't foldable!");
Dan Gohmanc54baa22008-12-03 18:43:12 +0000342#ifndef NDEBUG
343 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
344 assert(MI->getOperand(Ops[i]).isUse() && "Folding load into def!");
345#endif
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000346 MachineBasicBlock &MBB = *MI->getParent();
347 MachineFunction &MF = *MBB.getParent();
Dan Gohmanc54baa22008-12-03 18:43:12 +0000348
349 // Ask the target to do the actual folding.
350 MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, LoadMI);
351 if (!NewMI) return 0;
352
Jakob Stoklund Olesene05442d2010-07-09 17:29:08 +0000353 NewMI = MBB.insert(MI, NewMI);
354
Dan Gohmanc54baa22008-12-03 18:43:12 +0000355 // Copy the memoperands from the load to the folded instruction.
Dan Gohmanc76909a2009-09-25 20:36:54 +0000356 NewMI->setMemRefs(LoadMI->memoperands_begin(),
357 LoadMI->memoperands_end());
Dan Gohmanc54baa22008-12-03 18:43:12 +0000358
359 return NewMI;
360}
Dan Gohmana70dca12009-10-09 23:27:56 +0000361
Evan Cheng44acc242010-06-12 00:11:53 +0000362bool TargetInstrInfo::
363isReallyTriviallyReMaterializableGeneric(const MachineInstr *MI,
364 AliasAnalysis *AA) const {
Dan Gohmana70dca12009-10-09 23:27:56 +0000365 const MachineFunction &MF = *MI->getParent()->getParent();
366 const MachineRegisterInfo &MRI = MF.getRegInfo();
367 const TargetMachine &TM = MF.getTarget();
368 const TargetInstrInfo &TII = *TM.getInstrInfo();
369 const TargetRegisterInfo &TRI = *TM.getRegisterInfo();
370
Jakob Stoklund Olesen4a0a18a2011-09-01 18:27:51 +0000371 // Remat clients assume operand 0 is the defined register.
372 if (!MI->getNumOperands() || !MI->getOperand(0).isReg())
373 return false;
374 unsigned DefReg = MI->getOperand(0).getReg();
375
Jakob Stoklund Olesen9d548d02011-09-01 17:18:50 +0000376 // A sub-register definition can only be rematerialized if the instruction
377 // doesn't read the other parts of the register. Otherwise it is really a
378 // read-modify-write operation on the full virtual register which cannot be
379 // moved safely.
Jakob Stoklund Olesen4a0a18a2011-09-01 18:27:51 +0000380 if (TargetRegisterInfo::isVirtualRegister(DefReg) &&
381 MI->getOperand(0).getSubReg() && MI->readsVirtualRegister(DefReg))
Jakob Stoklund Olesen9d548d02011-09-01 17:18:50 +0000382 return false;
383
Dan Gohmana70dca12009-10-09 23:27:56 +0000384 // A load from a fixed stack slot can be rematerialized. This may be
385 // redundant with subsequent checks, but it's target-independent,
386 // simple, and a common case.
387 int FrameIdx = 0;
388 if (TII.isLoadFromStackSlot(MI, FrameIdx) &&
389 MF.getFrameInfo()->isImmutableObjectIndex(FrameIdx))
390 return true;
391
Dan Gohmana70dca12009-10-09 23:27:56 +0000392 // Avoid instructions obviously unsafe for remat.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000393 if (MI->isNotDuplicable() || MI->mayStore() ||
Evan Chengc36b7062011-01-07 23:50:32 +0000394 MI->hasUnmodeledSideEffects())
395 return false;
396
397 // Don't remat inline asm. We have no idea how expensive it is
398 // even if it's side effect free.
399 if (MI->isInlineAsm())
Dan Gohmana70dca12009-10-09 23:27:56 +0000400 return false;
401
402 // Avoid instructions which load from potentially varying memory.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000403 if (MI->mayLoad() && !MI->isInvariantLoad(AA))
Dan Gohmana70dca12009-10-09 23:27:56 +0000404 return false;
405
406 // If any of the registers accessed are non-constant, conservatively assume
407 // the instruction is not rematerializable.
408 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
409 const MachineOperand &MO = MI->getOperand(i);
410 if (!MO.isReg()) continue;
411 unsigned Reg = MO.getReg();
412 if (Reg == 0)
413 continue;
414
415 // Check for a well-behaved physical register.
416 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
417 if (MO.isUse()) {
418 // If the physreg has no defs anywhere, it's just an ambient register
419 // and we can freely move its uses. Alternatively, if it's allocatable,
420 // it could get allocated to something with a def during allocation.
421 if (!MRI.def_empty(Reg))
422 return false;
423 BitVector AllocatableRegs = TRI.getAllocatableSet(MF, 0);
424 if (AllocatableRegs.test(Reg))
425 return false;
426 // Check for a def among the register's aliases too.
427 for (const unsigned *Alias = TRI.getAliasSet(Reg); *Alias; ++Alias) {
428 unsigned AliasReg = *Alias;
429 if (!MRI.def_empty(AliasReg))
430 return false;
431 if (AllocatableRegs.test(AliasReg))
432 return false;
433 }
434 } else {
435 // A physreg def. We can't remat it.
436 return false;
437 }
438 continue;
439 }
440
Jakob Stoklund Olesen4a0a18a2011-09-01 18:27:51 +0000441 // Only allow one virtual-register def. There may be multiple defs of the
442 // same virtual register, though.
443 if (MO.isDef() && Reg != DefReg)
Dan Gohmana70dca12009-10-09 23:27:56 +0000444 return false;
445
Dan Gohmana70dca12009-10-09 23:27:56 +0000446 // Don't allow any virtual-register uses. Rematting an instruction with
447 // virtual register uses would length the live ranges of the uses, which
448 // is not necessarily a good idea, certainly not "trivial".
449 if (MO.isUse())
450 return false;
451 }
452
453 // Everything checked out.
454 return true;
455}
Evan Cheng774bc882010-06-14 21:06:53 +0000456
Evan Cheng86050dc2010-06-18 23:09:54 +0000457/// isSchedulingBoundary - Test if the given instruction should be
458/// considered a scheduling boundary. This primarily includes labels
459/// and terminators.
460bool TargetInstrInfoImpl::isSchedulingBoundary(const MachineInstr *MI,
461 const MachineBasicBlock *MBB,
462 const MachineFunction &MF) const{
463 // Terminators and labels can't be scheduled around.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000464 if (MI->isTerminator() || MI->isLabel())
Evan Cheng86050dc2010-06-18 23:09:54 +0000465 return true;
466
467 // Don't attempt to schedule around any instruction that defines
468 // a stack-oriented pointer, as it's unlikely to be profitable. This
469 // saves compile time, because it doesn't require every single
470 // stack slot reference to depend on the instruction that does the
471 // modification.
472 const TargetLowering &TLI = *MF.getTarget().getTargetLowering();
473 if (MI->definesRegister(TLI.getStackPointerRegisterToSaveRestore()))
474 return true;
475
476 return false;
477}
478
Andrew Trickc8bfd1d2011-01-21 05:51:33 +0000479// Provide a global flag for disabling the PreRA hazard recognizer that targets
480// may choose to honor.
481bool TargetInstrInfoImpl::usePreRAHazardRecognizer() const {
482 return !DisableHazardRecognizer;
483}
484
485// Default implementation of CreateTargetRAHazardRecognizer.
Andrew Trick2da8bc82010-12-24 05:03:26 +0000486ScheduleHazardRecognizer *TargetInstrInfoImpl::
487CreateTargetHazardRecognizer(const TargetMachine *TM,
488 const ScheduleDAG *DAG) const {
489 // Dummy hazard recognizer allows all instructions to issue.
490 return new ScheduleHazardRecognizer();
491}
492
Evan Cheng774bc882010-06-14 21:06:53 +0000493// Default implementation of CreateTargetPostRAHazardRecognizer.
494ScheduleHazardRecognizer *TargetInstrInfoImpl::
Andrew Trick2da8bc82010-12-24 05:03:26 +0000495CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
496 const ScheduleDAG *DAG) const {
497 return (ScheduleHazardRecognizer *)
498 new ScoreboardHazardRecognizer(II, DAG, "post-RA-sched");
Evan Cheng774bc882010-06-14 21:06:53 +0000499}