/src/llvm-project/llvm/lib/CodeGen/MachineOperand.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- lib/CodeGen/MachineOperand.cpp -------------------------------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | /// \file Methods common to all machine operands. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "llvm/CodeGen/MachineOperand.h" |
14 | | #include "llvm/ADT/StableHashing.h" |
15 | | #include "llvm/ADT/StringExtras.h" |
16 | | #include "llvm/Analysis/Loads.h" |
17 | | #include "llvm/CodeGen/MIRFormatter.h" |
18 | | #include "llvm/CodeGen/MachineFrameInfo.h" |
19 | | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
20 | | #include "llvm/CodeGen/MachineRegisterInfo.h" |
21 | | #include "llvm/CodeGen/PseudoSourceValueManager.h" |
22 | | #include "llvm/CodeGen/TargetInstrInfo.h" |
23 | | #include "llvm/CodeGen/TargetRegisterInfo.h" |
24 | | #include "llvm/Config/llvm-config.h" |
25 | | #include "llvm/IR/Constants.h" |
26 | | #include "llvm/IR/IRPrintingPasses.h" |
27 | | #include "llvm/IR/Instructions.h" |
28 | | #include "llvm/IR/ModuleSlotTracker.h" |
29 | | #include "llvm/MC/MCDwarf.h" |
30 | | #include "llvm/Target/TargetIntrinsicInfo.h" |
31 | | #include "llvm/Target/TargetMachine.h" |
32 | | #include <optional> |
33 | | |
34 | | using namespace llvm; |
35 | | |
36 | | static cl::opt<int> |
37 | | PrintRegMaskNumRegs("print-regmask-num-regs", |
38 | | cl::desc("Number of registers to limit to when " |
39 | | "printing regmask operands in IR dumps. " |
40 | | "unlimited = -1"), |
41 | | cl::init(32), cl::Hidden); |
42 | | |
43 | 11.5M | static const MachineFunction *getMFIfAvailable(const MachineOperand &MO) { |
44 | 11.5M | if (const MachineInstr *MI = MO.getParent()) |
45 | 11.5M | if (const MachineBasicBlock *MBB = MI->getParent()) |
46 | 11.4M | if (const MachineFunction *MF = MBB->getParent()) |
47 | 11.4M | return MF; |
48 | 122k | return nullptr; |
49 | 11.5M | } |
50 | | |
51 | 11.5M | static MachineFunction *getMFIfAvailable(MachineOperand &MO) { |
52 | 11.5M | return const_cast<MachineFunction *>( |
53 | 11.5M | getMFIfAvailable(const_cast<const MachineOperand &>(MO))); |
54 | 11.5M | } |
55 | | |
56 | 9.42M | unsigned MachineOperand::getOperandNo() const { |
57 | 9.42M | assert(getParent() && "Operand does not belong to any instruction!"); |
58 | 0 | return getParent()->getOperandNo(this); |
59 | 9.42M | } |
60 | | |
61 | 11.0M | void MachineOperand::setReg(Register Reg) { |
62 | 11.0M | if (getReg() == Reg) |
63 | 618k | return; // No change. |
64 | | |
65 | | // Clear the IsRenamable bit to keep it conservatively correct. |
66 | 10.4M | IsRenamable = false; |
67 | | |
68 | | // Otherwise, we have to change the register. If this operand is embedded |
69 | | // into a machine function, we need to update the old and new register's |
70 | | // use/def lists. |
71 | 10.4M | if (MachineFunction *MF = getMFIfAvailable(*this)) { |
72 | 10.3M | MachineRegisterInfo &MRI = MF->getRegInfo(); |
73 | 10.3M | MRI.removeRegOperandFromUseList(this); |
74 | 10.3M | SmallContents.RegNo = Reg; |
75 | 10.3M | MRI.addRegOperandToUseList(this); |
76 | 10.3M | return; |
77 | 10.3M | } |
78 | | |
79 | | // Otherwise, just change the register, no problem. :) |
80 | 122k | SmallContents.RegNo = Reg; |
81 | 122k | } |
82 | | |
83 | | void MachineOperand::substVirtReg(Register Reg, unsigned SubIdx, |
84 | 885k | const TargetRegisterInfo &TRI) { |
85 | 885k | assert(Reg.isVirtual()); |
86 | 885k | if (SubIdx && getSubReg()) |
87 | 1.15k | SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg()); |
88 | 885k | setReg(Reg); |
89 | 885k | if (SubIdx) |
90 | 248k | setSubReg(SubIdx); |
91 | 885k | } |
92 | | |
93 | 318k | void MachineOperand::substPhysReg(MCRegister Reg, const TargetRegisterInfo &TRI) { |
94 | 318k | assert(Register::isPhysicalRegister(Reg)); |
95 | 318k | if (getSubReg()) { |
96 | 1.01k | Reg = TRI.getSubReg(Reg, getSubReg()); |
97 | | // Note that getSubReg() may return 0 if the sub-register doesn't exist. |
98 | | // That won't happen in legal code. |
99 | 1.01k | setSubReg(0); |
100 | 1.01k | if (isDef()) |
101 | 816 | setIsUndef(false); |
102 | 1.01k | } |
103 | 318k | setReg(Reg); |
104 | 318k | } |
105 | | |
106 | | /// Change a def to a use, or a use to a def. |
107 | 8.05k | void MachineOperand::setIsDef(bool Val) { |
108 | 8.05k | assert(isReg() && "Wrong MachineOperand accessor"); |
109 | 0 | assert((!Val || !isDebug()) && "Marking a debug operation as def"); |
110 | 8.05k | if (IsDef == Val) |
111 | 5.38k | return; |
112 | 2.66k | assert(!IsDeadOrKill && "Changing def/use with dead/kill set not supported"); |
113 | | // MRI may keep uses and defs in different list positions. |
114 | 2.66k | if (MachineFunction *MF = getMFIfAvailable(*this)) { |
115 | 2.66k | MachineRegisterInfo &MRI = MF->getRegInfo(); |
116 | 2.66k | MRI.removeRegOperandFromUseList(this); |
117 | 2.66k | IsDef = Val; |
118 | 2.66k | MRI.addRegOperandToUseList(this); |
119 | 2.66k | return; |
120 | 2.66k | } |
121 | 1 | IsDef = Val; |
122 | 1 | } |
123 | | |
124 | 5.56M | bool MachineOperand::isRenamable() const { |
125 | 5.56M | assert(isReg() && "Wrong MachineOperand accessor"); |
126 | 0 | assert(getReg().isPhysical() && |
127 | 5.56M | "isRenamable should only be checked on physical registers"); |
128 | 5.56M | if (!IsRenamable) |
129 | 1.41M | return false; |
130 | | |
131 | 4.15M | const MachineInstr *MI = getParent(); |
132 | 4.15M | if (!MI) |
133 | 0 | return true; |
134 | | |
135 | 4.15M | if (isDef()) |
136 | 255k | return !MI->hasExtraDefRegAllocReq(MachineInstr::IgnoreBundle); |
137 | | |
138 | 3.89M | assert(isUse() && "Reg is not def or use"); |
139 | 0 | return !MI->hasExtraSrcRegAllocReq(MachineInstr::IgnoreBundle); |
140 | 4.15M | } |
141 | | |
142 | 6.28M | void MachineOperand::setIsRenamable(bool Val) { |
143 | 6.28M | assert(isReg() && "Wrong MachineOperand accessor"); |
144 | 0 | assert(getReg().isPhysical() && |
145 | 6.28M | "setIsRenamable should only be called on physical registers"); |
146 | 0 | IsRenamable = Val; |
147 | 6.28M | } |
148 | | |
149 | | // 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. |
151 | 1.10M | void MachineOperand::removeRegFromUses() { |
152 | 1.10M | if (!isReg() || !isOnRegUseList()) |
153 | 1.03M | return; |
154 | | |
155 | 68.9k | if (MachineFunction *MF = getMFIfAvailable(*this)) |
156 | 68.9k | MF->getRegInfo().removeRegOperandFromUseList(this); |
157 | 68.9k | } |
158 | | |
159 | | /// ChangeToImmediate - Replace this operand with a new immediate operand of |
160 | | /// the specified value. If an operand is known to be an immediate already, |
161 | | /// the setImm method should be used. |
162 | 1.10M | void MachineOperand::ChangeToImmediate(int64_t ImmVal, unsigned TargetFlags) { |
163 | 1.10M | assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm"); |
164 | | |
165 | 0 | removeRegFromUses(); |
166 | | |
167 | 1.10M | OpKind = MO_Immediate; |
168 | 1.10M | Contents.ImmVal = ImmVal; |
169 | 1.10M | setTargetFlags(TargetFlags); |
170 | 1.10M | } |
171 | | |
172 | | void MachineOperand::ChangeToFPImmediate(const ConstantFP *FPImm, |
173 | 0 | unsigned TargetFlags) { |
174 | 0 | assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm"); |
175 | | |
176 | 0 | removeRegFromUses(); |
177 | |
|
178 | 0 | OpKind = MO_FPImmediate; |
179 | 0 | Contents.CFP = FPImm; |
180 | 0 | setTargetFlags(TargetFlags); |
181 | 0 | } |
182 | | |
183 | | void MachineOperand::ChangeToES(const char *SymName, |
184 | 0 | unsigned TargetFlags) { |
185 | 0 | assert((!isReg() || !isTied()) && |
186 | 0 | "Cannot change a tied operand into an external symbol"); |
187 | | |
188 | 0 | removeRegFromUses(); |
189 | |
|
190 | 0 | OpKind = MO_ExternalSymbol; |
191 | 0 | Contents.OffsetedInfo.Val.SymbolName = SymName; |
192 | 0 | setOffset(0); // Offset is always 0. |
193 | 0 | setTargetFlags(TargetFlags); |
194 | 0 | } |
195 | | |
196 | | void MachineOperand::ChangeToGA(const GlobalValue *GV, int64_t Offset, |
197 | 0 | unsigned TargetFlags) { |
198 | 0 | assert((!isReg() || !isTied()) && |
199 | 0 | "Cannot change a tied operand into a global address"); |
200 | | |
201 | 0 | removeRegFromUses(); |
202 | |
|
203 | 0 | OpKind = MO_GlobalAddress; |
204 | 0 | Contents.OffsetedInfo.Val.GV = GV; |
205 | 0 | setOffset(Offset); |
206 | 0 | setTargetFlags(TargetFlags); |
207 | 0 | } |
208 | | |
209 | 0 | void MachineOperand::ChangeToMCSymbol(MCSymbol *Sym, unsigned TargetFlags) { |
210 | 0 | assert((!isReg() || !isTied()) && |
211 | 0 | "Cannot change a tied operand into an MCSymbol"); |
212 | | |
213 | 0 | removeRegFromUses(); |
214 | |
|
215 | 0 | OpKind = MO_MCSymbol; |
216 | 0 | Contents.Sym = Sym; |
217 | 0 | setTargetFlags(TargetFlags); |
218 | 0 | } |
219 | | |
220 | 2 | void MachineOperand::ChangeToFrameIndex(int Idx, unsigned TargetFlags) { |
221 | 2 | assert((!isReg() || !isTied()) && |
222 | 2 | "Cannot change a tied operand into a FrameIndex"); |
223 | | |
224 | 0 | removeRegFromUses(); |
225 | | |
226 | 2 | OpKind = MO_FrameIndex; |
227 | 2 | setIndex(Idx); |
228 | 2 | setTargetFlags(TargetFlags); |
229 | 2 | } |
230 | | |
231 | | void MachineOperand::ChangeToTargetIndex(unsigned Idx, int64_t Offset, |
232 | 9 | unsigned TargetFlags) { |
233 | 9 | assert((!isReg() || !isTied()) && |
234 | 9 | "Cannot change a tied operand into a FrameIndex"); |
235 | | |
236 | 0 | removeRegFromUses(); |
237 | | |
238 | 9 | OpKind = MO_TargetIndex; |
239 | 9 | setIndex(Idx); |
240 | 9 | setOffset(Offset); |
241 | 9 | setTargetFlags(TargetFlags); |
242 | 9 | } |
243 | | |
244 | | void MachineOperand::ChangeToDbgInstrRef(unsigned InstrIdx, unsigned OpIdx, |
245 | 1 | unsigned TargetFlags) { |
246 | 1 | assert((!isReg() || !isTied()) && |
247 | 1 | "Cannot change a tied operand into a DbgInstrRef"); |
248 | | |
249 | 0 | removeRegFromUses(); |
250 | | |
251 | 1 | OpKind = MO_DbgInstrRef; |
252 | 1 | setInstrRefInstrIndex(InstrIdx); |
253 | 1 | setInstrRefOpIndex(OpIdx); |
254 | 1 | setTargetFlags(TargetFlags); |
255 | 1 | } |
256 | | |
257 | | /// ChangeToRegister - Replace this operand with a new register operand of |
258 | | /// the specified value. If an operand is known to be an register already, |
259 | | /// the setReg method should be used. |
260 | | void MachineOperand::ChangeToRegister(Register Reg, bool isDef, bool isImp, |
261 | | bool isKill, bool isDead, bool isUndef, |
262 | 1.05M | bool isDebug) { |
263 | 1.05M | MachineRegisterInfo *RegInfo = nullptr; |
264 | 1.05M | if (MachineFunction *MF = getMFIfAvailable(*this)) |
265 | 1.05M | RegInfo = &MF->getRegInfo(); |
266 | | // If this operand is already a register operand, remove it from the |
267 | | // register's use/def lists. |
268 | 1.05M | bool WasReg = isReg(); |
269 | 1.05M | if (RegInfo && WasReg) |
270 | 5.04k | RegInfo->removeRegOperandFromUseList(this); |
271 | | |
272 | | // Ensure debug instructions set debug flag on register uses. |
273 | 1.05M | const MachineInstr *MI = getParent(); |
274 | 1.05M | if (!isDef && MI && MI->isDebugInstr()) |
275 | 64 | isDebug = true; |
276 | | |
277 | | // Change this to a register and set the reg#. |
278 | 1.05M | assert(!(isDead && !isDef) && "Dead flag on non-def"); |
279 | 0 | assert(!(isKill && isDef) && "Kill flag on def"); |
280 | 0 | OpKind = MO_Register; |
281 | 1.05M | SmallContents.RegNo = Reg; |
282 | 1.05M | SubReg_TargetFlags = 0; |
283 | 1.05M | IsDef = isDef; |
284 | 1.05M | IsImp = isImp; |
285 | 1.05M | IsDeadOrKill = isKill | isDead; |
286 | 1.05M | IsRenamable = false; |
287 | 1.05M | IsUndef = isUndef; |
288 | 1.05M | IsInternalRead = false; |
289 | 1.05M | IsEarlyClobber = false; |
290 | 1.05M | IsDebug = isDebug; |
291 | | // Ensure isOnRegUseList() returns false. |
292 | 1.05M | Contents.Reg.Prev = nullptr; |
293 | | // Preserve the tie when the operand was already a register. |
294 | 1.05M | if (!WasReg) |
295 | 1.04M | TiedTo = 0; |
296 | | |
297 | | // If this operand is embedded in a function, add the operand to the |
298 | | // register's use/def list. |
299 | 1.05M | if (RegInfo) |
300 | 1.05M | RegInfo->addRegOperandToUseList(this); |
301 | 1.05M | } |
302 | | |
303 | | /// isIdenticalTo - Return true if this operand is identical to the specified |
304 | | /// operand. Note that this should stay in sync with the hash_value overload |
305 | | /// below. |
306 | 31.7M | bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const { |
307 | 31.7M | if (getType() != Other.getType() || |
308 | 31.7M | getTargetFlags() != Other.getTargetFlags()) |
309 | 1.76M | return false; |
310 | | |
311 | 29.9M | switch (getType()) { |
312 | 23.7M | case MachineOperand::MO_Register: |
313 | 23.7M | return getReg() == Other.getReg() && isDef() == Other.isDef() && |
314 | 23.7M | getSubReg() == Other.getSubReg(); |
315 | 4.17M | case MachineOperand::MO_Immediate: |
316 | 4.17M | return getImm() == Other.getImm(); |
317 | 0 | case MachineOperand::MO_CImmediate: |
318 | 0 | return getCImm() == Other.getCImm(); |
319 | 1.23k | case MachineOperand::MO_FPImmediate: |
320 | 1.23k | return getFPImm() == Other.getFPImm(); |
321 | 10.1k | case MachineOperand::MO_MachineBasicBlock: |
322 | 10.1k | return getMBB() == Other.getMBB(); |
323 | 1.41M | case MachineOperand::MO_FrameIndex: |
324 | 1.41M | return getIndex() == Other.getIndex(); |
325 | 102k | case MachineOperand::MO_ConstantPoolIndex: |
326 | 102k | case MachineOperand::MO_TargetIndex: |
327 | 102k | return getIndex() == Other.getIndex() && getOffset() == Other.getOffset(); |
328 | 28 | case MachineOperand::MO_JumpTableIndex: |
329 | 28 | return getIndex() == Other.getIndex(); |
330 | 469k | case MachineOperand::MO_GlobalAddress: |
331 | 469k | return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset(); |
332 | 7.92k | case MachineOperand::MO_ExternalSymbol: |
333 | 7.92k | return strcmp(getSymbolName(), Other.getSymbolName()) == 0 && |
334 | 7.92k | getOffset() == Other.getOffset(); |
335 | 234 | case MachineOperand::MO_BlockAddress: |
336 | 234 | return getBlockAddress() == Other.getBlockAddress() && |
337 | 234 | getOffset() == Other.getOffset(); |
338 | 763 | case MachineOperand::MO_RegisterMask: |
339 | 763 | case MachineOperand::MO_RegisterLiveOut: { |
340 | | // Shallow compare of the two RegMasks |
341 | 763 | const uint32_t *RegMask = getRegMask(); |
342 | 763 | const uint32_t *OtherRegMask = Other.getRegMask(); |
343 | 763 | if (RegMask == OtherRegMask) |
344 | 763 | return true; |
345 | | |
346 | 0 | if (const MachineFunction *MF = getMFIfAvailable(*this)) { |
347 | 0 | const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); |
348 | 0 | unsigned RegMaskSize = MachineOperand::getRegMaskSize(TRI->getNumRegs()); |
349 | | // Deep compare of the two RegMasks |
350 | 0 | return std::equal(RegMask, RegMask + RegMaskSize, OtherRegMask); |
351 | 0 | } |
352 | | // We don't know the size of the RegMask, so we can't deep compare the two |
353 | | // reg masks. |
354 | 0 | return false; |
355 | 0 | } |
356 | 22 | case MachineOperand::MO_MCSymbol: |
357 | 22 | return getMCSymbol() == Other.getMCSymbol(); |
358 | 0 | case MachineOperand::MO_DbgInstrRef: |
359 | 0 | return getInstrRefInstrIndex() == Other.getInstrRefInstrIndex() && |
360 | 0 | getInstrRefOpIndex() == Other.getInstrRefOpIndex(); |
361 | 0 | case MachineOperand::MO_CFIIndex: |
362 | 0 | return getCFIIndex() == Other.getCFIIndex(); |
363 | 0 | case MachineOperand::MO_Metadata: |
364 | 0 | return getMetadata() == Other.getMetadata(); |
365 | 0 | case MachineOperand::MO_IntrinsicID: |
366 | 0 | return getIntrinsicID() == Other.getIntrinsicID(); |
367 | 0 | case MachineOperand::MO_Predicate: |
368 | 0 | return getPredicate() == Other.getPredicate(); |
369 | 0 | case MachineOperand::MO_ShuffleMask: |
370 | 0 | return getShuffleMask() == Other.getShuffleMask(); |
371 | 29.9M | } |
372 | 0 | llvm_unreachable("Invalid machine operand type"); |
373 | 0 | } |
374 | | |
375 | | // Note: this must stay exactly in sync with isIdenticalTo above. |
376 | 29.6M | hash_code llvm::hash_value(const MachineOperand &MO) { |
377 | 29.6M | switch (MO.getType()) { |
378 | 18.5M | case MachineOperand::MO_Register: |
379 | | // Register operands don't have target flags. |
380 | 18.5M | return hash_combine(MO.getType(), (unsigned)MO.getReg(), MO.getSubReg(), MO.isDef()); |
381 | 9.52M | case MachineOperand::MO_Immediate: |
382 | 9.52M | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm()); |
383 | 0 | case MachineOperand::MO_CImmediate: |
384 | 0 | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm()); |
385 | 2.71k | case MachineOperand::MO_FPImmediate: |
386 | 2.71k | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm()); |
387 | 118k | case MachineOperand::MO_MachineBasicBlock: |
388 | 118k | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB()); |
389 | 434k | case MachineOperand::MO_FrameIndex: |
390 | 434k | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex()); |
391 | 204k | case MachineOperand::MO_ConstantPoolIndex: |
392 | 204k | case MachineOperand::MO_TargetIndex: |
393 | 204k | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(), |
394 | 204k | MO.getOffset()); |
395 | 98 | case MachineOperand::MO_JumpTableIndex: |
396 | 98 | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex()); |
397 | 16.9k | case MachineOperand::MO_ExternalSymbol: |
398 | 16.9k | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(), |
399 | 16.9k | StringRef(MO.getSymbolName())); |
400 | 813k | case MachineOperand::MO_GlobalAddress: |
401 | 813k | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(), |
402 | 813k | MO.getOffset()); |
403 | 591 | case MachineOperand::MO_BlockAddress: |
404 | 591 | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getBlockAddress(), |
405 | 591 | MO.getOffset()); |
406 | 2 | case MachineOperand::MO_RegisterMask: |
407 | 2 | case MachineOperand::MO_RegisterLiveOut: { |
408 | 2 | if (const MachineFunction *MF = getMFIfAvailable(MO)) { |
409 | 2 | const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); |
410 | 2 | unsigned RegMaskSize = MachineOperand::getRegMaskSize(TRI->getNumRegs()); |
411 | 2 | const uint32_t *RegMask = MO.getRegMask(); |
412 | 2 | std::vector<stable_hash> RegMaskHashes(RegMask, RegMask + RegMaskSize); |
413 | 2 | return hash_combine(MO.getType(), MO.getTargetFlags(), |
414 | 2 | stable_hash_combine_array(RegMaskHashes.data(), |
415 | 2 | RegMaskHashes.size())); |
416 | 2 | } |
417 | | |
418 | 0 | assert(0 && "MachineOperand not associated with any MachineFunction"); |
419 | 0 | return hash_combine(MO.getType(), MO.getTargetFlags()); |
420 | 2 | } |
421 | 0 | case MachineOperand::MO_Metadata: |
422 | 0 | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata()); |
423 | 0 | case MachineOperand::MO_MCSymbol: |
424 | 0 | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol()); |
425 | 0 | case MachineOperand::MO_DbgInstrRef: |
426 | 0 | return hash_combine(MO.getType(), MO.getTargetFlags(), |
427 | 0 | MO.getInstrRefInstrIndex(), MO.getInstrRefOpIndex()); |
428 | 20 | case MachineOperand::MO_CFIIndex: |
429 | 20 | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex()); |
430 | 0 | case MachineOperand::MO_IntrinsicID: |
431 | 0 | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIntrinsicID()); |
432 | 0 | case MachineOperand::MO_Predicate: |
433 | 0 | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getPredicate()); |
434 | 0 | case MachineOperand::MO_ShuffleMask: |
435 | 0 | return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getShuffleMask()); |
436 | 29.6M | } |
437 | 0 | llvm_unreachable("Invalid machine operand type"); |
438 | 0 | } |
439 | | |
440 | | // Try to crawl up to the machine function and get TRI and IntrinsicInfo from |
441 | | // it. |
442 | | static void tryToGetTargetInfo(const MachineOperand &MO, |
443 | | const TargetRegisterInfo *&TRI, |
444 | 0 | const TargetIntrinsicInfo *&IntrinsicInfo) { |
445 | 0 | if (const MachineFunction *MF = getMFIfAvailable(MO)) { |
446 | 0 | TRI = MF->getSubtarget().getRegisterInfo(); |
447 | 0 | IntrinsicInfo = MF->getTarget().getIntrinsicInfo(); |
448 | 0 | } |
449 | 0 | } |
450 | | |
451 | 0 | static const char *getTargetIndexName(const MachineFunction &MF, int Index) { |
452 | 0 | const auto *TII = MF.getSubtarget().getInstrInfo(); |
453 | 0 | assert(TII && "expected instruction info"); |
454 | 0 | auto Indices = TII->getSerializableTargetIndices(); |
455 | 0 | auto Found = find_if(Indices, [&](const std::pair<int, const char *> &I) { |
456 | 0 | return I.first == Index; |
457 | 0 | }); |
458 | 0 | if (Found != Indices.end()) |
459 | 0 | return Found->second; |
460 | 0 | return nullptr; |
461 | 0 | } |
462 | | |
463 | 0 | const char *MachineOperand::getTargetIndexName() const { |
464 | 0 | const MachineFunction *MF = getMFIfAvailable(*this); |
465 | 0 | return MF ? ::getTargetIndexName(*MF, this->getIndex()) : nullptr; |
466 | 0 | } |
467 | | |
468 | 0 | static const char *getTargetFlagName(const TargetInstrInfo *TII, unsigned TF) { |
469 | 0 | auto Flags = TII->getSerializableDirectMachineOperandTargetFlags(); |
470 | 0 | for (const auto &I : Flags) { |
471 | 0 | if (I.first == TF) { |
472 | 0 | return I.second; |
473 | 0 | } |
474 | 0 | } |
475 | 0 | return nullptr; |
476 | 0 | } |
477 | | |
478 | | static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS, |
479 | 0 | const TargetRegisterInfo *TRI) { |
480 | 0 | if (!TRI) { |
481 | 0 | OS << "%dwarfreg." << DwarfReg; |
482 | 0 | return; |
483 | 0 | } |
484 | | |
485 | 0 | if (std::optional<unsigned> Reg = TRI->getLLVMRegNum(DwarfReg, true)) |
486 | 0 | OS << printReg(*Reg, TRI); |
487 | 0 | else |
488 | 0 | OS << "<badreg>"; |
489 | 0 | } |
490 | | |
491 | | static void printIRBlockReference(raw_ostream &OS, const BasicBlock &BB, |
492 | 0 | ModuleSlotTracker &MST) { |
493 | 0 | OS << "%ir-block."; |
494 | 0 | if (BB.hasName()) { |
495 | 0 | printLLVMNameWithoutPrefix(OS, BB.getName()); |
496 | 0 | return; |
497 | 0 | } |
498 | 0 | std::optional<int> Slot; |
499 | 0 | if (const Function *F = BB.getParent()) { |
500 | 0 | if (F == MST.getCurrentFunction()) { |
501 | 0 | Slot = MST.getLocalSlot(&BB); |
502 | 0 | } else if (const Module *M = F->getParent()) { |
503 | 0 | ModuleSlotTracker CustomMST(M, /*ShouldInitializeAllMetadata=*/false); |
504 | 0 | CustomMST.incorporateFunction(*F); |
505 | 0 | Slot = CustomMST.getLocalSlot(&BB); |
506 | 0 | } |
507 | 0 | } |
508 | 0 | if (Slot) |
509 | 0 | MachineOperand::printIRSlotNumber(OS, *Slot); |
510 | 0 | else |
511 | 0 | OS << "<unknown>"; |
512 | 0 | } |
513 | | |
514 | | static void printSyncScope(raw_ostream &OS, const LLVMContext &Context, |
515 | | SyncScope::ID SSID, |
516 | 0 | SmallVectorImpl<StringRef> &SSNs) { |
517 | 0 | switch (SSID) { |
518 | 0 | case SyncScope::System: |
519 | 0 | break; |
520 | 0 | default: |
521 | 0 | if (SSNs.empty()) |
522 | 0 | Context.getSyncScopeNames(SSNs); |
523 | |
|
524 | 0 | OS << "syncscope(\""; |
525 | 0 | printEscapedString(SSNs[SSID], OS); |
526 | 0 | OS << "\") "; |
527 | 0 | break; |
528 | 0 | } |
529 | 0 | } |
530 | | |
531 | | static const char *getTargetMMOFlagName(const TargetInstrInfo &TII, |
532 | 0 | unsigned TMMOFlag) { |
533 | 0 | auto Flags = TII.getSerializableMachineMemOperandTargetFlags(); |
534 | 0 | for (const auto &I : Flags) { |
535 | 0 | if (I.first == TMMOFlag) { |
536 | 0 | return I.second; |
537 | 0 | } |
538 | 0 | } |
539 | 0 | return nullptr; |
540 | 0 | } |
541 | | |
542 | | static void printFrameIndex(raw_ostream& OS, int FrameIndex, bool IsFixed, |
543 | 0 | const MachineFrameInfo *MFI) { |
544 | 0 | StringRef Name; |
545 | 0 | if (MFI) { |
546 | 0 | IsFixed = MFI->isFixedObjectIndex(FrameIndex); |
547 | 0 | if (const AllocaInst *Alloca = MFI->getObjectAllocation(FrameIndex)) |
548 | 0 | if (Alloca->hasName()) |
549 | 0 | Name = Alloca->getName(); |
550 | 0 | if (IsFixed) |
551 | 0 | FrameIndex -= MFI->getObjectIndexBegin(); |
552 | 0 | } |
553 | 0 | MachineOperand::printStackObjectReference(OS, FrameIndex, IsFixed, Name); |
554 | 0 | } |
555 | | |
556 | | void MachineOperand::printSubRegIdx(raw_ostream &OS, uint64_t Index, |
557 | 0 | const TargetRegisterInfo *TRI) { |
558 | 0 | OS << "%subreg."; |
559 | 0 | if (TRI && Index != 0 && Index < TRI->getNumSubRegIndices()) |
560 | 0 | OS << TRI->getSubRegIndexName(Index); |
561 | 0 | else |
562 | 0 | OS << Index; |
563 | 0 | } |
564 | | |
565 | | void MachineOperand::printTargetFlags(raw_ostream &OS, |
566 | 0 | const MachineOperand &Op) { |
567 | 0 | if (!Op.getTargetFlags()) |
568 | 0 | return; |
569 | 0 | const MachineFunction *MF = getMFIfAvailable(Op); |
570 | 0 | if (!MF) |
571 | 0 | return; |
572 | | |
573 | 0 | const auto *TII = MF->getSubtarget().getInstrInfo(); |
574 | 0 | assert(TII && "expected instruction info"); |
575 | 0 | auto Flags = TII->decomposeMachineOperandsTargetFlags(Op.getTargetFlags()); |
576 | 0 | OS << "target-flags("; |
577 | 0 | const bool HasDirectFlags = Flags.first; |
578 | 0 | const bool HasBitmaskFlags = Flags.second; |
579 | 0 | if (!HasDirectFlags && !HasBitmaskFlags) { |
580 | 0 | OS << "<unknown>) "; |
581 | 0 | return; |
582 | 0 | } |
583 | 0 | if (HasDirectFlags) { |
584 | 0 | if (const auto *Name = getTargetFlagName(TII, Flags.first)) |
585 | 0 | OS << Name; |
586 | 0 | else |
587 | 0 | OS << "<unknown target flag>"; |
588 | 0 | } |
589 | 0 | if (!HasBitmaskFlags) { |
590 | 0 | OS << ") "; |
591 | 0 | return; |
592 | 0 | } |
593 | 0 | bool IsCommaNeeded = HasDirectFlags; |
594 | 0 | unsigned BitMask = Flags.second; |
595 | 0 | auto BitMasks = TII->getSerializableBitmaskMachineOperandTargetFlags(); |
596 | 0 | for (const auto &Mask : BitMasks) { |
597 | | // Check if the flag's bitmask has the bits of the current mask set. |
598 | 0 | if ((BitMask & Mask.first) == Mask.first) { |
599 | 0 | if (IsCommaNeeded) |
600 | 0 | OS << ", "; |
601 | 0 | IsCommaNeeded = true; |
602 | 0 | OS << Mask.second; |
603 | | // Clear the bits which were serialized from the flag's bitmask. |
604 | 0 | BitMask &= ~(Mask.first); |
605 | 0 | } |
606 | 0 | } |
607 | 0 | if (BitMask) { |
608 | | // When the resulting flag's bitmask isn't zero, we know that we didn't |
609 | | // serialize all of the bit flags. |
610 | 0 | if (IsCommaNeeded) |
611 | 0 | OS << ", "; |
612 | 0 | OS << "<unknown bitmask target flag>"; |
613 | 0 | } |
614 | 0 | OS << ") "; |
615 | 0 | } |
616 | | |
617 | 0 | void MachineOperand::printSymbol(raw_ostream &OS, MCSymbol &Sym) { |
618 | 0 | OS << "<mcsymbol " << Sym << ">"; |
619 | 0 | } |
620 | | |
621 | | void MachineOperand::printStackObjectReference(raw_ostream &OS, |
622 | | unsigned FrameIndex, |
623 | 0 | bool IsFixed, StringRef Name) { |
624 | 0 | if (IsFixed) { |
625 | 0 | OS << "%fixed-stack." << FrameIndex; |
626 | 0 | return; |
627 | 0 | } |
628 | | |
629 | 0 | OS << "%stack." << FrameIndex; |
630 | 0 | if (!Name.empty()) |
631 | 0 | OS << '.' << Name; |
632 | 0 | } |
633 | | |
634 | 0 | void MachineOperand::printOperandOffset(raw_ostream &OS, int64_t Offset) { |
635 | 0 | if (Offset == 0) |
636 | 0 | return; |
637 | 0 | if (Offset < 0) { |
638 | 0 | OS << " - " << -Offset; |
639 | 0 | return; |
640 | 0 | } |
641 | 0 | OS << " + " << Offset; |
642 | 0 | } |
643 | | |
644 | 0 | void MachineOperand::printIRSlotNumber(raw_ostream &OS, int Slot) { |
645 | 0 | if (Slot == -1) |
646 | 0 | OS << "<badref>"; |
647 | 0 | else |
648 | 0 | OS << Slot; |
649 | 0 | } |
650 | | |
651 | | static void printCFI(raw_ostream &OS, const MCCFIInstruction &CFI, |
652 | 0 | const TargetRegisterInfo *TRI) { |
653 | 0 | switch (CFI.getOperation()) { |
654 | 0 | case MCCFIInstruction::OpSameValue: |
655 | 0 | OS << "same_value "; |
656 | 0 | if (MCSymbol *Label = CFI.getLabel()) |
657 | 0 | MachineOperand::printSymbol(OS, *Label); |
658 | 0 | printCFIRegister(CFI.getRegister(), OS, TRI); |
659 | 0 | break; |
660 | 0 | case MCCFIInstruction::OpRememberState: |
661 | 0 | OS << "remember_state "; |
662 | 0 | if (MCSymbol *Label = CFI.getLabel()) |
663 | 0 | MachineOperand::printSymbol(OS, *Label); |
664 | 0 | break; |
665 | 0 | case MCCFIInstruction::OpRestoreState: |
666 | 0 | OS << "restore_state "; |
667 | 0 | if (MCSymbol *Label = CFI.getLabel()) |
668 | 0 | MachineOperand::printSymbol(OS, *Label); |
669 | 0 | break; |
670 | 0 | case MCCFIInstruction::OpOffset: |
671 | 0 | OS << "offset "; |
672 | 0 | if (MCSymbol *Label = CFI.getLabel()) |
673 | 0 | MachineOperand::printSymbol(OS, *Label); |
674 | 0 | printCFIRegister(CFI.getRegister(), OS, TRI); |
675 | 0 | OS << ", " << CFI.getOffset(); |
676 | 0 | break; |
677 | 0 | case MCCFIInstruction::OpDefCfaRegister: |
678 | 0 | OS << "def_cfa_register "; |
679 | 0 | if (MCSymbol *Label = CFI.getLabel()) |
680 | 0 | MachineOperand::printSymbol(OS, *Label); |
681 | 0 | printCFIRegister(CFI.getRegister(), OS, TRI); |
682 | 0 | break; |
683 | 0 | case MCCFIInstruction::OpDefCfaOffset: |
684 | 0 | OS << "def_cfa_offset "; |
685 | 0 | if (MCSymbol *Label = CFI.getLabel()) |
686 | 0 | MachineOperand::printSymbol(OS, *Label); |
687 | 0 | OS << CFI.getOffset(); |
688 | 0 | break; |
689 | 0 | case MCCFIInstruction::OpDefCfa: |
690 | 0 | OS << "def_cfa "; |
691 | 0 | if (MCSymbol *Label = CFI.getLabel()) |
692 | 0 | MachineOperand::printSymbol(OS, *Label); |
693 | 0 | printCFIRegister(CFI.getRegister(), OS, TRI); |
694 | 0 | OS << ", " << CFI.getOffset(); |
695 | 0 | break; |
696 | 0 | case MCCFIInstruction::OpLLVMDefAspaceCfa: |
697 | 0 | OS << "llvm_def_aspace_cfa "; |
698 | 0 | if (MCSymbol *Label = CFI.getLabel()) |
699 | 0 | MachineOperand::printSymbol(OS, *Label); |
700 | 0 | printCFIRegister(CFI.getRegister(), OS, TRI); |
701 | 0 | OS << ", " << CFI.getOffset(); |
702 | 0 | OS << ", " << CFI.getAddressSpace(); |
703 | 0 | break; |
704 | 0 | case MCCFIInstruction::OpRelOffset: |
705 | 0 | OS << "rel_offset "; |
706 | 0 | if (MCSymbol *Label = CFI.getLabel()) |
707 | 0 | MachineOperand::printSymbol(OS, *Label); |
708 | 0 | printCFIRegister(CFI.getRegister(), OS, TRI); |
709 | 0 | OS << ", " << CFI.getOffset(); |
710 | 0 | break; |
711 | 0 | case MCCFIInstruction::OpAdjustCfaOffset: |
712 | 0 | OS << "adjust_cfa_offset "; |
713 | 0 | if (MCSymbol *Label = CFI.getLabel()) |
714 | 0 | MachineOperand::printSymbol(OS, *Label); |
715 | 0 | OS << CFI.getOffset(); |
716 | 0 | break; |
717 | 0 | case MCCFIInstruction::OpRestore: |
718 | 0 | OS << "restore "; |
719 | 0 | if (MCSymbol *Label = CFI.getLabel()) |
720 | 0 | MachineOperand::printSymbol(OS, *Label); |
721 | 0 | printCFIRegister(CFI.getRegister(), OS, TRI); |
722 | 0 | break; |
723 | 0 | case MCCFIInstruction::OpEscape: { |
724 | 0 | OS << "escape "; |
725 | 0 | if (MCSymbol *Label = CFI.getLabel()) |
726 | 0 | MachineOperand::printSymbol(OS, *Label); |
727 | 0 | if (!CFI.getValues().empty()) { |
728 | 0 | size_t e = CFI.getValues().size() - 1; |
729 | 0 | for (size_t i = 0; i < e; ++i) |
730 | 0 | OS << format("0x%02x", uint8_t(CFI.getValues()[i])) << ", "; |
731 | 0 | OS << format("0x%02x", uint8_t(CFI.getValues()[e])); |
732 | 0 | } |
733 | 0 | break; |
734 | 0 | } |
735 | 0 | case MCCFIInstruction::OpUndefined: |
736 | 0 | OS << "undefined "; |
737 | 0 | if (MCSymbol *Label = CFI.getLabel()) |
738 | 0 | MachineOperand::printSymbol(OS, *Label); |
739 | 0 | printCFIRegister(CFI.getRegister(), OS, TRI); |
740 | 0 | break; |
741 | 0 | case MCCFIInstruction::OpRegister: |
742 | 0 | OS << "register "; |
743 | 0 | if (MCSymbol *Label = CFI.getLabel()) |
744 | 0 | MachineOperand::printSymbol(OS, *Label); |
745 | 0 | printCFIRegister(CFI.getRegister(), OS, TRI); |
746 | 0 | OS << ", "; |
747 | 0 | printCFIRegister(CFI.getRegister2(), OS, TRI); |
748 | 0 | break; |
749 | 0 | case MCCFIInstruction::OpWindowSave: |
750 | 0 | OS << "window_save "; |
751 | 0 | if (MCSymbol *Label = CFI.getLabel()) |
752 | 0 | MachineOperand::printSymbol(OS, *Label); |
753 | 0 | break; |
754 | 0 | case MCCFIInstruction::OpNegateRAState: |
755 | 0 | OS << "negate_ra_sign_state "; |
756 | 0 | if (MCSymbol *Label = CFI.getLabel()) |
757 | 0 | MachineOperand::printSymbol(OS, *Label); |
758 | 0 | break; |
759 | 0 | default: |
760 | | // TODO: Print the other CFI Operations. |
761 | 0 | OS << "<unserializable cfi directive>"; |
762 | 0 | break; |
763 | 0 | } |
764 | 0 | } |
765 | | |
766 | | void MachineOperand::print(raw_ostream &OS, const TargetRegisterInfo *TRI, |
767 | 0 | const TargetIntrinsicInfo *IntrinsicInfo) const { |
768 | 0 | print(OS, LLT{}, TRI, IntrinsicInfo); |
769 | 0 | } |
770 | | |
771 | | void MachineOperand::print(raw_ostream &OS, LLT TypeToPrint, |
772 | | const TargetRegisterInfo *TRI, |
773 | 0 | const TargetIntrinsicInfo *IntrinsicInfo) const { |
774 | 0 | tryToGetTargetInfo(*this, TRI, IntrinsicInfo); |
775 | 0 | ModuleSlotTracker DummyMST(nullptr); |
776 | 0 | print(OS, DummyMST, TypeToPrint, std::nullopt, /*PrintDef=*/false, |
777 | 0 | /*IsStandalone=*/true, |
778 | 0 | /*ShouldPrintRegisterTies=*/true, |
779 | 0 | /*TiedOperandIdx=*/0, TRI, IntrinsicInfo); |
780 | 0 | } |
781 | | |
782 | | void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST, |
783 | | LLT TypeToPrint, std::optional<unsigned> OpIdx, |
784 | | bool PrintDef, bool IsStandalone, |
785 | | bool ShouldPrintRegisterTies, |
786 | | unsigned TiedOperandIdx, |
787 | | const TargetRegisterInfo *TRI, |
788 | 0 | const TargetIntrinsicInfo *IntrinsicInfo) const { |
789 | 0 | printTargetFlags(OS, *this); |
790 | 0 | switch (getType()) { |
791 | 0 | case MachineOperand::MO_Register: { |
792 | 0 | Register Reg = getReg(); |
793 | 0 | if (isImplicit()) |
794 | 0 | OS << (isDef() ? "implicit-def " : "implicit "); |
795 | 0 | else if (PrintDef && isDef()) |
796 | | // Print the 'def' flag only when the operand is defined after '='. |
797 | 0 | OS << "def "; |
798 | 0 | if (isInternalRead()) |
799 | 0 | OS << "internal "; |
800 | 0 | if (isDead()) |
801 | 0 | OS << "dead "; |
802 | 0 | if (isKill()) |
803 | 0 | OS << "killed "; |
804 | 0 | if (isUndef()) |
805 | 0 | OS << "undef "; |
806 | 0 | if (isEarlyClobber()) |
807 | 0 | OS << "early-clobber "; |
808 | 0 | if (getReg().isPhysical() && isRenamable()) |
809 | 0 | OS << "renamable "; |
810 | | // isDebug() is exactly true for register operands of a DBG_VALUE. So we |
811 | | // simply infer it when parsing and do not need to print it. |
812 | |
|
813 | 0 | const MachineRegisterInfo *MRI = nullptr; |
814 | 0 | if (Reg.isVirtual()) { |
815 | 0 | if (const MachineFunction *MF = getMFIfAvailable(*this)) { |
816 | 0 | MRI = &MF->getRegInfo(); |
817 | 0 | } |
818 | 0 | } |
819 | |
|
820 | 0 | OS << printReg(Reg, TRI, 0, MRI); |
821 | | // Print the sub register. |
822 | 0 | if (unsigned SubReg = getSubReg()) { |
823 | 0 | if (TRI) |
824 | 0 | OS << '.' << TRI->getSubRegIndexName(SubReg); |
825 | 0 | else |
826 | 0 | OS << ".subreg" << SubReg; |
827 | 0 | } |
828 | | // Print the register class / bank. |
829 | 0 | if (Reg.isVirtual()) { |
830 | 0 | if (const MachineFunction *MF = getMFIfAvailable(*this)) { |
831 | 0 | const MachineRegisterInfo &MRI = MF->getRegInfo(); |
832 | 0 | if (IsStandalone || !PrintDef || MRI.def_empty(Reg)) { |
833 | 0 | OS << ':'; |
834 | 0 | OS << printRegClassOrBank(Reg, MRI, TRI); |
835 | 0 | } |
836 | 0 | } |
837 | 0 | } |
838 | | // Print ties. |
839 | 0 | if (ShouldPrintRegisterTies && isTied() && !isDef()) |
840 | 0 | OS << "(tied-def " << TiedOperandIdx << ")"; |
841 | | // Print types. |
842 | 0 | if (TypeToPrint.isValid()) |
843 | 0 | OS << '(' << TypeToPrint << ')'; |
844 | 0 | break; |
845 | 0 | } |
846 | 0 | case MachineOperand::MO_Immediate: { |
847 | 0 | const MIRFormatter *Formatter = nullptr; |
848 | 0 | if (const MachineFunction *MF = getMFIfAvailable(*this)) { |
849 | 0 | const auto *TII = MF->getSubtarget().getInstrInfo(); |
850 | 0 | assert(TII && "expected instruction info"); |
851 | 0 | Formatter = TII->getMIRFormatter(); |
852 | 0 | } |
853 | 0 | if (Formatter) |
854 | 0 | Formatter->printImm(OS, *getParent(), OpIdx, getImm()); |
855 | 0 | else |
856 | 0 | OS << getImm(); |
857 | 0 | break; |
858 | 0 | } |
859 | 0 | case MachineOperand::MO_CImmediate: |
860 | 0 | getCImm()->printAsOperand(OS, /*PrintType=*/true, MST); |
861 | 0 | break; |
862 | 0 | case MachineOperand::MO_FPImmediate: |
863 | 0 | getFPImm()->printAsOperand(OS, /*PrintType=*/true, MST); |
864 | 0 | break; |
865 | 0 | case MachineOperand::MO_MachineBasicBlock: |
866 | 0 | OS << printMBBReference(*getMBB()); |
867 | 0 | break; |
868 | 0 | case MachineOperand::MO_FrameIndex: { |
869 | 0 | int FrameIndex = getIndex(); |
870 | 0 | bool IsFixed = false; |
871 | 0 | const MachineFrameInfo *MFI = nullptr; |
872 | 0 | if (const MachineFunction *MF = getMFIfAvailable(*this)) |
873 | 0 | MFI = &MF->getFrameInfo(); |
874 | 0 | printFrameIndex(OS, FrameIndex, IsFixed, MFI); |
875 | 0 | break; |
876 | 0 | } |
877 | 0 | case MachineOperand::MO_ConstantPoolIndex: |
878 | 0 | OS << "%const." << getIndex(); |
879 | 0 | printOperandOffset(OS, getOffset()); |
880 | 0 | break; |
881 | 0 | case MachineOperand::MO_TargetIndex: { |
882 | 0 | OS << "target-index("; |
883 | 0 | const char *Name = "<unknown>"; |
884 | 0 | if (const MachineFunction *MF = getMFIfAvailable(*this)) |
885 | 0 | if (const auto *TargetIndexName = ::getTargetIndexName(*MF, getIndex())) |
886 | 0 | Name = TargetIndexName; |
887 | 0 | OS << Name << ')'; |
888 | 0 | printOperandOffset(OS, getOffset()); |
889 | 0 | break; |
890 | 0 | } |
891 | 0 | case MachineOperand::MO_JumpTableIndex: |
892 | 0 | OS << printJumpTableEntryReference(getIndex()); |
893 | 0 | break; |
894 | 0 | case MachineOperand::MO_GlobalAddress: |
895 | 0 | getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST); |
896 | 0 | printOperandOffset(OS, getOffset()); |
897 | 0 | break; |
898 | 0 | case MachineOperand::MO_ExternalSymbol: { |
899 | 0 | StringRef Name = getSymbolName(); |
900 | 0 | OS << '&'; |
901 | 0 | if (Name.empty()) { |
902 | 0 | OS << "\"\""; |
903 | 0 | } else { |
904 | 0 | printLLVMNameWithoutPrefix(OS, Name); |
905 | 0 | } |
906 | 0 | printOperandOffset(OS, getOffset()); |
907 | 0 | break; |
908 | 0 | } |
909 | 0 | case MachineOperand::MO_BlockAddress: { |
910 | 0 | OS << "blockaddress("; |
911 | 0 | getBlockAddress()->getFunction()->printAsOperand(OS, /*PrintType=*/false, |
912 | 0 | MST); |
913 | 0 | OS << ", "; |
914 | 0 | printIRBlockReference(OS, *getBlockAddress()->getBasicBlock(), MST); |
915 | 0 | OS << ')'; |
916 | 0 | MachineOperand::printOperandOffset(OS, getOffset()); |
917 | 0 | break; |
918 | 0 | } |
919 | 0 | case MachineOperand::MO_RegisterMask: { |
920 | 0 | OS << "<regmask"; |
921 | 0 | if (TRI) { |
922 | 0 | unsigned NumRegsInMask = 0; |
923 | 0 | unsigned NumRegsEmitted = 0; |
924 | 0 | for (unsigned i = 0; i < TRI->getNumRegs(); ++i) { |
925 | 0 | unsigned MaskWord = i / 32; |
926 | 0 | unsigned MaskBit = i % 32; |
927 | 0 | if (getRegMask()[MaskWord] & (1 << MaskBit)) { |
928 | 0 | if (PrintRegMaskNumRegs < 0 || |
929 | 0 | NumRegsEmitted <= static_cast<unsigned>(PrintRegMaskNumRegs)) { |
930 | 0 | OS << " " << printReg(i, TRI); |
931 | 0 | NumRegsEmitted++; |
932 | 0 | } |
933 | 0 | NumRegsInMask++; |
934 | 0 | } |
935 | 0 | } |
936 | 0 | if (NumRegsEmitted != NumRegsInMask) |
937 | 0 | OS << " and " << (NumRegsInMask - NumRegsEmitted) << " more..."; |
938 | 0 | } else { |
939 | 0 | OS << " ..."; |
940 | 0 | } |
941 | 0 | OS << ">"; |
942 | 0 | break; |
943 | 0 | } |
944 | 0 | case MachineOperand::MO_RegisterLiveOut: { |
945 | 0 | const uint32_t *RegMask = getRegLiveOut(); |
946 | 0 | OS << "liveout("; |
947 | 0 | if (!TRI) { |
948 | 0 | OS << "<unknown>"; |
949 | 0 | } else { |
950 | 0 | bool IsCommaNeeded = false; |
951 | 0 | for (unsigned Reg = 0, E = TRI->getNumRegs(); Reg < E; ++Reg) { |
952 | 0 | if (RegMask[Reg / 32] & (1U << (Reg % 32))) { |
953 | 0 | if (IsCommaNeeded) |
954 | 0 | OS << ", "; |
955 | 0 | OS << printReg(Reg, TRI); |
956 | 0 | IsCommaNeeded = true; |
957 | 0 | } |
958 | 0 | } |
959 | 0 | } |
960 | 0 | OS << ")"; |
961 | 0 | break; |
962 | 0 | } |
963 | 0 | case MachineOperand::MO_Metadata: |
964 | 0 | getMetadata()->printAsOperand(OS, MST); |
965 | 0 | break; |
966 | 0 | case MachineOperand::MO_MCSymbol: |
967 | 0 | printSymbol(OS, *getMCSymbol()); |
968 | 0 | break; |
969 | 0 | case MachineOperand::MO_DbgInstrRef: { |
970 | 0 | OS << "dbg-instr-ref(" << getInstrRefInstrIndex() << ", " |
971 | 0 | << getInstrRefOpIndex() << ')'; |
972 | 0 | break; |
973 | 0 | } |
974 | 0 | case MachineOperand::MO_CFIIndex: { |
975 | 0 | if (const MachineFunction *MF = getMFIfAvailable(*this)) |
976 | 0 | printCFI(OS, MF->getFrameInstructions()[getCFIIndex()], TRI); |
977 | 0 | else |
978 | 0 | OS << "<cfi directive>"; |
979 | 0 | break; |
980 | 0 | } |
981 | 0 | case MachineOperand::MO_IntrinsicID: { |
982 | 0 | Intrinsic::ID ID = getIntrinsicID(); |
983 | 0 | if (ID < Intrinsic::num_intrinsics) |
984 | 0 | OS << "intrinsic(@" << Intrinsic::getBaseName(ID) << ')'; |
985 | 0 | else if (IntrinsicInfo) |
986 | 0 | OS << "intrinsic(@" << IntrinsicInfo->getName(ID) << ')'; |
987 | 0 | else |
988 | 0 | OS << "intrinsic(" << ID << ')'; |
989 | 0 | break; |
990 | 0 | } |
991 | 0 | case MachineOperand::MO_Predicate: { |
992 | 0 | auto Pred = static_cast<CmpInst::Predicate>(getPredicate()); |
993 | 0 | OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred(" |
994 | 0 | << Pred << ')'; |
995 | 0 | break; |
996 | 0 | } |
997 | 0 | case MachineOperand::MO_ShuffleMask: |
998 | 0 | OS << "shufflemask("; |
999 | 0 | ArrayRef<int> Mask = getShuffleMask(); |
1000 | 0 | StringRef Separator; |
1001 | 0 | for (int Elt : Mask) { |
1002 | 0 | if (Elt == -1) |
1003 | 0 | OS << Separator << "undef"; |
1004 | 0 | else |
1005 | 0 | OS << Separator << Elt; |
1006 | 0 | Separator = ", "; |
1007 | 0 | } |
1008 | |
|
1009 | 0 | OS << ')'; |
1010 | 0 | break; |
1011 | 0 | } |
1012 | 0 | } |
1013 | | |
1014 | | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
1015 | 0 | LLVM_DUMP_METHOD void MachineOperand::dump() const { dbgs() << *this << '\n'; } |
1016 | | #endif |
1017 | | |
1018 | | //===----------------------------------------------------------------------===// |
1019 | | // MachineMemOperand Implementation |
1020 | | //===----------------------------------------------------------------------===// |
1021 | | |
1022 | | /// getAddrSpace - Return the LLVM IR address space number that this pointer |
1023 | | /// points into. |
1024 | 17.1M | unsigned MachinePointerInfo::getAddrSpace() const { return AddrSpace; } |
1025 | | |
1026 | | /// isDereferenceable - Return true if V is always dereferenceable for |
1027 | | /// Offset + Size byte. |
1028 | | bool MachinePointerInfo::isDereferenceable(unsigned Size, LLVMContext &C, |
1029 | 782 | const DataLayout &DL) const { |
1030 | 782 | if (!isa<const Value *>(V)) |
1031 | 65 | return false; |
1032 | | |
1033 | 717 | const Value *BasePtr = cast<const Value *>(V); |
1034 | 717 | if (BasePtr == nullptr) |
1035 | 22 | return false; |
1036 | | |
1037 | 695 | return isDereferenceableAndAlignedPointer( |
1038 | 695 | BasePtr, Align(1), APInt(DL.getPointerSizeInBits(), Offset + Size), DL); |
1039 | 717 | } |
1040 | | |
1041 | | /// getConstantPool - Return a MachinePointerInfo record that refers to the |
1042 | | /// constant pool. |
1043 | 36.6k | MachinePointerInfo MachinePointerInfo::getConstantPool(MachineFunction &MF) { |
1044 | 36.6k | return MachinePointerInfo(MF.getPSVManager().getConstantPool()); |
1045 | 36.6k | } |
1046 | | |
1047 | | /// getFixedStack - Return a MachinePointerInfo record that refers to the |
1048 | | /// the specified FrameIndex. |
1049 | | MachinePointerInfo MachinePointerInfo::getFixedStack(MachineFunction &MF, |
1050 | 843k | int FI, int64_t Offset) { |
1051 | 843k | return MachinePointerInfo(MF.getPSVManager().getFixedStack(FI), Offset); |
1052 | 843k | } |
1053 | | |
1054 | 87 | MachinePointerInfo MachinePointerInfo::getJumpTable(MachineFunction &MF) { |
1055 | 87 | return MachinePointerInfo(MF.getPSVManager().getJumpTable()); |
1056 | 87 | } |
1057 | | |
1058 | 32.9k | MachinePointerInfo MachinePointerInfo::getGOT(MachineFunction &MF) { |
1059 | 32.9k | return MachinePointerInfo(MF.getPSVManager().getGOT()); |
1060 | 32.9k | } |
1061 | | |
1062 | | MachinePointerInfo MachinePointerInfo::getStack(MachineFunction &MF, |
1063 | 6.72k | int64_t Offset, uint8_t ID) { |
1064 | 6.72k | return MachinePointerInfo(MF.getPSVManager().getStack(), Offset, ID); |
1065 | 6.72k | } |
1066 | | |
1067 | 15.0k | MachinePointerInfo MachinePointerInfo::getUnknownStack(MachineFunction &MF) { |
1068 | 15.0k | return MachinePointerInfo(MF.getDataLayout().getAllocaAddrSpace()); |
1069 | 15.0k | } |
1070 | | |
1071 | | MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, Flags f, |
1072 | | LLT type, Align a, const AAMDNodes &AAInfo, |
1073 | | const MDNode *Ranges, SyncScope::ID SSID, |
1074 | | AtomicOrdering Ordering, |
1075 | | AtomicOrdering FailureOrdering) |
1076 | | : PtrInfo(ptrinfo), MemoryType(type), FlagVals(f), BaseAlign(a), |
1077 | 2.67M | AAInfo(AAInfo), Ranges(Ranges) { |
1078 | 2.67M | assert((PtrInfo.V.isNull() || isa<const PseudoSourceValue *>(PtrInfo.V) || |
1079 | 2.67M | isa<PointerType>(cast<const Value *>(PtrInfo.V)->getType())) && |
1080 | 2.67M | "invalid pointer value"); |
1081 | 0 | assert((isLoad() || isStore()) && "Not a load/store!"); |
1082 | | |
1083 | 0 | AtomicInfo.SSID = static_cast<unsigned>(SSID); |
1084 | 2.67M | assert(getSyncScopeID() == SSID && "Value truncated"); |
1085 | 0 | AtomicInfo.Ordering = static_cast<unsigned>(Ordering); |
1086 | 2.67M | assert(getSuccessOrdering() == Ordering && "Value truncated"); |
1087 | 0 | AtomicInfo.FailureOrdering = static_cast<unsigned>(FailureOrdering); |
1088 | 2.67M | assert(getFailureOrdering() == FailureOrdering && "Value truncated"); |
1089 | 2.67M | } |
1090 | | |
1091 | | MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, Flags f, |
1092 | | uint64_t s, Align a, |
1093 | | const AAMDNodes &AAInfo, |
1094 | | const MDNode *Ranges, SyncScope::ID SSID, |
1095 | | AtomicOrdering Ordering, |
1096 | | AtomicOrdering FailureOrdering) |
1097 | | : MachineMemOperand(ptrinfo, f, |
1098 | | s == ~UINT64_C(0) ? LLT() : LLT::scalar(8 * s), a, |
1099 | 2.64M | AAInfo, Ranges, SSID, Ordering, FailureOrdering) {} |
1100 | | |
1101 | 127k | void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) { |
1102 | | // The Value and Offset may differ due to CSE. But the flags and size |
1103 | | // should be the same. |
1104 | 127k | assert(MMO->getFlags() == getFlags() && "Flags mismatch!"); |
1105 | 0 | assert((MMO->getSize() == ~UINT64_C(0) || getSize() == ~UINT64_C(0) || |
1106 | 127k | MMO->getSize() == getSize()) && |
1107 | 127k | "Size mismatch!"); |
1108 | | |
1109 | 127k | if (MMO->getBaseAlign() >= getBaseAlign()) { |
1110 | | // Update the alignment value. |
1111 | 127k | BaseAlign = MMO->getBaseAlign(); |
1112 | | // Also update the base and offset, because the new alignment may |
1113 | | // not be applicable with the old ones. |
1114 | 127k | PtrInfo = MMO->PtrInfo; |
1115 | 127k | } |
1116 | 127k | } |
1117 | | |
1118 | | /// getAlign - Return the minimum known alignment in bytes of the |
1119 | | /// actual memory reference. |
1120 | 7.53M | Align MachineMemOperand::getAlign() const { |
1121 | 7.53M | return commonAlignment(getBaseAlign(), getOffset()); |
1122 | 7.53M | } |
1123 | | |
1124 | | void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST, |
1125 | | SmallVectorImpl<StringRef> &SSNs, |
1126 | | const LLVMContext &Context, |
1127 | | const MachineFrameInfo *MFI, |
1128 | 0 | const TargetInstrInfo *TII) const { |
1129 | 0 | OS << '('; |
1130 | 0 | if (isVolatile()) |
1131 | 0 | OS << "volatile "; |
1132 | 0 | if (isNonTemporal()) |
1133 | 0 | OS << "non-temporal "; |
1134 | 0 | if (isDereferenceable()) |
1135 | 0 | OS << "dereferenceable "; |
1136 | 0 | if (isInvariant()) |
1137 | 0 | OS << "invariant "; |
1138 | 0 | if (TII) { |
1139 | 0 | if (getFlags() & MachineMemOperand::MOTargetFlag1) |
1140 | 0 | OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag1) |
1141 | 0 | << "\" "; |
1142 | 0 | if (getFlags() & MachineMemOperand::MOTargetFlag2) |
1143 | 0 | OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag2) |
1144 | 0 | << "\" "; |
1145 | 0 | if (getFlags() & MachineMemOperand::MOTargetFlag3) |
1146 | 0 | OS << '"' << getTargetMMOFlagName(*TII, MachineMemOperand::MOTargetFlag3) |
1147 | 0 | << "\" "; |
1148 | 0 | } else { |
1149 | 0 | if (getFlags() & MachineMemOperand::MOTargetFlag1) |
1150 | 0 | OS << "\"MOTargetFlag1\" "; |
1151 | 0 | if (getFlags() & MachineMemOperand::MOTargetFlag2) |
1152 | 0 | OS << "\"MOTargetFlag2\" "; |
1153 | 0 | if (getFlags() & MachineMemOperand::MOTargetFlag3) |
1154 | 0 | OS << "\"MOTargetFlag3\" "; |
1155 | 0 | } |
1156 | |
|
1157 | 0 | assert((isLoad() || isStore()) && |
1158 | 0 | "machine memory operand must be a load or store (or both)"); |
1159 | 0 | if (isLoad()) |
1160 | 0 | OS << "load "; |
1161 | 0 | if (isStore()) |
1162 | 0 | OS << "store "; |
1163 | |
|
1164 | 0 | printSyncScope(OS, Context, getSyncScopeID(), SSNs); |
1165 | |
|
1166 | 0 | if (getSuccessOrdering() != AtomicOrdering::NotAtomic) |
1167 | 0 | OS << toIRString(getSuccessOrdering()) << ' '; |
1168 | 0 | if (getFailureOrdering() != AtomicOrdering::NotAtomic) |
1169 | 0 | OS << toIRString(getFailureOrdering()) << ' '; |
1170 | |
|
1171 | 0 | if (getMemoryType().isValid()) |
1172 | 0 | OS << '(' << getMemoryType() << ')'; |
1173 | 0 | else |
1174 | 0 | OS << "unknown-size"; |
1175 | |
|
1176 | 0 | if (const Value *Val = getValue()) { |
1177 | 0 | OS << ((isLoad() && isStore()) ? " on " : isLoad() ? " from " : " into "); |
1178 | 0 | MIRFormatter::printIRValue(OS, *Val, MST); |
1179 | 0 | } else if (const PseudoSourceValue *PVal = getPseudoValue()) { |
1180 | 0 | OS << ((isLoad() && isStore()) ? " on " : isLoad() ? " from " : " into "); |
1181 | 0 | assert(PVal && "Expected a pseudo source value"); |
1182 | 0 | switch (PVal->kind()) { |
1183 | 0 | case PseudoSourceValue::Stack: |
1184 | 0 | OS << "stack"; |
1185 | 0 | break; |
1186 | 0 | case PseudoSourceValue::GOT: |
1187 | 0 | OS << "got"; |
1188 | 0 | break; |
1189 | 0 | case PseudoSourceValue::JumpTable: |
1190 | 0 | OS << "jump-table"; |
1191 | 0 | break; |
1192 | 0 | case PseudoSourceValue::ConstantPool: |
1193 | 0 | OS << "constant-pool"; |
1194 | 0 | break; |
1195 | 0 | case PseudoSourceValue::FixedStack: { |
1196 | 0 | int FrameIndex = cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex(); |
1197 | 0 | bool IsFixed = true; |
1198 | 0 | printFrameIndex(OS, FrameIndex, IsFixed, MFI); |
1199 | 0 | break; |
1200 | 0 | } |
1201 | 0 | case PseudoSourceValue::GlobalValueCallEntry: |
1202 | 0 | OS << "call-entry "; |
1203 | 0 | cast<GlobalValuePseudoSourceValue>(PVal)->getValue()->printAsOperand( |
1204 | 0 | OS, /*PrintType=*/false, MST); |
1205 | 0 | break; |
1206 | 0 | case PseudoSourceValue::ExternalSymbolCallEntry: |
1207 | 0 | OS << "call-entry &"; |
1208 | 0 | printLLVMNameWithoutPrefix( |
1209 | 0 | OS, cast<ExternalSymbolPseudoSourceValue>(PVal)->getSymbol()); |
1210 | 0 | break; |
1211 | 0 | default: { |
1212 | 0 | const MIRFormatter *Formatter = TII->getMIRFormatter(); |
1213 | | // FIXME: This is not necessarily the correct MIR serialization format for |
1214 | | // a custom pseudo source value, but at least it allows |
1215 | | // MIR printing to work on a target with custom pseudo source |
1216 | | // values. |
1217 | 0 | OS << "custom \""; |
1218 | 0 | Formatter->printCustomPseudoSourceValue(OS, MST, *PVal); |
1219 | 0 | OS << '\"'; |
1220 | 0 | break; |
1221 | 0 | } |
1222 | 0 | } |
1223 | 0 | } else if (getOpaqueValue() == nullptr && getOffset() != 0) { |
1224 | 0 | OS << ((isLoad() && isStore()) ? " on " |
1225 | 0 | : isLoad() ? " from " |
1226 | 0 | : " into ") |
1227 | 0 | << "unknown-address"; |
1228 | 0 | } |
1229 | 0 | MachineOperand::printOperandOffset(OS, getOffset()); |
1230 | 0 | if (getSize() > 0 && getAlign() != getSize()) |
1231 | 0 | OS << ", align " << getAlign().value(); |
1232 | 0 | if (getAlign() != getBaseAlign()) |
1233 | 0 | OS << ", basealign " << getBaseAlign().value(); |
1234 | 0 | auto AAInfo = getAAInfo(); |
1235 | 0 | if (AAInfo.TBAA) { |
1236 | 0 | OS << ", !tbaa "; |
1237 | 0 | AAInfo.TBAA->printAsOperand(OS, MST); |
1238 | 0 | } |
1239 | 0 | if (AAInfo.Scope) { |
1240 | 0 | OS << ", !alias.scope "; |
1241 | 0 | AAInfo.Scope->printAsOperand(OS, MST); |
1242 | 0 | } |
1243 | 0 | if (AAInfo.NoAlias) { |
1244 | 0 | OS << ", !noalias "; |
1245 | 0 | AAInfo.NoAlias->printAsOperand(OS, MST); |
1246 | 0 | } |
1247 | 0 | if (getRanges()) { |
1248 | 0 | OS << ", !range "; |
1249 | 0 | getRanges()->printAsOperand(OS, MST); |
1250 | 0 | } |
1251 | | // FIXME: Implement addrspace printing/parsing in MIR. |
1252 | | // For now, print this even though parsing it is not available in MIR. |
1253 | 0 | if (unsigned AS = getAddrSpace()) |
1254 | 0 | OS << ", addrspace " << AS; |
1255 | |
|
1256 | 0 | OS << ')'; |
1257 | 0 | } |