blob: 53f3ee84910aa5a3c32c24a98b559a51b029de21 [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"
Evan Cheng774bc882010-06-14 21:06:53 +000025#include "llvm/CodeGen/PostRAHazardRecognizer.h"
Dan Gohmanc54baa22008-12-03 18:43:12 +000026#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Cheng34c75092009-07-10 23:26:12 +000027#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/raw_ostream.h"
Chris Lattner64105522008-01-01 01:03:04 +000029using namespace llvm;
30
Evan Cheng86050dc2010-06-18 23:09:54 +000031void
32TargetInstrInfoImpl::ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail,
33 MachineBasicBlock *NewDest) const {
34 MachineBasicBlock *MBB = Tail->getParent();
35
36 // Remove all the old successors of MBB from the CFG.
37 while (!MBB->succ_empty())
38 MBB->removeSuccessor(MBB->succ_begin());
39
40 // Remove all the dead instructions from the end of MBB.
41 MBB->erase(Tail, MBB->end());
42
43 // If MBB isn't immediately before MBB, insert a branch to it.
44 if (++MachineFunction::iterator(MBB) != MachineFunction::iterator(NewDest))
45 InsertBranch(*MBB, NewDest, 0, SmallVector<MachineOperand, 0>(),
46 Tail->getDebugLoc());
47 MBB->addSuccessor(NewDest);
48}
49
Chris Lattner64105522008-01-01 01:03:04 +000050// commuteInstruction - The default implementation of this method just exchanges
Evan Cheng34c75092009-07-10 23:26:12 +000051// the two operands returned by findCommutedOpIndices.
Evan Cheng58dcb0e2008-06-16 07:33:11 +000052MachineInstr *TargetInstrInfoImpl::commuteInstruction(MachineInstr *MI,
53 bool NewMI) const {
Evan Cheng498c2902009-07-01 08:29:08 +000054 const TargetInstrDesc &TID = MI->getDesc();
55 bool HasDef = TID.getNumDefs();
Evan Cheng34c75092009-07-10 23:26:12 +000056 if (HasDef && !MI->getOperand(0).isReg())
57 // No idea how to commute this instruction. Target should implement its own.
58 return 0;
59 unsigned Idx1, Idx2;
60 if (!findCommutedOpIndices(MI, Idx1, Idx2)) {
61 std::string msg;
62 raw_string_ostream Msg(msg);
63 Msg << "Don't know how to commute: " << *MI;
Chris Lattner75361b62010-04-07 22:58:41 +000064 report_fatal_error(Msg.str());
Evan Cheng34c75092009-07-10 23:26:12 +000065 }
Evan Cheng498c2902009-07-01 08:29:08 +000066
67 assert(MI->getOperand(Idx1).isReg() && MI->getOperand(Idx2).isReg() &&
Chris Lattner64105522008-01-01 01:03:04 +000068 "This only knows how to commute register operands so far");
Evan Cheng498c2902009-07-01 08:29:08 +000069 unsigned Reg1 = MI->getOperand(Idx1).getReg();
70 unsigned Reg2 = MI->getOperand(Idx2).getReg();
71 bool Reg1IsKill = MI->getOperand(Idx1).isKill();
72 bool Reg2IsKill = MI->getOperand(Idx2).isKill();
Evan Cheng58dcb0e2008-06-16 07:33:11 +000073 bool ChangeReg0 = false;
Evan Cheng498c2902009-07-01 08:29:08 +000074 if (HasDef && MI->getOperand(0).getReg() == Reg1) {
Evan Chenga4d16a12008-02-13 02:46:49 +000075 // Must be two address instruction!
76 assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
77 "Expecting a two-address instruction!");
78 Reg2IsKill = false;
Evan Cheng58dcb0e2008-06-16 07:33:11 +000079 ChangeReg0 = true;
Evan Chenga4d16a12008-02-13 02:46:49 +000080 }
Evan Cheng58dcb0e2008-06-16 07:33:11 +000081
82 if (NewMI) {
83 // Create a new instruction.
Evan Cheng498c2902009-07-01 08:29:08 +000084 unsigned Reg0 = HasDef
85 ? (ChangeReg0 ? Reg2 : MI->getOperand(0).getReg()) : 0;
86 bool Reg0IsDead = HasDef ? MI->getOperand(0).isDead() : false;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000087 MachineFunction &MF = *MI->getParent()->getParent();
Evan Cheng498c2902009-07-01 08:29:08 +000088 if (HasDef)
89 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
90 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
91 .addReg(Reg2, getKillRegState(Reg2IsKill))
92 .addReg(Reg1, getKillRegState(Reg2IsKill));
93 else
94 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
95 .addReg(Reg2, getKillRegState(Reg2IsKill))
96 .addReg(Reg1, getKillRegState(Reg2IsKill));
Evan Cheng58dcb0e2008-06-16 07:33:11 +000097 }
98
99 if (ChangeReg0)
100 MI->getOperand(0).setReg(Reg2);
Evan Cheng498c2902009-07-01 08:29:08 +0000101 MI->getOperand(Idx2).setReg(Reg1);
102 MI->getOperand(Idx1).setReg(Reg2);
103 MI->getOperand(Idx2).setIsKill(Reg1IsKill);
104 MI->getOperand(Idx1).setIsKill(Reg2IsKill);
Chris Lattner64105522008-01-01 01:03:04 +0000105 return MI;
106}
107
Evan Cheng261ce1d2009-07-10 19:15:51 +0000108/// findCommutedOpIndices - If specified MI is commutable, return the two
109/// operand indices that would swap value. Return true if the instruction
110/// is not in a form which this routine understands.
111bool TargetInstrInfoImpl::findCommutedOpIndices(MachineInstr *MI,
112 unsigned &SrcOpIdx1,
113 unsigned &SrcOpIdx2) const {
Evan Cheng498c2902009-07-01 08:29:08 +0000114 const TargetInstrDesc &TID = MI->getDesc();
Evan Cheng261ce1d2009-07-10 19:15:51 +0000115 if (!TID.isCommutable())
Evan Cheng498c2902009-07-01 08:29:08 +0000116 return false;
Evan Cheng261ce1d2009-07-10 19:15:51 +0000117 // This assumes v0 = op v1, v2 and commuting would swap v1 and v2. If this
118 // is not true, then the target must implement this.
119 SrcOpIdx1 = TID.getNumDefs();
120 SrcOpIdx2 = SrcOpIdx1 + 1;
121 if (!MI->getOperand(SrcOpIdx1).isReg() ||
122 !MI->getOperand(SrcOpIdx2).isReg())
123 // No idea.
124 return false;
125 return true;
Evan Chengf20db152008-02-15 18:21:33 +0000126}
127
128
Chris Lattner64105522008-01-01 01:03:04 +0000129bool TargetInstrInfoImpl::PredicateInstruction(MachineInstr *MI,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000130 const SmallVectorImpl<MachineOperand> &Pred) const {
Chris Lattner64105522008-01-01 01:03:04 +0000131 bool MadeChange = false;
Chris Lattner749c6f62008-01-07 07:27:27 +0000132 const TargetInstrDesc &TID = MI->getDesc();
133 if (!TID.isPredicable())
134 return false;
135
136 for (unsigned j = 0, i = 0, e = MI->getNumOperands(); i != e; ++i) {
137 if (TID.OpInfo[i].isPredicate()) {
138 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000139 if (MO.isReg()) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000140 MO.setReg(Pred[j].getReg());
141 MadeChange = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000142 } else if (MO.isImm()) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000143 MO.setImm(Pred[j].getImm());
144 MadeChange = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000145 } else if (MO.isMBB()) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000146 MO.setMBB(Pred[j].getMBB());
147 MadeChange = true;
Chris Lattner64105522008-01-01 01:03:04 +0000148 }
Chris Lattner749c6f62008-01-07 07:27:27 +0000149 ++j;
Chris Lattner64105522008-01-01 01:03:04 +0000150 }
151 }
152 return MadeChange;
153}
Evan Chengca1267c2008-03-31 20:40:39 +0000154
155void TargetInstrInfoImpl::reMaterialize(MachineBasicBlock &MBB,
156 MachineBasicBlock::iterator I,
157 unsigned DestReg,
Evan Cheng37844532009-07-16 09:20:10 +0000158 unsigned SubIdx,
Evan Chengd57cdd52009-11-14 02:55:43 +0000159 const MachineInstr *Orig,
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +0000160 const TargetRegisterInfo &TRI) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000161 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +0000162 MI->substituteRegister(MI->getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengca1267c2008-03-31 20:40:39 +0000163 MBB.insert(I, MI);
164}
165
Evan Cheng506049f2010-03-03 01:44:33 +0000166bool TargetInstrInfoImpl::produceSameValue(const MachineInstr *MI0,
167 const MachineInstr *MI1) const {
168 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
169}
170
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000171MachineInstr *TargetInstrInfoImpl::duplicate(MachineInstr *Orig,
172 MachineFunction &MF) const {
173 assert(!Orig->getDesc().isNotDuplicable() &&
174 "Instruction cannot be duplicated");
175 return MF.CloneMachineInstr(Orig);
176}
177
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000178unsigned
179TargetInstrInfoImpl::GetFunctionSizeInBytes(const MachineFunction &MF) const {
180 unsigned FnSize = 0;
181 for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end();
182 MBBI != E; ++MBBI) {
183 const MachineBasicBlock &MBB = *MBBI;
Evan Cheng38855782008-09-11 05:58:06 +0000184 for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end();
185 I != E; ++I)
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000186 FnSize += GetInstSizeInBytes(I);
187 }
188 return FnSize;
189}
Dan Gohmanc54baa22008-12-03 18:43:12 +0000190
191/// foldMemoryOperand - Attempt to fold a load or store of the specified stack
192/// slot into the specified machine instruction for the specified operand(s).
193/// If this is possible, a new instruction is returned with the specified
194/// operand folded, otherwise NULL is returned. The client is responsible for
195/// removing the old instruction and adding the new one in the instruction
196/// stream.
197MachineInstr*
198TargetInstrInfo::foldMemoryOperand(MachineFunction &MF,
199 MachineInstr* MI,
200 const SmallVectorImpl<unsigned> &Ops,
201 int FrameIndex) const {
202 unsigned Flags = 0;
203 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
204 if (MI->getOperand(Ops[i]).isDef())
205 Flags |= MachineMemOperand::MOStore;
206 else
207 Flags |= MachineMemOperand::MOLoad;
208
209 // Ask the target to do the actual folding.
210 MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, FrameIndex);
211 if (!NewMI) return 0;
212
213 assert((!(Flags & MachineMemOperand::MOStore) ||
214 NewMI->getDesc().mayStore()) &&
215 "Folded a def to a non-store!");
216 assert((!(Flags & MachineMemOperand::MOLoad) ||
217 NewMI->getDesc().mayLoad()) &&
218 "Folded a use to a non-load!");
219 const MachineFrameInfo &MFI = *MF.getFrameInfo();
220 assert(MFI.getObjectOffset(FrameIndex) != -1);
Dan Gohmanc76909a2009-09-25 20:36:54 +0000221 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +0000222 MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FrameIndex),
Dan Gohmanc76909a2009-09-25 20:36:54 +0000223 Flags, /*Offset=*/0,
224 MFI.getObjectSize(FrameIndex),
225 MFI.getObjectAlignment(FrameIndex));
Dan Gohmanc54baa22008-12-03 18:43:12 +0000226 NewMI->addMemOperand(MF, MMO);
227
228 return NewMI;
229}
230
231/// foldMemoryOperand - Same as the previous version except it allows folding
232/// of any load and store from / to any address, not just from a specific
233/// stack slot.
234MachineInstr*
235TargetInstrInfo::foldMemoryOperand(MachineFunction &MF,
236 MachineInstr* MI,
237 const SmallVectorImpl<unsigned> &Ops,
238 MachineInstr* LoadMI) const {
239 assert(LoadMI->getDesc().canFoldAsLoad() && "LoadMI isn't foldable!");
240#ifndef NDEBUG
241 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
242 assert(MI->getOperand(Ops[i]).isUse() && "Folding load into def!");
243#endif
244
245 // Ask the target to do the actual folding.
246 MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, LoadMI);
247 if (!NewMI) return 0;
248
249 // Copy the memoperands from the load to the folded instruction.
Dan Gohmanc76909a2009-09-25 20:36:54 +0000250 NewMI->setMemRefs(LoadMI->memoperands_begin(),
251 LoadMI->memoperands_end());
Dan Gohmanc54baa22008-12-03 18:43:12 +0000252
253 return NewMI;
254}
Dan Gohmana70dca12009-10-09 23:27:56 +0000255
Evan Cheng44acc242010-06-12 00:11:53 +0000256bool TargetInstrInfo::
257isReallyTriviallyReMaterializableGeneric(const MachineInstr *MI,
258 AliasAnalysis *AA) const {
Dan Gohmana70dca12009-10-09 23:27:56 +0000259 const MachineFunction &MF = *MI->getParent()->getParent();
260 const MachineRegisterInfo &MRI = MF.getRegInfo();
261 const TargetMachine &TM = MF.getTarget();
262 const TargetInstrInfo &TII = *TM.getInstrInfo();
263 const TargetRegisterInfo &TRI = *TM.getRegisterInfo();
264
265 // A load from a fixed stack slot can be rematerialized. This may be
266 // redundant with subsequent checks, but it's target-independent,
267 // simple, and a common case.
268 int FrameIdx = 0;
269 if (TII.isLoadFromStackSlot(MI, FrameIdx) &&
270 MF.getFrameInfo()->isImmutableObjectIndex(FrameIdx))
271 return true;
272
273 const TargetInstrDesc &TID = MI->getDesc();
274
275 // Avoid instructions obviously unsafe for remat.
276 if (TID.hasUnmodeledSideEffects() || TID.isNotDuplicable() ||
277 TID.mayStore())
278 return false;
279
280 // Avoid instructions which load from potentially varying memory.
281 if (TID.mayLoad() && !MI->isInvariantLoad(AA))
282 return false;
283
284 // If any of the registers accessed are non-constant, conservatively assume
285 // the instruction is not rematerializable.
286 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
287 const MachineOperand &MO = MI->getOperand(i);
288 if (!MO.isReg()) continue;
289 unsigned Reg = MO.getReg();
290 if (Reg == 0)
291 continue;
292
293 // Check for a well-behaved physical register.
294 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
295 if (MO.isUse()) {
296 // If the physreg has no defs anywhere, it's just an ambient register
297 // and we can freely move its uses. Alternatively, if it's allocatable,
298 // it could get allocated to something with a def during allocation.
299 if (!MRI.def_empty(Reg))
300 return false;
301 BitVector AllocatableRegs = TRI.getAllocatableSet(MF, 0);
302 if (AllocatableRegs.test(Reg))
303 return false;
304 // Check for a def among the register's aliases too.
305 for (const unsigned *Alias = TRI.getAliasSet(Reg); *Alias; ++Alias) {
306 unsigned AliasReg = *Alias;
307 if (!MRI.def_empty(AliasReg))
308 return false;
309 if (AllocatableRegs.test(AliasReg))
310 return false;
311 }
312 } else {
313 // A physreg def. We can't remat it.
314 return false;
315 }
316 continue;
317 }
318
319 // Only allow one virtual-register def, and that in the first operand.
320 if (MO.isDef() != (i == 0))
321 return false;
322
323 // For the def, it should be the only def of that register.
Chris Lattner7896c9f2009-12-03 00:50:42 +0000324 if (MO.isDef() && (llvm::next(MRI.def_begin(Reg)) != MRI.def_end() ||
Dan Gohmana70dca12009-10-09 23:27:56 +0000325 MRI.isLiveIn(Reg)))
326 return false;
327
328 // Don't allow any virtual-register uses. Rematting an instruction with
329 // virtual register uses would length the live ranges of the uses, which
330 // is not necessarily a good idea, certainly not "trivial".
331 if (MO.isUse())
332 return false;
333 }
334
335 // Everything checked out.
336 return true;
337}
Evan Cheng774bc882010-06-14 21:06:53 +0000338
Evan Cheng86050dc2010-06-18 23:09:54 +0000339/// isSchedulingBoundary - Test if the given instruction should be
340/// considered a scheduling boundary. This primarily includes labels
341/// and terminators.
342bool TargetInstrInfoImpl::isSchedulingBoundary(const MachineInstr *MI,
343 const MachineBasicBlock *MBB,
344 const MachineFunction &MF) const{
345 // Terminators and labels can't be scheduled around.
346 if (MI->getDesc().isTerminator() || MI->isLabel())
347 return true;
348
349 // Don't attempt to schedule around any instruction that defines
350 // a stack-oriented pointer, as it's unlikely to be profitable. This
351 // saves compile time, because it doesn't require every single
352 // stack slot reference to depend on the instruction that does the
353 // modification.
354 const TargetLowering &TLI = *MF.getTarget().getTargetLowering();
355 if (MI->definesRegister(TLI.getStackPointerRegisterToSaveRestore()))
356 return true;
357
358 return false;
359}
360
Evan Cheng774bc882010-06-14 21:06:53 +0000361// Default implementation of CreateTargetPostRAHazardRecognizer.
362ScheduleHazardRecognizer *TargetInstrInfoImpl::
363CreateTargetPostRAHazardRecognizer(const InstrItineraryData &II) const {
364 return (ScheduleHazardRecognizer *)new PostRAHazardRecognizer(II);
365}