1 //===-- TargetInstrInfoImpl.cpp - Target Instruction Information ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the TargetInstrInfoImpl class, it just provides default
11 // implementations of various methods.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Target/TargetInstrInfo.h"
16 #include "llvm/Target/TargetMachine.h"
17 #include "llvm/Target/TargetRegisterInfo.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineInstr.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineMemOperand.h"
23 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 #include "llvm/CodeGen/PostRAHazardRecognizer.h"
25 #include "llvm/CodeGen/PseudoSourceValue.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/raw_ostream.h"
30 // commuteInstruction - The default implementation of this method just exchanges
31 // the two operands returned by findCommutedOpIndices.
32 MachineInstr *TargetInstrInfoImpl::commuteInstruction(MachineInstr *MI,
34 const TargetInstrDesc &TID = MI->getDesc();
35 bool HasDef = TID.getNumDefs();
36 if (HasDef && !MI->getOperand(0).isReg())
37 // No idea how to commute this instruction. Target should implement its own.
40 if (!findCommutedOpIndices(MI, Idx1, Idx2)) {
42 raw_string_ostream Msg(msg);
43 Msg << "Don't know how to commute: " << *MI;
44 report_fatal_error(Msg.str());
47 assert(MI->getOperand(Idx1).isReg() && MI->getOperand(Idx2).isReg() &&
48 "This only knows how to commute register operands so far");
49 unsigned Reg1 = MI->getOperand(Idx1).getReg();
50 unsigned Reg2 = MI->getOperand(Idx2).getReg();
51 bool Reg1IsKill = MI->getOperand(Idx1).isKill();
52 bool Reg2IsKill = MI->getOperand(Idx2).isKill();
53 bool ChangeReg0 = false;
54 if (HasDef && MI->getOperand(0).getReg() == Reg1) {
55 // Must be two address instruction!
56 assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
57 "Expecting a two-address instruction!");
63 // Create a new instruction.
64 unsigned Reg0 = HasDef
65 ? (ChangeReg0 ? Reg2 : MI->getOperand(0).getReg()) : 0;
66 bool Reg0IsDead = HasDef ? MI->getOperand(0).isDead() : false;
67 MachineFunction &MF = *MI->getParent()->getParent();
69 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
70 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
71 .addReg(Reg2, getKillRegState(Reg2IsKill))
72 .addReg(Reg1, getKillRegState(Reg2IsKill));
74 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
75 .addReg(Reg2, getKillRegState(Reg2IsKill))
76 .addReg(Reg1, getKillRegState(Reg2IsKill));
80 MI->getOperand(0).setReg(Reg2);
81 MI->getOperand(Idx2).setReg(Reg1);
82 MI->getOperand(Idx1).setReg(Reg2);
83 MI->getOperand(Idx2).setIsKill(Reg1IsKill);
84 MI->getOperand(Idx1).setIsKill(Reg2IsKill);
88 /// findCommutedOpIndices - If specified MI is commutable, return the two
89 /// operand indices that would swap value. Return true if the instruction
90 /// is not in a form which this routine understands.
91 bool TargetInstrInfoImpl::findCommutedOpIndices(MachineInstr *MI,
93 unsigned &SrcOpIdx2) const {
94 const TargetInstrDesc &TID = MI->getDesc();
95 if (!TID.isCommutable())
97 // This assumes v0 = op v1, v2 and commuting would swap v1 and v2. If this
98 // is not true, then the target must implement this.
99 SrcOpIdx1 = TID.getNumDefs();
100 SrcOpIdx2 = SrcOpIdx1 + 1;
101 if (!MI->getOperand(SrcOpIdx1).isReg() ||
102 !MI->getOperand(SrcOpIdx2).isReg())
109 bool TargetInstrInfoImpl::PredicateInstruction(MachineInstr *MI,
110 const SmallVectorImpl<MachineOperand> &Pred) const {
111 bool MadeChange = false;
112 const TargetInstrDesc &TID = MI->getDesc();
113 if (!TID.isPredicable())
116 for (unsigned j = 0, i = 0, e = MI->getNumOperands(); i != e; ++i) {
117 if (TID.OpInfo[i].isPredicate()) {
118 MachineOperand &MO = MI->getOperand(i);
120 MO.setReg(Pred[j].getReg());
122 } else if (MO.isImm()) {
123 MO.setImm(Pred[j].getImm());
125 } else if (MO.isMBB()) {
126 MO.setMBB(Pred[j].getMBB());
135 void TargetInstrInfoImpl::reMaterialize(MachineBasicBlock &MBB,
136 MachineBasicBlock::iterator I,
139 const MachineInstr *Orig,
140 const TargetRegisterInfo &TRI) const {
141 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
142 MI->substituteRegister(MI->getOperand(0).getReg(), DestReg, SubIdx, TRI);
146 bool TargetInstrInfoImpl::produceSameValue(const MachineInstr *MI0,
147 const MachineInstr *MI1) const {
148 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
151 MachineInstr *TargetInstrInfoImpl::duplicate(MachineInstr *Orig,
152 MachineFunction &MF) const {
153 assert(!Orig->getDesc().isNotDuplicable() &&
154 "Instruction cannot be duplicated");
155 return MF.CloneMachineInstr(Orig);
159 TargetInstrInfoImpl::GetFunctionSizeInBytes(const MachineFunction &MF) const {
161 for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end();
163 const MachineBasicBlock &MBB = *MBBI;
164 for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end();
166 FnSize += GetInstSizeInBytes(I);
171 /// foldMemoryOperand - Attempt to fold a load or store of the specified stack
172 /// slot into the specified machine instruction for the specified operand(s).
173 /// If this is possible, a new instruction is returned with the specified
174 /// operand folded, otherwise NULL is returned. The client is responsible for
175 /// removing the old instruction and adding the new one in the instruction
178 TargetInstrInfo::foldMemoryOperand(MachineFunction &MF,
180 const SmallVectorImpl<unsigned> &Ops,
181 int FrameIndex) const {
183 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
184 if (MI->getOperand(Ops[i]).isDef())
185 Flags |= MachineMemOperand::MOStore;
187 Flags |= MachineMemOperand::MOLoad;
189 // Ask the target to do the actual folding.
190 MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, FrameIndex);
191 if (!NewMI) return 0;
193 assert((!(Flags & MachineMemOperand::MOStore) ||
194 NewMI->getDesc().mayStore()) &&
195 "Folded a def to a non-store!");
196 assert((!(Flags & MachineMemOperand::MOLoad) ||
197 NewMI->getDesc().mayLoad()) &&
198 "Folded a use to a non-load!");
199 const MachineFrameInfo &MFI = *MF.getFrameInfo();
200 assert(MFI.getObjectOffset(FrameIndex) != -1);
201 MachineMemOperand *MMO =
202 MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FrameIndex),
204 MFI.getObjectSize(FrameIndex),
205 MFI.getObjectAlignment(FrameIndex));
206 NewMI->addMemOperand(MF, MMO);
211 /// foldMemoryOperand - Same as the previous version except it allows folding
212 /// of any load and store from / to any address, not just from a specific
215 TargetInstrInfo::foldMemoryOperand(MachineFunction &MF,
217 const SmallVectorImpl<unsigned> &Ops,
218 MachineInstr* LoadMI) const {
219 assert(LoadMI->getDesc().canFoldAsLoad() && "LoadMI isn't foldable!");
221 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
222 assert(MI->getOperand(Ops[i]).isUse() && "Folding load into def!");
225 // Ask the target to do the actual folding.
226 MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, LoadMI);
227 if (!NewMI) return 0;
229 // Copy the memoperands from the load to the folded instruction.
230 NewMI->setMemRefs(LoadMI->memoperands_begin(),
231 LoadMI->memoperands_end());
236 bool TargetInstrInfo::
237 isReallyTriviallyReMaterializableGeneric(const MachineInstr *MI,
238 AliasAnalysis *AA) const {
239 const MachineFunction &MF = *MI->getParent()->getParent();
240 const MachineRegisterInfo &MRI = MF.getRegInfo();
241 const TargetMachine &TM = MF.getTarget();
242 const TargetInstrInfo &TII = *TM.getInstrInfo();
243 const TargetRegisterInfo &TRI = *TM.getRegisterInfo();
245 // A load from a fixed stack slot can be rematerialized. This may be
246 // redundant with subsequent checks, but it's target-independent,
247 // simple, and a common case.
249 if (TII.isLoadFromStackSlot(MI, FrameIdx) &&
250 MF.getFrameInfo()->isImmutableObjectIndex(FrameIdx))
253 const TargetInstrDesc &TID = MI->getDesc();
255 // Avoid instructions obviously unsafe for remat.
256 if (TID.hasUnmodeledSideEffects() || TID.isNotDuplicable() ||
260 // Avoid instructions which load from potentially varying memory.
261 if (TID.mayLoad() && !MI->isInvariantLoad(AA))
264 // If any of the registers accessed are non-constant, conservatively assume
265 // the instruction is not rematerializable.
266 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
267 const MachineOperand &MO = MI->getOperand(i);
268 if (!MO.isReg()) continue;
269 unsigned Reg = MO.getReg();
273 // Check for a well-behaved physical register.
274 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
276 // If the physreg has no defs anywhere, it's just an ambient register
277 // and we can freely move its uses. Alternatively, if it's allocatable,
278 // it could get allocated to something with a def during allocation.
279 if (!MRI.def_empty(Reg))
281 BitVector AllocatableRegs = TRI.getAllocatableSet(MF, 0);
282 if (AllocatableRegs.test(Reg))
284 // Check for a def among the register's aliases too.
285 for (const unsigned *Alias = TRI.getAliasSet(Reg); *Alias; ++Alias) {
286 unsigned AliasReg = *Alias;
287 if (!MRI.def_empty(AliasReg))
289 if (AllocatableRegs.test(AliasReg))
293 // A physreg def. We can't remat it.
299 // Only allow one virtual-register def, and that in the first operand.
300 if (MO.isDef() != (i == 0))
303 // For the def, it should be the only def of that register.
304 if (MO.isDef() && (llvm::next(MRI.def_begin(Reg)) != MRI.def_end() ||
308 // Don't allow any virtual-register uses. Rematting an instruction with
309 // virtual register uses would length the live ranges of the uses, which
310 // is not necessarily a good idea, certainly not "trivial".
315 // Everything checked out.
319 // Default implementation of CreateTargetPostRAHazardRecognizer.
320 ScheduleHazardRecognizer *TargetInstrInfoImpl::
321 CreateTargetPostRAHazardRecognizer(const InstrItineraryData &II) const {
322 return (ScheduleHazardRecognizer *)new PostRAHazardRecognizer(II);