blob: ab67cd2a08dd7214e7482df6990965adf60f53cf [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"
Owen Anderson44eb65c2008-08-14 22:49:33 +000016#include "llvm/ADT/SmallVector.h"
Dan Gohmanc54baa22008-12-03 18:43:12 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner64105522008-01-01 01:03:04 +000018#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng58dcb0e2008-06-16 07:33:11 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohmanc76909a2009-09-25 20:36:54 +000020#include "llvm/CodeGen/MachineMemOperand.h"
Dan Gohmanc54baa22008-12-03 18:43:12 +000021#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Cheng34c75092009-07-10 23:26:12 +000022#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/raw_ostream.h"
Chris Lattner64105522008-01-01 01:03:04 +000024using namespace llvm;
25
26// commuteInstruction - The default implementation of this method just exchanges
Evan Cheng34c75092009-07-10 23:26:12 +000027// the two operands returned by findCommutedOpIndices.
Evan Cheng58dcb0e2008-06-16 07:33:11 +000028MachineInstr *TargetInstrInfoImpl::commuteInstruction(MachineInstr *MI,
29 bool NewMI) const {
Evan Cheng498c2902009-07-01 08:29:08 +000030 const TargetInstrDesc &TID = MI->getDesc();
31 bool HasDef = TID.getNumDefs();
Evan Cheng34c75092009-07-10 23:26:12 +000032 if (HasDef && !MI->getOperand(0).isReg())
33 // No idea how to commute this instruction. Target should implement its own.
34 return 0;
35 unsigned Idx1, Idx2;
36 if (!findCommutedOpIndices(MI, Idx1, Idx2)) {
37 std::string msg;
38 raw_string_ostream Msg(msg);
39 Msg << "Don't know how to commute: " << *MI;
40 llvm_report_error(Msg.str());
41 }
Evan Cheng498c2902009-07-01 08:29:08 +000042
43 assert(MI->getOperand(Idx1).isReg() && MI->getOperand(Idx2).isReg() &&
Chris Lattner64105522008-01-01 01:03:04 +000044 "This only knows how to commute register operands so far");
Evan Cheng498c2902009-07-01 08:29:08 +000045 unsigned Reg1 = MI->getOperand(Idx1).getReg();
46 unsigned Reg2 = MI->getOperand(Idx2).getReg();
47 bool Reg1IsKill = MI->getOperand(Idx1).isKill();
48 bool Reg2IsKill = MI->getOperand(Idx2).isKill();
Evan Cheng58dcb0e2008-06-16 07:33:11 +000049 bool ChangeReg0 = false;
Evan Cheng498c2902009-07-01 08:29:08 +000050 if (HasDef && MI->getOperand(0).getReg() == Reg1) {
Evan Chenga4d16a12008-02-13 02:46:49 +000051 // Must be two address instruction!
52 assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) &&
53 "Expecting a two-address instruction!");
54 Reg2IsKill = false;
Evan Cheng58dcb0e2008-06-16 07:33:11 +000055 ChangeReg0 = true;
Evan Chenga4d16a12008-02-13 02:46:49 +000056 }
Evan Cheng58dcb0e2008-06-16 07:33:11 +000057
58 if (NewMI) {
59 // Create a new instruction.
Evan Cheng498c2902009-07-01 08:29:08 +000060 unsigned Reg0 = HasDef
61 ? (ChangeReg0 ? Reg2 : MI->getOperand(0).getReg()) : 0;
62 bool Reg0IsDead = HasDef ? MI->getOperand(0).isDead() : false;
Dan Gohman8e5f2c62008-07-07 23:14:23 +000063 MachineFunction &MF = *MI->getParent()->getParent();
Evan Cheng498c2902009-07-01 08:29:08 +000064 if (HasDef)
65 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
66 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
67 .addReg(Reg2, getKillRegState(Reg2IsKill))
68 .addReg(Reg1, getKillRegState(Reg2IsKill));
69 else
70 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
71 .addReg(Reg2, getKillRegState(Reg2IsKill))
72 .addReg(Reg1, getKillRegState(Reg2IsKill));
Evan Cheng58dcb0e2008-06-16 07:33:11 +000073 }
74
75 if (ChangeReg0)
76 MI->getOperand(0).setReg(Reg2);
Evan Cheng498c2902009-07-01 08:29:08 +000077 MI->getOperand(Idx2).setReg(Reg1);
78 MI->getOperand(Idx1).setReg(Reg2);
79 MI->getOperand(Idx2).setIsKill(Reg1IsKill);
80 MI->getOperand(Idx1).setIsKill(Reg2IsKill);
Chris Lattner64105522008-01-01 01:03:04 +000081 return MI;
82}
83
Evan Cheng261ce1d2009-07-10 19:15:51 +000084/// findCommutedOpIndices - If specified MI is commutable, return the two
85/// operand indices that would swap value. Return true if the instruction
86/// is not in a form which this routine understands.
87bool TargetInstrInfoImpl::findCommutedOpIndices(MachineInstr *MI,
88 unsigned &SrcOpIdx1,
89 unsigned &SrcOpIdx2) const {
Evan Cheng498c2902009-07-01 08:29:08 +000090 const TargetInstrDesc &TID = MI->getDesc();
Evan Cheng261ce1d2009-07-10 19:15:51 +000091 if (!TID.isCommutable())
Evan Cheng498c2902009-07-01 08:29:08 +000092 return false;
Evan Cheng261ce1d2009-07-10 19:15:51 +000093 // This assumes v0 = op v1, v2 and commuting would swap v1 and v2. If this
94 // is not true, then the target must implement this.
95 SrcOpIdx1 = TID.getNumDefs();
96 SrcOpIdx2 = SrcOpIdx1 + 1;
97 if (!MI->getOperand(SrcOpIdx1).isReg() ||
98 !MI->getOperand(SrcOpIdx2).isReg())
99 // No idea.
100 return false;
101 return true;
Evan Chengf20db152008-02-15 18:21:33 +0000102}
103
104
Chris Lattner64105522008-01-01 01:03:04 +0000105bool TargetInstrInfoImpl::PredicateInstruction(MachineInstr *MI,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000106 const SmallVectorImpl<MachineOperand> &Pred) const {
Chris Lattner64105522008-01-01 01:03:04 +0000107 bool MadeChange = false;
Chris Lattner749c6f62008-01-07 07:27:27 +0000108 const TargetInstrDesc &TID = MI->getDesc();
109 if (!TID.isPredicable())
110 return false;
111
112 for (unsigned j = 0, i = 0, e = MI->getNumOperands(); i != e; ++i) {
113 if (TID.OpInfo[i].isPredicate()) {
114 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000115 if (MO.isReg()) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000116 MO.setReg(Pred[j].getReg());
117 MadeChange = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000118 } else if (MO.isImm()) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000119 MO.setImm(Pred[j].getImm());
120 MadeChange = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000121 } else if (MO.isMBB()) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000122 MO.setMBB(Pred[j].getMBB());
123 MadeChange = true;
Chris Lattner64105522008-01-01 01:03:04 +0000124 }
Chris Lattner749c6f62008-01-07 07:27:27 +0000125 ++j;
Chris Lattner64105522008-01-01 01:03:04 +0000126 }
127 }
128 return MadeChange;
129}
Evan Chengca1267c2008-03-31 20:40:39 +0000130
131void TargetInstrInfoImpl::reMaterialize(MachineBasicBlock &MBB,
132 MachineBasicBlock::iterator I,
133 unsigned DestReg,
Evan Cheng37844532009-07-16 09:20:10 +0000134 unsigned SubIdx,
Evan Chengca1267c2008-03-31 20:40:39 +0000135 const MachineInstr *Orig) const {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000136 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Evan Cheng37844532009-07-16 09:20:10 +0000137 MachineOperand &MO = MI->getOperand(0);
138 MO.setReg(DestReg);
139 MO.setSubReg(SubIdx);
Evan Chengca1267c2008-03-31 20:40:39 +0000140 MBB.insert(I, MI);
141}
142
Evan Chengfc6ad402009-07-22 00:25:27 +0000143bool TargetInstrInfoImpl::isDeadInstruction(const MachineInstr *MI) const {
144 const TargetInstrDesc &TID = MI->getDesc();
145 if (TID.mayLoad() || TID.mayStore() || TID.isCall() || TID.isTerminator() ||
146 TID.isCall() || TID.isBarrier() || TID.isReturn() ||
147 TID.hasUnmodeledSideEffects())
148 return false;
149 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
150 const MachineOperand &MO = MI->getOperand(i);
151 if (!MO.isReg() || !MO.getReg())
152 continue;
153 if (MO.isDef() && !MO.isDead())
154 return false;
155 if (MO.isUse() && MO.isKill())
156 // FIXME: We can't remove kill markers or else the scavenger will assert.
157 // An alternative is to add a ADD pseudo instruction to replace kill
158 // markers.
159 return false;
160 }
161 return true;
162}
163
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000164unsigned
165TargetInstrInfoImpl::GetFunctionSizeInBytes(const MachineFunction &MF) const {
166 unsigned FnSize = 0;
167 for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end();
168 MBBI != E; ++MBBI) {
169 const MachineBasicBlock &MBB = *MBBI;
Evan Cheng38855782008-09-11 05:58:06 +0000170 for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end();
171 I != E; ++I)
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000172 FnSize += GetInstSizeInBytes(I);
173 }
174 return FnSize;
175}
Dan Gohmanc54baa22008-12-03 18:43:12 +0000176
177/// foldMemoryOperand - Attempt to fold a load or store of the specified stack
178/// slot into the specified machine instruction for the specified operand(s).
179/// If this is possible, a new instruction is returned with the specified
180/// operand folded, otherwise NULL is returned. The client is responsible for
181/// removing the old instruction and adding the new one in the instruction
182/// stream.
183MachineInstr*
184TargetInstrInfo::foldMemoryOperand(MachineFunction &MF,
185 MachineInstr* MI,
186 const SmallVectorImpl<unsigned> &Ops,
187 int FrameIndex) const {
188 unsigned Flags = 0;
189 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
190 if (MI->getOperand(Ops[i]).isDef())
191 Flags |= MachineMemOperand::MOStore;
192 else
193 Flags |= MachineMemOperand::MOLoad;
194
195 // Ask the target to do the actual folding.
196 MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, FrameIndex);
197 if (!NewMI) return 0;
198
199 assert((!(Flags & MachineMemOperand::MOStore) ||
200 NewMI->getDesc().mayStore()) &&
201 "Folded a def to a non-store!");
202 assert((!(Flags & MachineMemOperand::MOLoad) ||
203 NewMI->getDesc().mayLoad()) &&
204 "Folded a use to a non-load!");
205 const MachineFrameInfo &MFI = *MF.getFrameInfo();
206 assert(MFI.getObjectOffset(FrameIndex) != -1);
Dan Gohmanc76909a2009-09-25 20:36:54 +0000207 MachineMemOperand *MMO =
208 MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FrameIndex),
209 Flags, /*Offset=*/0,
210 MFI.getObjectSize(FrameIndex),
211 MFI.getObjectAlignment(FrameIndex));
Dan Gohmanc54baa22008-12-03 18:43:12 +0000212 NewMI->addMemOperand(MF, MMO);
213
214 return NewMI;
215}
216
217/// foldMemoryOperand - Same as the previous version except it allows folding
218/// of any load and store from / to any address, not just from a specific
219/// stack slot.
220MachineInstr*
221TargetInstrInfo::foldMemoryOperand(MachineFunction &MF,
222 MachineInstr* MI,
223 const SmallVectorImpl<unsigned> &Ops,
224 MachineInstr* LoadMI) const {
225 assert(LoadMI->getDesc().canFoldAsLoad() && "LoadMI isn't foldable!");
226#ifndef NDEBUG
227 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
228 assert(MI->getOperand(Ops[i]).isUse() && "Folding load into def!");
229#endif
230
231 // Ask the target to do the actual folding.
232 MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, LoadMI);
233 if (!NewMI) return 0;
234
235 // Copy the memoperands from the load to the folded instruction.
Dan Gohmanc76909a2009-09-25 20:36:54 +0000236 NewMI->setMemRefs(LoadMI->memoperands_begin(),
237 LoadMI->memoperands_end());
Dan Gohmanc54baa22008-12-03 18:43:12 +0000238
239 return NewMI;
240}