blob: 96722b26ee864f4742c3186cc0e1dc4a777ce375 [file] [log] [blame]
Eugene Zelenko4e9736b2017-05-31 01:10:10 +00001//===- lib/CodeGen/MachineInstr.cpp ---------------------------------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Brian Gaekee8f7c2f2004-02-13 04:39:32 +00009//
10// Methods common to all machine instructions.
11//
Chris Lattner959a5fb2002-08-09 20:08:06 +000012//===----------------------------------------------------------------------===//
Vikram S. Adveab9e5572001-07-21 12:41:50 +000013
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "llvm/CodeGen/MachineInstr.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000015#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/ArrayRef.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/FoldingSet.h"
18#include "llvm/ADT/Hashing.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000019#include "llvm/ADT/None.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000020#include "llvm/ADT/STLExtras.h"
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +000021#include "llvm/ADT/SmallBitVector.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000022#include "llvm/ADT/SmallString.h"
23#include "llvm/ADT/SmallVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Analysis/AliasAnalysis.h"
Hiroshi Inoue95f24dc2017-06-24 15:17:38 +000025#include "llvm/Analysis/Loads.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000026#include "llvm/Analysis/MemoryLocation.h"
27#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
28#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner63f41ab2004-02-19 16:17:08 +000029#include "llvm/CodeGen/MachineFunction.h"
Reid Kleckner28865802016-04-14 18:29:59 +000030#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000031#include "llvm/CodeGen/MachineInstrBundle.h"
Dan Gohman48b185d2009-09-25 20:36:54 +000032#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +000033#include "llvm/CodeGen/MachineModuleInfo.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000034#include "llvm/CodeGen/MachineOperand.h"
Chris Lattner961e7422008-01-01 01:12:31 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman2d489b52008-02-06 22:27:42 +000036#include "llvm/CodeGen/PseudoSourceValue.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000037#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000038#include "llvm/CodeGen/TargetRegisterInfo.h"
39#include "llvm/CodeGen/TargetSubtargetInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000040#include "llvm/IR/Constants.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000041#include "llvm/IR/DebugInfoMetadata.h"
42#include "llvm/IR/DebugLoc.h"
43#include "llvm/IR/DerivedTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000044#include "llvm/IR/Function.h"
45#include "llvm/IR/InlineAsm.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000046#include "llvm/IR/InstrTypes.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000047#include "llvm/IR/Intrinsics.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000048#include "llvm/IR/LLVMContext.h"
49#include "llvm/IR/Metadata.h"
50#include "llvm/IR/Module.h"
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +000051#include "llvm/IR/ModuleSlotTracker.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000052#include "llvm/IR/Type.h"
53#include "llvm/IR/Value.h"
Evan Cheng6cc775f2011-06-28 19:10:37 +000054#include "llvm/MC/MCInstrDesc.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000055#include "llvm/MC/MCRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000056#include "llvm/MC/MCSymbol.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000057#include "llvm/Support/Casting.h"
Daniel Sanders1e97a0b2015-08-19 12:03:04 +000058#include "llvm/Support/CommandLine.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000059#include "llvm/Support/Compiler.h"
David Greene29388d62010-01-04 23:48:20 +000060#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000061#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000062#include "llvm/Support/LowLevelTypeImpl.h"
Dan Gohmanaedb4a62008-07-07 20:32:02 +000063#include "llvm/Support/MathExtras.h"
Chris Lattnera078d832008-08-24 20:37:32 +000064#include "llvm/Support/raw_ostream.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000065#include "llvm/Target/TargetIntrinsicInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000066#include "llvm/Target/TargetMachine.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000067#include <algorithm>
68#include <cassert>
69#include <cstddef>
70#include <cstdint>
71#include <cstring>
72#include <iterator>
73#include <utility>
74
Chris Lattner43df6c22004-02-23 18:38:20 +000075using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000076
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +000077void MachineInstr::addImplicitDefUseOperands(MachineFunction &MF) {
Evan Cheng6cc775f2011-06-28 19:10:37 +000078 if (MCID->ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +000079 for (const MCPhysReg *ImpDefs = MCID->getImplicitDefs(); *ImpDefs;
80 ++ImpDefs)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +000081 addOperand(MF, MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng6cc775f2011-06-28 19:10:37 +000082 if (MCID->ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +000083 for (const MCPhysReg *ImpUses = MCID->getImplicitUses(); *ImpUses;
84 ++ImpUses)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +000085 addOperand(MF, MachineOperand::CreateReg(*ImpUses, false, true));
Evan Cheng77af6ac2006-11-13 23:34:06 +000086}
87
Bob Wilson406f2702010-04-09 04:34:03 +000088/// MachineInstr ctor - This constructor creates a MachineInstr and adds the
89/// implicit operands. It reserves space for the number of operands specified by
Evan Cheng6cc775f2011-06-28 19:10:37 +000090/// the MCInstrDesc.
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +000091MachineInstr::MachineInstr(MachineFunction &MF, const MCInstrDesc &tid,
Benjamin Kramera9591b52015-02-07 12:28:15 +000092 DebugLoc dl, bool NoImp)
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000093 : MCID(&tid), debugLoc(std::move(dl)) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000094 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
95
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +000096 // Reserve space for the expected number of operands.
97 if (unsigned NumOps = MCID->getNumOperands() +
98 MCID->getNumImplicitDefs() + MCID->getNumImplicitUses()) {
99 CapOperands = OperandCapacity::get(NumOps);
100 Operands = MF.allocateOperandArray(CapOperands);
101 }
102
Dale Johannesen4e04ef32009-01-27 23:20:29 +0000103 if (!NoImp)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000104 addImplicitDefUseOperands(MF);
Dale Johannesen4e04ef32009-01-27 23:20:29 +0000105}
106
Misha Brukmanb47ab7a2004-07-09 14:45:17 +0000107/// MachineInstr ctor - Copies MachineInstr arg exactly
108///
Evan Chenga7a20c42008-07-19 00:37:25 +0000109MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Eugene Zelenko4e9736b2017-05-31 01:10:10 +0000110 : MCID(&MI.getDesc()), NumMemRefs(MI.NumMemRefs), MemRefs(MI.MemRefs),
111 debugLoc(MI.getDebugLoc()) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000112 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
113
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000114 CapOperands = OperandCapacity::get(MI.getNumOperands());
115 Operands = MF.allocateOperandArray(CapOperands);
Tanya Lattner9953d862004-05-23 20:58:02 +0000116
Jakob Stoklund Olesendc5285f2013-01-05 05:05:51 +0000117 // Copy operands.
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000118 for (const MachineOperand &MO : MI.operands())
119 addOperand(MF, MO);
Tanya Lattnerbcee21b2004-05-24 03:14:18 +0000120
Jakob Stoklund Olesena33f5042012-12-18 21:36:05 +0000121 // Copy all the sensible flags.
122 setFlags(MI.Flags);
Alkis Evlogimenos14f3fe82004-02-16 07:17:43 +0000123}
124
Chris Lattner961e7422008-01-01 01:12:31 +0000125/// getRegInfo - If this instruction is embedded into a MachineFunction,
126/// return the MachineRegisterInfo object for the current function, otherwise
127/// return null.
128MachineRegisterInfo *MachineInstr::getRegInfo() {
129 if (MachineBasicBlock *MBB = getParent())
Dan Gohmanf188fa42008-07-08 23:59:09 +0000130 return &MBB->getParent()->getRegInfo();
Craig Topperc0196b12014-04-14 00:51:57 +0000131 return nullptr;
Chris Lattner961e7422008-01-01 01:12:31 +0000132}
133
134/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
135/// this instruction from their respective use lists. This requires that the
136/// operands already be on their use lists.
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000137void MachineInstr::RemoveRegOperandsFromUseLists(MachineRegisterInfo &MRI) {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000138 for (MachineOperand &MO : operands())
139 if (MO.isReg())
140 MRI.removeRegOperandFromUseList(&MO);
Chris Lattner961e7422008-01-01 01:12:31 +0000141}
142
143/// AddRegOperandsToUseLists - Add all of the register operands in
144/// this instruction from their respective use lists. This requires that the
145/// operands not be on their use lists yet.
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000146void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &MRI) {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000147 for (MachineOperand &MO : operands())
148 if (MO.isReg())
149 MRI.addRegOperandToUseList(&MO);
Chris Lattner961e7422008-01-01 01:12:31 +0000150}
151
Jakob Stoklund Olesen2455b5852012-12-20 22:54:05 +0000152void MachineInstr::addOperand(const MachineOperand &Op) {
153 MachineBasicBlock *MBB = getParent();
154 assert(MBB && "Use MachineInstrBuilder to add operands to dangling instrs");
155 MachineFunction *MF = MBB->getParent();
156 assert(MF && "Use MachineInstrBuilder to add operands to dangling instrs");
157 addOperand(*MF, Op);
158}
159
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000160/// Move NumOps MachineOperands from Src to Dst, with support for overlapping
161/// ranges. If MRI is non-null also update use-def chains.
162static void moveOperands(MachineOperand *Dst, MachineOperand *Src,
163 unsigned NumOps, MachineRegisterInfo *MRI) {
164 if (MRI)
165 return MRI->moveOperands(Dst, Src, NumOps);
166
JF Bastiena874d1a2016-03-26 18:20:02 +0000167 // MachineOperand is a trivially copyable type so we can just use memmove.
Benjamin Kramer5c0e64f2015-02-21 16:22:48 +0000168 std::memmove(Dst, Src, NumOps * sizeof(MachineOperand));
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000169}
170
Chris Lattner961e7422008-01-01 01:12:31 +0000171/// addOperand - Add the specified operand to the instruction. If it is an
172/// implicit operand, it is added to the end of the operand list. If it is
173/// an explicit operand it is added at the end of the explicit operand list
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000174/// (before the first implicit operand).
Jakob Stoklund Olesen2455b5852012-12-20 22:54:05 +0000175void MachineInstr::addOperand(MachineFunction &MF, const MachineOperand &Op) {
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000176 assert(MCID && "Cannot add operands before providing an instr descriptor");
Dan Gohman9356d8f2008-12-09 22:45:08 +0000177
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000178 // Check if we're adding one of our existing operands.
179 if (&Op >= Operands && &Op < Operands + NumOperands) {
180 // This is unusual: MI->addOperand(MI->getOperand(i)).
181 // If adding Op requires reallocating or moving existing operands around,
182 // the Op reference could go stale. Support it by copying Op.
183 MachineOperand CopyOp(Op);
184 return addOperand(MF, CopyOp);
185 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000186
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000187 // Find the insert location for the new operand. Implicit registers go at
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000188 // the end, everything else goes before the implicit regs.
189 //
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000190 // FIXME: Allow mixed explicit and implicit operands on inline asm.
191 // InstrEmitter::EmitSpecialNode() is marking inline asm clobbers as
192 // implicit-defs, but they must not be moved around. See the FIXME in
193 // InstrEmitter.cpp.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000194 unsigned OpNo = getNumOperands();
195 bool isImpReg = Op.isReg() && Op.isImplicit();
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000196 if (!isImpReg && !isInlineAsm()) {
197 while (OpNo && Operands[OpNo-1].isReg() && Operands[OpNo-1].isImplicit()) {
198 --OpNo;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000199 assert(!Operands[OpNo].isTied() && "Cannot move tied operands");
Chris Lattner961e7422008-01-01 01:12:31 +0000200 }
201 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000202
Pekka Jaaskelaineneb4a6e72013-10-15 14:40:46 +0000203#ifndef NDEBUG
Pekka Jaaskelaineneb08e2e2013-10-15 14:18:10 +0000204 bool isMetaDataOp = Op.getType() == MachineOperand::MO_Metadata;
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000205 // OpNo now points as the desired insertion point. Unless this is a variadic
206 // instruction, only implicit regs are allowed beyond MCID->getNumOperands().
Jakob Stoklund Olesenc300ef02012-07-04 23:53:23 +0000207 // RegMask operands go between the explicit and implicit operands.
208 assert((isImpReg || Op.isRegMask() || MCID->isVariadic() ||
Pekka Jaaskelaineneb08e2e2013-10-15 14:18:10 +0000209 OpNo < MCID->getNumOperands() || isMetaDataOp) &&
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000210 "Trying to add an operand to a machine instr that is already done!");
Pekka Jaaskelaineneb4a6e72013-10-15 14:40:46 +0000211#endif
Chris Lattner961e7422008-01-01 01:12:31 +0000212
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000213 MachineRegisterInfo *MRI = getRegInfo();
Chris Lattner961e7422008-01-01 01:12:31 +0000214
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000215 // Determine if the Operands array needs to be reallocated.
216 // Save the old capacity and operand array.
217 OperandCapacity OldCap = CapOperands;
218 MachineOperand *OldOperands = Operands;
219 if (!OldOperands || OldCap.getSize() == getNumOperands()) {
220 CapOperands = OldOperands ? OldCap.getNext() : OldCap.get(1);
221 Operands = MF.allocateOperandArray(CapOperands);
222 // Move the operands before the insertion point.
223 if (OpNo)
224 moveOperands(Operands, OldOperands, OpNo, MRI);
225 }
Chris Lattner961e7422008-01-01 01:12:31 +0000226
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000227 // Move the operands following the insertion point.
228 if (OpNo != NumOperands)
229 moveOperands(Operands + OpNo + 1, OldOperands + OpNo, NumOperands - OpNo,
230 MRI);
231 ++NumOperands;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000232
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000233 // Deallocate the old operand array.
234 if (OldOperands != Operands && OldOperands)
235 MF.deallocateOperandArray(OldCap, OldOperands);
236
237 // Copy Op into place. It still needs to be inserted into the MRI use lists.
238 MachineOperand *NewMO = new (Operands + OpNo) MachineOperand(Op);
239 NewMO->ParentMI = this;
240
241 // When adding a register operand, tell MRI about it.
242 if (NewMO->isReg()) {
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000243 // Ensure isOnRegUseList() returns false, regardless of Op's status.
Craig Topperc0196b12014-04-14 00:51:57 +0000244 NewMO->Contents.Reg.Prev = nullptr;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000245 // Ignore existing ties. This is not a property that can be copied.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000246 NewMO->TiedTo = 0;
247 // Add the new operand to MRI, but only for instructions in an MBB.
248 if (MRI)
249 MRI->addRegOperandToUseList(NewMO);
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000250 // The MCID operand information isn't accurate until we start adding
251 // explicit operands. The implicit operands are added first, then the
252 // explicits are inserted before them.
253 if (!isImpReg) {
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000254 // Tie uses to defs as indicated in MCInstrDesc.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000255 if (NewMO->isUse()) {
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000256 int DefIdx = MCID->getOperandConstraint(OpNo, MCOI::TIED_TO);
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +0000257 if (DefIdx != -1)
258 tieOperands(DefIdx, OpNo);
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000259 }
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +0000260 // If the register operand is flagged as early, mark the operand as such.
261 if (MCID->getOperandConstraint(OpNo, MCOI::EARLY_CLOBBER) != -1)
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000262 NewMO->setIsEarlyClobber(true);
Chris Lattner961e7422008-01-01 01:12:31 +0000263 }
Chris Lattner961e7422008-01-01 01:12:31 +0000264 }
265}
266
267/// RemoveOperand - Erase an operand from an instruction, leaving it with one
268/// fewer operand than it started with.
269///
270void MachineInstr::RemoveOperand(unsigned OpNo) {
Jakob Stoklund Olesenb0894832012-12-22 17:13:06 +0000271 assert(OpNo < getNumOperands() && "Invalid operand number");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000272 untieRegOperand(OpNo);
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000273
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000274#ifndef NDEBUG
275 // Moving tied operands would break the ties.
Jakob Stoklund Olesenb0894832012-12-22 17:13:06 +0000276 for (unsigned i = OpNo + 1, e = getNumOperands(); i != e; ++i)
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000277 if (Operands[i].isReg())
278 assert(!Operands[i].isTied() && "Cannot move tied operands");
279#endif
280
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000281 MachineRegisterInfo *MRI = getRegInfo();
282 if (MRI && Operands[OpNo].isReg())
283 MRI->removeRegOperandFromUseList(Operands + OpNo);
Chris Lattner961e7422008-01-01 01:12:31 +0000284
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000285 // Don't call the MachineOperand destructor. A lot of this code depends on
286 // MachineOperand having a trivial destructor anyway, and adding a call here
287 // wouldn't make it 'destructor-correct'.
288
289 if (unsigned N = NumOperands - 1 - OpNo)
290 moveOperands(Operands + OpNo, Operands + OpNo + 1, N, MRI);
291 --NumOperands;
Chris Lattner961e7422008-01-01 01:12:31 +0000292}
293
Dan Gohman48b185d2009-09-25 20:36:54 +0000294/// addMemOperand - Add a MachineMemOperand to the machine instruction.
295/// This function should be used only occasionally. The setMemRefs function
296/// is the primary method for setting up a MachineInstr's MemRefs list.
Dan Gohman3b460302008-07-07 23:14:23 +0000297void MachineInstr::addMemOperand(MachineFunction &MF,
Dan Gohman48b185d2009-09-25 20:36:54 +0000298 MachineMemOperand *MO) {
299 mmo_iterator OldMemRefs = MemRefs;
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000300 unsigned OldNumMemRefs = NumMemRefs;
Dan Gohman3b460302008-07-07 23:14:23 +0000301
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000302 unsigned NewNum = NumMemRefs + 1;
Dan Gohman48b185d2009-09-25 20:36:54 +0000303 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum);
Dan Gohman3b460302008-07-07 23:14:23 +0000304
Benjamin Kramerd03878b2012-03-16 16:39:27 +0000305 std::copy(OldMemRefs, OldMemRefs + OldNumMemRefs, NewMemRefs);
Dan Gohman48b185d2009-09-25 20:36:54 +0000306 NewMemRefs[NewNum - 1] = MO;
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +0000307 setMemRefs(NewMemRefs, NewMemRefs + NewNum);
Dan Gohman48b185d2009-09-25 20:36:54 +0000308}
Chris Lattner961e7422008-01-01 01:12:31 +0000309
Philip Reames5eb90a72016-01-06 19:33:12 +0000310/// Check to see if the MMOs pointed to by the two MemRefs arrays are
Junmo Park820e3922016-02-26 02:07:36 +0000311/// identical.
Philip Reames5eb90a72016-01-06 19:33:12 +0000312static bool hasIdenticalMMOs(const MachineInstr &MI1, const MachineInstr &MI2) {
313 auto I1 = MI1.memoperands_begin(), E1 = MI1.memoperands_end();
314 auto I2 = MI2.memoperands_begin(), E2 = MI2.memoperands_end();
315 if ((E1 - I1) != (E2 - I2))
316 return false;
317 for (; I1 != E1; ++I1, ++I2) {
318 if (**I1 != **I2)
319 return false;
320 }
321 return true;
322}
323
Philip Reamesc86ed002016-01-06 04:39:03 +0000324std::pair<MachineInstr::mmo_iterator, unsigned>
325MachineInstr::mergeMemRefsWith(const MachineInstr& Other) {
Philip Reames5eb90a72016-01-06 19:33:12 +0000326
327 // If either of the incoming memrefs are empty, we must be conservative and
328 // treat this as if we've exhausted our space for memrefs and dropped them.
329 if (memoperands_empty() || Other.memoperands_empty())
330 return std::make_pair(nullptr, 0);
331
332 // If both instructions have identical memrefs, we don't need to merge them.
333 // Since many instructions have a single memref, and we tend to merge things
334 // like pairs of loads from the same location, this catches a large number of
335 // cases in practice.
336 if (hasIdenticalMMOs(*this, Other))
337 return std::make_pair(MemRefs, NumMemRefs);
Junmo Park820e3922016-02-26 02:07:36 +0000338
Philip Reamesc86ed002016-01-06 04:39:03 +0000339 // TODO: consider uniquing elements within the operand lists to reduce
340 // space usage and fall back to conservative information less often.
Philip Reames5eb90a72016-01-06 19:33:12 +0000341 size_t CombinedNumMemRefs = NumMemRefs + Other.NumMemRefs;
342
343 // If we don't have enough room to store this many memrefs, be conservative
344 // and drop them. Otherwise, we'd fail asserts when trying to add them to
345 // the new instruction.
346 if (CombinedNumMemRefs != uint8_t(CombinedNumMemRefs))
347 return std::make_pair(nullptr, 0);
Philip Reamesc86ed002016-01-06 04:39:03 +0000348
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000349 MachineFunction *MF = getMF();
Philip Reamesc86ed002016-01-06 04:39:03 +0000350 mmo_iterator MemBegin = MF->allocateMemRefsArray(CombinedNumMemRefs);
351 mmo_iterator MemEnd = std::copy(memoperands_begin(), memoperands_end(),
352 MemBegin);
353 MemEnd = std::copy(Other.memoperands_begin(), Other.memoperands_end(),
354 MemEnd);
Philip Reames2d2fc4a2016-01-06 05:53:09 +0000355 assert(MemEnd - MemBegin == (ptrdiff_t)CombinedNumMemRefs &&
356 "missing memrefs");
Junmo Park820e3922016-02-26 02:07:36 +0000357
Philip Reamesc86ed002016-01-06 04:39:03 +0000358 return std::make_pair(MemBegin, CombinedNumMemRefs);
359}
360
Benjamin Kramer97f889f2012-03-17 17:03:45 +0000361bool MachineInstr::hasPropertyInBundle(unsigned Mask, QueryType Type) const {
Jakob Stoklund Olesenf0615c72013-01-10 18:42:44 +0000362 assert(!isBundledWithPred() && "Must be called on bundle header");
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000363 for (MachineBasicBlock::const_instr_iterator MII = getIterator();; ++MII) {
Benjamin Kramer97f889f2012-03-17 17:03:45 +0000364 if (MII->getDesc().getFlags() & Mask) {
Evan Chengcdf89fd2011-12-08 19:23:10 +0000365 if (Type == AnyInBundle)
Evan Cheng7f8e5632011-12-07 07:15:52 +0000366 return true;
367 } else {
Jakob Stoklund Olesen55a7be22013-01-10 01:29:42 +0000368 if (Type == AllInBundle && !MII->isBundle())
Evan Cheng7f8e5632011-12-07 07:15:52 +0000369 return false;
370 }
Jakob Stoklund Olesen55a7be22013-01-10 01:29:42 +0000371 // This was the last instruction in the bundle.
372 if (!MII->isBundledWithSucc())
373 return Type == AllInBundle;
Evan Cheng2a81dd42011-12-06 22:12:01 +0000374 }
Evan Cheng2a81dd42011-12-06 22:12:01 +0000375}
376
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +0000377bool MachineInstr::isIdenticalTo(const MachineInstr &Other,
Evan Chenge9c46c22010-03-03 01:44:33 +0000378 MICheckType Check) const {
Evan Cheng0f260e12010-03-03 21:54:14 +0000379 // If opcodes or number of operands are not the same then the two
380 // instructions are obviously not identical.
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +0000381 if (Other.getOpcode() != getOpcode() ||
382 Other.getNumOperands() != getNumOperands())
Evan Cheng0f260e12010-03-03 21:54:14 +0000383 return false;
384
Evan Cheng7fae11b2011-12-14 02:11:42 +0000385 if (isBundle()) {
Bjorn Petterssonb29a15e2016-12-19 11:20:57 +0000386 // We have passed the test above that both instructions have the same
387 // opcode, so we know that both instructions are bundles here. Let's compare
388 // MIs inside the bundle.
389 assert(Other.isBundle() && "Expected that both instructions are bundles.");
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000390 MachineBasicBlock::const_instr_iterator I1 = getIterator();
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +0000391 MachineBasicBlock::const_instr_iterator I2 = Other.getIterator();
Bjorn Petterssonb29a15e2016-12-19 11:20:57 +0000392 // Loop until we analysed the last intruction inside at least one of the
393 // bundles.
394 while (I1->isBundledWithSucc() && I2->isBundledWithSucc()) {
395 ++I1;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000396 ++I2;
Bjorn Petterssonb29a15e2016-12-19 11:20:57 +0000397 if (!I1->isIdenticalTo(*I2, Check))
Evan Cheng7fae11b2011-12-14 02:11:42 +0000398 return false;
399 }
Bjorn Petterssonb29a15e2016-12-19 11:20:57 +0000400 // If we've reached the end of just one of the two bundles, but not both,
401 // the instructions are not identical.
402 if (I1->isBundledWithSucc() || I2->isBundledWithSucc())
403 return false;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000404 }
405
Evan Cheng0f260e12010-03-03 21:54:14 +0000406 // Check operands to make sure they match.
407 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
408 const MachineOperand &MO = getOperand(i);
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +0000409 const MachineOperand &OMO = Other.getOperand(i);
Evan Chengcfdf3392011-05-12 00:56:58 +0000410 if (!MO.isReg()) {
411 if (!MO.isIdenticalTo(OMO))
412 return false;
413 continue;
414 }
415
Evan Cheng0f260e12010-03-03 21:54:14 +0000416 // Clients may or may not want to ignore defs when testing for equality.
417 // For example, machine CSE pass only cares about finding common
418 // subexpressions, so it's safe to ignore virtual register defs.
Evan Chengcfdf3392011-05-12 00:56:58 +0000419 if (MO.isDef()) {
Evan Cheng0f260e12010-03-03 21:54:14 +0000420 if (Check == IgnoreDefs)
421 continue;
Evan Chengcfdf3392011-05-12 00:56:58 +0000422 else if (Check == IgnoreVRegDefs) {
Diana Picus4a5f5222017-10-12 13:59:51 +0000423 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg()) ||
424 !TargetRegisterInfo::isVirtualRegister(OMO.getReg()))
425 if (!MO.isIdenticalTo(OMO))
Evan Chengcfdf3392011-05-12 00:56:58 +0000426 return false;
427 } else {
428 if (!MO.isIdenticalTo(OMO))
Evan Cheng0f260e12010-03-03 21:54:14 +0000429 return false;
Evan Chengcfdf3392011-05-12 00:56:58 +0000430 if (Check == CheckKillDead && MO.isDead() != OMO.isDead())
431 return false;
432 }
433 } else {
434 if (!MO.isIdenticalTo(OMO))
435 return false;
436 if (Check == CheckKillDead && MO.isKill() != OMO.isKill())
437 return false;
438 }
Evan Cheng0f260e12010-03-03 21:54:14 +0000439 }
Devang Patelbf8cc602011-07-07 17:45:33 +0000440 // If DebugLoc does not match then two dbg.values are not identical.
441 if (isDebugValue())
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +0000442 if (getDebugLoc() && Other.getDebugLoc() &&
443 getDebugLoc() != Other.getDebugLoc())
Devang Patelbf8cc602011-07-07 17:45:33 +0000444 return false;
Evan Cheng0f260e12010-03-03 21:54:14 +0000445 return true;
Evan Chenge9c46c22010-03-03 01:44:33 +0000446}
447
Justin Bognerec7cba52017-10-10 23:34:01 +0000448const MachineFunction *MachineInstr::getMF() const {
449 return getParent()->getParent();
450}
451
Chris Lattnerbec79b42006-04-17 21:35:41 +0000452MachineInstr *MachineInstr::removeFromParent() {
453 assert(getParent() && "Not embedded in a basic block!");
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +0000454 return getParent()->remove(this);
Chris Lattnerbec79b42006-04-17 21:35:41 +0000455}
456
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +0000457MachineInstr *MachineInstr::removeFromBundle() {
458 assert(getParent() && "Not embedded in a basic block!");
459 return getParent()->remove_instr(this);
460}
Chris Lattnerbec79b42006-04-17 21:35:41 +0000461
Dan Gohman3b460302008-07-07 23:14:23 +0000462void MachineInstr::eraseFromParent() {
463 assert(getParent() && "Not embedded in a basic block!");
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +0000464 getParent()->erase(this);
Dan Gohman3b460302008-07-07 23:14:23 +0000465}
466
Gerolf Hoflehnercaa8bfd2014-08-13 21:15:23 +0000467void MachineInstr::eraseFromParentAndMarkDBGValuesForRemoval() {
468 assert(getParent() && "Not embedded in a basic block!");
469 MachineBasicBlock *MBB = getParent();
470 MachineFunction *MF = MBB->getParent();
471 assert(MF && "Not embedded in a function!");
472
473 MachineInstr *MI = (MachineInstr *)this;
474 MachineRegisterInfo &MRI = MF->getRegInfo();
475
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000476 for (const MachineOperand &MO : MI->operands()) {
Gerolf Hoflehnercaa8bfd2014-08-13 21:15:23 +0000477 if (!MO.isReg() || !MO.isDef())
478 continue;
479 unsigned Reg = MO.getReg();
480 if (!TargetRegisterInfo::isVirtualRegister(Reg))
481 continue;
482 MRI.markUsesInDebugValueAsUndef(Reg);
483 }
484 MI->eraseFromParent();
485}
486
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +0000487void MachineInstr::eraseFromBundle() {
488 assert(getParent() && "Not embedded in a basic block!");
489 getParent()->erase_instr(this);
490}
Dan Gohman3b460302008-07-07 23:14:23 +0000491
Evan Cheng4d728b02007-05-15 01:26:09 +0000492/// getNumExplicitOperands - Returns the number of non-implicit operands.
493///
494unsigned MachineInstr::getNumExplicitOperands() const {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000495 unsigned NumOperands = MCID->getNumOperands();
496 if (!MCID->isVariadic())
Evan Cheng4d728b02007-05-15 01:26:09 +0000497 return NumOperands;
498
Dan Gohman37608532009-04-15 17:59:11 +0000499 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
500 const MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000501 if (!MO.isReg() || !MO.isImplicit())
Evan Cheng4d728b02007-05-15 01:26:09 +0000502 NumOperands++;
503 }
504 return NumOperands;
505}
506
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +0000507void MachineInstr::bundleWithPred() {
508 assert(!isBundledWithPred() && "MI is already bundled with its predecessor");
509 setFlag(BundledPred);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000510 MachineBasicBlock::instr_iterator Pred = getIterator();
511 --Pred;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +0000512 assert(!Pred->isBundledWithSucc() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +0000513 Pred->setFlag(BundledSucc);
514}
515
516void MachineInstr::bundleWithSucc() {
517 assert(!isBundledWithSucc() && "MI is already bundled with its successor");
518 setFlag(BundledSucc);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000519 MachineBasicBlock::instr_iterator Succ = getIterator();
520 ++Succ;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +0000521 assert(!Succ->isBundledWithPred() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +0000522 Succ->setFlag(BundledPred);
523}
524
525void MachineInstr::unbundleFromPred() {
526 assert(isBundledWithPred() && "MI isn't bundled with its predecessor");
527 clearFlag(BundledPred);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000528 MachineBasicBlock::instr_iterator Pred = getIterator();
529 --Pred;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +0000530 assert(Pred->isBundledWithSucc() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +0000531 Pred->clearFlag(BundledSucc);
532}
533
534void MachineInstr::unbundleFromSucc() {
535 assert(isBundledWithSucc() && "MI isn't bundled with its successor");
536 clearFlag(BundledSucc);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000537 MachineBasicBlock::instr_iterator Succ = getIterator();
538 ++Succ;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +0000539 assert(Succ->isBundledWithPred() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +0000540 Succ->clearFlag(BundledPred);
541}
542
Evan Cheng6eb516d2011-01-07 23:50:32 +0000543bool MachineInstr::isStackAligningInlineAsm() const {
544 if (isInlineAsm()) {
545 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
546 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
547 return true;
548 }
549 return false;
550}
Chris Lattner33f5af02006-10-20 22:39:59 +0000551
Chad Rosier994f4042012-09-05 21:00:58 +0000552InlineAsm::AsmDialect MachineInstr::getInlineAsmDialect() const {
553 assert(isInlineAsm() && "getInlineAsmDialect() only works for inline asms!");
554 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
Chad Rosiere53314f2012-09-05 22:40:13 +0000555 return InlineAsm::AsmDialect((ExtraInfo & InlineAsm::Extra_AsmDialect) != 0);
Chad Rosier994f4042012-09-05 21:00:58 +0000556}
557
Jakob Stoklund Olesen1e737162011-10-12 23:37:33 +0000558int MachineInstr::findInlineAsmFlagIdx(unsigned OpIdx,
559 unsigned *GroupNo) const {
560 assert(isInlineAsm() && "Expected an inline asm instruction");
561 assert(OpIdx < getNumOperands() && "OpIdx out of range");
562
563 // Ignore queries about the initial operands.
564 if (OpIdx < InlineAsm::MIOp_FirstOperand)
565 return -1;
566
567 unsigned Group = 0;
568 unsigned NumOps;
569 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
570 i += NumOps) {
571 const MachineOperand &FlagMO = getOperand(i);
572 // If we reach the implicit register operands, stop looking.
573 if (!FlagMO.isImm())
574 return -1;
575 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
576 if (i + NumOps > OpIdx) {
577 if (GroupNo)
578 *GroupNo = Group;
579 return i;
580 }
581 ++Group;
582 }
583 return -1;
584}
585
Reid Kleckner28865802016-04-14 18:29:59 +0000586const DILocalVariable *MachineInstr::getDebugVariable() const {
587 assert(isDebugValue() && "not a DBG_VALUE");
588 return cast<DILocalVariable>(getOperand(2).getMetadata());
589}
590
591const DIExpression *MachineInstr::getDebugExpression() const {
592 assert(isDebugValue() && "not a DBG_VALUE");
593 return cast<DIExpression>(getOperand(3).getMetadata());
594}
595
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +0000596const TargetRegisterClass*
597MachineInstr::getRegClassConstraint(unsigned OpIdx,
598 const TargetInstrInfo *TII,
599 const TargetRegisterInfo *TRI) const {
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000600 assert(getParent() && "Can't have an MBB reference here!");
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000601 assert(getMF() && "Can't have an MF reference here!");
602 const MachineFunction &MF = *getMF();
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000603
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +0000604 // Most opcodes have fixed constraints in their MCInstrDesc.
605 if (!isInlineAsm())
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000606 return TII->getRegClass(getDesc(), OpIdx, TRI, MF);
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +0000607
608 if (!getOperand(OpIdx).isReg())
Craig Topperc0196b12014-04-14 00:51:57 +0000609 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +0000610
611 // For tied uses on inline asm, get the constraint from the def.
612 unsigned DefIdx;
613 if (getOperand(OpIdx).isUse() && isRegTiedToDefOperand(OpIdx, &DefIdx))
614 OpIdx = DefIdx;
615
616 // Inline asm stores register class constraints in the flag word.
617 int FlagIdx = findInlineAsmFlagIdx(OpIdx);
618 if (FlagIdx < 0)
Craig Topperc0196b12014-04-14 00:51:57 +0000619 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +0000620
621 unsigned Flag = getOperand(FlagIdx).getImm();
622 unsigned RCID;
Simon Dardisd32a2d32016-07-18 13:17:31 +0000623 if ((InlineAsm::getKind(Flag) == InlineAsm::Kind_RegUse ||
624 InlineAsm::getKind(Flag) == InlineAsm::Kind_RegDef ||
625 InlineAsm::getKind(Flag) == InlineAsm::Kind_RegDefEarlyClobber) &&
626 InlineAsm::hasRegClassConstraint(Flag, RCID))
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +0000627 return TRI->getRegClass(RCID);
628
629 // Assume that all registers in a memory operand are pointers.
630 if (InlineAsm::getKind(Flag) == InlineAsm::Kind_Mem)
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000631 return TRI->getPointerRegClass(MF);
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +0000632
Craig Topperc0196b12014-04-14 00:51:57 +0000633 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +0000634}
635
Quentin Colombet1fb3362a2014-01-02 22:47:22 +0000636const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVReg(
637 unsigned Reg, const TargetRegisterClass *CurRC, const TargetInstrInfo *TII,
638 const TargetRegisterInfo *TRI, bool ExploreBundle) const {
639 // Check every operands inside the bundle if we have
640 // been asked to.
641 if (ExploreBundle)
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +0000642 for (ConstMIBundleOperands OpndIt(*this); OpndIt.isValid() && CurRC;
Quentin Colombet1fb3362a2014-01-02 22:47:22 +0000643 ++OpndIt)
644 CurRC = OpndIt->getParent()->getRegClassConstraintEffectForVRegImpl(
645 OpndIt.getOperandNo(), Reg, CurRC, TII, TRI);
646 else
647 // Otherwise, just check the current operands.
Matthias Braune41e1462015-05-29 02:56:46 +0000648 for (unsigned i = 0, e = NumOperands; i < e && CurRC; ++i)
649 CurRC = getRegClassConstraintEffectForVRegImpl(i, Reg, CurRC, TII, TRI);
Quentin Colombet1fb3362a2014-01-02 22:47:22 +0000650 return CurRC;
651}
652
653const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVRegImpl(
654 unsigned OpIdx, unsigned Reg, const TargetRegisterClass *CurRC,
655 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
656 assert(CurRC && "Invalid initial register class");
657 // Check if Reg is constrained by some of its use/def from MI.
658 const MachineOperand &MO = getOperand(OpIdx);
659 if (!MO.isReg() || MO.getReg() != Reg)
660 return CurRC;
661 // If yes, accumulate the constraints through the operand.
662 return getRegClassConstraintEffect(OpIdx, CurRC, TII, TRI);
663}
664
665const TargetRegisterClass *MachineInstr::getRegClassConstraintEffect(
666 unsigned OpIdx, const TargetRegisterClass *CurRC,
667 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
668 const TargetRegisterClass *OpRC = getRegClassConstraint(OpIdx, TII, TRI);
669 const MachineOperand &MO = getOperand(OpIdx);
670 assert(MO.isReg() &&
671 "Cannot get register constraints for non-register operand");
672 assert(CurRC && "Invalid initial register class");
673 if (unsigned SubIdx = MO.getSubReg()) {
674 if (OpRC)
675 CurRC = TRI->getMatchingSuperRegClass(CurRC, OpRC, SubIdx);
676 else
677 CurRC = TRI->getSubClassWithSubReg(CurRC, SubIdx);
678 } else if (OpRC)
679 CurRC = TRI->getCommonSubClass(CurRC, OpRC);
680 return CurRC;
681}
682
Jakob Stoklund Olesen68d752b2013-01-09 18:28:16 +0000683/// Return the number of instructions inside the MI bundle, not counting the
684/// header instruction.
Evan Cheng7fae11b2011-12-14 02:11:42 +0000685unsigned MachineInstr::getBundleSize() const {
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +0000686 MachineBasicBlock::const_instr_iterator I = getIterator();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000687 unsigned Size = 0;
Richard Trieu7a083812016-02-18 22:09:30 +0000688 while (I->isBundledWithSucc()) {
689 ++Size;
690 ++I;
691 }
Evan Cheng7fae11b2011-12-14 02:11:42 +0000692 return Size;
693}
694
Nicolai Haehnleb0c97482016-04-22 04:04:08 +0000695/// Returns true if the MachineInstr has an implicit-use operand of exactly
696/// the given register (not considering sub/super-registers).
697bool MachineInstr::hasRegisterImplicitUseOperand(unsigned Reg) const {
698 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
699 const MachineOperand &MO = getOperand(i);
700 if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == Reg)
701 return true;
702 }
703 return false;
704}
705
Evan Cheng910c8082007-04-26 19:00:32 +0000706/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Jim Grosbach9632c142009-09-17 17:57:26 +0000707/// the specific register or -1 if it is not found. It further tightens
Evan Cheng9965aeb2007-02-23 01:04:26 +0000708/// the search criteria to a use that kills the register if isKill is true.
Fraser Cormack48d9fdc2016-10-11 09:09:21 +0000709int MachineInstr::findRegisterUseOperandIdx(
710 unsigned Reg, bool isKill, const TargetRegisterInfo *TRI) const {
Evan Cheng75c21942006-12-06 08:27:42 +0000711 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng5983bdb2007-05-29 18:35:22 +0000712 const MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000713 if (!MO.isReg() || !MO.isUse())
Evan Cheng63254462008-03-05 00:59:57 +0000714 continue;
715 unsigned MOReg = MO.getReg();
716 if (!MOReg)
717 continue;
Fraser Cormack48d9fdc2016-10-11 09:09:21 +0000718 if (MOReg == Reg || (TRI && TargetRegisterInfo::isPhysicalRegister(MOReg) &&
719 TargetRegisterInfo::isPhysicalRegister(Reg) &&
720 TRI->isSubRegister(MOReg, Reg)))
Evan Cheng9965aeb2007-02-23 01:04:26 +0000721 if (!isKill || MO.isKill())
Evan Chengec3ac312007-03-26 22:37:45 +0000722 return i;
Evan Cheng75c21942006-12-06 08:27:42 +0000723 }
Evan Chengec3ac312007-03-26 22:37:45 +0000724 return -1;
Evan Cheng75c21942006-12-06 08:27:42 +0000725}
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +0000726
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +0000727/// readsWritesVirtualRegister - Return a pair of bools (reads, writes)
728/// indicating if this instruction reads or writes Reg. This also considers
729/// partial defines.
730std::pair<bool,bool>
731MachineInstr::readsWritesVirtualRegister(unsigned Reg,
732 SmallVectorImpl<unsigned> *Ops) const {
733 bool PartDef = false; // Partial redefine.
734 bool FullDef = false; // Full define.
735 bool Use = false;
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +0000736
737 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
738 const MachineOperand &MO = getOperand(i);
739 if (!MO.isReg() || MO.getReg() != Reg)
740 continue;
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +0000741 if (Ops)
742 Ops->push_back(i);
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +0000743 if (MO.isUse())
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +0000744 Use |= !MO.isUndef();
Jakob Stoklund Olesen9eb77bf2011-08-19 00:30:17 +0000745 else if (MO.getSubReg() && !MO.isUndef())
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000746 // A partial def undef doesn't count as reading the register.
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +0000747 PartDef = true;
748 else
749 FullDef = true;
750 }
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +0000751 // A partial redefine uses Reg unless there is also a full define.
752 return std::make_pair(Use || (PartDef && !FullDef), PartDef || FullDef);
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +0000753}
754
Evan Cheng63254462008-03-05 00:59:57 +0000755/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman72a0bc12008-05-06 00:20:10 +0000756/// the specified register or -1 if it is not found. If isDead is true, defs
757/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
758/// also checks if there is a def of a super-register.
Evan Cheng38584512010-05-21 20:53:24 +0000759int
760MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, bool Overlap,
761 const TargetRegisterInfo *TRI) const {
762 bool isPhys = TargetRegisterInfo::isPhysicalRegister(Reg);
Evan Chengf7ed82d2007-02-19 21:49:54 +0000763 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng63254462008-03-05 00:59:57 +0000764 const MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesene7d3f442012-02-14 23:49:37 +0000765 // Accept regmask operands when Overlap is set.
766 // Ignore them when looking for a specific def operand (Overlap == false).
767 if (isPhys && Overlap && MO.isRegMask() && MO.clobbersPhysReg(Reg))
768 return i;
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000769 if (!MO.isReg() || !MO.isDef())
Evan Cheng63254462008-03-05 00:59:57 +0000770 continue;
771 unsigned MOReg = MO.getReg();
Evan Cheng38584512010-05-21 20:53:24 +0000772 bool Found = (MOReg == Reg);
773 if (!Found && TRI && isPhys &&
774 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
775 if (Overlap)
776 Found = TRI->regsOverlap(MOReg, Reg);
777 else
778 Found = TRI->isSubRegister(MOReg, Reg);
779 }
780 if (Found && (!isDead || MO.isDead()))
781 return i;
Evan Chengf7ed82d2007-02-19 21:49:54 +0000782 }
Evan Cheng63254462008-03-05 00:59:57 +0000783 return -1;
Evan Chengf7ed82d2007-02-19 21:49:54 +0000784}
Evan Cheng4d728b02007-05-15 01:26:09 +0000785
Evan Cheng5983bdb2007-05-29 18:35:22 +0000786/// findFirstPredOperandIdx() - Find the index of the first operand in the
787/// operand list that is used to represent the predicate. It returns -1 if
788/// none is found.
789int MachineInstr::findFirstPredOperandIdx() const {
Jim Grosbached16ec42011-08-29 22:24:09 +0000790 // Don't call MCID.findFirstPredOperandIdx() because this variant
791 // is sometimes called on an instruction that's not yet complete, and
792 // so the number of operands is less than the MCID indicates. In
793 // particular, the PTX target does this.
Evan Cheng6cc775f2011-06-28 19:10:37 +0000794 const MCInstrDesc &MCID = getDesc();
795 if (MCID.isPredicable()) {
Evan Cheng4d728b02007-05-15 01:26:09 +0000796 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Evan Cheng6cc775f2011-06-28 19:10:37 +0000797 if (MCID.OpInfo[i].isPredicate())
Evan Cheng5983bdb2007-05-29 18:35:22 +0000798 return i;
Evan Cheng4d728b02007-05-15 01:26:09 +0000799 }
800
Evan Cheng5983bdb2007-05-29 18:35:22 +0000801 return -1;
Evan Cheng4d728b02007-05-15 01:26:09 +0000802}
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000803
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000804// MachineOperand::TiedTo is 4 bits wide.
805const unsigned TiedMax = 15;
806
807/// tieOperands - Mark operands at DefIdx and UseIdx as tied to each other.
808///
809/// Use and def operands can be tied together, indicated by a non-zero TiedTo
810/// field. TiedTo can have these values:
811///
812/// 0: Operand is not tied to anything.
813/// 1 to TiedMax-1: Tied to getOperand(TiedTo-1).
814/// TiedMax: Tied to an operand >= TiedMax-1.
815///
816/// The tied def must be one of the first TiedMax operands on a normal
817/// instruction. INLINEASM instructions allow more tied defs.
818///
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +0000819void MachineInstr::tieOperands(unsigned DefIdx, unsigned UseIdx) {
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +0000820 MachineOperand &DefMO = getOperand(DefIdx);
821 MachineOperand &UseMO = getOperand(UseIdx);
822 assert(DefMO.isDef() && "DefIdx must be a def operand");
823 assert(UseMO.isUse() && "UseIdx must be a use operand");
824 assert(!DefMO.isTied() && "Def is already tied to another use");
825 assert(!UseMO.isTied() && "Use is already tied to another def");
826
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000827 if (DefIdx < TiedMax)
828 UseMO.TiedTo = DefIdx + 1;
829 else {
830 // Inline asm can use the group descriptors to find tied operands, but on
831 // normal instruction, the tied def must be within the first TiedMax
832 // operands.
833 assert(isInlineAsm() && "DefIdx out of range");
834 UseMO.TiedTo = TiedMax;
835 }
836
837 // UseIdx can be out of range, we'll search for it in findTiedOperandIdx().
838 DefMO.TiedTo = std::min(UseIdx + 1, TiedMax);
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +0000839}
840
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000841/// Given the index of a tied register operand, find the operand it is tied to.
842/// Defs are tied to uses and vice versa. Returns the index of the tied operand
843/// which must exist.
844unsigned MachineInstr::findTiedOperandIdx(unsigned OpIdx) const {
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000845 const MachineOperand &MO = getOperand(OpIdx);
846 assert(MO.isTied() && "Operand isn't tied");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000847
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000848 // Normally TiedTo is in range.
849 if (MO.TiedTo < TiedMax)
850 return MO.TiedTo - 1;
851
852 // Uses on normal instructions can be out of range.
853 if (!isInlineAsm()) {
854 // Normal tied defs must be in the 0..TiedMax-1 range.
855 if (MO.isUse())
856 return TiedMax - 1;
857 // MO is a def. Search for the tied use.
858 for (unsigned i = TiedMax - 1, e = getNumOperands(); i != e; ++i) {
859 const MachineOperand &UseMO = getOperand(i);
860 if (UseMO.isReg() && UseMO.isUse() && UseMO.TiedTo == OpIdx + 1)
861 return i;
862 }
863 llvm_unreachable("Can't find tied use");
864 }
865
866 // Now deal with inline asm by parsing the operand group descriptor flags.
867 // Find the beginning of each operand group.
868 SmallVector<unsigned, 8> GroupIdx;
869 unsigned OpIdxGroup = ~0u;
870 unsigned NumOps;
871 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
872 i += NumOps) {
873 const MachineOperand &FlagMO = getOperand(i);
874 assert(FlagMO.isImm() && "Invalid tied operand on inline asm");
875 unsigned CurGroup = GroupIdx.size();
876 GroupIdx.push_back(i);
877 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
878 // OpIdx belongs to this operand group.
879 if (OpIdx > i && OpIdx < i + NumOps)
880 OpIdxGroup = CurGroup;
881 unsigned TiedGroup;
882 if (!InlineAsm::isUseOperandTiedToDef(FlagMO.getImm(), TiedGroup))
883 continue;
884 // Operands in this group are tied to operands in TiedGroup which must be
885 // earlier. Find the number of operands between the two groups.
886 unsigned Delta = i - GroupIdx[TiedGroup];
887
888 // OpIdx is a use tied to TiedGroup.
889 if (OpIdxGroup == CurGroup)
890 return OpIdx - Delta;
891
892 // OpIdx is a def tied to this use group.
893 if (OpIdxGroup == TiedGroup)
894 return OpIdx + Delta;
895 }
896 llvm_unreachable("Invalid tied operand on inline asm");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000897}
898
Dan Gohmanc90f51c2010-05-13 20:34:42 +0000899/// clearKillInfo - Clears kill flags on all operands.
900///
901void MachineInstr::clearKillInfo() {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000902 for (MachineOperand &MO : operands()) {
Dan Gohmanc90f51c2010-05-13 20:34:42 +0000903 if (MO.isReg() && MO.isUse())
904 MO.setIsKill(false);
905 }
906}
907
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +0000908void MachineInstr::substituteRegister(unsigned FromReg,
909 unsigned ToReg,
910 unsigned SubIdx,
911 const TargetRegisterInfo &RegInfo) {
912 if (TargetRegisterInfo::isPhysicalRegister(ToReg)) {
913 if (SubIdx)
914 ToReg = RegInfo.getSubReg(ToReg, SubIdx);
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000915 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +0000916 if (!MO.isReg() || MO.getReg() != FromReg)
917 continue;
918 MO.substPhysReg(ToReg, RegInfo);
919 }
920 } else {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000921 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +0000922 if (!MO.isReg() || MO.getReg() != FromReg)
923 continue;
924 MO.substVirtReg(ToReg, SubIdx, RegInfo);
925 }
926 }
927}
928
Evan Cheng7d98a482008-07-03 09:09:37 +0000929/// isSafeToMove - Return true if it is safe to move this instruction. If
930/// SawStore is set to true, it means that there is a store (or call) between
931/// the instruction's location and its intended destination.
Matthias Braun07066cc2015-05-19 21:22:20 +0000932bool MachineInstr::isSafeToMove(AliasAnalysis *AA, bool &SawStore) const {
Evan Cheng399e1102008-03-13 00:44:09 +0000933 // Ignore stuff that we obviously can't move.
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +0000934 //
935 // Treat volatile loads as stores. This is not strictly necessary for
Jakob Stoklund Olesend92e2bc2012-09-04 18:44:43 +0000936 // volatiles, but it is required for atomic loads. It is not allowed to move
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +0000937 // a load across an atomic load with Ordering > Monotonic.
Alex Bradburyfa18b9e2017-11-08 20:19:16 +0000938 if (mayStore() || isCall() || isPHI() ||
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +0000939 (mayLoad() && hasOrderedMemoryRef())) {
Evan Cheng399e1102008-03-13 00:44:09 +0000940 SawStore = true;
941 return false;
942 }
Evan Cheng0638c202011-01-07 21:08:26 +0000943
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000944 if (isPosition() || isDebugValue() || isTerminator() ||
945 hasUnmodeledSideEffects())
Evan Cheng399e1102008-03-13 00:44:09 +0000946 return false;
947
948 // See if this instruction does a load. If so, we have to guarantee that the
949 // loaded value doesn't change between the load and the its intended
950 // destination. The check for isInvariantLoad gives the targe the chance to
951 // classify the load as always returning a constant, e.g. a constant pool
952 // load.
Justin Lebard98cf002016-09-10 01:03:20 +0000953 if (mayLoad() && !isDereferenceableInvariantLoad(AA))
Evan Cheng399e1102008-03-13 00:44:09 +0000954 // Otherwise, this is a real load. If there is a store between the load and
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +0000955 // end of block, we can't move it.
956 return !SawStore;
Dan Gohman7c59ed62008-09-24 00:06:15 +0000957
Evan Cheng399e1102008-03-13 00:44:09 +0000958 return true;
959}
960
Eli Friedman93f47e52017-03-09 23:33:36 +0000961bool MachineInstr::mayAlias(AliasAnalysis *AA, MachineInstr &Other,
962 bool UseTBAA) {
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000963 const MachineFunction *MF = getMF();
Eli Friedman93f47e52017-03-09 23:33:36 +0000964 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
Balaram Makam42adadf2017-08-30 14:57:12 +0000965 const MachineFrameInfo &MFI = MF->getFrameInfo();
Eli Friedman93f47e52017-03-09 23:33:36 +0000966
967 // If neither instruction stores to memory, they can't alias in any
968 // meaningful way, even if they read from the same address.
969 if (!mayStore() && !Other.mayStore())
970 return false;
971
972 // Let the target decide if memory accesses cannot possibly overlap.
973 if (TII->areMemAccessesTriviallyDisjoint(*this, Other, AA))
974 return false;
975
Eli Friedman93f47e52017-03-09 23:33:36 +0000976 // FIXME: Need to handle multiple memory operands to support all targets.
977 if (!hasOneMemOperand() || !Other.hasOneMemOperand())
978 return true;
979
980 MachineMemOperand *MMOa = *memoperands_begin();
981 MachineMemOperand *MMOb = *Other.memoperands_begin();
982
Eli Friedman93f47e52017-03-09 23:33:36 +0000983 // The following interface to AA is fashioned after DAGCombiner::isAlias
984 // and operates with MachineMemOperand offset with some important
985 // assumptions:
986 // - LLVM fundamentally assumes flat address spaces.
987 // - MachineOperand offset can *only* result from legalization and
988 // cannot affect queries other than the trivial case of overlap
989 // checking.
990 // - These offsets never wrap and never step outside
991 // of allocated objects.
992 // - There should never be any negative offsets here.
993 //
994 // FIXME: Modify API to hide this math from "user"
Balaram Makam42adadf2017-08-30 14:57:12 +0000995 // Even before we go to AA we can reason locally about some
Eli Friedman93f47e52017-03-09 23:33:36 +0000996 // memory objects. It can save compile time, and possibly catch some
997 // corner cases not currently covered.
998
Balaram Makam42adadf2017-08-30 14:57:12 +0000999 int64_t OffsetA = MMOa->getOffset();
1000 int64_t OffsetB = MMOb->getOffset();
Eli Friedman93f47e52017-03-09 23:33:36 +00001001
Balaram Makam42adadf2017-08-30 14:57:12 +00001002 int64_t MinOffset = std::min(OffsetA, OffsetB);
1003 int64_t WidthA = MMOa->getSize();
1004 int64_t WidthB = MMOb->getSize();
1005 const Value *ValA = MMOa->getValue();
1006 const Value *ValB = MMOb->getValue();
1007 bool SameVal = (ValA && ValB && (ValA == ValB));
1008 if (!SameVal) {
1009 const PseudoSourceValue *PSVa = MMOa->getPseudoValue();
1010 const PseudoSourceValue *PSVb = MMOb->getPseudoValue();
1011 if (PSVa && ValB && !PSVa->mayAlias(&MFI))
1012 return false;
1013 if (PSVb && ValA && !PSVb->mayAlias(&MFI))
1014 return false;
1015 if (PSVa && PSVb && (PSVa == PSVb))
1016 SameVal = true;
1017 }
Eli Friedman93f47e52017-03-09 23:33:36 +00001018
Balaram Makam42adadf2017-08-30 14:57:12 +00001019 if (SameVal) {
1020 int64_t MaxOffset = std::max(OffsetA, OffsetB);
1021 int64_t LowWidth = (MinOffset == OffsetA) ? WidthA : WidthB;
1022 return (MinOffset + LowWidth > MaxOffset);
1023 }
1024
1025 if (!AA)
1026 return true;
1027
1028 if (!ValA || !ValB)
1029 return true;
1030
1031 assert((OffsetA >= 0) && "Negative MachineMemOperand offset");
1032 assert((OffsetB >= 0) && "Negative MachineMemOperand offset");
1033
1034 int64_t Overlapa = WidthA + OffsetA - MinOffset;
1035 int64_t Overlapb = WidthB + OffsetB - MinOffset;
1036
1037 AliasResult AAResult = AA->alias(
1038 MemoryLocation(ValA, Overlapa,
1039 UseTBAA ? MMOa->getAAInfo() : AAMDNodes()),
1040 MemoryLocation(ValB, Overlapb,
1041 UseTBAA ? MMOb->getAAInfo() : AAMDNodes()));
Eli Friedman93f47e52017-03-09 23:33:36 +00001042
1043 return (AAResult != NoAlias);
1044}
1045
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001046/// hasOrderedMemoryRef - Return true if this instruction may have an ordered
1047/// or volatile memory reference, or if the information describing the memory
1048/// reference is not available. Return false if it is known to have no ordered
1049/// memory references.
1050bool MachineInstr::hasOrderedMemoryRef() const {
Dan Gohman7c59ed62008-09-24 00:06:15 +00001051 // An instruction known never to access memory won't have a volatile access.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001052 if (!mayStore() &&
1053 !mayLoad() &&
1054 !isCall() &&
Evan Cheng6eb516d2011-01-07 23:50:32 +00001055 !hasUnmodeledSideEffects())
Dan Gohman7c59ed62008-09-24 00:06:15 +00001056 return false;
1057
1058 // Otherwise, if the instruction has no memory reference information,
1059 // conservatively assume it wasn't preserved.
1060 if (memoperands_empty())
1061 return true;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001062
Justin Lebardede81e2016-07-13 22:35:19 +00001063 // Check if any of our memory operands are ordered.
Eugene Zelenko4e9736b2017-05-31 01:10:10 +00001064 return llvm::any_of(memoperands(), [](const MachineMemOperand *MMO) {
Justin Lebardede81e2016-07-13 22:35:19 +00001065 return !MMO->isUnordered();
1066 });
Dan Gohman7c59ed62008-09-24 00:06:15 +00001067}
1068
Justin Lebard98cf002016-09-10 01:03:20 +00001069/// isDereferenceableInvariantLoad - Return true if this instruction will never
1070/// trap and is loading from a location whose value is invariant across a run of
1071/// this function.
1072bool MachineInstr::isDereferenceableInvariantLoad(AliasAnalysis *AA) const {
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001073 // If the instruction doesn't load at all, it isn't an invariant load.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001074 if (!mayLoad())
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001075 return false;
1076
1077 // If the instruction has lost its memoperands, conservatively assume that
1078 // it may not be an invariant load.
1079 if (memoperands_empty())
1080 return false;
1081
Matthias Braun941a7052016-07-28 18:40:00 +00001082 const MachineFrameInfo &MFI = getParent()->getParent()->getFrameInfo();
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001083
Justin Lebardede81e2016-07-13 22:35:19 +00001084 for (MachineMemOperand *MMO : memoperands()) {
1085 if (MMO->isVolatile()) return false;
1086 if (MMO->isStore()) return false;
Justin Lebaradbf09e2016-09-11 01:38:58 +00001087 if (MMO->isInvariant() && MMO->isDereferenceable())
1088 continue;
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001089
1090 // A load from a constant PseudoSourceValue is invariant.
Justin Lebardede81e2016-07-13 22:35:19 +00001091 if (const PseudoSourceValue *PSV = MMO->getPseudoValue())
Matthias Braun941a7052016-07-28 18:40:00 +00001092 if (PSV->isConstant(&MFI))
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001093 continue;
1094
Justin Lebardede81e2016-07-13 22:35:19 +00001095 if (const Value *V = MMO->getValue()) {
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001096 // If we have an AliasAnalysis, ask it whether the memory is constant.
Chandler Carruthac80dc72015-06-17 07:18:54 +00001097 if (AA &&
1098 AA->pointsToConstantMemory(
Justin Lebardede81e2016-07-13 22:35:19 +00001099 MemoryLocation(V, MMO->getSize(), MMO->getAAInfo())))
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001100 continue;
1101 }
1102
1103 // Otherwise assume conservatively.
1104 return false;
1105 }
1106
1107 // Everything checks out.
1108 return true;
1109}
1110
Evan Cheng71453822009-12-03 02:31:43 +00001111/// isConstantValuePHI - If the specified instruction is a PHI that always
1112/// merges together the same virtual register, return the register, otherwise
1113/// return 0.
1114unsigned MachineInstr::isConstantValuePHI() const {
Chris Lattnerb06015a2010-02-09 19:54:29 +00001115 if (!isPHI())
Evan Cheng71453822009-12-03 02:31:43 +00001116 return 0;
Evan Cheng5c668a22009-12-07 23:10:34 +00001117 assert(getNumOperands() >= 3 &&
1118 "It's illegal to have a PHI without source operands");
Evan Cheng71453822009-12-03 02:31:43 +00001119
1120 unsigned Reg = getOperand(1).getReg();
1121 for (unsigned i = 3, e = getNumOperands(); i < e; i += 2)
1122 if (getOperand(i).getReg() != Reg)
1123 return 0;
1124 return Reg;
1125}
1126
Evan Cheng6eb516d2011-01-07 23:50:32 +00001127bool MachineInstr::hasUnmodeledSideEffects() const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00001128 if (hasProperty(MCID::UnmodeledSideEffects))
Evan Cheng6eb516d2011-01-07 23:50:32 +00001129 return true;
1130 if (isInlineAsm()) {
1131 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1132 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1133 return true;
1134 }
1135
1136 return false;
1137}
1138
Michael Kupersteinbc7f99a2015-08-12 10:14:58 +00001139bool MachineInstr::isLoadFoldBarrier() const {
1140 return mayStore() || isCall() || hasUnmodeledSideEffects();
1141}
1142
Evan Chengb083c472010-04-08 20:02:37 +00001143/// allDefsAreDead - Return true if all the defs of this instruction are dead.
1144///
1145bool MachineInstr::allDefsAreDead() const {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001146 for (const MachineOperand &MO : operands()) {
Evan Chengb083c472010-04-08 20:02:37 +00001147 if (!MO.isReg() || MO.isUse())
1148 continue;
1149 if (!MO.isDead())
1150 return false;
1151 }
1152 return true;
1153}
1154
Evan Cheng21eedfb2010-10-22 21:49:09 +00001155/// copyImplicitOps - Copy implicit register operands from specified
1156/// instruction to this instruction.
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +00001157void MachineInstr::copyImplicitOps(MachineFunction &MF,
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001158 const MachineInstr &MI) {
1159 for (unsigned i = MI.getDesc().getNumOperands(), e = MI.getNumOperands();
Evan Cheng21eedfb2010-10-22 21:49:09 +00001160 i != e; ++i) {
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001161 const MachineOperand &MO = MI.getOperand(i);
Lang Hames7c8189c2014-03-17 01:22:54 +00001162 if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask())
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +00001163 addOperand(MF, MO);
Evan Cheng21eedfb2010-10-22 21:49:09 +00001164 }
1165}
1166
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001167bool MachineInstr::hasComplexRegisterTies() const {
1168 const MCInstrDesc &MCID = getDesc();
1169 for (unsigned I = 0, E = getNumOperands(); I < E; ++I) {
1170 const auto &Operand = getOperand(I);
1171 if (!Operand.isReg() || Operand.isDef())
1172 // Ignore the defined registers as MCID marks only the uses as tied.
1173 continue;
1174 int ExpectedTiedIdx = MCID.getOperandConstraint(I, MCOI::TIED_TO);
1175 int TiedIdx = Operand.isTied() ? int(findTiedOperandIdx(I)) : -1;
1176 if (ExpectedTiedIdx != TiedIdx)
1177 return true;
1178 }
1179 return false;
1180}
1181
1182LLT MachineInstr::getTypeToPrint(unsigned OpIdx, SmallBitVector &PrintedTypes,
1183 const MachineRegisterInfo &MRI) const {
1184 const MachineOperand &Op = getOperand(OpIdx);
1185 if (!Op.isReg())
1186 return LLT{};
1187
1188 if (isVariadic() || OpIdx >= getNumExplicitOperands())
1189 return MRI.getType(Op.getReg());
1190
1191 auto &OpInfo = getDesc().OpInfo[OpIdx];
1192 if (!OpInfo.isGenericType())
1193 return MRI.getType(Op.getReg());
1194
1195 if (PrintedTypes[OpInfo.getGenericTypeIndex()])
1196 return LLT{};
1197
1198 PrintedTypes.set(OpInfo.getGenericTypeIndex());
1199 return MRI.getType(Op.getReg());
1200}
1201
Aaron Ballman615eb472017-10-15 14:32:27 +00001202#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Brauna4976c62017-01-29 18:20:42 +00001203LLVM_DUMP_METHOD void MachineInstr::dump() const {
Sebastian Pop77794842016-12-21 01:41:12 +00001204 dbgs() << " ";
Matthias Brauna4976c62017-01-29 18:20:42 +00001205 print(dbgs());
Mon P Wangdfcc1ff2008-10-10 01:43:55 +00001206}
Matthias Braun8c209aa2017-01-28 02:02:38 +00001207#endif
Mon P Wangdfcc1ff2008-10-10 01:43:55 +00001208
Ahmed Bougacha43192242017-02-23 19:17:31 +00001209void MachineInstr::print(raw_ostream &OS, bool SkipOpers, bool SkipDebugLoc,
Sebastian Pop77794842016-12-21 01:41:12 +00001210 const TargetInstrInfo *TII) const {
Duncan P. N. Exon Smithc0374522015-06-26 23:18:44 +00001211 const Module *M = nullptr;
1212 if (const MachineBasicBlock *MBB = getParent())
1213 if (const MachineFunction *MF = MBB->getParent())
1214 M = MF->getFunction()->getParent();
1215
1216 ModuleSlotTracker MST(M);
Ahmed Bougacha43192242017-02-23 19:17:31 +00001217 print(OS, MST, SkipOpers, SkipDebugLoc, TII);
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001218}
1219
1220void MachineInstr::print(raw_ostream &OS, ModuleSlotTracker &MST,
Ahmed Bougacha43192242017-02-23 19:17:31 +00001221 bool SkipOpers, bool SkipDebugLoc,
1222 const TargetInstrInfo *TII) const {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001223 // We can be a bit tidier if we know the MachineFunction.
Craig Topperc0196b12014-04-14 00:51:57 +00001224 const MachineFunction *MF = nullptr;
Eric Christopher1cdefae2015-02-27 00:11:34 +00001225 const TargetRegisterInfo *TRI = nullptr;
Craig Topperc0196b12014-04-14 00:51:57 +00001226 const MachineRegisterInfo *MRI = nullptr;
Tim Northover6b3bd612016-07-29 20:32:59 +00001227 const TargetIntrinsicInfo *IntrinsicInfo = nullptr;
1228
Dan Gohman2745d192009-11-09 19:38:45 +00001229 if (const MachineBasicBlock *MBB = getParent()) {
1230 MF = MBB->getParent();
Eric Christopher1cdefae2015-02-27 00:11:34 +00001231 if (MF) {
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001232 MRI = &MF->getRegInfo();
Eric Christopher1cdefae2015-02-27 00:11:34 +00001233 TRI = MF->getSubtarget().getRegisterInfo();
Sebastian Pop77794842016-12-21 01:41:12 +00001234 if (!TII)
1235 TII = MF->getSubtarget().getInstrInfo();
Tim Northover6b3bd612016-07-29 20:32:59 +00001236 IntrinsicInfo = MF->getTarget().getIntrinsicInfo();
Eric Christopher1cdefae2015-02-27 00:11:34 +00001237 }
Dan Gohman2745d192009-11-09 19:38:45 +00001238 }
Dan Gohman34341e62009-10-31 20:19:03 +00001239
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001240 // Save a list of virtual registers.
1241 SmallVector<unsigned, 8> VirtRegs;
1242
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001243 SmallBitVector PrintedTypes(8);
1244 bool ShouldPrintRegisterTies = hasComplexRegisterTies();
1245 auto getTiedOperandIdx = [&](unsigned OpIdx) {
1246 if (!ShouldPrintRegisterTies)
1247 return 0U;
1248 const MachineOperand &MO = getOperand(OpIdx);
1249 if (MO.isReg() && MO.isTied() && !MO.isDef())
1250 return findTiedOperandIdx(OpIdx);
1251 return 0U;
1252 };
Dan Gohman34341e62009-10-31 20:19:03 +00001253 // Print explicitly defined operands on the left of an assignment syntax.
Dan Gohman2745d192009-11-09 19:38:45 +00001254 unsigned StartOp = 0, e = getNumOperands();
Dan Gohman34341e62009-10-31 20:19:03 +00001255 for (; StartOp < e && getOperand(StartOp).isReg() &&
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001256 getOperand(StartOp).isDef() && !getOperand(StartOp).isImplicit();
Dan Gohman34341e62009-10-31 20:19:03 +00001257 ++StartOp) {
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001258 if (StartOp != 0)
1259 OS << ", ";
1260 LLT TypeToPrint = MRI ? getTypeToPrint(StartOp, PrintedTypes, *MRI) : LLT{};
1261 unsigned TiedOperandIdx = getTiedOperandIdx(StartOp);
1262 getOperand(StartOp).print(OS, MST, TypeToPrint, /*PrintDef=*/false,
1263 ShouldPrintRegisterTies, TiedOperandIdx, TRI,
1264 IntrinsicInfo);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001265 unsigned Reg = getOperand(StartOp).getReg();
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001266 if (TargetRegisterInfo::isVirtualRegister(Reg))
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001267 VirtRegs.push_back(Reg);
Chris Lattnerac6e9742002-10-30 01:55:38 +00001268 }
Tanya Lattner23dbc812004-06-25 00:13:11 +00001269
Dan Gohman34341e62009-10-31 20:19:03 +00001270 if (StartOp != 0)
1271 OS << " = ";
1272
1273 // Print the opcode name.
Eric Christopher1cdefae2015-02-27 00:11:34 +00001274 if (TII)
1275 OS << TII->getName(getOpcode());
Benjamin Kramerbf152d52012-02-10 13:18:44 +00001276 else
1277 OS << "UNKNOWN";
Misha Brukman835702a2005-04-21 22:36:52 +00001278
Andrew Trickb36388a2013-01-25 07:45:25 +00001279 if (SkipOpers)
1280 return;
1281
Dan Gohman34341e62009-10-31 20:19:03 +00001282 // Print the rest of the operands.
Dan Gohman2745d192009-11-09 19:38:45 +00001283 bool FirstOp = true;
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001284 unsigned AsmDescOp = ~0u;
1285 unsigned AsmOpCount = 0;
Evan Cheng6eb516d2011-01-07 23:50:32 +00001286
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +00001287 if (isInlineAsm() && e >= InlineAsm::MIOp_FirstOperand) {
Evan Cheng6eb516d2011-01-07 23:50:32 +00001288 // Print asm string.
1289 OS << " ";
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001290 const unsigned OpIdx = InlineAsm::MIOp_AsmString;
1291 LLT TypeToPrint = MRI ? getTypeToPrint(OpIdx, PrintedTypes, *MRI) : LLT{};
Francis Visoiu Mistrihe6fc3ce2017-12-07 17:12:30 +00001292 unsigned TiedOperandIdx = getTiedOperandIdx(OpIdx);
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001293 getOperand(OpIdx).print(OS, MST, TypeToPrint, /*PrintDef=*/true,
1294 ShouldPrintRegisterTies, TiedOperandIdx, TRI,
1295 IntrinsicInfo);
Evan Cheng6eb516d2011-01-07 23:50:32 +00001296
Eric Christopher0cb6fd92013-01-11 18:12:39 +00001297 // Print HasSideEffects, MayLoad, MayStore, IsAlignStack
Evan Cheng6eb516d2011-01-07 23:50:32 +00001298 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1299 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1300 OS << " [sideeffect]";
Eric Christopher0cb6fd92013-01-11 18:12:39 +00001301 if (ExtraInfo & InlineAsm::Extra_MayLoad)
1302 OS << " [mayload]";
1303 if (ExtraInfo & InlineAsm::Extra_MayStore)
1304 OS << " [maystore]";
Wei Ding0526e7f2016-06-22 18:51:08 +00001305 if (ExtraInfo & InlineAsm::Extra_IsConvergent)
1306 OS << " [isconvergent]";
Evan Cheng6eb516d2011-01-07 23:50:32 +00001307 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
1308 OS << " [alignstack]";
Chad Rosiercbd2a192012-09-05 22:17:43 +00001309 if (getInlineAsmDialect() == InlineAsm::AD_ATT)
Chad Rosier994f4042012-09-05 21:00:58 +00001310 OS << " [attdialect]";
Chad Rosiercbd2a192012-09-05 22:17:43 +00001311 if (getInlineAsmDialect() == InlineAsm::AD_Intel)
Chad Rosier994f4042012-09-05 21:00:58 +00001312 OS << " [inteldialect]";
Evan Cheng6eb516d2011-01-07 23:50:32 +00001313
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001314 StartOp = AsmDescOp = InlineAsm::MIOp_FirstOperand;
Evan Cheng6eb516d2011-01-07 23:50:32 +00001315 FirstOp = false;
1316 }
1317
Chris Lattnerac6e9742002-10-30 01:55:38 +00001318 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Dan Gohman2745d192009-11-09 19:38:45 +00001319 const MachineOperand &MO = getOperand(i);
1320
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001321 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001322 VirtRegs.push_back(MO.getReg());
1323
Dan Gohman2745d192009-11-09 19:38:45 +00001324 if (FirstOp) FirstOp = false; else OS << ",";
Chris Lattnerac6e9742002-10-30 01:55:38 +00001325 OS << " ";
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001326 if (i < getDesc().NumOperands) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001327 const MCOperandInfo &MCOI = getDesc().OpInfo[i];
1328 if (MCOI.isPredicate())
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001329 OS << "pred:";
Evan Cheng6cc775f2011-06-28 19:10:37 +00001330 if (MCOI.isOptionalDef())
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00001331 OS << "opt:";
1332 }
Evan Chengd4d1a512010-04-28 20:03:13 +00001333 if (isDebugValue() && MO.isMetadata()) {
1334 // Pretty print DBG_VALUE instructions.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001335 auto *DIV = dyn_cast<DILocalVariable>(MO.getMetadata());
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +00001336 if (DIV && !DIV->getName().empty())
1337 OS << "!\"" << DIV->getName() << '\"';
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001338 else {
1339 LLT TypeToPrint = MRI ? getTypeToPrint(i, PrintedTypes, *MRI) : LLT{};
Francis Visoiu Mistrihe6fc3ce2017-12-07 17:12:30 +00001340 unsigned TiedOperandIdx = getTiedOperandIdx(i);
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001341 MO.print(OS, MST, TypeToPrint, /*PrintDef=*/true,
1342 ShouldPrintRegisterTies, TiedOperandIdx, TRI, IntrinsicInfo);
1343 }
Matthias Brauna3743082017-01-09 21:38:10 +00001344 } else if (TRI && (isInsertSubreg() || isRegSequence() ||
1345 (isSubregToReg() && i == 3)) && MO.isImm()) {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001346 OS << TRI->getSubRegIndexName(MO.getImm());
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001347 } else if (i == AsmDescOp && MO.isImm()) {
1348 // Pretty print the inline asm operand descriptor.
1349 OS << '$' << AsmOpCount++;
1350 unsigned Flag = MO.getImm();
1351 switch (InlineAsm::getKind(Flag)) {
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001352 case InlineAsm::Kind_RegUse: OS << ":[reguse"; break;
1353 case InlineAsm::Kind_RegDef: OS << ":[regdef"; break;
1354 case InlineAsm::Kind_RegDefEarlyClobber: OS << ":[regdef-ec"; break;
1355 case InlineAsm::Kind_Clobber: OS << ":[clobber"; break;
1356 case InlineAsm::Kind_Imm: OS << ":[imm"; break;
1357 case InlineAsm::Kind_Mem: OS << ":[mem"; break;
1358 default: OS << ":[??" << InlineAsm::getKind(Flag); break;
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001359 }
1360
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001361 unsigned RCID = 0;
Simon Dardisd32a2d32016-07-18 13:17:31 +00001362 if (!InlineAsm::isImmKind(Flag) && !InlineAsm::isMemKind(Flag) &&
1363 InlineAsm::hasRegClassConstraint(Flag, RCID)) {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001364 if (TRI) {
1365 OS << ':' << TRI->getRegClassName(TRI->getRegClass(RCID));
Craig Toppercf0444b2014-11-17 05:50:14 +00001366 } else
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001367 OS << ":RC" << RCID;
Nick Lewycky84882252011-10-13 00:54:59 +00001368 }
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001369
Simon Dardisd32a2d32016-07-18 13:17:31 +00001370 if (InlineAsm::isMemKind(Flag)) {
1371 unsigned MCID = InlineAsm::getMemoryConstraintID(Flag);
1372 switch (MCID) {
1373 case InlineAsm::Constraint_es: OS << ":es"; break;
1374 case InlineAsm::Constraint_i: OS << ":i"; break;
1375 case InlineAsm::Constraint_m: OS << ":m"; break;
1376 case InlineAsm::Constraint_o: OS << ":o"; break;
1377 case InlineAsm::Constraint_v: OS << ":v"; break;
1378 case InlineAsm::Constraint_Q: OS << ":Q"; break;
1379 case InlineAsm::Constraint_R: OS << ":R"; break;
1380 case InlineAsm::Constraint_S: OS << ":S"; break;
1381 case InlineAsm::Constraint_T: OS << ":T"; break;
1382 case InlineAsm::Constraint_Um: OS << ":Um"; break;
1383 case InlineAsm::Constraint_Un: OS << ":Un"; break;
1384 case InlineAsm::Constraint_Uq: OS << ":Uq"; break;
1385 case InlineAsm::Constraint_Us: OS << ":Us"; break;
1386 case InlineAsm::Constraint_Ut: OS << ":Ut"; break;
1387 case InlineAsm::Constraint_Uv: OS << ":Uv"; break;
1388 case InlineAsm::Constraint_Uy: OS << ":Uy"; break;
1389 case InlineAsm::Constraint_X: OS << ":X"; break;
1390 case InlineAsm::Constraint_Z: OS << ":Z"; break;
1391 case InlineAsm::Constraint_ZC: OS << ":ZC"; break;
1392 case InlineAsm::Constraint_Zy: OS << ":Zy"; break;
1393 default: OS << ":?"; break;
1394 }
1395 }
1396
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001397 unsigned TiedTo = 0;
1398 if (InlineAsm::isUseOperandTiedToDef(Flag, TiedTo))
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00001399 OS << " tiedto:$" << TiedTo;
1400
1401 OS << ']';
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001402
1403 // Compute the index of the next operand descriptor.
1404 AsmDescOp += 1 + InlineAsm::getNumOperandRegisters(Flag);
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001405 } else {
1406 LLT TypeToPrint = MRI ? getTypeToPrint(i, PrintedTypes, *MRI) : LLT{};
Francis Visoiu Mistrihe6fc3ce2017-12-07 17:12:30 +00001407 unsigned TiedOperandIdx = getTiedOperandIdx(i);
Francis Visoiu Mistrih440f69c2017-12-08 22:53:21 +00001408 if (MO.isImm() && isOperandSubregIdx(i))
1409 MachineOperand::printSubregIdx(OS, MO.getImm(), TRI);
1410 else
1411 MO.print(OS, MST, TypeToPrint, /*PrintDef=*/true,
1412 ShouldPrintRegisterTies, TiedOperandIdx, TRI, IntrinsicInfo);
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001413 }
Dan Gohman2745d192009-11-09 19:38:45 +00001414 }
1415
Dan Gohman34341e62009-10-31 20:19:03 +00001416 bool HaveSemi = false;
Michael Kuperstein098cd9f2015-09-16 11:18:25 +00001417 const unsigned PrintableFlags = FrameSetup | FrameDestroy;
Jakob Stoklund Olesen6922e9c2013-01-09 18:35:09 +00001418 if (Flags & PrintableFlags) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00001419 if (!HaveSemi) {
1420 OS << ";";
1421 HaveSemi = true;
1422 }
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00001423 OS << " flags: ";
1424
1425 if (Flags & FrameSetup)
1426 OS << "FrameSetup";
Michael Kuperstein098cd9f2015-09-16 11:18:25 +00001427
1428 if (Flags & FrameDestroy)
1429 OS << "FrameDestroy";
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00001430 }
1431
Dan Gohman3b460302008-07-07 23:14:23 +00001432 if (!memoperands_empty()) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00001433 if (!HaveSemi) {
1434 OS << ";";
1435 HaveSemi = true;
1436 }
Dan Gohman34341e62009-10-31 20:19:03 +00001437
1438 OS << " mem:";
Dan Gohman48b185d2009-09-25 20:36:54 +00001439 for (mmo_iterator i = memoperands_begin(), e = memoperands_end();
1440 i != e; ++i) {
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001441 (*i)->print(OS, MST);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001442 if (std::next(i) != e)
Dan Gohmanc0353bf2009-09-23 01:33:16 +00001443 OS << " ";
Dan Gohman2d489b52008-02-06 22:27:42 +00001444 }
1445 }
1446
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001447 // Print the regclass of any virtual registers encountered.
1448 if (MRI && !VirtRegs.empty()) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00001449 if (!HaveSemi) {
1450 OS << ";";
1451 HaveSemi = true;
1452 }
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001453 for (unsigned i = 0; i != VirtRegs.size(); ++i) {
Quentin Colombet03c41962016-04-07 23:18:11 +00001454 const RegClassOrRegBank &RC = MRI->getRegClassOrRegBank(VirtRegs[i]);
Quentin Colombete1494c32016-02-11 00:19:17 +00001455 if (!RC)
1456 continue;
Quentin Colombet03c41962016-04-07 23:18:11 +00001457 // Generic virtual registers do not have register classes.
1458 if (RC.is<const RegisterBank *>())
1459 OS << " " << RC.get<const RegisterBank *>()->getName();
1460 else
1461 OS << " "
1462 << TRI->getRegClassName(RC.get<const TargetRegisterClass *>());
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00001463 OS << ':' << printReg(VirtRegs[i]);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001464 for (unsigned j = i+1; j != VirtRegs.size();) {
Quentin Colombet03c41962016-04-07 23:18:11 +00001465 if (MRI->getRegClassOrRegBank(VirtRegs[j]) != RC) {
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001466 ++j;
1467 continue;
1468 }
1469 if (VirtRegs[i] != VirtRegs[j])
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +00001470 OS << "," << printReg(VirtRegs[j]);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001471 VirtRegs.erase(VirtRegs.begin()+j);
1472 }
1473 }
1474 }
1475
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00001476 // Print debug location information.
Duncan P. N. Exon Smithc5bd3e02015-04-03 16:23:04 +00001477 if (isDebugValue() && getOperand(e - 2).isMetadata()) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00001478 if (!HaveSemi)
1479 OS << ";";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001480 auto *DV = cast<DILocalVariable>(getOperand(e - 2).getMetadata());
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +00001481 OS << " line no:" << DV->getLine();
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001482 if (auto *InlinedAt = debugLoc->getInlinedAt()) {
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +00001483 DebugLoc InlinedAtDL(InlinedAt);
1484 if (InlinedAtDL && MF) {
Devang Pateld61b1d52011-08-04 20:44:26 +00001485 OS << " inlined @[ ";
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00001486 InlinedAtDL.print(OS);
Devang Pateld61b1d52011-08-04 20:44:26 +00001487 OS << " ]";
1488 }
1489 }
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001490 if (isIndirectDebugValue())
1491 OS << " indirect";
Ahmed Bougacha97119d42017-02-23 21:05:29 +00001492 } else if (SkipDebugLoc) {
1493 return;
1494 } else if (debugLoc && MF) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00001495 if (!HaveSemi)
1496 OS << ";";
Dan Gohman2e3f1872009-11-23 21:29:08 +00001497 OS << " dbg:";
Eric Christopherb9f00092015-02-26 23:32:17 +00001498 debugLoc.print(OS);
Bill Wendling1a0a3d02009-02-19 21:44:55 +00001499 }
1500
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00001501 OS << '\n';
Chris Lattner214808f2002-10-30 00:48:05 +00001502}
1503
Owen Anderson2a8a4852008-01-24 01:10:07 +00001504bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001505 const TargetRegisterInfo *RegInfo,
Owen Anderson2a8a4852008-01-24 01:10:07 +00001506 bool AddIfNotFound) {
Evan Cheng6c177732008-04-16 09:41:59 +00001507 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001508 bool hasAliases = isPhysReg &&
1509 MCRegAliasIterator(IncomingReg, RegInfo, false).isValid();
Dan Gohmanc7367b42008-09-03 15:56:16 +00001510 bool Found = false;
Evan Cheng6c177732008-04-16 09:41:59 +00001511 SmallVector<unsigned,4> DeadOps;
Bill Wendling7921ad02008-03-03 22:14:33 +00001512 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1513 MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesenf465f062009-08-04 20:09:25 +00001514 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng6c177732008-04-16 09:41:59 +00001515 continue;
Mandeep Singh Grange5a2f112016-05-10 17:57:27 +00001516
1517 // DEBUG_VALUE nodes do not contribute to code generation and should
1518 // always be ignored. Failure to do so may result in trying to modify
1519 // KILL flags on DEBUG_VALUE nodes.
1520 if (MO.isDebug())
1521 continue;
1522
Evan Cheng6c177732008-04-16 09:41:59 +00001523 unsigned Reg = MO.getReg();
1524 if (!Reg)
1525 continue;
Bill Wendling7921ad02008-03-03 22:14:33 +00001526
Evan Cheng6c177732008-04-16 09:41:59 +00001527 if (Reg == IncomingReg) {
Dan Gohmanc7367b42008-09-03 15:56:16 +00001528 if (!Found) {
1529 if (MO.isKill())
1530 // The register is already marked kill.
1531 return true;
Jakob Stoklund Olesenc59cd9b2009-08-02 19:13:03 +00001532 if (isPhysReg && isRegTiedToDefOperand(i))
1533 // Two-address uses of physregs must not be marked kill.
1534 return true;
Dan Gohmanc7367b42008-09-03 15:56:16 +00001535 MO.setIsKill();
1536 Found = true;
1537 }
1538 } else if (hasAliases && MO.isKill() &&
1539 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng6c177732008-04-16 09:41:59 +00001540 // A super-register kill already exists.
1541 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohmanb2612922008-07-03 01:18:51 +00001542 return true;
1543 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng6c177732008-04-16 09:41:59 +00001544 DeadOps.push_back(i);
Bill Wendling7921ad02008-03-03 22:14:33 +00001545 }
1546 }
1547
Evan Cheng6c177732008-04-16 09:41:59 +00001548 // Trim unneeded kill operands.
1549 while (!DeadOps.empty()) {
1550 unsigned OpIdx = DeadOps.back();
1551 if (getOperand(OpIdx).isImplicit())
1552 RemoveOperand(OpIdx);
1553 else
1554 getOperand(OpIdx).setIsKill(false);
1555 DeadOps.pop_back();
1556 }
1557
Bill Wendling7921ad02008-03-03 22:14:33 +00001558 // If not found, this means an alias of one of the operands is killed. Add a
Owen Anderson2a8a4852008-01-24 01:10:07 +00001559 // new implicit operand if required.
Dan Gohmanc7367b42008-09-03 15:56:16 +00001560 if (!Found && AddIfNotFound) {
Bill Wendling7921ad02008-03-03 22:14:33 +00001561 addOperand(MachineOperand::CreateReg(IncomingReg,
1562 false /*IsDef*/,
1563 true /*IsImp*/,
1564 true /*IsKill*/));
Owen Anderson2a8a4852008-01-24 01:10:07 +00001565 return true;
1566 }
Dan Gohmanc7367b42008-09-03 15:56:16 +00001567 return Found;
Owen Anderson2a8a4852008-01-24 01:10:07 +00001568}
1569
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00001570void MachineInstr::clearRegisterKills(unsigned Reg,
1571 const TargetRegisterInfo *RegInfo) {
1572 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
Craig Topperc0196b12014-04-14 00:51:57 +00001573 RegInfo = nullptr;
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001574 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00001575 if (!MO.isReg() || !MO.isUse() || !MO.isKill())
1576 continue;
1577 unsigned OpReg = MO.getReg();
Matthias Braunaca625a2016-02-24 19:21:48 +00001578 if ((RegInfo && RegInfo->regsOverlap(Reg, OpReg)) || Reg == OpReg)
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00001579 MO.setIsKill(false);
1580 }
1581}
1582
Matthias Braun1965bfa2013-10-10 21:28:38 +00001583bool MachineInstr::addRegisterDead(unsigned Reg,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00001584 const TargetRegisterInfo *RegInfo,
Owen Anderson2a8a4852008-01-24 01:10:07 +00001585 bool AddIfNotFound) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00001586 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(Reg);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001587 bool hasAliases = isPhysReg &&
Matthias Braun1965bfa2013-10-10 21:28:38 +00001588 MCRegAliasIterator(Reg, RegInfo, false).isValid();
Dan Gohmanc7367b42008-09-03 15:56:16 +00001589 bool Found = false;
Evan Cheng6c177732008-04-16 09:41:59 +00001590 SmallVector<unsigned,4> DeadOps;
Owen Anderson2a8a4852008-01-24 01:10:07 +00001591 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1592 MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001593 if (!MO.isReg() || !MO.isDef())
Evan Cheng6c177732008-04-16 09:41:59 +00001594 continue;
Matthias Braun1965bfa2013-10-10 21:28:38 +00001595 unsigned MOReg = MO.getReg();
1596 if (!MOReg)
Dan Gohmanc7367b42008-09-03 15:56:16 +00001597 continue;
1598
Matthias Braun1965bfa2013-10-10 21:28:38 +00001599 if (MOReg == Reg) {
Jakob Stoklund Olesen76ad3de2011-04-05 16:53:50 +00001600 MO.setIsDead();
1601 Found = true;
Dan Gohmanc7367b42008-09-03 15:56:16 +00001602 } else if (hasAliases && MO.isDead() &&
Matthias Braun1965bfa2013-10-10 21:28:38 +00001603 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
Evan Cheng6c177732008-04-16 09:41:59 +00001604 // There exists a super-register that's marked dead.
Matthias Braun1965bfa2013-10-10 21:28:38 +00001605 if (RegInfo->isSuperRegister(Reg, MOReg))
Dan Gohmanb2612922008-07-03 01:18:51 +00001606 return true;
Matthias Braun1965bfa2013-10-10 21:28:38 +00001607 if (RegInfo->isSubRegister(Reg, MOReg))
Evan Cheng6c177732008-04-16 09:41:59 +00001608 DeadOps.push_back(i);
Owen Anderson2a8a4852008-01-24 01:10:07 +00001609 }
1610 }
1611
Evan Cheng6c177732008-04-16 09:41:59 +00001612 // Trim unneeded dead operands.
1613 while (!DeadOps.empty()) {
1614 unsigned OpIdx = DeadOps.back();
1615 if (getOperand(OpIdx).isImplicit())
1616 RemoveOperand(OpIdx);
1617 else
1618 getOperand(OpIdx).setIsDead(false);
1619 DeadOps.pop_back();
1620 }
1621
Dan Gohmanc7367b42008-09-03 15:56:16 +00001622 // If not found, this means an alias of one of the operands is dead. Add a
1623 // new implicit operand if required.
Chris Lattnerfd682802009-06-24 17:54:48 +00001624 if (Found || !AddIfNotFound)
1625 return Found;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001626
Matthias Braun1965bfa2013-10-10 21:28:38 +00001627 addOperand(MachineOperand::CreateReg(Reg,
Chris Lattnerfd682802009-06-24 17:54:48 +00001628 true /*IsDef*/,
1629 true /*IsImp*/,
1630 false /*IsKill*/,
1631 true /*IsDead*/));
1632 return true;
Owen Anderson2a8a4852008-01-24 01:10:07 +00001633}
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00001634
Matthias Braun26e7ea62015-02-04 19:35:16 +00001635void MachineInstr::clearRegisterDeads(unsigned Reg) {
1636 for (MachineOperand &MO : operands()) {
1637 if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg)
1638 continue;
1639 MO.setIsDead(false);
1640 }
1641}
1642
Matthias Braun2c98d0f2015-11-11 00:41:58 +00001643void MachineInstr::setRegisterDefReadUndef(unsigned Reg, bool IsUndef) {
Matthias Braunc1988f32015-01-21 22:55:13 +00001644 for (MachineOperand &MO : operands()) {
1645 if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg || MO.getSubReg() == 0)
1646 continue;
Matthias Braun2c98d0f2015-11-11 00:41:58 +00001647 MO.setIsUndef(IsUndef);
Matthias Braunc1988f32015-01-21 22:55:13 +00001648 }
1649}
1650
Matthias Braun1965bfa2013-10-10 21:28:38 +00001651void MachineInstr::addRegisterDefined(unsigned Reg,
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00001652 const TargetRegisterInfo *RegInfo) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00001653 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1654 MachineOperand *MO = findRegisterDefOperand(Reg, false, RegInfo);
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00001655 if (MO)
1656 return;
1657 } else {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001658 for (const MachineOperand &MO : operands()) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00001659 if (MO.isReg() && MO.getReg() == Reg && MO.isDef() &&
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00001660 MO.getSubReg() == 0)
1661 return;
1662 }
1663 }
Matthias Braun1965bfa2013-10-10 21:28:38 +00001664 addOperand(MachineOperand::CreateReg(Reg,
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00001665 true /*IsDef*/,
1666 true /*IsImp*/));
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00001667}
Evan Cheng59d27fe2010-03-03 23:37:30 +00001668
Jakob Stoklund Olesen4290be42012-02-03 20:43:39 +00001669void MachineInstr::setPhysRegsDeadExcept(ArrayRef<unsigned> UsedRegs,
Dan Gohman86936502010-06-18 23:28:01 +00001670 const TargetRegisterInfo &TRI) {
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00001671 bool HasRegMask = false;
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001672 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00001673 if (MO.isRegMask()) {
1674 HasRegMask = true;
1675 continue;
1676 }
Dan Gohman86936502010-06-18 23:28:01 +00001677 if (!MO.isReg() || !MO.isDef()) continue;
1678 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenf6507322012-02-03 20:43:35 +00001679 if (!TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Dan Gohman86936502010-06-18 23:28:01 +00001680 // If there are no uses, including partial uses, the def is dead.
Eugene Zelenko4e9736b2017-05-31 01:10:10 +00001681 if (llvm::none_of(UsedRegs,
1682 [&](unsigned Use) { return TRI.regsOverlap(Use, Reg); }))
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001683 MO.setIsDead();
Dan Gohman86936502010-06-18 23:28:01 +00001684 }
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00001685
1686 // This is a call with a register mask operand.
1687 // Mask clobbers are always dead, so add defs for the non-dead defines.
1688 if (HasRegMask)
1689 for (ArrayRef<unsigned>::iterator I = UsedRegs.begin(), E = UsedRegs.end();
1690 I != E; ++I)
1691 addRegisterDefined(*I, &TRI);
Dan Gohman86936502010-06-18 23:28:01 +00001692}
1693
Evan Cheng59d27fe2010-03-03 23:37:30 +00001694unsigned
1695MachineInstrExpressionTrait::getHashValue(const MachineInstr* const &MI) {
Chandler Carruth962152c2012-03-07 09:39:46 +00001696 // Build up a buffer of hash code components.
Chandler Carruth962152c2012-03-07 09:39:46 +00001697 SmallVector<size_t, 8> HashComponents;
1698 HashComponents.reserve(MI->getNumOperands() + 1);
1699 HashComponents.push_back(MI->getOpcode());
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001700 for (const MachineOperand &MO : MI->operands()) {
Chandler Carruth264854f2012-07-05 11:06:22 +00001701 if (MO.isReg() && MO.isDef() &&
1702 TargetRegisterInfo::isVirtualRegister(MO.getReg()))
1703 continue; // Skip virtual register defs.
1704
1705 HashComponents.push_back(hash_value(MO));
Evan Cheng59d27fe2010-03-03 23:37:30 +00001706 }
Chandler Carruth962152c2012-03-07 09:39:46 +00001707 return hash_combine_range(HashComponents.begin(), HashComponents.end());
Evan Cheng59d27fe2010-03-03 23:37:30 +00001708}
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00001709
1710void MachineInstr::emitError(StringRef Msg) const {
1711 // Find the source location cookie.
1712 unsigned LocCookie = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00001713 const MDNode *LocMD = nullptr;
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00001714 for (unsigned i = getNumOperands(); i != 0; --i) {
1715 if (getOperand(i-1).isMetadata() &&
1716 (LocMD = getOperand(i-1).getMetadata()) &&
1717 LocMD->getNumOperands() != 0) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001718 if (const ConstantInt *CI =
1719 mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00001720 LocCookie = CI->getZExtValue();
1721 break;
1722 }
1723 }
1724 }
1725
1726 if (const MachineBasicBlock *MBB = getParent())
1727 if (const MachineFunction *MF = MBB->getParent())
1728 return MF->getMMI().getModule()->getContext().emitError(LocCookie, Msg);
1729 report_fatal_error(Msg);
1730}
Reid Kleckner28865802016-04-14 18:29:59 +00001731
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001732MachineInstrBuilder llvm::BuildMI(MachineFunction &MF, const DebugLoc &DL,
Reid Kleckner28865802016-04-14 18:29:59 +00001733 const MCInstrDesc &MCID, bool IsIndirect,
Adrian Prantl8b9bb532017-07-28 23:00:45 +00001734 unsigned Reg, const MDNode *Variable,
1735 const MDNode *Expr) {
Reid Kleckner28865802016-04-14 18:29:59 +00001736 assert(isa<DILocalVariable>(Variable) && "not a variable");
1737 assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
1738 assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
1739 "Expected inlined-at fields to agree");
1740 if (IsIndirect)
1741 return BuildMI(MF, DL, MCID)
1742 .addReg(Reg, RegState::Debug)
Adrian Prantl8b9bb532017-07-28 23:00:45 +00001743 .addImm(0U)
Reid Kleckner28865802016-04-14 18:29:59 +00001744 .addMetadata(Variable)
1745 .addMetadata(Expr);
Adrian Prantl8b9bb532017-07-28 23:00:45 +00001746 else
Reid Kleckner28865802016-04-14 18:29:59 +00001747 return BuildMI(MF, DL, MCID)
1748 .addReg(Reg, RegState::Debug)
1749 .addReg(0U, RegState::Debug)
1750 .addMetadata(Variable)
1751 .addMetadata(Expr);
Reid Kleckner28865802016-04-14 18:29:59 +00001752}
1753
1754MachineInstrBuilder llvm::BuildMI(MachineBasicBlock &BB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001755 MachineBasicBlock::iterator I,
1756 const DebugLoc &DL, const MCInstrDesc &MCID,
1757 bool IsIndirect, unsigned Reg,
Adrian Prantl8b9bb532017-07-28 23:00:45 +00001758 const MDNode *Variable, const MDNode *Expr) {
Reid Kleckner28865802016-04-14 18:29:59 +00001759 assert(isa<DILocalVariable>(Variable) && "not a variable");
1760 assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
1761 MachineFunction &MF = *BB.getParent();
Adrian Prantl8b9bb532017-07-28 23:00:45 +00001762 MachineInstr *MI = BuildMI(MF, DL, MCID, IsIndirect, Reg, Variable, Expr);
Reid Kleckner28865802016-04-14 18:29:59 +00001763 BB.insert(I, MI);
1764 return MachineInstrBuilder(MF, MI);
1765}
Adrian Prantl6825fb62017-04-18 01:21:53 +00001766
Reid Kleckner9e6c3092017-09-15 21:49:56 +00001767/// Compute the new DIExpression to use with a DBG_VALUE for a spill slot.
1768/// This prepends DW_OP_deref when spilling an indirect DBG_VALUE.
1769static const DIExpression *computeExprForSpill(const MachineInstr &MI) {
1770 assert(MI.getOperand(0).isReg() && "can't spill non-register");
1771 assert(MI.getDebugVariable()->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
1772 "Expected inlined-at fields to agree");
1773
1774 const DIExpression *Expr = MI.getDebugExpression();
1775 if (MI.isIndirectDebugValue()) {
1776 assert(MI.getOperand(1).getImm() == 0 && "DBG_VALUE with nonzero offset");
1777 Expr = DIExpression::prepend(Expr, DIExpression::WithDeref);
1778 }
1779 return Expr;
1780}
1781
Adrian Prantl6825fb62017-04-18 01:21:53 +00001782MachineInstr *llvm::buildDbgValueForSpill(MachineBasicBlock &BB,
1783 MachineBasicBlock::iterator I,
1784 const MachineInstr &Orig,
1785 int FrameIndex) {
Reid Kleckner9e6c3092017-09-15 21:49:56 +00001786 const DIExpression *Expr = computeExprForSpill(Orig);
1787 return BuildMI(BB, I, Orig.getDebugLoc(), Orig.getDesc())
Adrian Prantl6825fb62017-04-18 01:21:53 +00001788 .addFrameIndex(FrameIndex)
Adrian Prantl8b9bb532017-07-28 23:00:45 +00001789 .addImm(0U)
Reid Kleckner9e6c3092017-09-15 21:49:56 +00001790 .addMetadata(Orig.getDebugVariable())
Adrian Prantl6825fb62017-04-18 01:21:53 +00001791 .addMetadata(Expr);
1792}
Reid Kleckner9e6c3092017-09-15 21:49:56 +00001793
1794void llvm::updateDbgValueForSpill(MachineInstr &Orig, int FrameIndex) {
1795 const DIExpression *Expr = computeExprForSpill(Orig);
1796 Orig.getOperand(0).ChangeToFrameIndex(FrameIndex);
1797 Orig.getOperand(1).ChangeToImmediate(0U);
1798 Orig.getOperand(3).setMetadata(Expr);
1799}