blob: 2c81218f8f6c95c21a812546c5f5c41f816b38a8 [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"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000021#include "llvm/ADT/SmallString.h"
22#include "llvm/ADT/SmallVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/Analysis/AliasAnalysis.h"
Hiroshi Inoue95f24dc2017-06-24 15:17:38 +000024#include "llvm/Analysis/Loads.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000025#include "llvm/Analysis/MemoryLocation.h"
26#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
27#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner63f41ab2004-02-19 16:17:08 +000028#include "llvm/CodeGen/MachineFunction.h"
Reid Kleckner28865802016-04-14 18:29:59 +000029#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000030#include "llvm/CodeGen/MachineInstrBundle.h"
Dan Gohman48b185d2009-09-25 20:36:54 +000031#include "llvm/CodeGen/MachineMemOperand.h"
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000033#include "llvm/CodeGen/MachineOperand.h"
Chris Lattner961e7422008-01-01 01:12:31 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman2d489b52008-02-06 22:27:42 +000035#include "llvm/CodeGen/PseudoSourceValue.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000036#include "llvm/CodeGen/TargetInstrInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000037#include "llvm/IR/Constants.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000038#include "llvm/IR/DebugInfoMetadata.h"
39#include "llvm/IR/DebugLoc.h"
40#include "llvm/IR/DerivedTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000041#include "llvm/IR/Function.h"
42#include "llvm/IR/InlineAsm.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000043#include "llvm/IR/InstrTypes.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000044#include "llvm/IR/Intrinsics.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000045#include "llvm/IR/LLVMContext.h"
46#include "llvm/IR/Metadata.h"
47#include "llvm/IR/Module.h"
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +000048#include "llvm/IR/ModuleSlotTracker.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000049#include "llvm/IR/Type.h"
50#include "llvm/IR/Value.h"
Evan Cheng6cc775f2011-06-28 19:10:37 +000051#include "llvm/MC/MCInstrDesc.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000052#include "llvm/MC/MCRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000053#include "llvm/MC/MCSymbol.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000054#include "llvm/Support/Casting.h"
Daniel Sanders1e97a0b2015-08-19 12:03:04 +000055#include "llvm/Support/CommandLine.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000056#include "llvm/Support/Compiler.h"
David Greene29388d62010-01-04 23:48:20 +000057#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000058#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000059#include "llvm/Support/LowLevelTypeImpl.h"
Dan Gohmanaedb4a62008-07-07 20:32:02 +000060#include "llvm/Support/MathExtras.h"
Chris Lattnera078d832008-08-24 20:37:32 +000061#include "llvm/Support/raw_ostream.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000062#include "llvm/Target/TargetIntrinsicInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000063#include "llvm/Target/TargetMachine.h"
64#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000065#include "llvm/Target/TargetSubtargetInfo.h"
Eugene Zelenko4e9736b2017-05-31 01:10:10 +000066#include <algorithm>
67#include <cassert>
68#include <cstddef>
69#include <cstdint>
70#include <cstring>
71#include <iterator>
72#include <utility>
73
Chris Lattner43df6c22004-02-23 18:38:20 +000074using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000075
Matt Arsenaultd62fe832017-07-20 00:37:31 +000076static cl::opt<int> PrintRegMaskNumRegs(
77 "print-regmask-num-regs",
78 cl::desc("Number of registers to limit to when "
79 "printing regmask operands in IR dumps. "
80 "unlimited = -1"),
81 cl::init(32), cl::Hidden);
Daniel Sanders1e97a0b2015-08-19 12:03:04 +000082
Chris Lattner60055892007-12-30 21:56:09 +000083//===----------------------------------------------------------------------===//
84// MachineOperand Implementation
85//===----------------------------------------------------------------------===//
86
Chris Lattner961e7422008-01-01 01:12:31 +000087void MachineOperand::setReg(unsigned Reg) {
88 if (getReg() == Reg) return; // No change.
Jim Grosbachdee9e8a2011-08-24 16:44:17 +000089
Chris Lattner961e7422008-01-01 01:12:31 +000090 // Otherwise, we have to change the register. If this operand is embedded
91 // into a machine function, we need to update the old and new register's
92 // use/def lists.
93 if (MachineInstr *MI = getParent())
94 if (MachineBasicBlock *MBB = MI->getParent())
95 if (MachineFunction *MF = MBB->getParent()) {
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +000096 MachineRegisterInfo &MRI = MF->getRegInfo();
97 MRI.removeRegOperandFromUseList(this);
Jakob Stoklund Olesena4941692010-10-19 20:56:32 +000098 SmallContents.RegNo = Reg;
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +000099 MRI.addRegOperandToUseList(this);
Chris Lattner961e7422008-01-01 01:12:31 +0000100 return;
101 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000102
Chris Lattner961e7422008-01-01 01:12:31 +0000103 // Otherwise, just change the register, no problem. :)
Jakob Stoklund Olesena4941692010-10-19 20:56:32 +0000104 SmallContents.RegNo = Reg;
Chris Lattner961e7422008-01-01 01:12:31 +0000105}
106
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +0000107void MachineOperand::substVirtReg(unsigned Reg, unsigned SubIdx,
108 const TargetRegisterInfo &TRI) {
109 assert(TargetRegisterInfo::isVirtualRegister(Reg));
110 if (SubIdx && getSubReg())
111 SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg());
112 setReg(Reg);
Jakob Stoklund Olesen7b0ac862010-06-01 22:39:25 +0000113 if (SubIdx)
114 setSubReg(SubIdx);
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +0000115}
116
117void MachineOperand::substPhysReg(unsigned Reg, const TargetRegisterInfo &TRI) {
118 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
119 if (getSubReg()) {
120 Reg = TRI.getSubReg(Reg, getSubReg());
Jakob Stoklund Olesen89bd2ae2011-05-08 19:21:08 +0000121 // Note that getSubReg() may return 0 if the sub-register doesn't exist.
122 // That won't happen in legal code.
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +0000123 setSubReg(0);
Krzysztof Parzyszek673b3472016-08-22 14:50:12 +0000124 if (isDef())
125 setIsUndef(false);
Jakob Stoklund Olesen64824ea2010-05-28 18:18:53 +0000126 }
127 setReg(Reg);
128}
129
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000130/// Change a def to a use, or a use to a def.
131void MachineOperand::setIsDef(bool Val) {
132 assert(isReg() && "Wrong MachineOperand accessor");
133 assert((!Val || !isDebug()) && "Marking a debug operation as def");
134 if (IsDef == Val)
135 return;
136 // MRI may keep uses and defs in different list positions.
137 if (MachineInstr *MI = getParent())
138 if (MachineBasicBlock *MBB = MI->getParent())
139 if (MachineFunction *MF = MBB->getParent()) {
140 MachineRegisterInfo &MRI = MF->getRegInfo();
141 MRI.removeRegOperandFromUseList(this);
142 IsDef = Val;
143 MRI.addRegOperandToUseList(this);
144 return;
145 }
146 IsDef = Val;
147}
148
Matt Arsenault93ffe582014-09-28 19:24:59 +0000149// If this operand is currently a register operand, and if this is in a
150// function, deregister the operand from the register's use/def list.
151void MachineOperand::removeRegFromUses() {
152 if (!isReg() || !isOnRegUseList())
153 return;
154
155 if (MachineInstr *MI = getParent()) {
156 if (MachineBasicBlock *MBB = MI->getParent()) {
157 if (MachineFunction *MF = MBB->getParent())
158 MF->getRegInfo().removeRegOperandFromUseList(this);
159 }
160 }
161}
162
Chris Lattner961e7422008-01-01 01:12:31 +0000163/// ChangeToImmediate - Replace this operand with a new immediate operand of
164/// the specified value. If an operand is known to be an immediate already,
165/// the setImm method should be used.
166void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000167 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
Matt Arsenault93ffe582014-09-28 19:24:59 +0000168
169 removeRegFromUses();
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000170
Chris Lattner961e7422008-01-01 01:12:31 +0000171 OpKind = MO_Immediate;
172 Contents.ImmVal = ImmVal;
173}
174
Matt Arsenault93ffe582014-09-28 19:24:59 +0000175void MachineOperand::ChangeToFPImmediate(const ConstantFP *FPImm) {
176 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
177
178 removeRegFromUses();
179
180 OpKind = MO_FPImmediate;
181 Contents.CFP = FPImm;
182}
183
Matt Arsenault633dba42015-05-06 17:05:54 +0000184void MachineOperand::ChangeToES(const char *SymName, unsigned char TargetFlags) {
185 assert((!isReg() || !isTied()) &&
186 "Cannot change a tied operand into an external symbol");
187
188 removeRegFromUses();
189
190 OpKind = MO_ExternalSymbol;
191 Contents.OffsetedInfo.Val.SymbolName = SymName;
192 setOffset(0); // Offset is always 0.
193 setTargetFlags(TargetFlags);
194}
195
196void MachineOperand::ChangeToMCSymbol(MCSymbol *Sym) {
197 assert((!isReg() || !isTied()) &&
198 "Cannot change a tied operand into an MCSymbol");
199
200 removeRegFromUses();
201
202 OpKind = MO_MCSymbol;
203 Contents.Sym = Sym;
204}
205
Matt Arsenault25dba302016-09-13 19:03:12 +0000206void MachineOperand::ChangeToFrameIndex(int Idx) {
207 assert((!isReg() || !isTied()) &&
208 "Cannot change a tied operand into a FrameIndex");
209
210 removeRegFromUses();
211
212 OpKind = MO_FrameIndex;
213 setIndex(Idx);
214}
215
Marcello Maggioni8de4bbd2017-08-04 18:24:09 +0000216void MachineOperand::ChangeToTargetIndex(unsigned Idx, int64_t Offset,
217 unsigned char TargetFlags) {
218 assert((!isReg() || !isTied()) &&
219 "Cannot change a tied operand into a FrameIndex");
220
221 removeRegFromUses();
222
223 OpKind = MO_TargetIndex;
224 setIndex(Idx);
225 setOffset(Offset);
226 setTargetFlags(TargetFlags);
227}
228
Chris Lattner961e7422008-01-01 01:12:31 +0000229/// ChangeToRegister - Replace this operand with a new register operand of
230/// the specified value. If an operand is known to be an register already,
231/// the setReg method should be used.
232void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
Dale Johannesend40d42c2010-02-10 00:41:49 +0000233 bool isKill, bool isDead, bool isUndef,
234 bool isDebug) {
Craig Topperc0196b12014-04-14 00:51:57 +0000235 MachineRegisterInfo *RegInfo = nullptr;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000236 if (MachineInstr *MI = getParent())
237 if (MachineBasicBlock *MBB = MI->getParent())
238 if (MachineFunction *MF = MBB->getParent())
239 RegInfo = &MF->getRegInfo();
240 // If this operand is already a register operand, remove it from the
Chris Lattner961e7422008-01-01 01:12:31 +0000241 // register's use/def lists.
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000242 bool WasReg = isReg();
243 if (RegInfo && WasReg)
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000244 RegInfo->removeRegOperandFromUseList(this);
Chris Lattner961e7422008-01-01 01:12:31 +0000245
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000246 // Change this to a register and set the reg#.
247 OpKind = MO_Register;
248 SmallContents.RegNo = Reg;
Jakob Stoklund Olesena1b246d2013-01-07 23:21:44 +0000249 SubReg_TargetFlags = 0;
Chris Lattner961e7422008-01-01 01:12:31 +0000250 IsDef = isDef;
251 IsImp = isImp;
252 IsKill = isKill;
253 IsDead = isDead;
Evan Cheng0dc101b2009-06-30 08:49:04 +0000254 IsUndef = isUndef;
Jakob Stoklund Olesenb0d91ab2011-12-07 00:22:07 +0000255 IsInternalRead = false;
Dale Johannesenc0d712d2008-09-14 01:44:36 +0000256 IsEarlyClobber = false;
Dale Johannesend40d42c2010-02-10 00:41:49 +0000257 IsDebug = isDebug;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000258 // Ensure isOnRegUseList() returns false.
Craig Topperc0196b12014-04-14 00:51:57 +0000259 Contents.Reg.Prev = nullptr;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000260 // Preserve the tie when the operand was already a register.
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +0000261 if (!WasReg)
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000262 TiedTo = 0;
Jakob Stoklund Olesenae7b9712012-08-10 00:21:26 +0000263
264 // If this operand is embedded in a function, add the operand to the
265 // register's use/def list.
266 if (RegInfo)
267 RegInfo->addRegOperandToUseList(this);
Chris Lattner961e7422008-01-01 01:12:31 +0000268}
269
Chris Lattner60055892007-12-30 21:56:09 +0000270/// isIdenticalTo - Return true if this operand is identical to the specified
Chandler Carruth264854f2012-07-05 11:06:22 +0000271/// operand. Note that this should stay in sync with the hash_value overload
272/// below.
Chris Lattner60055892007-12-30 21:56:09 +0000273bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
Chris Lattnerfd682802009-06-24 17:54:48 +0000274 if (getType() != Other.getType() ||
275 getTargetFlags() != Other.getTargetFlags())
276 return false;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000277
Chris Lattner60055892007-12-30 21:56:09 +0000278 switch (getType()) {
Chris Lattner60055892007-12-30 21:56:09 +0000279 case MachineOperand::MO_Register:
280 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
281 getSubReg() == Other.getSubReg();
282 case MachineOperand::MO_Immediate:
283 return getImm() == Other.getImm();
Cameron Zwarich7da0f9a2011-07-01 23:45:21 +0000284 case MachineOperand::MO_CImmediate:
285 return getCImm() == Other.getCImm();
Nate Begeman26b76b62008-02-14 07:39:30 +0000286 case MachineOperand::MO_FPImmediate:
287 return getFPImm() == Other.getFPImm();
Chris Lattner60055892007-12-30 21:56:09 +0000288 case MachineOperand::MO_MachineBasicBlock:
289 return getMBB() == Other.getMBB();
290 case MachineOperand::MO_FrameIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000291 return getIndex() == Other.getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000292 case MachineOperand::MO_ConstantPoolIndex:
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000293 case MachineOperand::MO_TargetIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000294 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
Chris Lattner60055892007-12-30 21:56:09 +0000295 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000296 return getIndex() == Other.getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000297 case MachineOperand::MO_GlobalAddress:
298 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
299 case MachineOperand::MO_ExternalSymbol:
Eugene Zelenko4e9736b2017-05-31 01:10:10 +0000300 return strcmp(getSymbolName(), Other.getSymbolName()) == 0 &&
Chris Lattner60055892007-12-30 21:56:09 +0000301 getOffset() == Other.getOffset();
Dan Gohman6c938802009-10-30 01:27:03 +0000302 case MachineOperand::MO_BlockAddress:
Michael Liaoabb87d42012-09-12 21:43:09 +0000303 return getBlockAddress() == Other.getBlockAddress() &&
304 getOffset() == Other.getOffset();
Juergen Ributzkae8294752013-12-14 06:53:06 +0000305 case MachineOperand::MO_RegisterMask:
Oren Ben Simhonfe34c5e2017-03-14 09:09:26 +0000306 case MachineOperand::MO_RegisterLiveOut: {
307 // Shallow compare of the two RegMasks
308 const uint32_t *RegMask = getRegMask();
309 const uint32_t *OtherRegMask = Other.getRegMask();
310 if (RegMask == OtherRegMask)
311 return true;
312
313 // Calculate the size of the RegMask
Justin Bognerfdf9bf42017-10-10 23:50:49 +0000314 const MachineFunction *MF = getParent()->getMF();
Oren Ben Simhonfe34c5e2017-03-14 09:09:26 +0000315 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
316 unsigned RegMaskSize = (TRI->getNumRegs() + 31) / 32;
317
318 // Deep compare of the two RegMasks
319 return std::equal(RegMask, RegMask + RegMaskSize, OtherRegMask);
320 }
Chris Lattner6c604e32010-03-13 08:14:18 +0000321 case MachineOperand::MO_MCSymbol:
322 return getMCSymbol() == Other.getMCSymbol();
Petar Jovanovice2a585d2017-11-07 14:40:27 +0000323 case MachineOperand::MO_CFIIndex: {
324 const MachineFunction *MF = getParent()->getParent()->getParent();
325 const MachineFunction *OtherMF =
326 Other.getParent()->getParent()->getParent();
327 MCCFIInstruction Inst = MF->getFrameInstructions()[getCFIIndex()];
328 MCCFIInstruction OtherInst =
329 OtherMF->getFrameInstructions()[Other.getCFIIndex()];
330 MCCFIInstruction::OpType op = Inst.getOperation();
331 if (op != OtherInst.getOperation()) return false;
332 switch (op) {
333 case MCCFIInstruction::OpDefCfa:
334 case MCCFIInstruction::OpOffset:
335 case MCCFIInstruction::OpRelOffset:
336 if (Inst.getRegister() != OtherInst.getRegister()) return false;
337 if (Inst.getOffset() != OtherInst.getOffset()) return false;
338 break;
339 case MCCFIInstruction::OpRestore:
340 case MCCFIInstruction::OpUndefined:
341 case MCCFIInstruction::OpSameValue:
342 case MCCFIInstruction::OpDefCfaRegister:
343 if (Inst.getRegister() != OtherInst.getRegister()) return false;
344 break;
345 case MCCFIInstruction::OpRegister:
346 if (Inst.getRegister() != OtherInst.getRegister()) return false;
347 if (Inst.getRegister2() != OtherInst.getRegister2()) return false;
348 break;
349 case MCCFIInstruction::OpDefCfaOffset:
350 case MCCFIInstruction::OpAdjustCfaOffset:
351 case MCCFIInstruction::OpGnuArgsSize:
352 if (Inst.getOffset() != OtherInst.getOffset()) return false;
353 break;
354 case MCCFIInstruction::OpRememberState:
355 case MCCFIInstruction::OpRestoreState:
356 case MCCFIInstruction::OpEscape:
357 case MCCFIInstruction::OpWindowSave:
358 break;
359 }
360 return true;
361 }
Chris Lattnerf839ee02010-04-07 18:03:19 +0000362 case MachineOperand::MO_Metadata:
363 return getMetadata() == Other.getMetadata();
Tim Northover6b3bd612016-07-29 20:32:59 +0000364 case MachineOperand::MO_IntrinsicID:
365 return getIntrinsicID() == Other.getIntrinsicID();
Tim Northoverde3aea0412016-08-17 20:25:25 +0000366 case MachineOperand::MO_Predicate:
367 return getPredicate() == Other.getPredicate();
Chris Lattner60055892007-12-30 21:56:09 +0000368 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000369 llvm_unreachable("Invalid machine operand type");
Chris Lattner60055892007-12-30 21:56:09 +0000370}
371
Chandler Carruth264854f2012-07-05 11:06:22 +0000372// Note: this must stay exactly in sync with isIdenticalTo above.
373hash_code llvm::hash_value(const MachineOperand &MO) {
374 switch (MO.getType()) {
375 case MachineOperand::MO_Register:
Jakob Stoklund Olesendba99d02012-08-28 18:05:48 +0000376 // Register operands don't have target flags.
377 return hash_combine(MO.getType(), MO.getReg(), MO.getSubReg(), MO.isDef());
Chandler Carruth264854f2012-07-05 11:06:22 +0000378 case MachineOperand::MO_Immediate:
379 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm());
380 case MachineOperand::MO_CImmediate:
381 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm());
382 case MachineOperand::MO_FPImmediate:
383 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm());
384 case MachineOperand::MO_MachineBasicBlock:
385 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB());
386 case MachineOperand::MO_FrameIndex:
387 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
388 case MachineOperand::MO_ConstantPoolIndex:
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000389 case MachineOperand::MO_TargetIndex:
Chandler Carruth264854f2012-07-05 11:06:22 +0000390 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(),
391 MO.getOffset());
392 case MachineOperand::MO_JumpTableIndex:
393 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
394 case MachineOperand::MO_ExternalSymbol:
395 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(),
396 MO.getSymbolName());
397 case MachineOperand::MO_GlobalAddress:
398 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(),
399 MO.getOffset());
400 case MachineOperand::MO_BlockAddress:
401 return hash_combine(MO.getType(), MO.getTargetFlags(),
Michael Liaoabb87d42012-09-12 21:43:09 +0000402 MO.getBlockAddress(), MO.getOffset());
Chandler Carruth264854f2012-07-05 11:06:22 +0000403 case MachineOperand::MO_RegisterMask:
Juergen Ributzkae8294752013-12-14 06:53:06 +0000404 case MachineOperand::MO_RegisterLiveOut:
Chandler Carruth264854f2012-07-05 11:06:22 +0000405 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getRegMask());
406 case MachineOperand::MO_Metadata:
407 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata());
408 case MachineOperand::MO_MCSymbol:
409 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol());
Petar Jovanovice2a585d2017-11-07 14:40:27 +0000410 case MachineOperand::MO_CFIIndex: {
411 const MachineFunction *MF = MO.getParent()->getParent()->getParent();
412 MCCFIInstruction Inst = MF->getFrameInstructions()[MO.getCFIIndex()];
413 return hash_combine(MO.getType(), MO.getTargetFlags(), Inst.getOperation(),
414 Inst.getRegister(), Inst.getRegister2(),
415 Inst.getOffset());
416 }
Tim Northover6b3bd612016-07-29 20:32:59 +0000417 case MachineOperand::MO_IntrinsicID:
418 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIntrinsicID());
Tim Northoverde3aea0412016-08-17 20:25:25 +0000419 case MachineOperand::MO_Predicate:
420 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getPredicate());
Chandler Carruth264854f2012-07-05 11:06:22 +0000421 }
422 llvm_unreachable("Invalid machine operand type");
423}
424
Tim Northover6b3bd612016-07-29 20:32:59 +0000425void MachineOperand::print(raw_ostream &OS, const TargetRegisterInfo *TRI,
426 const TargetIntrinsicInfo *IntrinsicInfo) const {
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000427 ModuleSlotTracker DummyMST(nullptr);
Tim Northover6b3bd612016-07-29 20:32:59 +0000428 print(OS, DummyMST, TRI, IntrinsicInfo);
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000429}
430
431void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
Tim Northover6b3bd612016-07-29 20:32:59 +0000432 const TargetRegisterInfo *TRI,
433 const TargetIntrinsicInfo *IntrinsicInfo) const {
Chris Lattner60055892007-12-30 21:56:09 +0000434 switch (getType()) {
435 case MachineOperand::MO_Register:
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +0000436 OS << PrintReg(getReg(), TRI, getSubReg());
Dan Gohman0ab11442008-12-18 21:51:27 +0000437
Evan Cheng0dc101b2009-06-30 08:49:04 +0000438 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000439 isInternalRead() || isEarlyClobber() || isTied()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000440 OS << '<';
Chris Lattner60055892007-12-30 21:56:09 +0000441 bool NeedComma = false;
Evan Cheng70b1fa52009-10-14 23:37:31 +0000442 if (isDef()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000443 if (NeedComma) OS << ',';
Dale Johannesen1f3ab862008-09-12 17:49:03 +0000444 if (isEarlyClobber())
445 OS << "earlyclobber,";
Evan Cheng70b1fa52009-10-14 23:37:31 +0000446 if (isImplicit())
447 OS << "imp-";
Chris Lattner60055892007-12-30 21:56:09 +0000448 OS << "def";
449 NeedComma = true;
Jakob Stoklund Olesen7111a632012-04-20 21:45:33 +0000450 // <def,read-undef> only makes sense when getSubReg() is set.
451 // Don't clutter the output otherwise.
452 if (isUndef() && getSubReg())
453 OS << ",read-undef";
Evan Chengf781bd82009-10-21 07:56:02 +0000454 } else if (isImplicit()) {
Craig Topper9a9d58a2015-05-16 05:42:08 +0000455 OS << "imp-use";
456 NeedComma = true;
Evan Chengf781bd82009-10-21 07:56:02 +0000457 }
Evan Cheng70b1fa52009-10-14 23:37:31 +0000458
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000459 if (isKill()) {
Chris Lattnerfd682802009-06-24 17:54:48 +0000460 if (NeedComma) OS << ',';
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +0000461 OS << "kill";
462 NeedComma = true;
463 }
464 if (isDead()) {
465 if (NeedComma) OS << ',';
466 OS << "dead";
467 NeedComma = true;
468 }
469 if (isUndef() && isUse()) {
470 if (NeedComma) OS << ',';
471 OS << "undef";
472 NeedComma = true;
473 }
474 if (isInternalRead()) {
475 if (NeedComma) OS << ',';
476 OS << "internal";
477 NeedComma = true;
478 }
479 if (isTied()) {
480 if (NeedComma) OS << ',';
481 OS << "tied";
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000482 if (TiedTo != 15)
483 OS << unsigned(TiedTo - 1);
Chris Lattner60055892007-12-30 21:56:09 +0000484 }
Chris Lattnerfd682802009-06-24 17:54:48 +0000485 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000486 }
487 break;
488 case MachineOperand::MO_Immediate:
489 OS << getImm();
490 break;
Devang Patelf071d722011-06-24 20:46:11 +0000491 case MachineOperand::MO_CImmediate:
492 getCImm()->getValue().print(OS, false);
493 break;
Nate Begeman26b76b62008-02-14 07:39:30 +0000494 case MachineOperand::MO_FPImmediate:
Matt Arsenault59239732016-02-05 00:50:18 +0000495 if (getFPImm()->getType()->isFloatTy()) {
Nate Begeman26b76b62008-02-14 07:39:30 +0000496 OS << getFPImm()->getValueAPF().convertToFloat();
Matt Arsenault59239732016-02-05 00:50:18 +0000497 } else if (getFPImm()->getType()->isHalfTy()) {
498 APFloat APF = getFPImm()->getValueAPF();
499 bool Unused;
Stephan Bergmann17c7f702016-12-14 11:57:17 +0000500 APF.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &Unused);
Matt Arsenault59239732016-02-05 00:50:18 +0000501 OS << "half " << APF.convertToFloat();
Tim Northover89268b12017-03-20 16:52:08 +0000502 } else if (getFPImm()->getType()->isFP128Ty()) {
503 APFloat APF = getFPImm()->getValueAPF();
504 SmallString<16> Str;
505 getFPImm()->getValueAPF().toString(Str);
506 OS << "quad " << Str;
Igor Breger55e2f592017-07-05 11:11:10 +0000507 } else if (getFPImm()->getType()->isX86_FP80Ty()) {
508 APFloat APF = getFPImm()->getValueAPF();
509 OS << "x86_fp80 0xK";
510 APInt API = APF.bitcastToAPInt();
511 OS << format_hex_no_prefix(API.getHiBits(16).getZExtValue(), 4,
512 /*Upper=*/true);
513 OS << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
514 /*Upper=*/true);
Matt Arsenault59239732016-02-05 00:50:18 +0000515 } else {
Nate Begeman26b76b62008-02-14 07:39:30 +0000516 OS << getFPImm()->getValueAPF().convertToDouble();
Matt Arsenault59239732016-02-05 00:50:18 +0000517 }
Nate Begeman26b76b62008-02-14 07:39:30 +0000518 break;
Chris Lattner60055892007-12-30 21:56:09 +0000519 case MachineOperand::MO_MachineBasicBlock:
Dan Gohman34341e62009-10-31 20:19:03 +0000520 OS << "<BB#" << getMBB()->getNumber() << ">";
Chris Lattner60055892007-12-30 21:56:09 +0000521 break;
522 case MachineOperand::MO_FrameIndex:
Chris Lattnerfd682802009-06-24 17:54:48 +0000523 OS << "<fi#" << getIndex() << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000524 break;
525 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000526 OS << "<cp#" << getIndex();
Chris Lattner60055892007-12-30 21:56:09 +0000527 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000528 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000529 break;
Jakob Stoklund Olesen84689b02012-08-07 18:56:39 +0000530 case MachineOperand::MO_TargetIndex:
531 OS << "<ti#" << getIndex();
532 if (getOffset()) OS << "+" << getOffset();
533 OS << '>';
534 break;
Chris Lattner60055892007-12-30 21:56:09 +0000535 case MachineOperand::MO_JumpTableIndex:
Chris Lattnerfd682802009-06-24 17:54:48 +0000536 OS << "<jt#" << getIndex() << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000537 break;
538 case MachineOperand::MO_GlobalAddress:
Dan Gohman0080ee22009-11-06 18:03:10 +0000539 OS << "<ga:";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000540 getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
Chris Lattner60055892007-12-30 21:56:09 +0000541 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000542 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000543 break;
544 case MachineOperand::MO_ExternalSymbol:
545 OS << "<es:" << getSymbolName();
546 if (getOffset()) OS << "+" << getOffset();
Chris Lattnerfd682802009-06-24 17:54:48 +0000547 OS << '>';
Chris Lattner60055892007-12-30 21:56:09 +0000548 break;
Dan Gohman6c938802009-10-30 01:27:03 +0000549 case MachineOperand::MO_BlockAddress:
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000550 OS << '<';
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000551 getBlockAddress()->printAsOperand(OS, /*PrintType=*/false, MST);
Michael Liaoabb87d42012-09-12 21:43:09 +0000552 if (getOffset()) OS << "+" << getOffset();
Dan Gohman6c938802009-10-30 01:27:03 +0000553 OS << '>';
554 break;
Daniel Sanders1e97a0b2015-08-19 12:03:04 +0000555 case MachineOperand::MO_RegisterMask: {
556 unsigned NumRegsInMask = 0;
557 unsigned NumRegsEmitted = 0;
558 OS << "<regmask";
559 for (unsigned i = 0; i < TRI->getNumRegs(); ++i) {
560 unsigned MaskWord = i / 32;
561 unsigned MaskBit = i % 32;
562 if (getRegMask()[MaskWord] & (1 << MaskBit)) {
Matt Arsenaultd62fe832017-07-20 00:37:31 +0000563 if (PrintRegMaskNumRegs < 0 ||
564 NumRegsEmitted <= static_cast<unsigned>(PrintRegMaskNumRegs)) {
Daniel Sanders1e97a0b2015-08-19 12:03:04 +0000565 OS << " " << PrintReg(i, TRI);
566 NumRegsEmitted++;
567 }
568 NumRegsInMask++;
569 }
570 }
571 if (NumRegsEmitted != NumRegsInMask)
572 OS << " and " << (NumRegsInMask - NumRegsEmitted) << " more...";
573 OS << ">";
Jakob Stoklund Olesen374ed322012-01-16 19:22:00 +0000574 break;
Daniel Sanders1e97a0b2015-08-19 12:03:04 +0000575 }
Juergen Ributzkae8294752013-12-14 06:53:06 +0000576 case MachineOperand::MO_RegisterLiveOut:
577 OS << "<regliveout>";
578 break;
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000579 case MachineOperand::MO_Metadata:
580 OS << '<';
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000581 getMetadata()->printAsOperand(OS, MST);
Dale Johannesen7b1a7ed2010-01-13 00:00:24 +0000582 OS << '>';
583 break;
Chris Lattner6c604e32010-03-13 08:14:18 +0000584 case MachineOperand::MO_MCSymbol:
585 OS << "<MCSym=" << *getMCSymbol() << '>';
586 break;
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000587 case MachineOperand::MO_CFIIndex:
588 OS << "<call frame instruction>";
589 break;
Tim Northover6b3bd612016-07-29 20:32:59 +0000590 case MachineOperand::MO_IntrinsicID: {
591 Intrinsic::ID ID = getIntrinsicID();
592 if (ID < Intrinsic::num_intrinsics)
Ahmed Bougacha925961b2016-09-12 16:21:49 +0000593 OS << "<intrinsic:@" << Intrinsic::getName(ID, None) << '>';
Tim Northover6b3bd612016-07-29 20:32:59 +0000594 else if (IntrinsicInfo)
Ahmed Bougacha925961b2016-09-12 16:21:49 +0000595 OS << "<intrinsic:@" << IntrinsicInfo->getName(ID) << '>';
Tim Northover6b3bd612016-07-29 20:32:59 +0000596 else
597 OS << "<intrinsic:" << ID << '>';
598 break;
599 }
Tim Northoverde3aea0412016-08-17 20:25:25 +0000600 case MachineOperand::MO_Predicate: {
601 auto Pred = static_cast<CmpInst::Predicate>(getPredicate());
602 OS << '<' << (CmpInst::isIntPredicate(Pred) ? "intpred" : "floatpred")
603 << CmpInst::getPredicateName(Pred) << '>';
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000604 break;
Chris Lattner60055892007-12-30 21:56:09 +0000605 }
Tim Northoverde3aea0412016-08-17 20:25:25 +0000606 }
Chris Lattnerfd682802009-06-24 17:54:48 +0000607 if (unsigned TF = getTargetFlags())
608 OS << "[TF=" << TF << ']';
Chris Lattner60055892007-12-30 21:56:09 +0000609}
610
Aaron Ballman615eb472017-10-15 14:32:27 +0000611#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braun637488d2016-11-18 02:40:40 +0000612LLVM_DUMP_METHOD void MachineOperand::dump() const {
613 dbgs() << *this << '\n';
614}
615#endif
616
Chris Lattner60055892007-12-30 21:56:09 +0000617//===----------------------------------------------------------------------===//
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000618// MachineMemOperand Implementation
619//===----------------------------------------------------------------------===//
620
Chris Lattnerde93bb02010-09-21 05:39:30 +0000621/// getAddrSpace - Return the LLVM IR address space number that this pointer
622/// points into.
623unsigned MachinePointerInfo::getAddrSpace() const {
Jan Sjodin312ccf72017-09-14 20:53:51 +0000624 if (V.isNull()) return 0;
625
626 if (V.is<const PseudoSourceValue*>())
627 return V.get<const PseudoSourceValue*>()->getAddressSpace();
628
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000629 return cast<PointerType>(V.get<const Value*>()->getType())->getAddressSpace();
Chris Lattnerde93bb02010-09-21 05:39:30 +0000630}
631
Hiroshi Inoue95f24dc2017-06-24 15:17:38 +0000632/// isDereferenceable - Return true if V is always dereferenceable for
633/// Offset + Size byte.
634bool MachinePointerInfo::isDereferenceable(unsigned Size, LLVMContext &C,
635 const DataLayout &DL) const {
636 if (!V.is<const Value*>())
637 return false;
638
639 const Value *BasePtr = V.get<const Value*>();
640 if (BasePtr == nullptr)
641 return false;
642
Simon Dardis51296592017-08-03 09:38:46 +0000643 return isDereferenceableAndAlignedPointer(
644 BasePtr, 1, APInt(DL.getPointerSizeInBits(), Offset + Size), DL);
Hiroshi Inoue95f24dc2017-06-24 15:17:38 +0000645}
646
Chris Lattner82fd06d2010-09-21 06:22:23 +0000647/// getConstantPool - Return a MachinePointerInfo record that refers to the
648/// constant pool.
Alex Lorenze40c8a22015-08-11 23:09:45 +0000649MachinePointerInfo MachinePointerInfo::getConstantPool(MachineFunction &MF) {
650 return MachinePointerInfo(MF.getPSVManager().getConstantPool());
Chris Lattner82fd06d2010-09-21 06:22:23 +0000651}
652
653/// getFixedStack - Return a MachinePointerInfo record that refers to the
654/// the specified FrameIndex.
Alex Lorenze40c8a22015-08-11 23:09:45 +0000655MachinePointerInfo MachinePointerInfo::getFixedStack(MachineFunction &MF,
656 int FI, int64_t Offset) {
657 return MachinePointerInfo(MF.getPSVManager().getFixedStack(FI), Offset);
Chris Lattner82fd06d2010-09-21 06:22:23 +0000658}
659
Alex Lorenze40c8a22015-08-11 23:09:45 +0000660MachinePointerInfo MachinePointerInfo::getJumpTable(MachineFunction &MF) {
661 return MachinePointerInfo(MF.getPSVManager().getJumpTable());
Chris Lattner50287ea2010-09-21 06:43:24 +0000662}
663
Alex Lorenze40c8a22015-08-11 23:09:45 +0000664MachinePointerInfo MachinePointerInfo::getGOT(MachineFunction &MF) {
665 return MachinePointerInfo(MF.getPSVManager().getGOT());
Chris Lattner50287ea2010-09-21 06:43:24 +0000666}
Chris Lattnerde93bb02010-09-21 05:39:30 +0000667
Alex Lorenze40c8a22015-08-11 23:09:45 +0000668MachinePointerInfo MachinePointerInfo::getStack(MachineFunction &MF,
Matt Arsenaultdb782732017-07-20 21:03:45 +0000669 int64_t Offset,
670 uint8_t ID) {
671 return MachinePointerInfo(MF.getPSVManager().getStack(), Offset,ID);
Chris Lattner886250c2010-09-21 18:51:21 +0000672}
673
Justin Lebara3b786a2016-07-14 17:07:44 +0000674MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, Flags f,
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000675 uint64_t s, unsigned int a,
Hal Finkelcc39b672014-07-24 12:16:19 +0000676 const AAMDNodes &AAInfo,
Konstantin Zhuravlyov8ea02462016-10-15 22:01:18 +0000677 const MDNode *Ranges,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000678 SyncScope::ID SSID,
Konstantin Zhuravlyov8ea02462016-10-15 22:01:18 +0000679 AtomicOrdering Ordering,
680 AtomicOrdering FailureOrdering)
Justin Lebara3b786a2016-07-14 17:07:44 +0000681 : PtrInfo(ptrinfo), Size(s), FlagVals(f), BaseAlignLog2(Log2_32(a) + 1),
682 AAInfo(AAInfo), Ranges(Ranges) {
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000683 assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue*>() ||
684 isa<PointerType>(PtrInfo.V.get<const Value*>()->getType())) &&
Chris Lattner00ca0b82010-09-21 04:32:08 +0000685 "invalid pointer value");
Dan Gohmane7c82422009-09-21 19:47:04 +0000686 assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
Dan Gohmanbf98f682008-07-16 15:56:42 +0000687 assert((isLoad() || isStore()) && "Not a load/store!");
Konstantin Zhuravlyov8ea02462016-10-15 22:01:18 +0000688
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000689 AtomicInfo.SSID = static_cast<unsigned>(SSID);
690 assert(getSyncScopeID() == SSID && "Value truncated");
Konstantin Zhuravlyov8ea02462016-10-15 22:01:18 +0000691 AtomicInfo.Ordering = static_cast<unsigned>(Ordering);
692 assert(getOrdering() == Ordering && "Value truncated");
693 AtomicInfo.FailureOrdering = static_cast<unsigned>(FailureOrdering);
694 assert(getFailureOrdering() == FailureOrdering && "Value truncated");
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000695}
696
Dan Gohman2da2bed2008-08-20 15:58:01 +0000697/// Profile - Gather unique data for the object.
698///
699void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
Chris Lattner187f6532010-09-21 04:23:39 +0000700 ID.AddInteger(getOffset());
Dan Gohman2da2bed2008-08-20 15:58:01 +0000701 ID.AddInteger(Size);
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000702 ID.AddPointer(getOpaqueValue());
Justin Lebara3b786a2016-07-14 17:07:44 +0000703 ID.AddInteger(getFlags());
704 ID.AddInteger(getBaseAlignment());
Dan Gohman2da2bed2008-08-20 15:58:01 +0000705}
706
Dan Gohman48b185d2009-09-25 20:36:54 +0000707void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
708 // The Value and Offset may differ due to CSE. But the flags and size
709 // should be the same.
710 assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
711 assert(MMO->getSize() == getSize() && "Size mismatch!");
712
713 if (MMO->getBaseAlignment() >= getBaseAlignment()) {
714 // Update the alignment value.
Justin Lebara3b786a2016-07-14 17:07:44 +0000715 BaseAlignLog2 = Log2_32(MMO->getBaseAlignment()) + 1;
Dan Gohman48b185d2009-09-25 20:36:54 +0000716 // Also update the base and offset, because the new alignment may
717 // not be applicable with the old ones.
Chris Lattner187f6532010-09-21 04:23:39 +0000718 PtrInfo = MMO->PtrInfo;
Dan Gohman48b185d2009-09-25 20:36:54 +0000719 }
720}
721
Dan Gohman5a6b11c2009-09-25 23:33:20 +0000722/// getAlignment - Return the minimum known alignment in bytes of the
723/// actual memory reference.
724uint64_t MachineMemOperand::getAlignment() const {
725 return MinAlign(getBaseAlignment(), getOffset());
726}
727
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000728void MachineMemOperand::print(raw_ostream &OS) const {
729 ModuleSlotTracker DummyMST(nullptr);
730 print(OS, DummyMST);
731}
732void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST) const {
733 assert((isLoad() || isStore()) &&
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000734 "SV has to be a load, store or both.");
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000735
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000736 if (isVolatile())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000737 OS << "Volatile ";
738
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000739 if (isLoad())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000740 OS << "LD";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000741 if (isStore())
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000742 OS << "ST";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000743 OS << getSize();
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000744
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000745 // Print the address information.
746 OS << "[";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000747 if (const Value *V = getValue())
748 V->printAsOperand(OS, /*PrintType=*/false, MST);
749 else if (const PseudoSourceValue *PSV = getPseudoValue())
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000750 PSV->printCustom(OS);
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000751 else
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000752 OS << "<unknown>";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000753
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000754 unsigned AS = getAddrSpace();
Matt Arsenault68c38fd2013-12-14 00:24:02 +0000755 if (AS != 0)
756 OS << "(addrspace=" << AS << ')';
757
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000758 // If the alignment of the memory reference itself differs from the alignment
759 // of the base pointer, print the base alignment explicitly, next to the base
760 // pointer.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000761 if (getBaseAlignment() != getAlignment())
762 OS << "(align=" << getBaseAlignment() << ")";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000763
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000764 if (getOffset() != 0)
765 OS << "+" << getOffset();
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000766 OS << "]";
767
768 // Print the alignment of the reference.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000769 if (getBaseAlignment() != getAlignment() || getBaseAlignment() != getSize())
770 OS << "(align=" << getAlignment() << ")";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000771
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000772 // Print TBAA info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000773 if (const MDNode *TBAAInfo = getAAInfo().TBAA) {
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000774 OS << "(tbaa=";
775 if (TBAAInfo->getNumOperands() > 0)
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000776 TBAAInfo->getOperand(0)->printAsOperand(OS, MST);
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000777 else
778 OS << "<unknown>";
779 OS << ")";
780 }
781
Hal Finkel94146652014-07-24 14:25:39 +0000782 // Print AA scope info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000783 if (const MDNode *ScopeInfo = getAAInfo().Scope) {
Hal Finkel94146652014-07-24 14:25:39 +0000784 OS << "(alias.scope=";
785 if (ScopeInfo->getNumOperands() > 0)
786 for (unsigned i = 0, ie = ScopeInfo->getNumOperands(); i != ie; ++i) {
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000787 ScopeInfo->getOperand(i)->printAsOperand(OS, MST);
Hal Finkel94146652014-07-24 14:25:39 +0000788 if (i != ie-1)
789 OS << ",";
790 }
791 else
792 OS << "<unknown>";
793 OS << ")";
794 }
795
796 // Print AA noalias scope info.
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000797 if (const MDNode *NoAliasInfo = getAAInfo().NoAlias) {
Hal Finkel94146652014-07-24 14:25:39 +0000798 OS << "(noalias=";
799 if (NoAliasInfo->getNumOperands() > 0)
800 for (unsigned i = 0, ie = NoAliasInfo->getNumOperands(); i != ie; ++i) {
Duncan P. N. Exon Smith6529ed42015-06-26 22:28:47 +0000801 NoAliasInfo->getOperand(i)->printAsOperand(OS, MST);
Hal Finkel94146652014-07-24 14:25:39 +0000802 if (i != ie-1)
803 OS << ",";
804 }
805 else
806 OS << "<unknown>";
807 OS << ")";
808 }
809
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000810 if (isNonTemporal())
Bill Wendling9f638ab2011-04-29 23:45:22 +0000811 OS << "(nontemporal)";
Justin Lebaradbf09e2016-09-11 01:38:58 +0000812 if (isDereferenceable())
813 OS << "(dereferenceable)";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +0000814 if (isInvariant())
Matt Arsenault572c29a2015-06-26 19:00:11 +0000815 OS << "(invariant)";
Geoff Berry6748abe2017-07-13 02:28:54 +0000816 if (getFlags() & MOTargetFlag1)
817 OS << "(flag1)";
818 if (getFlags() & MOTargetFlag2)
819 OS << "(flag2)";
820 if (getFlags() & MOTargetFlag3)
821 OS << "(flag3)";
Dan Gohmanc0353bf2009-09-23 01:33:16 +0000822}
823
Dan Gohmanaedb4a62008-07-07 20:32:02 +0000824//===----------------------------------------------------------------------===//
Chris Lattner60055892007-12-30 21:56:09 +0000825// MachineInstr Implementation
826//===----------------------------------------------------------------------===//
827
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000828void MachineInstr::addImplicitDefUseOperands(MachineFunction &MF) {
Evan Cheng6cc775f2011-06-28 19:10:37 +0000829 if (MCID->ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +0000830 for (const MCPhysReg *ImpDefs = MCID->getImplicitDefs(); *ImpDefs;
831 ++ImpDefs)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000832 addOperand(MF, MachineOperand::CreateReg(*ImpDefs, true, true));
Evan Cheng6cc775f2011-06-28 19:10:37 +0000833 if (MCID->ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +0000834 for (const MCPhysReg *ImpUses = MCID->getImplicitUses(); *ImpUses;
835 ++ImpUses)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000836 addOperand(MF, MachineOperand::CreateReg(*ImpUses, false, true));
Evan Cheng77af6ac2006-11-13 23:34:06 +0000837}
838
Bob Wilson406f2702010-04-09 04:34:03 +0000839/// MachineInstr ctor - This constructor creates a MachineInstr and adds the
840/// implicit operands. It reserves space for the number of operands specified by
Evan Cheng6cc775f2011-06-28 19:10:37 +0000841/// the MCInstrDesc.
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000842MachineInstr::MachineInstr(MachineFunction &MF, const MCInstrDesc &tid,
Benjamin Kramera9591b52015-02-07 12:28:15 +0000843 DebugLoc dl, bool NoImp)
Eugene Zelenko4e9736b2017-05-31 01:10:10 +0000844 : MCID(&tid), debugLoc(std::move(dl)) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000845 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
846
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000847 // Reserve space for the expected number of operands.
848 if (unsigned NumOps = MCID->getNumOperands() +
849 MCID->getNumImplicitDefs() + MCID->getNumImplicitUses()) {
850 CapOperands = OperandCapacity::get(NumOps);
851 Operands = MF.allocateOperandArray(CapOperands);
852 }
853
Dale Johannesen4e04ef32009-01-27 23:20:29 +0000854 if (!NoImp)
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000855 addImplicitDefUseOperands(MF);
Dale Johannesen4e04ef32009-01-27 23:20:29 +0000856}
857
Misha Brukmanb47ab7a2004-07-09 14:45:17 +0000858/// MachineInstr ctor - Copies MachineInstr arg exactly
859///
Evan Chenga7a20c42008-07-19 00:37:25 +0000860MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI)
Eugene Zelenko4e9736b2017-05-31 01:10:10 +0000861 : MCID(&MI.getDesc()), NumMemRefs(MI.NumMemRefs), MemRefs(MI.MemRefs),
862 debugLoc(MI.getDebugLoc()) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000863 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
864
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000865 CapOperands = OperandCapacity::get(MI.getNumOperands());
866 Operands = MF.allocateOperandArray(CapOperands);
Tanya Lattner9953d862004-05-23 20:58:02 +0000867
Jakob Stoklund Olesendc5285f2013-01-05 05:05:51 +0000868 // Copy operands.
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000869 for (const MachineOperand &MO : MI.operands())
870 addOperand(MF, MO);
Tanya Lattnerbcee21b2004-05-24 03:14:18 +0000871
Jakob Stoklund Olesena33f5042012-12-18 21:36:05 +0000872 // Copy all the sensible flags.
873 setFlags(MI.Flags);
Alkis Evlogimenos14f3fe82004-02-16 07:17:43 +0000874}
875
Chris Lattner961e7422008-01-01 01:12:31 +0000876/// getRegInfo - If this instruction is embedded into a MachineFunction,
877/// return the MachineRegisterInfo object for the current function, otherwise
878/// return null.
879MachineRegisterInfo *MachineInstr::getRegInfo() {
880 if (MachineBasicBlock *MBB = getParent())
Dan Gohmanf188fa42008-07-08 23:59:09 +0000881 return &MBB->getParent()->getRegInfo();
Craig Topperc0196b12014-04-14 00:51:57 +0000882 return nullptr;
Chris Lattner961e7422008-01-01 01:12:31 +0000883}
884
885/// RemoveRegOperandsFromUseLists - Unlink all of the register operands in
886/// this instruction from their respective use lists. This requires that the
887/// operands already be on their use lists.
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000888void MachineInstr::RemoveRegOperandsFromUseLists(MachineRegisterInfo &MRI) {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000889 for (MachineOperand &MO : operands())
890 if (MO.isReg())
891 MRI.removeRegOperandFromUseList(&MO);
Chris Lattner961e7422008-01-01 01:12:31 +0000892}
893
894/// AddRegOperandsToUseLists - Add all of the register operands in
895/// this instruction from their respective use lists. This requires that the
896/// operands not be on their use lists yet.
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000897void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &MRI) {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +0000898 for (MachineOperand &MO : operands())
899 if (MO.isReg())
900 MRI.addRegOperandToUseList(&MO);
Chris Lattner961e7422008-01-01 01:12:31 +0000901}
902
Jakob Stoklund Olesen2455b5852012-12-20 22:54:05 +0000903void MachineInstr::addOperand(const MachineOperand &Op) {
904 MachineBasicBlock *MBB = getParent();
905 assert(MBB && "Use MachineInstrBuilder to add operands to dangling instrs");
906 MachineFunction *MF = MBB->getParent();
907 assert(MF && "Use MachineInstrBuilder to add operands to dangling instrs");
908 addOperand(*MF, Op);
909}
910
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000911/// Move NumOps MachineOperands from Src to Dst, with support for overlapping
912/// ranges. If MRI is non-null also update use-def chains.
913static void moveOperands(MachineOperand *Dst, MachineOperand *Src,
914 unsigned NumOps, MachineRegisterInfo *MRI) {
915 if (MRI)
916 return MRI->moveOperands(Dst, Src, NumOps);
917
JF Bastiena874d1a2016-03-26 18:20:02 +0000918 // MachineOperand is a trivially copyable type so we can just use memmove.
Benjamin Kramer5c0e64f2015-02-21 16:22:48 +0000919 std::memmove(Dst, Src, NumOps * sizeof(MachineOperand));
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000920}
921
Chris Lattner961e7422008-01-01 01:12:31 +0000922/// addOperand - Add the specified operand to the instruction. If it is an
923/// implicit operand, it is added to the end of the operand list. If it is
924/// an explicit operand it is added at the end of the explicit operand list
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000925/// (before the first implicit operand).
Jakob Stoklund Olesen2455b5852012-12-20 22:54:05 +0000926void MachineInstr::addOperand(MachineFunction &MF, const MachineOperand &Op) {
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000927 assert(MCID && "Cannot add operands before providing an instr descriptor");
Dan Gohman9356d8f2008-12-09 22:45:08 +0000928
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000929 // Check if we're adding one of our existing operands.
930 if (&Op >= Operands && &Op < Operands + NumOperands) {
931 // This is unusual: MI->addOperand(MI->getOperand(i)).
932 // If adding Op requires reallocating or moving existing operands around,
933 // the Op reference could go stale. Support it by copying Op.
934 MachineOperand CopyOp(Op);
935 return addOperand(MF, CopyOp);
936 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000937
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000938 // Find the insert location for the new operand. Implicit registers go at
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000939 // the end, everything else goes before the implicit regs.
940 //
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000941 // FIXME: Allow mixed explicit and implicit operands on inline asm.
942 // InstrEmitter::EmitSpecialNode() is marking inline asm clobbers as
943 // implicit-defs, but they must not be moved around. See the FIXME in
944 // InstrEmitter.cpp.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000945 unsigned OpNo = getNumOperands();
946 bool isImpReg = Op.isReg() && Op.isImplicit();
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000947 if (!isImpReg && !isInlineAsm()) {
948 while (OpNo && Operands[OpNo-1].isReg() && Operands[OpNo-1].isImplicit()) {
949 --OpNo;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000950 assert(!Operands[OpNo].isTied() && "Cannot move tied operands");
Chris Lattner961e7422008-01-01 01:12:31 +0000951 }
952 }
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000953
Pekka Jaaskelaineneb4a6e72013-10-15 14:40:46 +0000954#ifndef NDEBUG
Pekka Jaaskelaineneb08e2e2013-10-15 14:18:10 +0000955 bool isMetaDataOp = Op.getType() == MachineOperand::MO_Metadata;
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000956 // OpNo now points as the desired insertion point. Unless this is a variadic
957 // instruction, only implicit regs are allowed beyond MCID->getNumOperands().
Jakob Stoklund Olesenc300ef02012-07-04 23:53:23 +0000958 // RegMask operands go between the explicit and implicit operands.
959 assert((isImpReg || Op.isRegMask() || MCID->isVariadic() ||
Pekka Jaaskelaineneb08e2e2013-10-15 14:18:10 +0000960 OpNo < MCID->getNumOperands() || isMetaDataOp) &&
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +0000961 "Trying to add an operand to a machine instr that is already done!");
Pekka Jaaskelaineneb4a6e72013-10-15 14:40:46 +0000962#endif
Chris Lattner961e7422008-01-01 01:12:31 +0000963
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000964 MachineRegisterInfo *MRI = getRegInfo();
Chris Lattner961e7422008-01-01 01:12:31 +0000965
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000966 // Determine if the Operands array needs to be reallocated.
967 // Save the old capacity and operand array.
968 OperandCapacity OldCap = CapOperands;
969 MachineOperand *OldOperands = Operands;
970 if (!OldOperands || OldCap.getSize() == getNumOperands()) {
971 CapOperands = OldOperands ? OldCap.getNext() : OldCap.get(1);
972 Operands = MF.allocateOperandArray(CapOperands);
973 // Move the operands before the insertion point.
974 if (OpNo)
975 moveOperands(Operands, OldOperands, OpNo, MRI);
976 }
Chris Lattner961e7422008-01-01 01:12:31 +0000977
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000978 // Move the operands following the insertion point.
979 if (OpNo != NumOperands)
980 moveOperands(Operands + OpNo + 1, OldOperands + OpNo, NumOperands - OpNo,
981 MRI);
982 ++NumOperands;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +0000983
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000984 // Deallocate the old operand array.
985 if (OldOperands != Operands && OldOperands)
986 MF.deallocateOperandArray(OldCap, OldOperands);
987
988 // Copy Op into place. It still needs to be inserted into the MRI use lists.
989 MachineOperand *NewMO = new (Operands + OpNo) MachineOperand(Op);
990 NewMO->ParentMI = this;
991
992 // When adding a register operand, tell MRI about it.
993 if (NewMO->isReg()) {
Jakob Stoklund Olesenc4102d42012-08-09 22:49:37 +0000994 // Ensure isOnRegUseList() returns false, regardless of Op's status.
Craig Topperc0196b12014-04-14 00:51:57 +0000995 NewMO->Contents.Reg.Prev = nullptr;
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +0000996 // Ignore existing ties. This is not a property that can be copied.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000997 NewMO->TiedTo = 0;
998 // Add the new operand to MRI, but only for instructions in an MBB.
999 if (MRI)
1000 MRI->addRegOperandToUseList(NewMO);
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +00001001 // The MCID operand information isn't accurate until we start adding
1002 // explicit operands. The implicit operands are added first, then the
1003 // explicits are inserted before them.
1004 if (!isImpReg) {
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001005 // Tie uses to defs as indicated in MCInstrDesc.
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +00001006 if (NewMO->isUse()) {
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +00001007 int DefIdx = MCID->getOperandConstraint(OpNo, MCOI::TIED_TO);
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001008 if (DefIdx != -1)
1009 tieOperands(DefIdx, OpNo);
Jakob Stoklund Olesene56c60c2012-08-28 18:34:41 +00001010 }
Jakob Stoklund Olesen0eecbbe2012-08-30 14:39:06 +00001011 // If the register operand is flagged as early, mark the operand as such.
1012 if (MCID->getOperandConstraint(OpNo, MCOI::EARLY_CLOBBER) != -1)
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +00001013 NewMO->setIsEarlyClobber(true);
Chris Lattner961e7422008-01-01 01:12:31 +00001014 }
Chris Lattner961e7422008-01-01 01:12:31 +00001015 }
1016}
1017
1018/// RemoveOperand - Erase an operand from an instruction, leaving it with one
1019/// fewer operand than it started with.
1020///
1021void MachineInstr::RemoveOperand(unsigned OpNo) {
Jakob Stoklund Olesenb0894832012-12-22 17:13:06 +00001022 assert(OpNo < getNumOperands() && "Invalid operand number");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001023 untieRegOperand(OpNo);
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001024
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001025#ifndef NDEBUG
1026 // Moving tied operands would break the ties.
Jakob Stoklund Olesenb0894832012-12-22 17:13:06 +00001027 for (unsigned i = OpNo + 1, e = getNumOperands(); i != e; ++i)
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001028 if (Operands[i].isReg())
1029 assert(!Operands[i].isTied() && "Cannot move tied operands");
1030#endif
1031
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +00001032 MachineRegisterInfo *MRI = getRegInfo();
1033 if (MRI && Operands[OpNo].isReg())
1034 MRI->removeRegOperandFromUseList(Operands + OpNo);
Chris Lattner961e7422008-01-01 01:12:31 +00001035
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +00001036 // Don't call the MachineOperand destructor. A lot of this code depends on
1037 // MachineOperand having a trivial destructor anyway, and adding a call here
1038 // wouldn't make it 'destructor-correct'.
1039
1040 if (unsigned N = NumOperands - 1 - OpNo)
1041 moveOperands(Operands + OpNo, Operands + OpNo + 1, N, MRI);
1042 --NumOperands;
Chris Lattner961e7422008-01-01 01:12:31 +00001043}
1044
Dan Gohman48b185d2009-09-25 20:36:54 +00001045/// addMemOperand - Add a MachineMemOperand to the machine instruction.
1046/// This function should be used only occasionally. The setMemRefs function
1047/// is the primary method for setting up a MachineInstr's MemRefs list.
Dan Gohman3b460302008-07-07 23:14:23 +00001048void MachineInstr::addMemOperand(MachineFunction &MF,
Dan Gohman48b185d2009-09-25 20:36:54 +00001049 MachineMemOperand *MO) {
1050 mmo_iterator OldMemRefs = MemRefs;
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +00001051 unsigned OldNumMemRefs = NumMemRefs;
Dan Gohman3b460302008-07-07 23:14:23 +00001052
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +00001053 unsigned NewNum = NumMemRefs + 1;
Dan Gohman48b185d2009-09-25 20:36:54 +00001054 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum);
Dan Gohman3b460302008-07-07 23:14:23 +00001055
Benjamin Kramerd03878b2012-03-16 16:39:27 +00001056 std::copy(OldMemRefs, OldMemRefs + OldNumMemRefs, NewMemRefs);
Dan Gohman48b185d2009-09-25 20:36:54 +00001057 NewMemRefs[NewNum - 1] = MO;
Jakob Stoklund Olesen5adc4a12013-01-07 23:21:41 +00001058 setMemRefs(NewMemRefs, NewMemRefs + NewNum);
Dan Gohman48b185d2009-09-25 20:36:54 +00001059}
Chris Lattner961e7422008-01-01 01:12:31 +00001060
Philip Reames5eb90a72016-01-06 19:33:12 +00001061/// Check to see if the MMOs pointed to by the two MemRefs arrays are
Junmo Park820e3922016-02-26 02:07:36 +00001062/// identical.
Philip Reames5eb90a72016-01-06 19:33:12 +00001063static bool hasIdenticalMMOs(const MachineInstr &MI1, const MachineInstr &MI2) {
1064 auto I1 = MI1.memoperands_begin(), E1 = MI1.memoperands_end();
1065 auto I2 = MI2.memoperands_begin(), E2 = MI2.memoperands_end();
1066 if ((E1 - I1) != (E2 - I2))
1067 return false;
1068 for (; I1 != E1; ++I1, ++I2) {
1069 if (**I1 != **I2)
1070 return false;
1071 }
1072 return true;
1073}
1074
Philip Reamesc86ed002016-01-06 04:39:03 +00001075std::pair<MachineInstr::mmo_iterator, unsigned>
1076MachineInstr::mergeMemRefsWith(const MachineInstr& Other) {
Philip Reames5eb90a72016-01-06 19:33:12 +00001077
1078 // If either of the incoming memrefs are empty, we must be conservative and
1079 // treat this as if we've exhausted our space for memrefs and dropped them.
1080 if (memoperands_empty() || Other.memoperands_empty())
1081 return std::make_pair(nullptr, 0);
1082
1083 // If both instructions have identical memrefs, we don't need to merge them.
1084 // Since many instructions have a single memref, and we tend to merge things
1085 // like pairs of loads from the same location, this catches a large number of
1086 // cases in practice.
1087 if (hasIdenticalMMOs(*this, Other))
1088 return std::make_pair(MemRefs, NumMemRefs);
Junmo Park820e3922016-02-26 02:07:36 +00001089
Philip Reamesc86ed002016-01-06 04:39:03 +00001090 // TODO: consider uniquing elements within the operand lists to reduce
1091 // space usage and fall back to conservative information less often.
Philip Reames5eb90a72016-01-06 19:33:12 +00001092 size_t CombinedNumMemRefs = NumMemRefs + Other.NumMemRefs;
1093
1094 // If we don't have enough room to store this many memrefs, be conservative
1095 // and drop them. Otherwise, we'd fail asserts when trying to add them to
1096 // the new instruction.
1097 if (CombinedNumMemRefs != uint8_t(CombinedNumMemRefs))
1098 return std::make_pair(nullptr, 0);
Philip Reamesc86ed002016-01-06 04:39:03 +00001099
Justin Bognerfdf9bf42017-10-10 23:50:49 +00001100 MachineFunction *MF = getMF();
Philip Reamesc86ed002016-01-06 04:39:03 +00001101 mmo_iterator MemBegin = MF->allocateMemRefsArray(CombinedNumMemRefs);
1102 mmo_iterator MemEnd = std::copy(memoperands_begin(), memoperands_end(),
1103 MemBegin);
1104 MemEnd = std::copy(Other.memoperands_begin(), Other.memoperands_end(),
1105 MemEnd);
Philip Reames2d2fc4a2016-01-06 05:53:09 +00001106 assert(MemEnd - MemBegin == (ptrdiff_t)CombinedNumMemRefs &&
1107 "missing memrefs");
Junmo Park820e3922016-02-26 02:07:36 +00001108
Philip Reamesc86ed002016-01-06 04:39:03 +00001109 return std::make_pair(MemBegin, CombinedNumMemRefs);
1110}
1111
Benjamin Kramer97f889f2012-03-17 17:03:45 +00001112bool MachineInstr::hasPropertyInBundle(unsigned Mask, QueryType Type) const {
Jakob Stoklund Olesenf0615c72013-01-10 18:42:44 +00001113 assert(!isBundledWithPred() && "Must be called on bundle header");
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001114 for (MachineBasicBlock::const_instr_iterator MII = getIterator();; ++MII) {
Benjamin Kramer97f889f2012-03-17 17:03:45 +00001115 if (MII->getDesc().getFlags() & Mask) {
Evan Chengcdf89fd2011-12-08 19:23:10 +00001116 if (Type == AnyInBundle)
Evan Cheng7f8e5632011-12-07 07:15:52 +00001117 return true;
1118 } else {
Jakob Stoklund Olesen55a7be22013-01-10 01:29:42 +00001119 if (Type == AllInBundle && !MII->isBundle())
Evan Cheng7f8e5632011-12-07 07:15:52 +00001120 return false;
1121 }
Jakob Stoklund Olesen55a7be22013-01-10 01:29:42 +00001122 // This was the last instruction in the bundle.
1123 if (!MII->isBundledWithSucc())
1124 return Type == AllInBundle;
Evan Cheng2a81dd42011-12-06 22:12:01 +00001125 }
Evan Cheng2a81dd42011-12-06 22:12:01 +00001126}
1127
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001128bool MachineInstr::isIdenticalTo(const MachineInstr &Other,
Evan Chenge9c46c22010-03-03 01:44:33 +00001129 MICheckType Check) const {
Evan Cheng0f260e12010-03-03 21:54:14 +00001130 // If opcodes or number of operands are not the same then the two
1131 // instructions are obviously not identical.
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001132 if (Other.getOpcode() != getOpcode() ||
1133 Other.getNumOperands() != getNumOperands())
Evan Cheng0f260e12010-03-03 21:54:14 +00001134 return false;
1135
Evan Cheng7fae11b2011-12-14 02:11:42 +00001136 if (isBundle()) {
Bjorn Petterssonb29a15e2016-12-19 11:20:57 +00001137 // We have passed the test above that both instructions have the same
1138 // opcode, so we know that both instructions are bundles here. Let's compare
1139 // MIs inside the bundle.
1140 assert(Other.isBundle() && "Expected that both instructions are bundles.");
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001141 MachineBasicBlock::const_instr_iterator I1 = getIterator();
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001142 MachineBasicBlock::const_instr_iterator I2 = Other.getIterator();
Bjorn Petterssonb29a15e2016-12-19 11:20:57 +00001143 // Loop until we analysed the last intruction inside at least one of the
1144 // bundles.
1145 while (I1->isBundledWithSucc() && I2->isBundledWithSucc()) {
1146 ++I1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00001147 ++I2;
Bjorn Petterssonb29a15e2016-12-19 11:20:57 +00001148 if (!I1->isIdenticalTo(*I2, Check))
Evan Cheng7fae11b2011-12-14 02:11:42 +00001149 return false;
1150 }
Bjorn Petterssonb29a15e2016-12-19 11:20:57 +00001151 // If we've reached the end of just one of the two bundles, but not both,
1152 // the instructions are not identical.
1153 if (I1->isBundledWithSucc() || I2->isBundledWithSucc())
1154 return false;
Evan Cheng7fae11b2011-12-14 02:11:42 +00001155 }
1156
Evan Cheng0f260e12010-03-03 21:54:14 +00001157 // Check operands to make sure they match.
1158 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1159 const MachineOperand &MO = getOperand(i);
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001160 const MachineOperand &OMO = Other.getOperand(i);
Evan Chengcfdf3392011-05-12 00:56:58 +00001161 if (!MO.isReg()) {
1162 if (!MO.isIdenticalTo(OMO))
1163 return false;
1164 continue;
1165 }
1166
Evan Cheng0f260e12010-03-03 21:54:14 +00001167 // Clients may or may not want to ignore defs when testing for equality.
1168 // For example, machine CSE pass only cares about finding common
1169 // subexpressions, so it's safe to ignore virtual register defs.
Evan Chengcfdf3392011-05-12 00:56:58 +00001170 if (MO.isDef()) {
Evan Cheng0f260e12010-03-03 21:54:14 +00001171 if (Check == IgnoreDefs)
1172 continue;
Evan Chengcfdf3392011-05-12 00:56:58 +00001173 else if (Check == IgnoreVRegDefs) {
Diana Picus4a5f5222017-10-12 13:59:51 +00001174 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg()) ||
1175 !TargetRegisterInfo::isVirtualRegister(OMO.getReg()))
1176 if (!MO.isIdenticalTo(OMO))
Evan Chengcfdf3392011-05-12 00:56:58 +00001177 return false;
1178 } else {
1179 if (!MO.isIdenticalTo(OMO))
Evan Cheng0f260e12010-03-03 21:54:14 +00001180 return false;
Evan Chengcfdf3392011-05-12 00:56:58 +00001181 if (Check == CheckKillDead && MO.isDead() != OMO.isDead())
1182 return false;
1183 }
1184 } else {
1185 if (!MO.isIdenticalTo(OMO))
1186 return false;
1187 if (Check == CheckKillDead && MO.isKill() != OMO.isKill())
1188 return false;
1189 }
Evan Cheng0f260e12010-03-03 21:54:14 +00001190 }
Devang Patelbf8cc602011-07-07 17:45:33 +00001191 // If DebugLoc does not match then two dbg.values are not identical.
1192 if (isDebugValue())
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001193 if (getDebugLoc() && Other.getDebugLoc() &&
1194 getDebugLoc() != Other.getDebugLoc())
Devang Patelbf8cc602011-07-07 17:45:33 +00001195 return false;
Evan Cheng0f260e12010-03-03 21:54:14 +00001196 return true;
Evan Chenge9c46c22010-03-03 01:44:33 +00001197}
1198
Justin Bognerec7cba52017-10-10 23:34:01 +00001199const MachineFunction *MachineInstr::getMF() const {
1200 return getParent()->getParent();
1201}
1202
Chris Lattnerbec79b42006-04-17 21:35:41 +00001203MachineInstr *MachineInstr::removeFromParent() {
1204 assert(getParent() && "Not embedded in a basic block!");
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001205 return getParent()->remove(this);
Chris Lattnerbec79b42006-04-17 21:35:41 +00001206}
1207
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001208MachineInstr *MachineInstr::removeFromBundle() {
1209 assert(getParent() && "Not embedded in a basic block!");
1210 return getParent()->remove_instr(this);
1211}
Chris Lattnerbec79b42006-04-17 21:35:41 +00001212
Dan Gohman3b460302008-07-07 23:14:23 +00001213void MachineInstr::eraseFromParent() {
1214 assert(getParent() && "Not embedded in a basic block!");
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001215 getParent()->erase(this);
Dan Gohman3b460302008-07-07 23:14:23 +00001216}
1217
Gerolf Hoflehnercaa8bfd2014-08-13 21:15:23 +00001218void MachineInstr::eraseFromParentAndMarkDBGValuesForRemoval() {
1219 assert(getParent() && "Not embedded in a basic block!");
1220 MachineBasicBlock *MBB = getParent();
1221 MachineFunction *MF = MBB->getParent();
1222 assert(MF && "Not embedded in a function!");
1223
1224 MachineInstr *MI = (MachineInstr *)this;
1225 MachineRegisterInfo &MRI = MF->getRegInfo();
1226
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001227 for (const MachineOperand &MO : MI->operands()) {
Gerolf Hoflehnercaa8bfd2014-08-13 21:15:23 +00001228 if (!MO.isReg() || !MO.isDef())
1229 continue;
1230 unsigned Reg = MO.getReg();
1231 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1232 continue;
1233 MRI.markUsesInDebugValueAsUndef(Reg);
1234 }
1235 MI->eraseFromParent();
1236}
1237
Jakob Stoklund Olesenccfb5fb2012-12-17 23:55:38 +00001238void MachineInstr::eraseFromBundle() {
1239 assert(getParent() && "Not embedded in a basic block!");
1240 getParent()->erase_instr(this);
1241}
Dan Gohman3b460302008-07-07 23:14:23 +00001242
Evan Cheng4d728b02007-05-15 01:26:09 +00001243/// getNumExplicitOperands - Returns the number of non-implicit operands.
1244///
1245unsigned MachineInstr::getNumExplicitOperands() const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00001246 unsigned NumOperands = MCID->getNumOperands();
1247 if (!MCID->isVariadic())
Evan Cheng4d728b02007-05-15 01:26:09 +00001248 return NumOperands;
1249
Dan Gohman37608532009-04-15 17:59:11 +00001250 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) {
1251 const MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001252 if (!MO.isReg() || !MO.isImplicit())
Evan Cheng4d728b02007-05-15 01:26:09 +00001253 NumOperands++;
1254 }
1255 return NumOperands;
1256}
1257
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001258void MachineInstr::bundleWithPred() {
1259 assert(!isBundledWithPred() && "MI is already bundled with its predecessor");
1260 setFlag(BundledPred);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001261 MachineBasicBlock::instr_iterator Pred = getIterator();
1262 --Pred;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001263 assert(!Pred->isBundledWithSucc() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001264 Pred->setFlag(BundledSucc);
1265}
1266
1267void MachineInstr::bundleWithSucc() {
1268 assert(!isBundledWithSucc() && "MI is already bundled with its successor");
1269 setFlag(BundledSucc);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001270 MachineBasicBlock::instr_iterator Succ = getIterator();
1271 ++Succ;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001272 assert(!Succ->isBundledWithPred() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001273 Succ->setFlag(BundledPred);
1274}
1275
1276void MachineInstr::unbundleFromPred() {
1277 assert(isBundledWithPred() && "MI isn't bundled with its predecessor");
1278 clearFlag(BundledPred);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001279 MachineBasicBlock::instr_iterator Pred = getIterator();
1280 --Pred;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001281 assert(Pred->isBundledWithSucc() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001282 Pred->clearFlag(BundledSucc);
1283}
1284
1285void MachineInstr::unbundleFromSucc() {
1286 assert(isBundledWithSucc() && "MI isn't bundled with its successor");
1287 clearFlag(BundledSucc);
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001288 MachineBasicBlock::instr_iterator Succ = getIterator();
1289 ++Succ;
Jakob Stoklund Olesen00f6c772012-12-18 23:00:28 +00001290 assert(Succ->isBundledWithPred() && "Inconsistent bundle flags");
Jakob Stoklund Olesenfead62d2012-12-07 04:23:29 +00001291 Succ->clearFlag(BundledPred);
1292}
1293
Evan Cheng6eb516d2011-01-07 23:50:32 +00001294bool MachineInstr::isStackAligningInlineAsm() const {
1295 if (isInlineAsm()) {
1296 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1297 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
1298 return true;
1299 }
1300 return false;
1301}
Chris Lattner33f5af02006-10-20 22:39:59 +00001302
Chad Rosier994f4042012-09-05 21:00:58 +00001303InlineAsm::AsmDialect MachineInstr::getInlineAsmDialect() const {
1304 assert(isInlineAsm() && "getInlineAsmDialect() only works for inline asms!");
1305 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
Chad Rosiere53314f2012-09-05 22:40:13 +00001306 return InlineAsm::AsmDialect((ExtraInfo & InlineAsm::Extra_AsmDialect) != 0);
Chad Rosier994f4042012-09-05 21:00:58 +00001307}
1308
Jakob Stoklund Olesen1e737162011-10-12 23:37:33 +00001309int MachineInstr::findInlineAsmFlagIdx(unsigned OpIdx,
1310 unsigned *GroupNo) const {
1311 assert(isInlineAsm() && "Expected an inline asm instruction");
1312 assert(OpIdx < getNumOperands() && "OpIdx out of range");
1313
1314 // Ignore queries about the initial operands.
1315 if (OpIdx < InlineAsm::MIOp_FirstOperand)
1316 return -1;
1317
1318 unsigned Group = 0;
1319 unsigned NumOps;
1320 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
1321 i += NumOps) {
1322 const MachineOperand &FlagMO = getOperand(i);
1323 // If we reach the implicit register operands, stop looking.
1324 if (!FlagMO.isImm())
1325 return -1;
1326 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
1327 if (i + NumOps > OpIdx) {
1328 if (GroupNo)
1329 *GroupNo = Group;
1330 return i;
1331 }
1332 ++Group;
1333 }
1334 return -1;
1335}
1336
Reid Kleckner28865802016-04-14 18:29:59 +00001337const DILocalVariable *MachineInstr::getDebugVariable() const {
1338 assert(isDebugValue() && "not a DBG_VALUE");
1339 return cast<DILocalVariable>(getOperand(2).getMetadata());
1340}
1341
1342const DIExpression *MachineInstr::getDebugExpression() const {
1343 assert(isDebugValue() && "not a DBG_VALUE");
1344 return cast<DIExpression>(getOperand(3).getMetadata());
1345}
1346
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001347const TargetRegisterClass*
1348MachineInstr::getRegClassConstraint(unsigned OpIdx,
1349 const TargetInstrInfo *TII,
1350 const TargetRegisterInfo *TRI) const {
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001351 assert(getParent() && "Can't have an MBB reference here!");
Justin Bognerfdf9bf42017-10-10 23:50:49 +00001352 assert(getMF() && "Can't have an MF reference here!");
1353 const MachineFunction &MF = *getMF();
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001354
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001355 // Most opcodes have fixed constraints in their MCInstrDesc.
1356 if (!isInlineAsm())
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001357 return TII->getRegClass(getDesc(), OpIdx, TRI, MF);
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001358
1359 if (!getOperand(OpIdx).isReg())
Craig Topperc0196b12014-04-14 00:51:57 +00001360 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001361
1362 // For tied uses on inline asm, get the constraint from the def.
1363 unsigned DefIdx;
1364 if (getOperand(OpIdx).isUse() && isRegTiedToDefOperand(OpIdx, &DefIdx))
1365 OpIdx = DefIdx;
1366
1367 // Inline asm stores register class constraints in the flag word.
1368 int FlagIdx = findInlineAsmFlagIdx(OpIdx);
1369 if (FlagIdx < 0)
Craig Topperc0196b12014-04-14 00:51:57 +00001370 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001371
1372 unsigned Flag = getOperand(FlagIdx).getImm();
1373 unsigned RCID;
Simon Dardisd32a2d32016-07-18 13:17:31 +00001374 if ((InlineAsm::getKind(Flag) == InlineAsm::Kind_RegUse ||
1375 InlineAsm::getKind(Flag) == InlineAsm::Kind_RegDef ||
1376 InlineAsm::getKind(Flag) == InlineAsm::Kind_RegDefEarlyClobber) &&
1377 InlineAsm::hasRegClassConstraint(Flag, RCID))
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001378 return TRI->getRegClass(RCID);
1379
1380 // Assume that all registers in a memory operand are pointers.
1381 if (InlineAsm::getKind(Flag) == InlineAsm::Kind_Mem)
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001382 return TRI->getPointerRegClass(MF);
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001383
Craig Topperc0196b12014-04-14 00:51:57 +00001384 return nullptr;
Jakob Stoklund Olesen35b362f2011-10-12 23:37:36 +00001385}
1386
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001387const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVReg(
1388 unsigned Reg, const TargetRegisterClass *CurRC, const TargetInstrInfo *TII,
1389 const TargetRegisterInfo *TRI, bool ExploreBundle) const {
1390 // Check every operands inside the bundle if we have
1391 // been asked to.
1392 if (ExploreBundle)
Duncan P. N. Exon Smithf9ab4162016-02-27 17:05:33 +00001393 for (ConstMIBundleOperands OpndIt(*this); OpndIt.isValid() && CurRC;
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001394 ++OpndIt)
1395 CurRC = OpndIt->getParent()->getRegClassConstraintEffectForVRegImpl(
1396 OpndIt.getOperandNo(), Reg, CurRC, TII, TRI);
1397 else
1398 // Otherwise, just check the current operands.
Matthias Braune41e1462015-05-29 02:56:46 +00001399 for (unsigned i = 0, e = NumOperands; i < e && CurRC; ++i)
1400 CurRC = getRegClassConstraintEffectForVRegImpl(i, Reg, CurRC, TII, TRI);
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001401 return CurRC;
1402}
1403
1404const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVRegImpl(
1405 unsigned OpIdx, unsigned Reg, const TargetRegisterClass *CurRC,
1406 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
1407 assert(CurRC && "Invalid initial register class");
1408 // Check if Reg is constrained by some of its use/def from MI.
1409 const MachineOperand &MO = getOperand(OpIdx);
1410 if (!MO.isReg() || MO.getReg() != Reg)
1411 return CurRC;
1412 // If yes, accumulate the constraints through the operand.
1413 return getRegClassConstraintEffect(OpIdx, CurRC, TII, TRI);
1414}
1415
1416const TargetRegisterClass *MachineInstr::getRegClassConstraintEffect(
1417 unsigned OpIdx, const TargetRegisterClass *CurRC,
1418 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const {
1419 const TargetRegisterClass *OpRC = getRegClassConstraint(OpIdx, TII, TRI);
1420 const MachineOperand &MO = getOperand(OpIdx);
1421 assert(MO.isReg() &&
1422 "Cannot get register constraints for non-register operand");
1423 assert(CurRC && "Invalid initial register class");
1424 if (unsigned SubIdx = MO.getSubReg()) {
1425 if (OpRC)
1426 CurRC = TRI->getMatchingSuperRegClass(CurRC, OpRC, SubIdx);
1427 else
1428 CurRC = TRI->getSubClassWithSubReg(CurRC, SubIdx);
1429 } else if (OpRC)
1430 CurRC = TRI->getCommonSubClass(CurRC, OpRC);
1431 return CurRC;
1432}
1433
Jakob Stoklund Olesen68d752b2013-01-09 18:28:16 +00001434/// Return the number of instructions inside the MI bundle, not counting the
1435/// header instruction.
Evan Cheng7fae11b2011-12-14 02:11:42 +00001436unsigned MachineInstr::getBundleSize() const {
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +00001437 MachineBasicBlock::const_instr_iterator I = getIterator();
Evan Cheng7fae11b2011-12-14 02:11:42 +00001438 unsigned Size = 0;
Richard Trieu7a083812016-02-18 22:09:30 +00001439 while (I->isBundledWithSucc()) {
1440 ++Size;
1441 ++I;
1442 }
Evan Cheng7fae11b2011-12-14 02:11:42 +00001443 return Size;
1444}
1445
Nicolai Haehnleb0c97482016-04-22 04:04:08 +00001446/// Returns true if the MachineInstr has an implicit-use operand of exactly
1447/// the given register (not considering sub/super-registers).
1448bool MachineInstr::hasRegisterImplicitUseOperand(unsigned Reg) const {
1449 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1450 const MachineOperand &MO = getOperand(i);
1451 if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == Reg)
1452 return true;
1453 }
1454 return false;
1455}
1456
Evan Cheng910c8082007-04-26 19:00:32 +00001457/// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of
Jim Grosbach9632c142009-09-17 17:57:26 +00001458/// the specific register or -1 if it is not found. It further tightens
Evan Cheng9965aeb2007-02-23 01:04:26 +00001459/// the search criteria to a use that kills the register if isKill is true.
Fraser Cormack48d9fdc2016-10-11 09:09:21 +00001460int MachineInstr::findRegisterUseOperandIdx(
1461 unsigned Reg, bool isKill, const TargetRegisterInfo *TRI) const {
Evan Cheng75c21942006-12-06 08:27:42 +00001462 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng5983bdb2007-05-29 18:35:22 +00001463 const MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001464 if (!MO.isReg() || !MO.isUse())
Evan Cheng63254462008-03-05 00:59:57 +00001465 continue;
1466 unsigned MOReg = MO.getReg();
1467 if (!MOReg)
1468 continue;
Fraser Cormack48d9fdc2016-10-11 09:09:21 +00001469 if (MOReg == Reg || (TRI && TargetRegisterInfo::isPhysicalRegister(MOReg) &&
1470 TargetRegisterInfo::isPhysicalRegister(Reg) &&
1471 TRI->isSubRegister(MOReg, Reg)))
Evan Cheng9965aeb2007-02-23 01:04:26 +00001472 if (!isKill || MO.isKill())
Evan Chengec3ac312007-03-26 22:37:45 +00001473 return i;
Evan Cheng75c21942006-12-06 08:27:42 +00001474 }
Evan Chengec3ac312007-03-26 22:37:45 +00001475 return -1;
Evan Cheng75c21942006-12-06 08:27:42 +00001476}
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001477
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001478/// readsWritesVirtualRegister - Return a pair of bools (reads, writes)
1479/// indicating if this instruction reads or writes Reg. This also considers
1480/// partial defines.
1481std::pair<bool,bool>
1482MachineInstr::readsWritesVirtualRegister(unsigned Reg,
1483 SmallVectorImpl<unsigned> *Ops) const {
1484 bool PartDef = false; // Partial redefine.
1485 bool FullDef = false; // Full define.
1486 bool Use = false;
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001487
1488 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1489 const MachineOperand &MO = getOperand(i);
1490 if (!MO.isReg() || MO.getReg() != Reg)
1491 continue;
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001492 if (Ops)
1493 Ops->push_back(i);
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001494 if (MO.isUse())
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001495 Use |= !MO.isUndef();
Jakob Stoklund Olesen9eb77bf2011-08-19 00:30:17 +00001496 else if (MO.getSubReg() && !MO.isUndef())
1497 // A partial <def,undef> doesn't count as reading the register.
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001498 PartDef = true;
1499 else
1500 FullDef = true;
1501 }
Jakob Stoklund Olesen7d7f6042010-05-21 20:02:01 +00001502 // A partial redefine uses Reg unless there is also a full define.
1503 return std::make_pair(Use || (PartDef && !FullDef), PartDef || FullDef);
Jakob Stoklund Olesen5d4c1342010-05-19 20:36:22 +00001504}
1505
Evan Cheng63254462008-03-05 00:59:57 +00001506/// findRegisterDefOperandIdx() - Returns the operand index that is a def of
Dan Gohman72a0bc12008-05-06 00:20:10 +00001507/// the specified register or -1 if it is not found. If isDead is true, defs
1508/// that are not dead are skipped. If TargetRegisterInfo is non-null, then it
1509/// also checks if there is a def of a super-register.
Evan Cheng38584512010-05-21 20:53:24 +00001510int
1511MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, bool Overlap,
1512 const TargetRegisterInfo *TRI) const {
1513 bool isPhys = TargetRegisterInfo::isPhysicalRegister(Reg);
Evan Chengf7ed82d2007-02-19 21:49:54 +00001514 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Evan Cheng63254462008-03-05 00:59:57 +00001515 const MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesene7d3f442012-02-14 23:49:37 +00001516 // Accept regmask operands when Overlap is set.
1517 // Ignore them when looking for a specific def operand (Overlap == false).
1518 if (isPhys && Overlap && MO.isRegMask() && MO.clobbersPhysReg(Reg))
1519 return i;
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001520 if (!MO.isReg() || !MO.isDef())
Evan Cheng63254462008-03-05 00:59:57 +00001521 continue;
1522 unsigned MOReg = MO.getReg();
Evan Cheng38584512010-05-21 20:53:24 +00001523 bool Found = (MOReg == Reg);
1524 if (!Found && TRI && isPhys &&
1525 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
1526 if (Overlap)
1527 Found = TRI->regsOverlap(MOReg, Reg);
1528 else
1529 Found = TRI->isSubRegister(MOReg, Reg);
1530 }
1531 if (Found && (!isDead || MO.isDead()))
1532 return i;
Evan Chengf7ed82d2007-02-19 21:49:54 +00001533 }
Evan Cheng63254462008-03-05 00:59:57 +00001534 return -1;
Evan Chengf7ed82d2007-02-19 21:49:54 +00001535}
Evan Cheng4d728b02007-05-15 01:26:09 +00001536
Evan Cheng5983bdb2007-05-29 18:35:22 +00001537/// findFirstPredOperandIdx() - Find the index of the first operand in the
1538/// operand list that is used to represent the predicate. It returns -1 if
1539/// none is found.
1540int MachineInstr::findFirstPredOperandIdx() const {
Jim Grosbached16ec42011-08-29 22:24:09 +00001541 // Don't call MCID.findFirstPredOperandIdx() because this variant
1542 // is sometimes called on an instruction that's not yet complete, and
1543 // so the number of operands is less than the MCID indicates. In
1544 // particular, the PTX target does this.
Evan Cheng6cc775f2011-06-28 19:10:37 +00001545 const MCInstrDesc &MCID = getDesc();
1546 if (MCID.isPredicable()) {
Evan Cheng4d728b02007-05-15 01:26:09 +00001547 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Evan Cheng6cc775f2011-06-28 19:10:37 +00001548 if (MCID.OpInfo[i].isPredicate())
Evan Cheng5983bdb2007-05-29 18:35:22 +00001549 return i;
Evan Cheng4d728b02007-05-15 01:26:09 +00001550 }
1551
Evan Cheng5983bdb2007-05-29 18:35:22 +00001552 return -1;
Evan Cheng4d728b02007-05-15 01:26:09 +00001553}
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001554
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001555// MachineOperand::TiedTo is 4 bits wide.
1556const unsigned TiedMax = 15;
1557
1558/// tieOperands - Mark operands at DefIdx and UseIdx as tied to each other.
1559///
1560/// Use and def operands can be tied together, indicated by a non-zero TiedTo
1561/// field. TiedTo can have these values:
1562///
1563/// 0: Operand is not tied to anything.
1564/// 1 to TiedMax-1: Tied to getOperand(TiedTo-1).
1565/// TiedMax: Tied to an operand >= TiedMax-1.
1566///
1567/// The tied def must be one of the first TiedMax operands on a normal
1568/// instruction. INLINEASM instructions allow more tied defs.
1569///
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001570void MachineInstr::tieOperands(unsigned DefIdx, unsigned UseIdx) {
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001571 MachineOperand &DefMO = getOperand(DefIdx);
1572 MachineOperand &UseMO = getOperand(UseIdx);
1573 assert(DefMO.isDef() && "DefIdx must be a def operand");
1574 assert(UseMO.isUse() && "UseIdx must be a use operand");
1575 assert(!DefMO.isTied() && "Def is already tied to another use");
1576 assert(!UseMO.isTied() && "Use is already tied to another def");
1577
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001578 if (DefIdx < TiedMax)
1579 UseMO.TiedTo = DefIdx + 1;
1580 else {
1581 // Inline asm can use the group descriptors to find tied operands, but on
1582 // normal instruction, the tied def must be within the first TiedMax
1583 // operands.
1584 assert(isInlineAsm() && "DefIdx out of range");
1585 UseMO.TiedTo = TiedMax;
1586 }
1587
1588 // UseIdx can be out of range, we'll search for it in findTiedOperandIdx().
1589 DefMO.TiedTo = std::min(UseIdx + 1, TiedMax);
Jakob Stoklund Olesen5c8eda02012-08-31 20:50:53 +00001590}
1591
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001592/// Given the index of a tied register operand, find the operand it is tied to.
1593/// Defs are tied to uses and vice versa. Returns the index of the tied operand
1594/// which must exist.
1595unsigned MachineInstr::findTiedOperandIdx(unsigned OpIdx) const {
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001596 const MachineOperand &MO = getOperand(OpIdx);
1597 assert(MO.isTied() && "Operand isn't tied");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001598
Jakob Stoklund Olesen0a09da82012-09-04 18:36:28 +00001599 // Normally TiedTo is in range.
1600 if (MO.TiedTo < TiedMax)
1601 return MO.TiedTo - 1;
1602
1603 // Uses on normal instructions can be out of range.
1604 if (!isInlineAsm()) {
1605 // Normal tied defs must be in the 0..TiedMax-1 range.
1606 if (MO.isUse())
1607 return TiedMax - 1;
1608 // MO is a def. Search for the tied use.
1609 for (unsigned i = TiedMax - 1, e = getNumOperands(); i != e; ++i) {
1610 const MachineOperand &UseMO = getOperand(i);
1611 if (UseMO.isReg() && UseMO.isUse() && UseMO.TiedTo == OpIdx + 1)
1612 return i;
1613 }
1614 llvm_unreachable("Can't find tied use");
1615 }
1616
1617 // Now deal with inline asm by parsing the operand group descriptor flags.
1618 // Find the beginning of each operand group.
1619 SmallVector<unsigned, 8> GroupIdx;
1620 unsigned OpIdxGroup = ~0u;
1621 unsigned NumOps;
1622 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e;
1623 i += NumOps) {
1624 const MachineOperand &FlagMO = getOperand(i);
1625 assert(FlagMO.isImm() && "Invalid tied operand on inline asm");
1626 unsigned CurGroup = GroupIdx.size();
1627 GroupIdx.push_back(i);
1628 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm());
1629 // OpIdx belongs to this operand group.
1630 if (OpIdx > i && OpIdx < i + NumOps)
1631 OpIdxGroup = CurGroup;
1632 unsigned TiedGroup;
1633 if (!InlineAsm::isUseOperandTiedToDef(FlagMO.getImm(), TiedGroup))
1634 continue;
1635 // Operands in this group are tied to operands in TiedGroup which must be
1636 // earlier. Find the number of operands between the two groups.
1637 unsigned Delta = i - GroupIdx[TiedGroup];
1638
1639 // OpIdx is a use tied to TiedGroup.
1640 if (OpIdxGroup == CurGroup)
1641 return OpIdx - Delta;
1642
1643 // OpIdx is a def tied to this use group.
1644 if (OpIdxGroup == TiedGroup)
1645 return OpIdx + Delta;
1646 }
1647 llvm_unreachable("Invalid tied operand on inline asm");
Jakob Stoklund Olesen2b166642012-08-29 00:37:58 +00001648}
1649
Dan Gohmanc90f51c2010-05-13 20:34:42 +00001650/// clearKillInfo - Clears kill flags on all operands.
1651///
1652void MachineInstr::clearKillInfo() {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001653 for (MachineOperand &MO : operands()) {
Dan Gohmanc90f51c2010-05-13 20:34:42 +00001654 if (MO.isReg() && MO.isUse())
1655 MO.setIsKill(false);
1656 }
1657}
1658
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001659void MachineInstr::substituteRegister(unsigned FromReg,
1660 unsigned ToReg,
1661 unsigned SubIdx,
1662 const TargetRegisterInfo &RegInfo) {
1663 if (TargetRegisterInfo::isPhysicalRegister(ToReg)) {
1664 if (SubIdx)
1665 ToReg = RegInfo.getSubReg(ToReg, SubIdx);
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001666 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001667 if (!MO.isReg() || MO.getReg() != FromReg)
1668 continue;
1669 MO.substPhysReg(ToReg, RegInfo);
1670 }
1671 } else {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001672 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001673 if (!MO.isReg() || MO.getReg() != FromReg)
1674 continue;
1675 MO.substVirtReg(ToReg, SubIdx, RegInfo);
1676 }
1677 }
1678}
1679
Evan Cheng7d98a482008-07-03 09:09:37 +00001680/// isSafeToMove - Return true if it is safe to move this instruction. If
1681/// SawStore is set to true, it means that there is a store (or call) between
1682/// the instruction's location and its intended destination.
Matthias Braun07066cc2015-05-19 21:22:20 +00001683bool MachineInstr::isSafeToMove(AliasAnalysis *AA, bool &SawStore) const {
Evan Cheng399e1102008-03-13 00:44:09 +00001684 // Ignore stuff that we obviously can't move.
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001685 //
1686 // Treat volatile loads as stores. This is not strictly necessary for
Jakob Stoklund Olesend92e2bc2012-09-04 18:44:43 +00001687 // volatiles, but it is required for atomic loads. It is not allowed to move
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001688 // a load across an atomic load with Ordering > Monotonic.
1689 if (mayStore() || isCall() ||
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001690 (mayLoad() && hasOrderedMemoryRef())) {
Evan Cheng399e1102008-03-13 00:44:09 +00001691 SawStore = true;
1692 return false;
1693 }
Evan Cheng0638c202011-01-07 21:08:26 +00001694
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001695 if (isPosition() || isDebugValue() || isTerminator() ||
1696 hasUnmodeledSideEffects())
Evan Cheng399e1102008-03-13 00:44:09 +00001697 return false;
1698
1699 // See if this instruction does a load. If so, we have to guarantee that the
1700 // loaded value doesn't change between the load and the its intended
1701 // destination. The check for isInvariantLoad gives the targe the chance to
1702 // classify the load as always returning a constant, e.g. a constant pool
1703 // load.
Justin Lebard98cf002016-09-10 01:03:20 +00001704 if (mayLoad() && !isDereferenceableInvariantLoad(AA))
Evan Cheng399e1102008-03-13 00:44:09 +00001705 // Otherwise, this is a real load. If there is a store between the load and
Jakob Stoklund Olesen813a1092012-08-29 20:48:45 +00001706 // end of block, we can't move it.
1707 return !SawStore;
Dan Gohman7c59ed62008-09-24 00:06:15 +00001708
Evan Cheng399e1102008-03-13 00:44:09 +00001709 return true;
1710}
1711
Eli Friedman93f47e52017-03-09 23:33:36 +00001712bool MachineInstr::mayAlias(AliasAnalysis *AA, MachineInstr &Other,
1713 bool UseTBAA) {
Justin Bognerfdf9bf42017-10-10 23:50:49 +00001714 const MachineFunction *MF = getMF();
Eli Friedman93f47e52017-03-09 23:33:36 +00001715 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
Balaram Makam42adadf2017-08-30 14:57:12 +00001716 const MachineFrameInfo &MFI = MF->getFrameInfo();
Eli Friedman93f47e52017-03-09 23:33:36 +00001717
1718 // If neither instruction stores to memory, they can't alias in any
1719 // meaningful way, even if they read from the same address.
1720 if (!mayStore() && !Other.mayStore())
1721 return false;
1722
1723 // Let the target decide if memory accesses cannot possibly overlap.
1724 if (TII->areMemAccessesTriviallyDisjoint(*this, Other, AA))
1725 return false;
1726
Eli Friedman93f47e52017-03-09 23:33:36 +00001727 // FIXME: Need to handle multiple memory operands to support all targets.
1728 if (!hasOneMemOperand() || !Other.hasOneMemOperand())
1729 return true;
1730
1731 MachineMemOperand *MMOa = *memoperands_begin();
1732 MachineMemOperand *MMOb = *Other.memoperands_begin();
1733
Eli Friedman93f47e52017-03-09 23:33:36 +00001734 // The following interface to AA is fashioned after DAGCombiner::isAlias
1735 // and operates with MachineMemOperand offset with some important
1736 // assumptions:
1737 // - LLVM fundamentally assumes flat address spaces.
1738 // - MachineOperand offset can *only* result from legalization and
1739 // cannot affect queries other than the trivial case of overlap
1740 // checking.
1741 // - These offsets never wrap and never step outside
1742 // of allocated objects.
1743 // - There should never be any negative offsets here.
1744 //
1745 // FIXME: Modify API to hide this math from "user"
Balaram Makam42adadf2017-08-30 14:57:12 +00001746 // Even before we go to AA we can reason locally about some
Eli Friedman93f47e52017-03-09 23:33:36 +00001747 // memory objects. It can save compile time, and possibly catch some
1748 // corner cases not currently covered.
1749
Balaram Makam42adadf2017-08-30 14:57:12 +00001750 int64_t OffsetA = MMOa->getOffset();
1751 int64_t OffsetB = MMOb->getOffset();
Eli Friedman93f47e52017-03-09 23:33:36 +00001752
Balaram Makam42adadf2017-08-30 14:57:12 +00001753 int64_t MinOffset = std::min(OffsetA, OffsetB);
1754 int64_t WidthA = MMOa->getSize();
1755 int64_t WidthB = MMOb->getSize();
1756 const Value *ValA = MMOa->getValue();
1757 const Value *ValB = MMOb->getValue();
1758 bool SameVal = (ValA && ValB && (ValA == ValB));
1759 if (!SameVal) {
1760 const PseudoSourceValue *PSVa = MMOa->getPseudoValue();
1761 const PseudoSourceValue *PSVb = MMOb->getPseudoValue();
1762 if (PSVa && ValB && !PSVa->mayAlias(&MFI))
1763 return false;
1764 if (PSVb && ValA && !PSVb->mayAlias(&MFI))
1765 return false;
1766 if (PSVa && PSVb && (PSVa == PSVb))
1767 SameVal = true;
1768 }
Eli Friedman93f47e52017-03-09 23:33:36 +00001769
Balaram Makam42adadf2017-08-30 14:57:12 +00001770 if (SameVal) {
1771 int64_t MaxOffset = std::max(OffsetA, OffsetB);
1772 int64_t LowWidth = (MinOffset == OffsetA) ? WidthA : WidthB;
1773 return (MinOffset + LowWidth > MaxOffset);
1774 }
1775
1776 if (!AA)
1777 return true;
1778
1779 if (!ValA || !ValB)
1780 return true;
1781
1782 assert((OffsetA >= 0) && "Negative MachineMemOperand offset");
1783 assert((OffsetB >= 0) && "Negative MachineMemOperand offset");
1784
1785 int64_t Overlapa = WidthA + OffsetA - MinOffset;
1786 int64_t Overlapb = WidthB + OffsetB - MinOffset;
1787
1788 AliasResult AAResult = AA->alias(
1789 MemoryLocation(ValA, Overlapa,
1790 UseTBAA ? MMOa->getAAInfo() : AAMDNodes()),
1791 MemoryLocation(ValB, Overlapb,
1792 UseTBAA ? MMOb->getAAInfo() : AAMDNodes()));
Eli Friedman93f47e52017-03-09 23:33:36 +00001793
1794 return (AAResult != NoAlias);
1795}
1796
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001797/// hasOrderedMemoryRef - Return true if this instruction may have an ordered
1798/// or volatile memory reference, or if the information describing the memory
1799/// reference is not available. Return false if it is known to have no ordered
1800/// memory references.
1801bool MachineInstr::hasOrderedMemoryRef() const {
Dan Gohman7c59ed62008-09-24 00:06:15 +00001802 // An instruction known never to access memory won't have a volatile access.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001803 if (!mayStore() &&
1804 !mayLoad() &&
1805 !isCall() &&
Evan Cheng6eb516d2011-01-07 23:50:32 +00001806 !hasUnmodeledSideEffects())
Dan Gohman7c59ed62008-09-24 00:06:15 +00001807 return false;
1808
1809 // Otherwise, if the instruction has no memory reference information,
1810 // conservatively assume it wasn't preserved.
1811 if (memoperands_empty())
1812 return true;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00001813
Justin Lebardede81e2016-07-13 22:35:19 +00001814 // Check if any of our memory operands are ordered.
Eugene Zelenko4e9736b2017-05-31 01:10:10 +00001815 return llvm::any_of(memoperands(), [](const MachineMemOperand *MMO) {
Justin Lebardede81e2016-07-13 22:35:19 +00001816 return !MMO->isUnordered();
1817 });
Dan Gohman7c59ed62008-09-24 00:06:15 +00001818}
1819
Justin Lebard98cf002016-09-10 01:03:20 +00001820/// isDereferenceableInvariantLoad - Return true if this instruction will never
1821/// trap and is loading from a location whose value is invariant across a run of
1822/// this function.
1823bool MachineInstr::isDereferenceableInvariantLoad(AliasAnalysis *AA) const {
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001824 // If the instruction doesn't load at all, it isn't an invariant load.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001825 if (!mayLoad())
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001826 return false;
1827
1828 // If the instruction has lost its memoperands, conservatively assume that
1829 // it may not be an invariant load.
1830 if (memoperands_empty())
1831 return false;
1832
Matthias Braun941a7052016-07-28 18:40:00 +00001833 const MachineFrameInfo &MFI = getParent()->getParent()->getFrameInfo();
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001834
Justin Lebardede81e2016-07-13 22:35:19 +00001835 for (MachineMemOperand *MMO : memoperands()) {
1836 if (MMO->isVolatile()) return false;
1837 if (MMO->isStore()) return false;
Justin Lebaradbf09e2016-09-11 01:38:58 +00001838 if (MMO->isInvariant() && MMO->isDereferenceable())
1839 continue;
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001840
1841 // A load from a constant PseudoSourceValue is invariant.
Justin Lebardede81e2016-07-13 22:35:19 +00001842 if (const PseudoSourceValue *PSV = MMO->getPseudoValue())
Matthias Braun941a7052016-07-28 18:40:00 +00001843 if (PSV->isConstant(&MFI))
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001844 continue;
1845
Justin Lebardede81e2016-07-13 22:35:19 +00001846 if (const Value *V = MMO->getValue()) {
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001847 // If we have an AliasAnalysis, ask it whether the memory is constant.
Chandler Carruthac80dc72015-06-17 07:18:54 +00001848 if (AA &&
1849 AA->pointsToConstantMemory(
Justin Lebardede81e2016-07-13 22:35:19 +00001850 MemoryLocation(V, MMO->getSize(), MMO->getAAInfo())))
Dan Gohmanbe8137b2009-10-07 17:38:06 +00001851 continue;
1852 }
1853
1854 // Otherwise assume conservatively.
1855 return false;
1856 }
1857
1858 // Everything checks out.
1859 return true;
1860}
1861
Evan Cheng71453822009-12-03 02:31:43 +00001862/// isConstantValuePHI - If the specified instruction is a PHI that always
1863/// merges together the same virtual register, return the register, otherwise
1864/// return 0.
1865unsigned MachineInstr::isConstantValuePHI() const {
Chris Lattnerb06015a2010-02-09 19:54:29 +00001866 if (!isPHI())
Evan Cheng71453822009-12-03 02:31:43 +00001867 return 0;
Evan Cheng5c668a22009-12-07 23:10:34 +00001868 assert(getNumOperands() >= 3 &&
1869 "It's illegal to have a PHI without source operands");
Evan Cheng71453822009-12-03 02:31:43 +00001870
1871 unsigned Reg = getOperand(1).getReg();
1872 for (unsigned i = 3, e = getNumOperands(); i < e; i += 2)
1873 if (getOperand(i).getReg() != Reg)
1874 return 0;
1875 return Reg;
1876}
1877
Evan Cheng6eb516d2011-01-07 23:50:32 +00001878bool MachineInstr::hasUnmodeledSideEffects() const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00001879 if (hasProperty(MCID::UnmodeledSideEffects))
Evan Cheng6eb516d2011-01-07 23:50:32 +00001880 return true;
1881 if (isInlineAsm()) {
1882 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
1883 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
1884 return true;
1885 }
1886
1887 return false;
1888}
1889
Michael Kupersteinbc7f99a2015-08-12 10:14:58 +00001890bool MachineInstr::isLoadFoldBarrier() const {
1891 return mayStore() || isCall() || hasUnmodeledSideEffects();
1892}
1893
Evan Chengb083c472010-04-08 20:02:37 +00001894/// allDefsAreDead - Return true if all the defs of this instruction are dead.
1895///
1896bool MachineInstr::allDefsAreDead() const {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00001897 for (const MachineOperand &MO : operands()) {
Evan Chengb083c472010-04-08 20:02:37 +00001898 if (!MO.isReg() || MO.isUse())
1899 continue;
1900 if (!MO.isDead())
1901 return false;
1902 }
1903 return true;
1904}
1905
Evan Cheng21eedfb2010-10-22 21:49:09 +00001906/// copyImplicitOps - Copy implicit register operands from specified
1907/// instruction to this instruction.
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +00001908void MachineInstr::copyImplicitOps(MachineFunction &MF,
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001909 const MachineInstr &MI) {
1910 for (unsigned i = MI.getDesc().getNumOperands(), e = MI.getNumOperands();
Evan Cheng21eedfb2010-10-22 21:49:09 +00001911 i != e; ++i) {
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +00001912 const MachineOperand &MO = MI.getOperand(i);
Lang Hames7c8189c2014-03-17 01:22:54 +00001913 if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask())
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +00001914 addOperand(MF, MO);
Evan Cheng21eedfb2010-10-22 21:49:09 +00001915 }
1916}
1917
Aaron Ballman615eb472017-10-15 14:32:27 +00001918#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Brauna4976c62017-01-29 18:20:42 +00001919LLVM_DUMP_METHOD void MachineInstr::dump() const {
Sebastian Pop77794842016-12-21 01:41:12 +00001920 dbgs() << " ";
Matthias Brauna4976c62017-01-29 18:20:42 +00001921 print(dbgs());
Mon P Wangdfcc1ff2008-10-10 01:43:55 +00001922}
Matthias Braun8c209aa2017-01-28 02:02:38 +00001923#endif
Mon P Wangdfcc1ff2008-10-10 01:43:55 +00001924
Ahmed Bougacha43192242017-02-23 19:17:31 +00001925void MachineInstr::print(raw_ostream &OS, bool SkipOpers, bool SkipDebugLoc,
Sebastian Pop77794842016-12-21 01:41:12 +00001926 const TargetInstrInfo *TII) const {
Duncan P. N. Exon Smithc0374522015-06-26 23:18:44 +00001927 const Module *M = nullptr;
1928 if (const MachineBasicBlock *MBB = getParent())
1929 if (const MachineFunction *MF = MBB->getParent())
1930 M = MF->getFunction()->getParent();
1931
1932 ModuleSlotTracker MST(M);
Ahmed Bougacha43192242017-02-23 19:17:31 +00001933 print(OS, MST, SkipOpers, SkipDebugLoc, TII);
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001934}
1935
1936void MachineInstr::print(raw_ostream &OS, ModuleSlotTracker &MST,
Ahmed Bougacha43192242017-02-23 19:17:31 +00001937 bool SkipOpers, bool SkipDebugLoc,
1938 const TargetInstrInfo *TII) const {
Eric Christopher1cdefae2015-02-27 00:11:34 +00001939 // We can be a bit tidier if we know the MachineFunction.
Craig Topperc0196b12014-04-14 00:51:57 +00001940 const MachineFunction *MF = nullptr;
Eric Christopher1cdefae2015-02-27 00:11:34 +00001941 const TargetRegisterInfo *TRI = nullptr;
Craig Topperc0196b12014-04-14 00:51:57 +00001942 const MachineRegisterInfo *MRI = nullptr;
Tim Northover6b3bd612016-07-29 20:32:59 +00001943 const TargetIntrinsicInfo *IntrinsicInfo = nullptr;
1944
Dan Gohman2745d192009-11-09 19:38:45 +00001945 if (const MachineBasicBlock *MBB = getParent()) {
1946 MF = MBB->getParent();
Eric Christopher1cdefae2015-02-27 00:11:34 +00001947 if (MF) {
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001948 MRI = &MF->getRegInfo();
Eric Christopher1cdefae2015-02-27 00:11:34 +00001949 TRI = MF->getSubtarget().getRegisterInfo();
Sebastian Pop77794842016-12-21 01:41:12 +00001950 if (!TII)
1951 TII = MF->getSubtarget().getInstrInfo();
Tim Northover6b3bd612016-07-29 20:32:59 +00001952 IntrinsicInfo = MF->getTarget().getIntrinsicInfo();
Eric Christopher1cdefae2015-02-27 00:11:34 +00001953 }
Dan Gohman2745d192009-11-09 19:38:45 +00001954 }
Dan Gohman34341e62009-10-31 20:19:03 +00001955
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001956 // Save a list of virtual registers.
1957 SmallVector<unsigned, 8> VirtRegs;
1958
Dan Gohman34341e62009-10-31 20:19:03 +00001959 // Print explicitly defined operands on the left of an assignment syntax.
Dan Gohman2745d192009-11-09 19:38:45 +00001960 unsigned StartOp = 0, e = getNumOperands();
Dan Gohman34341e62009-10-31 20:19:03 +00001961 for (; StartOp < e && getOperand(StartOp).isReg() &&
1962 getOperand(StartOp).isDef() &&
1963 !getOperand(StartOp).isImplicit();
1964 ++StartOp) {
1965 if (StartOp != 0) OS << ", ";
Tim Northover6b3bd612016-07-29 20:32:59 +00001966 getOperand(StartOp).print(OS, MST, TRI, IntrinsicInfo);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001967 unsigned Reg = getOperand(StartOp).getReg();
Quentin Colombet36ce1b02016-02-10 23:43:48 +00001968 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00001969 VirtRegs.push_back(Reg);
Tim Northover0f140c72016-09-09 11:46:34 +00001970 LLT Ty = MRI ? MRI->getType(Reg) : LLT{};
1971 if (Ty.isValid())
1972 OS << '(' << Ty << ')';
Quentin Colombet36ce1b02016-02-10 23:43:48 +00001973 }
Chris Lattnerac6e9742002-10-30 01:55:38 +00001974 }
Tanya Lattner23dbc812004-06-25 00:13:11 +00001975
Dan Gohman34341e62009-10-31 20:19:03 +00001976 if (StartOp != 0)
1977 OS << " = ";
1978
1979 // Print the opcode name.
Eric Christopher1cdefae2015-02-27 00:11:34 +00001980 if (TII)
1981 OS << TII->getName(getOpcode());
Benjamin Kramerbf152d52012-02-10 13:18:44 +00001982 else
1983 OS << "UNKNOWN";
Misha Brukman835702a2005-04-21 22:36:52 +00001984
Andrew Trickb36388a2013-01-25 07:45:25 +00001985 if (SkipOpers)
1986 return;
1987
Dan Gohman34341e62009-10-31 20:19:03 +00001988 // Print the rest of the operands.
Dan Gohman2745d192009-11-09 19:38:45 +00001989 bool FirstOp = true;
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00001990 unsigned AsmDescOp = ~0u;
1991 unsigned AsmOpCount = 0;
Evan Cheng6eb516d2011-01-07 23:50:32 +00001992
Jakob Stoklund Olesen2318d1e2011-09-29 00:40:51 +00001993 if (isInlineAsm() && e >= InlineAsm::MIOp_FirstOperand) {
Evan Cheng6eb516d2011-01-07 23:50:32 +00001994 // Print asm string.
1995 OS << " ";
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00001996 getOperand(InlineAsm::MIOp_AsmString).print(OS, MST, TRI);
Evan Cheng6eb516d2011-01-07 23:50:32 +00001997
Eric Christopher0cb6fd92013-01-11 18:12:39 +00001998 // Print HasSideEffects, MayLoad, MayStore, IsAlignStack
Evan Cheng6eb516d2011-01-07 23:50:32 +00001999 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
2000 if (ExtraInfo & InlineAsm::Extra_HasSideEffects)
2001 OS << " [sideeffect]";
Eric Christopher0cb6fd92013-01-11 18:12:39 +00002002 if (ExtraInfo & InlineAsm::Extra_MayLoad)
2003 OS << " [mayload]";
2004 if (ExtraInfo & InlineAsm::Extra_MayStore)
2005 OS << " [maystore]";
Wei Ding0526e7f2016-06-22 18:51:08 +00002006 if (ExtraInfo & InlineAsm::Extra_IsConvergent)
2007 OS << " [isconvergent]";
Evan Cheng6eb516d2011-01-07 23:50:32 +00002008 if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
2009 OS << " [alignstack]";
Chad Rosiercbd2a192012-09-05 22:17:43 +00002010 if (getInlineAsmDialect() == InlineAsm::AD_ATT)
Chad Rosier994f4042012-09-05 21:00:58 +00002011 OS << " [attdialect]";
Chad Rosiercbd2a192012-09-05 22:17:43 +00002012 if (getInlineAsmDialect() == InlineAsm::AD_Intel)
Chad Rosier994f4042012-09-05 21:00:58 +00002013 OS << " [inteldialect]";
Evan Cheng6eb516d2011-01-07 23:50:32 +00002014
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00002015 StartOp = AsmDescOp = InlineAsm::MIOp_FirstOperand;
Evan Cheng6eb516d2011-01-07 23:50:32 +00002016 FirstOp = false;
2017 }
2018
Chris Lattnerac6e9742002-10-30 01:55:38 +00002019 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
Dan Gohman2745d192009-11-09 19:38:45 +00002020 const MachineOperand &MO = getOperand(i);
2021
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00002022 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg()))
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00002023 VirtRegs.push_back(MO.getReg());
2024
Dan Gohman2745d192009-11-09 19:38:45 +00002025 if (FirstOp) FirstOp = false; else OS << ",";
Chris Lattnerac6e9742002-10-30 01:55:38 +00002026 OS << " ";
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00002027 if (i < getDesc().NumOperands) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00002028 const MCOperandInfo &MCOI = getDesc().OpInfo[i];
2029 if (MCOI.isPredicate())
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00002030 OS << "pred:";
Evan Cheng6cc775f2011-06-28 19:10:37 +00002031 if (MCOI.isOptionalDef())
Jakob Stoklund Olesene8800b82010-01-19 22:08:34 +00002032 OS << "opt:";
2033 }
Evan Chengd4d1a512010-04-28 20:03:13 +00002034 if (isDebugValue() && MO.isMetadata()) {
2035 // Pretty print DBG_VALUE instructions.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002036 auto *DIV = dyn_cast<DILocalVariable>(MO.getMetadata());
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +00002037 if (DIV && !DIV->getName().empty())
2038 OS << "!\"" << DIV->getName() << '\"';
Evan Chengd4d1a512010-04-28 20:03:13 +00002039 else
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00002040 MO.print(OS, MST, TRI);
Matthias Brauna3743082017-01-09 21:38:10 +00002041 } else if (TRI && (isInsertSubreg() || isRegSequence() ||
2042 (isSubregToReg() && i == 3)) && MO.isImm()) {
Eric Christopher1cdefae2015-02-27 00:11:34 +00002043 OS << TRI->getSubRegIndexName(MO.getImm());
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00002044 } else if (i == AsmDescOp && MO.isImm()) {
2045 // Pretty print the inline asm operand descriptor.
2046 OS << '$' << AsmOpCount++;
2047 unsigned Flag = MO.getImm();
2048 switch (InlineAsm::getKind(Flag)) {
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00002049 case InlineAsm::Kind_RegUse: OS << ":[reguse"; break;
2050 case InlineAsm::Kind_RegDef: OS << ":[regdef"; break;
2051 case InlineAsm::Kind_RegDefEarlyClobber: OS << ":[regdef-ec"; break;
2052 case InlineAsm::Kind_Clobber: OS << ":[clobber"; break;
2053 case InlineAsm::Kind_Imm: OS << ":[imm"; break;
2054 case InlineAsm::Kind_Mem: OS << ":[mem"; break;
2055 default: OS << ":[??" << InlineAsm::getKind(Flag); break;
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00002056 }
2057
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00002058 unsigned RCID = 0;
Simon Dardisd32a2d32016-07-18 13:17:31 +00002059 if (!InlineAsm::isImmKind(Flag) && !InlineAsm::isMemKind(Flag) &&
2060 InlineAsm::hasRegClassConstraint(Flag, RCID)) {
Eric Christopher1cdefae2015-02-27 00:11:34 +00002061 if (TRI) {
2062 OS << ':' << TRI->getRegClassName(TRI->getRegClass(RCID));
Craig Toppercf0444b2014-11-17 05:50:14 +00002063 } else
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00002064 OS << ":RC" << RCID;
Nick Lewycky84882252011-10-13 00:54:59 +00002065 }
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00002066
Simon Dardisd32a2d32016-07-18 13:17:31 +00002067 if (InlineAsm::isMemKind(Flag)) {
2068 unsigned MCID = InlineAsm::getMemoryConstraintID(Flag);
2069 switch (MCID) {
2070 case InlineAsm::Constraint_es: OS << ":es"; break;
2071 case InlineAsm::Constraint_i: OS << ":i"; break;
2072 case InlineAsm::Constraint_m: OS << ":m"; break;
2073 case InlineAsm::Constraint_o: OS << ":o"; break;
2074 case InlineAsm::Constraint_v: OS << ":v"; break;
2075 case InlineAsm::Constraint_Q: OS << ":Q"; break;
2076 case InlineAsm::Constraint_R: OS << ":R"; break;
2077 case InlineAsm::Constraint_S: OS << ":S"; break;
2078 case InlineAsm::Constraint_T: OS << ":T"; break;
2079 case InlineAsm::Constraint_Um: OS << ":Um"; break;
2080 case InlineAsm::Constraint_Un: OS << ":Un"; break;
2081 case InlineAsm::Constraint_Uq: OS << ":Uq"; break;
2082 case InlineAsm::Constraint_Us: OS << ":Us"; break;
2083 case InlineAsm::Constraint_Ut: OS << ":Ut"; break;
2084 case InlineAsm::Constraint_Uv: OS << ":Uv"; break;
2085 case InlineAsm::Constraint_Uy: OS << ":Uy"; break;
2086 case InlineAsm::Constraint_X: OS << ":X"; break;
2087 case InlineAsm::Constraint_Z: OS << ":Z"; break;
2088 case InlineAsm::Constraint_ZC: OS << ":ZC"; break;
2089 case InlineAsm::Constraint_Zy: OS << ":Zy"; break;
2090 default: OS << ":?"; break;
2091 }
2092 }
2093
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00002094 unsigned TiedTo = 0;
2095 if (InlineAsm::isUseOperandTiedToDef(Flag, TiedTo))
Jakob Stoklund Olesen24abd9d2011-10-12 23:37:29 +00002096 OS << " tiedto:$" << TiedTo;
2097
2098 OS << ']';
Jakob Stoklund Olesen6b356b12011-06-27 04:08:29 +00002099
2100 // Compute the index of the next operand descriptor.
2101 AsmDescOp += 1 + InlineAsm::getNumOperandRegisters(Flag);
Evan Chengd4d1a512010-04-28 20:03:13 +00002102 } else
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00002103 MO.print(OS, MST, TRI);
Dan Gohman2745d192009-11-09 19:38:45 +00002104 }
2105
Dan Gohman34341e62009-10-31 20:19:03 +00002106 bool HaveSemi = false;
Michael Kuperstein098cd9f2015-09-16 11:18:25 +00002107 const unsigned PrintableFlags = FrameSetup | FrameDestroy;
Jakob Stoklund Olesen6922e9c2013-01-09 18:35:09 +00002108 if (Flags & PrintableFlags) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00002109 if (!HaveSemi) {
2110 OS << ";";
2111 HaveSemi = true;
2112 }
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00002113 OS << " flags: ";
2114
2115 if (Flags & FrameSetup)
2116 OS << "FrameSetup";
Michael Kuperstein098cd9f2015-09-16 11:18:25 +00002117
2118 if (Flags & FrameDestroy)
2119 OS << "FrameDestroy";
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00002120 }
2121
Dan Gohman3b460302008-07-07 23:14:23 +00002122 if (!memoperands_empty()) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00002123 if (!HaveSemi) {
2124 OS << ";";
2125 HaveSemi = true;
2126 }
Dan Gohman34341e62009-10-31 20:19:03 +00002127
2128 OS << " mem:";
Dan Gohman48b185d2009-09-25 20:36:54 +00002129 for (mmo_iterator i = memoperands_begin(), e = memoperands_end();
2130 i != e; ++i) {
Duncan P. N. Exon Smithf48e9822015-06-26 22:06:47 +00002131 (*i)->print(OS, MST);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002132 if (std::next(i) != e)
Dan Gohmanc0353bf2009-09-23 01:33:16 +00002133 OS << " ";
Dan Gohman2d489b52008-02-06 22:27:42 +00002134 }
2135 }
2136
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00002137 // Print the regclass of any virtual registers encountered.
2138 if (MRI && !VirtRegs.empty()) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00002139 if (!HaveSemi) {
2140 OS << ";";
2141 HaveSemi = true;
2142 }
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00002143 for (unsigned i = 0; i != VirtRegs.size(); ++i) {
Quentin Colombet03c41962016-04-07 23:18:11 +00002144 const RegClassOrRegBank &RC = MRI->getRegClassOrRegBank(VirtRegs[i]);
Quentin Colombete1494c32016-02-11 00:19:17 +00002145 if (!RC)
2146 continue;
Quentin Colombet03c41962016-04-07 23:18:11 +00002147 // Generic virtual registers do not have register classes.
2148 if (RC.is<const RegisterBank *>())
2149 OS << " " << RC.get<const RegisterBank *>()->getName();
2150 else
2151 OS << " "
2152 << TRI->getRegClassName(RC.get<const TargetRegisterClass *>());
2153 OS << ':' << PrintReg(VirtRegs[i]);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00002154 for (unsigned j = i+1; j != VirtRegs.size();) {
Quentin Colombet03c41962016-04-07 23:18:11 +00002155 if (MRI->getRegClassOrRegBank(VirtRegs[j]) != RC) {
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00002156 ++j;
2157 continue;
2158 }
2159 if (VirtRegs[i] != VirtRegs[j])
Jakob Stoklund Olesen1331a152011-01-09 03:05:53 +00002160 OS << "," << PrintReg(VirtRegs[j]);
Jakob Stoklund Olesen0ff2c112010-07-28 18:35:46 +00002161 VirtRegs.erase(VirtRegs.begin()+j);
2162 }
2163 }
2164 }
2165
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00002166 // Print debug location information.
Duncan P. N. Exon Smithc5bd3e02015-04-03 16:23:04 +00002167 if (isDebugValue() && getOperand(e - 2).isMetadata()) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00002168 if (!HaveSemi)
2169 OS << ";";
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002170 auto *DV = cast<DILocalVariable>(getOperand(e - 2).getMetadata());
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +00002171 OS << " line no:" << DV->getLine();
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00002172 if (auto *InlinedAt = debugLoc->getInlinedAt()) {
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +00002173 DebugLoc InlinedAtDL(InlinedAt);
2174 if (InlinedAtDL && MF) {
Devang Pateld61b1d52011-08-04 20:44:26 +00002175 OS << " inlined @[ ";
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002176 InlinedAtDL.print(OS);
Devang Pateld61b1d52011-08-04 20:44:26 +00002177 OS << " ]";
2178 }
2179 }
Adrian Prantl87b7eb92014-10-01 18:55:02 +00002180 if (isIndirectDebugValue())
2181 OS << " indirect";
Ahmed Bougacha97119d42017-02-23 21:05:29 +00002182 } else if (SkipDebugLoc) {
2183 return;
2184 } else if (debugLoc && MF) {
Yaron Kerenc47c6ac2016-01-02 13:40:36 +00002185 if (!HaveSemi)
2186 OS << ";";
Dan Gohman2e3f1872009-11-23 21:29:08 +00002187 OS << " dbg:";
Eric Christopherb9f00092015-02-26 23:32:17 +00002188 debugLoc.print(OS);
Bill Wendling1a0a3d02009-02-19 21:44:55 +00002189 }
2190
Anton Korobeynikov65cff4142011-03-05 18:43:04 +00002191 OS << '\n';
Chris Lattner214808f2002-10-30 00:48:05 +00002192}
2193
Owen Anderson2a8a4852008-01-24 01:10:07 +00002194bool MachineInstr::addRegisterKilled(unsigned IncomingReg,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00002195 const TargetRegisterInfo *RegInfo,
Owen Anderson2a8a4852008-01-24 01:10:07 +00002196 bool AddIfNotFound) {
Evan Cheng6c177732008-04-16 09:41:59 +00002197 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00002198 bool hasAliases = isPhysReg &&
2199 MCRegAliasIterator(IncomingReg, RegInfo, false).isValid();
Dan Gohmanc7367b42008-09-03 15:56:16 +00002200 bool Found = false;
Evan Cheng6c177732008-04-16 09:41:59 +00002201 SmallVector<unsigned,4> DeadOps;
Bill Wendling7921ad02008-03-03 22:14:33 +00002202 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2203 MachineOperand &MO = getOperand(i);
Jakob Stoklund Olesenf465f062009-08-04 20:09:25 +00002204 if (!MO.isReg() || !MO.isUse() || MO.isUndef())
Evan Cheng6c177732008-04-16 09:41:59 +00002205 continue;
Mandeep Singh Grange5a2f112016-05-10 17:57:27 +00002206
2207 // DEBUG_VALUE nodes do not contribute to code generation and should
2208 // always be ignored. Failure to do so may result in trying to modify
2209 // KILL flags on DEBUG_VALUE nodes.
2210 if (MO.isDebug())
2211 continue;
2212
Evan Cheng6c177732008-04-16 09:41:59 +00002213 unsigned Reg = MO.getReg();
2214 if (!Reg)
2215 continue;
Bill Wendling7921ad02008-03-03 22:14:33 +00002216
Evan Cheng6c177732008-04-16 09:41:59 +00002217 if (Reg == IncomingReg) {
Dan Gohmanc7367b42008-09-03 15:56:16 +00002218 if (!Found) {
2219 if (MO.isKill())
2220 // The register is already marked kill.
2221 return true;
Jakob Stoklund Olesenc59cd9b2009-08-02 19:13:03 +00002222 if (isPhysReg && isRegTiedToDefOperand(i))
2223 // Two-address uses of physregs must not be marked kill.
2224 return true;
Dan Gohmanc7367b42008-09-03 15:56:16 +00002225 MO.setIsKill();
2226 Found = true;
2227 }
2228 } else if (hasAliases && MO.isKill() &&
2229 TargetRegisterInfo::isPhysicalRegister(Reg)) {
Evan Cheng6c177732008-04-16 09:41:59 +00002230 // A super-register kill already exists.
2231 if (RegInfo->isSuperRegister(IncomingReg, Reg))
Dan Gohmanb2612922008-07-03 01:18:51 +00002232 return true;
2233 if (RegInfo->isSubRegister(IncomingReg, Reg))
Evan Cheng6c177732008-04-16 09:41:59 +00002234 DeadOps.push_back(i);
Bill Wendling7921ad02008-03-03 22:14:33 +00002235 }
2236 }
2237
Evan Cheng6c177732008-04-16 09:41:59 +00002238 // Trim unneeded kill operands.
2239 while (!DeadOps.empty()) {
2240 unsigned OpIdx = DeadOps.back();
2241 if (getOperand(OpIdx).isImplicit())
2242 RemoveOperand(OpIdx);
2243 else
2244 getOperand(OpIdx).setIsKill(false);
2245 DeadOps.pop_back();
2246 }
2247
Bill Wendling7921ad02008-03-03 22:14:33 +00002248 // If not found, this means an alias of one of the operands is killed. Add a
Owen Anderson2a8a4852008-01-24 01:10:07 +00002249 // new implicit operand if required.
Dan Gohmanc7367b42008-09-03 15:56:16 +00002250 if (!Found && AddIfNotFound) {
Bill Wendling7921ad02008-03-03 22:14:33 +00002251 addOperand(MachineOperand::CreateReg(IncomingReg,
2252 false /*IsDef*/,
2253 true /*IsImp*/,
2254 true /*IsKill*/));
Owen Anderson2a8a4852008-01-24 01:10:07 +00002255 return true;
2256 }
Dan Gohmanc7367b42008-09-03 15:56:16 +00002257 return Found;
Owen Anderson2a8a4852008-01-24 01:10:07 +00002258}
2259
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00002260void MachineInstr::clearRegisterKills(unsigned Reg,
2261 const TargetRegisterInfo *RegInfo) {
2262 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
Craig Topperc0196b12014-04-14 00:51:57 +00002263 RegInfo = nullptr;
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002264 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00002265 if (!MO.isReg() || !MO.isUse() || !MO.isKill())
2266 continue;
2267 unsigned OpReg = MO.getReg();
Matthias Braunaca625a2016-02-24 19:21:48 +00002268 if ((RegInfo && RegInfo->regsOverlap(Reg, OpReg)) || Reg == OpReg)
Jakob Stoklund Olesen8c139a52012-01-26 17:52:15 +00002269 MO.setIsKill(false);
2270 }
2271}
2272
Matthias Braun1965bfa2013-10-10 21:28:38 +00002273bool MachineInstr::addRegisterDead(unsigned Reg,
Dan Gohman3a4be0f2008-02-10 18:45:23 +00002274 const TargetRegisterInfo *RegInfo,
Owen Anderson2a8a4852008-01-24 01:10:07 +00002275 bool AddIfNotFound) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00002276 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(Reg);
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00002277 bool hasAliases = isPhysReg &&
Matthias Braun1965bfa2013-10-10 21:28:38 +00002278 MCRegAliasIterator(Reg, RegInfo, false).isValid();
Dan Gohmanc7367b42008-09-03 15:56:16 +00002279 bool Found = false;
Evan Cheng6c177732008-04-16 09:41:59 +00002280 SmallVector<unsigned,4> DeadOps;
Owen Anderson2a8a4852008-01-24 01:10:07 +00002281 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2282 MachineOperand &MO = getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00002283 if (!MO.isReg() || !MO.isDef())
Evan Cheng6c177732008-04-16 09:41:59 +00002284 continue;
Matthias Braun1965bfa2013-10-10 21:28:38 +00002285 unsigned MOReg = MO.getReg();
2286 if (!MOReg)
Dan Gohmanc7367b42008-09-03 15:56:16 +00002287 continue;
2288
Matthias Braun1965bfa2013-10-10 21:28:38 +00002289 if (MOReg == Reg) {
Jakob Stoklund Olesen76ad3de2011-04-05 16:53:50 +00002290 MO.setIsDead();
2291 Found = true;
Dan Gohmanc7367b42008-09-03 15:56:16 +00002292 } else if (hasAliases && MO.isDead() &&
Matthias Braun1965bfa2013-10-10 21:28:38 +00002293 TargetRegisterInfo::isPhysicalRegister(MOReg)) {
Evan Cheng6c177732008-04-16 09:41:59 +00002294 // There exists a super-register that's marked dead.
Matthias Braun1965bfa2013-10-10 21:28:38 +00002295 if (RegInfo->isSuperRegister(Reg, MOReg))
Dan Gohmanb2612922008-07-03 01:18:51 +00002296 return true;
Matthias Braun1965bfa2013-10-10 21:28:38 +00002297 if (RegInfo->isSubRegister(Reg, MOReg))
Evan Cheng6c177732008-04-16 09:41:59 +00002298 DeadOps.push_back(i);
Owen Anderson2a8a4852008-01-24 01:10:07 +00002299 }
2300 }
2301
Evan Cheng6c177732008-04-16 09:41:59 +00002302 // Trim unneeded dead operands.
2303 while (!DeadOps.empty()) {
2304 unsigned OpIdx = DeadOps.back();
2305 if (getOperand(OpIdx).isImplicit())
2306 RemoveOperand(OpIdx);
2307 else
2308 getOperand(OpIdx).setIsDead(false);
2309 DeadOps.pop_back();
2310 }
2311
Dan Gohmanc7367b42008-09-03 15:56:16 +00002312 // If not found, this means an alias of one of the operands is dead. Add a
2313 // new implicit operand if required.
Chris Lattnerfd682802009-06-24 17:54:48 +00002314 if (Found || !AddIfNotFound)
2315 return Found;
Jim Grosbachdee9e8a2011-08-24 16:44:17 +00002316
Matthias Braun1965bfa2013-10-10 21:28:38 +00002317 addOperand(MachineOperand::CreateReg(Reg,
Chris Lattnerfd682802009-06-24 17:54:48 +00002318 true /*IsDef*/,
2319 true /*IsImp*/,
2320 false /*IsKill*/,
2321 true /*IsDead*/));
2322 return true;
Owen Anderson2a8a4852008-01-24 01:10:07 +00002323}
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00002324
Matthias Braun26e7ea62015-02-04 19:35:16 +00002325void MachineInstr::clearRegisterDeads(unsigned Reg) {
2326 for (MachineOperand &MO : operands()) {
2327 if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg)
2328 continue;
2329 MO.setIsDead(false);
2330 }
2331}
2332
Matthias Braun2c98d0f2015-11-11 00:41:58 +00002333void MachineInstr::setRegisterDefReadUndef(unsigned Reg, bool IsUndef) {
Matthias Braunc1988f32015-01-21 22:55:13 +00002334 for (MachineOperand &MO : operands()) {
2335 if (!MO.isReg() || !MO.isDef() || MO.getReg() != Reg || MO.getSubReg() == 0)
2336 continue;
Matthias Braun2c98d0f2015-11-11 00:41:58 +00002337 MO.setIsUndef(IsUndef);
Matthias Braunc1988f32015-01-21 22:55:13 +00002338 }
2339}
2340
Matthias Braun1965bfa2013-10-10 21:28:38 +00002341void MachineInstr::addRegisterDefined(unsigned Reg,
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00002342 const TargetRegisterInfo *RegInfo) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00002343 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
2344 MachineOperand *MO = findRegisterDefOperand(Reg, false, RegInfo);
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00002345 if (MO)
2346 return;
2347 } else {
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002348 for (const MachineOperand &MO : operands()) {
Matthias Braun1965bfa2013-10-10 21:28:38 +00002349 if (MO.isReg() && MO.getReg() == Reg && MO.isDef() &&
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00002350 MO.getSubReg() == 0)
2351 return;
2352 }
2353 }
Matthias Braun1965bfa2013-10-10 21:28:38 +00002354 addOperand(MachineOperand::CreateReg(Reg,
Jakob Stoklund Olesen1f380102010-05-21 16:32:16 +00002355 true /*IsDef*/,
2356 true /*IsImp*/));
Jakob Stoklund Olesen77255262010-01-06 00:29:28 +00002357}
Evan Cheng59d27fe2010-03-03 23:37:30 +00002358
Jakob Stoklund Olesen4290be42012-02-03 20:43:39 +00002359void MachineInstr::setPhysRegsDeadExcept(ArrayRef<unsigned> UsedRegs,
Dan Gohman86936502010-06-18 23:28:01 +00002360 const TargetRegisterInfo &TRI) {
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00002361 bool HasRegMask = false;
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002362 for (MachineOperand &MO : operands()) {
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00002363 if (MO.isRegMask()) {
2364 HasRegMask = true;
2365 continue;
2366 }
Dan Gohman86936502010-06-18 23:28:01 +00002367 if (!MO.isReg() || !MO.isDef()) continue;
2368 unsigned Reg = MO.getReg();
Jakob Stoklund Olesenf6507322012-02-03 20:43:35 +00002369 if (!TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
Dan Gohman86936502010-06-18 23:28:01 +00002370 // If there are no uses, including partial uses, the def is dead.
Eugene Zelenko4e9736b2017-05-31 01:10:10 +00002371 if (llvm::none_of(UsedRegs,
2372 [&](unsigned Use) { return TRI.regsOverlap(Use, Reg); }))
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002373 MO.setIsDead();
Dan Gohman86936502010-06-18 23:28:01 +00002374 }
Jakob Stoklund Olesen56fe2ed2012-02-03 21:23:14 +00002375
2376 // This is a call with a register mask operand.
2377 // Mask clobbers are always dead, so add defs for the non-dead defines.
2378 if (HasRegMask)
2379 for (ArrayRef<unsigned>::iterator I = UsedRegs.begin(), E = UsedRegs.end();
2380 I != E; ++I)
2381 addRegisterDefined(*I, &TRI);
Dan Gohman86936502010-06-18 23:28:01 +00002382}
2383
Evan Cheng59d27fe2010-03-03 23:37:30 +00002384unsigned
2385MachineInstrExpressionTrait::getHashValue(const MachineInstr* const &MI) {
Chandler Carruth962152c2012-03-07 09:39:46 +00002386 // Build up a buffer of hash code components.
Chandler Carruth962152c2012-03-07 09:39:46 +00002387 SmallVector<size_t, 8> HashComponents;
2388 HashComponents.reserve(MI->getNumOperands() + 1);
2389 HashComponents.push_back(MI->getOpcode());
Benjamin Kramer60c5bbf2015-02-21 17:08:08 +00002390 for (const MachineOperand &MO : MI->operands()) {
Chandler Carruth264854f2012-07-05 11:06:22 +00002391 if (MO.isReg() && MO.isDef() &&
2392 TargetRegisterInfo::isVirtualRegister(MO.getReg()))
2393 continue; // Skip virtual register defs.
2394
2395 HashComponents.push_back(hash_value(MO));
Evan Cheng59d27fe2010-03-03 23:37:30 +00002396 }
Chandler Carruth962152c2012-03-07 09:39:46 +00002397 return hash_combine_range(HashComponents.begin(), HashComponents.end());
Evan Cheng59d27fe2010-03-03 23:37:30 +00002398}
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00002399
2400void MachineInstr::emitError(StringRef Msg) const {
2401 // Find the source location cookie.
2402 unsigned LocCookie = 0;
Craig Topperc0196b12014-04-14 00:51:57 +00002403 const MDNode *LocMD = nullptr;
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00002404 for (unsigned i = getNumOperands(); i != 0; --i) {
2405 if (getOperand(i-1).isMetadata() &&
2406 (LocMD = getOperand(i-1).getMetadata()) &&
2407 LocMD->getNumOperands() != 0) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002408 if (const ConstantInt *CI =
2409 mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
Jakob Stoklund Olesen25a404e2011-07-02 03:53:34 +00002410 LocCookie = CI->getZExtValue();
2411 break;
2412 }
2413 }
2414 }
2415
2416 if (const MachineBasicBlock *MBB = getParent())
2417 if (const MachineFunction *MF = MBB->getParent())
2418 return MF->getMMI().getModule()->getContext().emitError(LocCookie, Msg);
2419 report_fatal_error(Msg);
2420}
Reid Kleckner28865802016-04-14 18:29:59 +00002421
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002422MachineInstrBuilder llvm::BuildMI(MachineFunction &MF, const DebugLoc &DL,
Reid Kleckner28865802016-04-14 18:29:59 +00002423 const MCInstrDesc &MCID, bool IsIndirect,
Adrian Prantl8b9bb532017-07-28 23:00:45 +00002424 unsigned Reg, const MDNode *Variable,
2425 const MDNode *Expr) {
Reid Kleckner28865802016-04-14 18:29:59 +00002426 assert(isa<DILocalVariable>(Variable) && "not a variable");
2427 assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
2428 assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
2429 "Expected inlined-at fields to agree");
2430 if (IsIndirect)
2431 return BuildMI(MF, DL, MCID)
2432 .addReg(Reg, RegState::Debug)
Adrian Prantl8b9bb532017-07-28 23:00:45 +00002433 .addImm(0U)
Reid Kleckner28865802016-04-14 18:29:59 +00002434 .addMetadata(Variable)
2435 .addMetadata(Expr);
Adrian Prantl8b9bb532017-07-28 23:00:45 +00002436 else
Reid Kleckner28865802016-04-14 18:29:59 +00002437 return BuildMI(MF, DL, MCID)
2438 .addReg(Reg, RegState::Debug)
2439 .addReg(0U, RegState::Debug)
2440 .addMetadata(Variable)
2441 .addMetadata(Expr);
Reid Kleckner28865802016-04-14 18:29:59 +00002442}
2443
2444MachineInstrBuilder llvm::BuildMI(MachineBasicBlock &BB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002445 MachineBasicBlock::iterator I,
2446 const DebugLoc &DL, const MCInstrDesc &MCID,
2447 bool IsIndirect, unsigned Reg,
Adrian Prantl8b9bb532017-07-28 23:00:45 +00002448 const MDNode *Variable, const MDNode *Expr) {
Reid Kleckner28865802016-04-14 18:29:59 +00002449 assert(isa<DILocalVariable>(Variable) && "not a variable");
2450 assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
2451 MachineFunction &MF = *BB.getParent();
Adrian Prantl8b9bb532017-07-28 23:00:45 +00002452 MachineInstr *MI = BuildMI(MF, DL, MCID, IsIndirect, Reg, Variable, Expr);
Reid Kleckner28865802016-04-14 18:29:59 +00002453 BB.insert(I, MI);
2454 return MachineInstrBuilder(MF, MI);
2455}
Adrian Prantl6825fb62017-04-18 01:21:53 +00002456
Reid Kleckner9e6c3092017-09-15 21:49:56 +00002457/// Compute the new DIExpression to use with a DBG_VALUE for a spill slot.
2458/// This prepends DW_OP_deref when spilling an indirect DBG_VALUE.
2459static const DIExpression *computeExprForSpill(const MachineInstr &MI) {
2460 assert(MI.getOperand(0).isReg() && "can't spill non-register");
2461 assert(MI.getDebugVariable()->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
2462 "Expected inlined-at fields to agree");
2463
2464 const DIExpression *Expr = MI.getDebugExpression();
2465 if (MI.isIndirectDebugValue()) {
2466 assert(MI.getOperand(1).getImm() == 0 && "DBG_VALUE with nonzero offset");
2467 Expr = DIExpression::prepend(Expr, DIExpression::WithDeref);
2468 }
2469 return Expr;
2470}
2471
Adrian Prantl6825fb62017-04-18 01:21:53 +00002472MachineInstr *llvm::buildDbgValueForSpill(MachineBasicBlock &BB,
2473 MachineBasicBlock::iterator I,
2474 const MachineInstr &Orig,
2475 int FrameIndex) {
Reid Kleckner9e6c3092017-09-15 21:49:56 +00002476 const DIExpression *Expr = computeExprForSpill(Orig);
2477 return BuildMI(BB, I, Orig.getDebugLoc(), Orig.getDesc())
Adrian Prantl6825fb62017-04-18 01:21:53 +00002478 .addFrameIndex(FrameIndex)
Adrian Prantl8b9bb532017-07-28 23:00:45 +00002479 .addImm(0U)
Reid Kleckner9e6c3092017-09-15 21:49:56 +00002480 .addMetadata(Orig.getDebugVariable())
Adrian Prantl6825fb62017-04-18 01:21:53 +00002481 .addMetadata(Expr);
2482}
Reid Kleckner9e6c3092017-09-15 21:49:56 +00002483
2484void llvm::updateDbgValueForSpill(MachineInstr &Orig, int FrameIndex) {
2485 const DIExpression *Expr = computeExprForSpill(Orig);
2486 Orig.getOperand(0).ChangeToFrameIndex(FrameIndex);
2487 Orig.getOperand(1).ChangeToImmediate(0U);
2488 Orig.getOperand(3).setMetadata(Expr);
2489}