blob: def4d682decc525abd8f634c5cc3d2405977884a [file] [log] [blame]
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +00001//===- lib/CodeGen/MachineOperand.cpp -------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Francis Visoiu Mistrih3aa8eaa2017-11-28 19:23:39 +000010/// \file Methods common to all machine operands.
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/MachineOperand.h"
15#include "llvm/Analysis/Loads.h"
16#include "llvm/CodeGen/MIRPrinter.h"
17#include "llvm/CodeGen/MachineRegisterInfo.h"
18#include "llvm/Target/TargetIntrinsicInfo.h"
19#include "llvm/CodeGen/TargetRegisterInfo.h"
20#include "llvm/IR/Constants.h"
21#include "llvm/IR/ModuleSlotTracker.h"
22
23using namespace llvm;
24
25static cl::opt<int>
26 PrintRegMaskNumRegs("print-regmask-num-regs",
27 cl::desc("Number of registers to limit to when "
28 "printing regmask operands in IR dumps. "
29 "unlimited = -1"),
30 cl::init(32), cl::Hidden);
31
Francis Visoiu Mistrih95a05912017-12-06 11:55:42 +000032static const MachineFunction *getMFIfAvailable(const MachineOperand &MO) {
33 if (const MachineInstr *MI = MO.getParent())
34 if (const MachineBasicBlock *MBB = MI->getParent())
35 if (const MachineFunction *MF = MBB->getParent())
36 return MF;
37 return nullptr;
38}
39static MachineFunction *getMFIfAvailable(MachineOperand &MO) {
40 return const_cast<MachineFunction *>(
41 getMFIfAvailable(const_cast<const MachineOperand &>(MO)));
42}
43
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000044void MachineOperand::setReg(unsigned Reg) {
45 if (getReg() == Reg)
46 return; // No change.
47
48 // Otherwise, we have to change the register. If this operand is embedded
49 // into a machine function, we need to update the old and new register's
50 // use/def lists.
Francis Visoiu Mistrih95a05912017-12-06 11:55:42 +000051 if (MachineFunction *MF = getMFIfAvailable(*this)) {
52 MachineRegisterInfo &MRI = MF->getRegInfo();
53 MRI.removeRegOperandFromUseList(this);
54 SmallContents.RegNo = Reg;
55 MRI.addRegOperandToUseList(this);
56 return;
Francis Visoiu Mistrihc2641d62017-12-06 11:57:53 +000057 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +000058
59 // Otherwise, just change the register, no problem. :)
60 SmallContents.RegNo = Reg;
61}
62
63void MachineOperand::substVirtReg(unsigned Reg, unsigned SubIdx,
64 const TargetRegisterInfo &TRI) {
65 assert(TargetRegisterInfo::isVirtualRegister(Reg));
66 if (SubIdx && getSubReg())
67 SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg());
68 setReg(Reg);
69 if (SubIdx)
70 setSubReg(SubIdx);
71}
72
73void MachineOperand::substPhysReg(unsigned Reg, const TargetRegisterInfo &TRI) {
74 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
75 if (getSubReg()) {
76 Reg = TRI.getSubReg(Reg, getSubReg());
77 // Note that getSubReg() may return 0 if the sub-register doesn't exist.
78 // That won't happen in legal code.
79 setSubReg(0);
80 if (isDef())
81 setIsUndef(false);
82 }
83 setReg(Reg);
84}
85
86/// Change a def to a use, or a use to a def.
87void MachineOperand::setIsDef(bool Val) {
88 assert(isReg() && "Wrong MachineOperand accessor");
89 assert((!Val || !isDebug()) && "Marking a debug operation as def");
90 if (IsDef == Val)
91 return;
92 // MRI may keep uses and defs in different list positions.
Francis Visoiu Mistrih95a05912017-12-06 11:55:42 +000093 if (MachineFunction *MF = getMFIfAvailable(*this)) {
94 MachineRegisterInfo &MRI = MF->getRegInfo();
95 MRI.removeRegOperandFromUseList(this);
96 IsDef = Val;
97 MRI.addRegOperandToUseList(this);
98 return;
99 }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000100 IsDef = Val;
101}
102
103// If this operand is currently a register operand, and if this is in a
104// function, deregister the operand from the register's use/def list.
105void MachineOperand::removeRegFromUses() {
106 if (!isReg() || !isOnRegUseList())
107 return;
108
Francis Visoiu Mistrih95a05912017-12-06 11:55:42 +0000109 if (MachineFunction *MF = getMFIfAvailable(*this))
110 MF->getRegInfo().removeRegOperandFromUseList(this);
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000111}
112
113/// ChangeToImmediate - Replace this operand with a new immediate operand of
114/// the specified value. If an operand is known to be an immediate already,
115/// the setImm method should be used.
116void MachineOperand::ChangeToImmediate(int64_t ImmVal) {
117 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
118
119 removeRegFromUses();
120
121 OpKind = MO_Immediate;
122 Contents.ImmVal = ImmVal;
123}
124
125void MachineOperand::ChangeToFPImmediate(const ConstantFP *FPImm) {
126 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm");
127
128 removeRegFromUses();
129
130 OpKind = MO_FPImmediate;
131 Contents.CFP = FPImm;
132}
133
134void MachineOperand::ChangeToES(const char *SymName,
135 unsigned char TargetFlags) {
136 assert((!isReg() || !isTied()) &&
137 "Cannot change a tied operand into an external symbol");
138
139 removeRegFromUses();
140
141 OpKind = MO_ExternalSymbol;
142 Contents.OffsetedInfo.Val.SymbolName = SymName;
143 setOffset(0); // Offset is always 0.
144 setTargetFlags(TargetFlags);
145}
146
147void MachineOperand::ChangeToMCSymbol(MCSymbol *Sym) {
148 assert((!isReg() || !isTied()) &&
149 "Cannot change a tied operand into an MCSymbol");
150
151 removeRegFromUses();
152
153 OpKind = MO_MCSymbol;
154 Contents.Sym = Sym;
155}
156
157void MachineOperand::ChangeToFrameIndex(int Idx) {
158 assert((!isReg() || !isTied()) &&
159 "Cannot change a tied operand into a FrameIndex");
160
161 removeRegFromUses();
162
163 OpKind = MO_FrameIndex;
164 setIndex(Idx);
165}
166
167void MachineOperand::ChangeToTargetIndex(unsigned Idx, int64_t Offset,
168 unsigned char TargetFlags) {
169 assert((!isReg() || !isTied()) &&
170 "Cannot change a tied operand into a FrameIndex");
171
172 removeRegFromUses();
173
174 OpKind = MO_TargetIndex;
175 setIndex(Idx);
176 setOffset(Offset);
177 setTargetFlags(TargetFlags);
178}
179
180/// ChangeToRegister - Replace this operand with a new register operand of
181/// the specified value. If an operand is known to be an register already,
182/// the setReg method should be used.
183void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp,
184 bool isKill, bool isDead, bool isUndef,
185 bool isDebug) {
186 MachineRegisterInfo *RegInfo = nullptr;
Francis Visoiu Mistrih95a05912017-12-06 11:55:42 +0000187 if (MachineFunction *MF = getMFIfAvailable(*this))
188 RegInfo = &MF->getRegInfo();
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000189 // If this operand is already a register operand, remove it from the
190 // register's use/def lists.
191 bool WasReg = isReg();
192 if (RegInfo && WasReg)
193 RegInfo->removeRegOperandFromUseList(this);
194
195 // Change this to a register and set the reg#.
196 OpKind = MO_Register;
197 SmallContents.RegNo = Reg;
198 SubReg_TargetFlags = 0;
199 IsDef = isDef;
200 IsImp = isImp;
201 IsKill = isKill;
202 IsDead = isDead;
203 IsUndef = isUndef;
204 IsInternalRead = false;
205 IsEarlyClobber = false;
206 IsDebug = isDebug;
207 // Ensure isOnRegUseList() returns false.
208 Contents.Reg.Prev = nullptr;
209 // Preserve the tie when the operand was already a register.
210 if (!WasReg)
211 TiedTo = 0;
212
213 // If this operand is embedded in a function, add the operand to the
214 // register's use/def list.
215 if (RegInfo)
216 RegInfo->addRegOperandToUseList(this);
217}
218
219/// isIdenticalTo - Return true if this operand is identical to the specified
220/// operand. Note that this should stay in sync with the hash_value overload
221/// below.
222bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
223 if (getType() != Other.getType() ||
224 getTargetFlags() != Other.getTargetFlags())
225 return false;
226
227 switch (getType()) {
228 case MachineOperand::MO_Register:
229 return getReg() == Other.getReg() && isDef() == Other.isDef() &&
230 getSubReg() == Other.getSubReg();
231 case MachineOperand::MO_Immediate:
232 return getImm() == Other.getImm();
233 case MachineOperand::MO_CImmediate:
234 return getCImm() == Other.getCImm();
235 case MachineOperand::MO_FPImmediate:
236 return getFPImm() == Other.getFPImm();
237 case MachineOperand::MO_MachineBasicBlock:
238 return getMBB() == Other.getMBB();
239 case MachineOperand::MO_FrameIndex:
240 return getIndex() == Other.getIndex();
241 case MachineOperand::MO_ConstantPoolIndex:
242 case MachineOperand::MO_TargetIndex:
243 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset();
244 case MachineOperand::MO_JumpTableIndex:
245 return getIndex() == Other.getIndex();
246 case MachineOperand::MO_GlobalAddress:
247 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
248 case MachineOperand::MO_ExternalSymbol:
249 return strcmp(getSymbolName(), Other.getSymbolName()) == 0 &&
250 getOffset() == Other.getOffset();
251 case MachineOperand::MO_BlockAddress:
252 return getBlockAddress() == Other.getBlockAddress() &&
253 getOffset() == Other.getOffset();
254 case MachineOperand::MO_RegisterMask:
255 case MachineOperand::MO_RegisterLiveOut: {
256 // Shallow compare of the two RegMasks
257 const uint32_t *RegMask = getRegMask();
258 const uint32_t *OtherRegMask = Other.getRegMask();
259 if (RegMask == OtherRegMask)
260 return true;
261
Francis Visoiu Mistrih95a05912017-12-06 11:55:42 +0000262 if (const MachineFunction *MF = getMFIfAvailable(*this)) {
263 // Calculate the size of the RegMask
264 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
265 unsigned RegMaskSize = (TRI->getNumRegs() + 31) / 32;
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000266
Francis Visoiu Mistrih95a05912017-12-06 11:55:42 +0000267 // Deep compare of the two RegMasks
268 return std::equal(RegMask, RegMask + RegMaskSize, OtherRegMask);
269 }
270 // We don't know the size of the RegMask, so we can't deep compare the two
271 // reg masks.
272 return false;
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000273 }
274 case MachineOperand::MO_MCSymbol:
275 return getMCSymbol() == Other.getMCSymbol();
276 case MachineOperand::MO_CFIIndex:
277 return getCFIIndex() == Other.getCFIIndex();
278 case MachineOperand::MO_Metadata:
279 return getMetadata() == Other.getMetadata();
280 case MachineOperand::MO_IntrinsicID:
281 return getIntrinsicID() == Other.getIntrinsicID();
282 case MachineOperand::MO_Predicate:
283 return getPredicate() == Other.getPredicate();
284 }
285 llvm_unreachable("Invalid machine operand type");
286}
287
288// Note: this must stay exactly in sync with isIdenticalTo above.
289hash_code llvm::hash_value(const MachineOperand &MO) {
290 switch (MO.getType()) {
291 case MachineOperand::MO_Register:
292 // Register operands don't have target flags.
293 return hash_combine(MO.getType(), MO.getReg(), MO.getSubReg(), MO.isDef());
294 case MachineOperand::MO_Immediate:
295 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm());
296 case MachineOperand::MO_CImmediate:
297 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm());
298 case MachineOperand::MO_FPImmediate:
299 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm());
300 case MachineOperand::MO_MachineBasicBlock:
301 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB());
302 case MachineOperand::MO_FrameIndex:
303 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
304 case MachineOperand::MO_ConstantPoolIndex:
305 case MachineOperand::MO_TargetIndex:
306 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(),
307 MO.getOffset());
308 case MachineOperand::MO_JumpTableIndex:
309 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex());
310 case MachineOperand::MO_ExternalSymbol:
311 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(),
312 MO.getSymbolName());
313 case MachineOperand::MO_GlobalAddress:
314 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(),
315 MO.getOffset());
316 case MachineOperand::MO_BlockAddress:
317 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getBlockAddress(),
318 MO.getOffset());
319 case MachineOperand::MO_RegisterMask:
320 case MachineOperand::MO_RegisterLiveOut:
321 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getRegMask());
322 case MachineOperand::MO_Metadata:
323 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata());
324 case MachineOperand::MO_MCSymbol:
325 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol());
326 case MachineOperand::MO_CFIIndex:
327 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex());
328 case MachineOperand::MO_IntrinsicID:
329 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIntrinsicID());
330 case MachineOperand::MO_Predicate:
331 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getPredicate());
332 }
333 llvm_unreachable("Invalid machine operand type");
334}
335
336void MachineOperand::print(raw_ostream &OS, const TargetRegisterInfo *TRI,
337 const TargetIntrinsicInfo *IntrinsicInfo) const {
338 ModuleSlotTracker DummyMST(nullptr);
339 print(OS, DummyMST, TRI, IntrinsicInfo);
340}
341
342void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
343 const TargetRegisterInfo *TRI,
344 const TargetIntrinsicInfo *IntrinsicInfo) const {
345 switch (getType()) {
346 case MachineOperand::MO_Register:
347 OS << printReg(getReg(), TRI, getSubReg());
348
349 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() ||
350 isInternalRead() || isEarlyClobber() || isTied()) {
351 OS << '<';
352 bool NeedComma = false;
353 if (isDef()) {
354 if (NeedComma)
355 OS << ',';
356 if (isEarlyClobber())
357 OS << "earlyclobber,";
358 if (isImplicit())
359 OS << "imp-";
360 OS << "def";
361 NeedComma = true;
362 // <def,read-undef> only makes sense when getSubReg() is set.
363 // Don't clutter the output otherwise.
364 if (isUndef() && getSubReg())
365 OS << ",read-undef";
366 } else if (isImplicit()) {
367 OS << "imp-use";
368 NeedComma = true;
369 }
370
371 if (isKill()) {
372 if (NeedComma)
373 OS << ',';
374 OS << "kill";
375 NeedComma = true;
376 }
377 if (isDead()) {
378 if (NeedComma)
379 OS << ',';
380 OS << "dead";
381 NeedComma = true;
382 }
383 if (isUndef() && isUse()) {
384 if (NeedComma)
385 OS << ',';
386 OS << "undef";
387 NeedComma = true;
388 }
389 if (isInternalRead()) {
390 if (NeedComma)
391 OS << ',';
392 OS << "internal";
393 NeedComma = true;
394 }
395 if (isTied()) {
396 if (NeedComma)
397 OS << ',';
398 OS << "tied";
399 if (TiedTo != 15)
400 OS << unsigned(TiedTo - 1);
401 }
402 OS << '>';
403 }
404 break;
405 case MachineOperand::MO_Immediate:
406 OS << getImm();
407 break;
408 case MachineOperand::MO_CImmediate:
409 getCImm()->getValue().print(OS, false);
410 break;
411 case MachineOperand::MO_FPImmediate:
412 if (getFPImm()->getType()->isFloatTy()) {
413 OS << getFPImm()->getValueAPF().convertToFloat();
414 } else if (getFPImm()->getType()->isHalfTy()) {
415 APFloat APF = getFPImm()->getValueAPF();
416 bool Unused;
417 APF.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &Unused);
418 OS << "half " << APF.convertToFloat();
419 } else if (getFPImm()->getType()->isFP128Ty()) {
420 APFloat APF = getFPImm()->getValueAPF();
421 SmallString<16> Str;
422 getFPImm()->getValueAPF().toString(Str);
423 OS << "quad " << Str;
424 } else if (getFPImm()->getType()->isX86_FP80Ty()) {
425 APFloat APF = getFPImm()->getValueAPF();
426 OS << "x86_fp80 0xK";
427 APInt API = APF.bitcastToAPInt();
428 OS << format_hex_no_prefix(API.getHiBits(16).getZExtValue(), 4,
429 /*Upper=*/true);
430 OS << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
431 /*Upper=*/true);
432 } else {
433 OS << getFPImm()->getValueAPF().convertToDouble();
434 }
435 break;
436 case MachineOperand::MO_MachineBasicBlock:
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000437 OS << printMBBReference(*getMBB());
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000438 break;
439 case MachineOperand::MO_FrameIndex:
440 OS << "<fi#" << getIndex() << '>';
441 break;
442 case MachineOperand::MO_ConstantPoolIndex:
443 OS << "<cp#" << getIndex();
444 if (getOffset())
445 OS << "+" << getOffset();
446 OS << '>';
447 break;
448 case MachineOperand::MO_TargetIndex:
449 OS << "<ti#" << getIndex();
450 if (getOffset())
451 OS << "+" << getOffset();
452 OS << '>';
453 break;
454 case MachineOperand::MO_JumpTableIndex:
455 OS << "<jt#" << getIndex() << '>';
456 break;
457 case MachineOperand::MO_GlobalAddress:
458 OS << "<ga:";
459 getGlobal()->printAsOperand(OS, /*PrintType=*/false, MST);
460 if (getOffset())
461 OS << "+" << getOffset();
462 OS << '>';
463 break;
464 case MachineOperand::MO_ExternalSymbol:
465 OS << "<es:" << getSymbolName();
466 if (getOffset())
467 OS << "+" << getOffset();
468 OS << '>';
469 break;
470 case MachineOperand::MO_BlockAddress:
471 OS << '<';
472 getBlockAddress()->printAsOperand(OS, /*PrintType=*/false, MST);
473 if (getOffset())
474 OS << "+" << getOffset();
475 OS << '>';
476 break;
477 case MachineOperand::MO_RegisterMask: {
478 unsigned NumRegsInMask = 0;
479 unsigned NumRegsEmitted = 0;
480 OS << "<regmask";
481 for (unsigned i = 0; i < TRI->getNumRegs(); ++i) {
482 unsigned MaskWord = i / 32;
483 unsigned MaskBit = i % 32;
484 if (getRegMask()[MaskWord] & (1 << MaskBit)) {
485 if (PrintRegMaskNumRegs < 0 ||
486 NumRegsEmitted <= static_cast<unsigned>(PrintRegMaskNumRegs)) {
487 OS << " " << printReg(i, TRI);
488 NumRegsEmitted++;
489 }
490 NumRegsInMask++;
491 }
492 }
493 if (NumRegsEmitted != NumRegsInMask)
494 OS << " and " << (NumRegsInMask - NumRegsEmitted) << " more...";
495 OS << ">";
496 break;
497 }
498 case MachineOperand::MO_RegisterLiveOut:
499 OS << "<regliveout>";
500 break;
501 case MachineOperand::MO_Metadata:
502 OS << '<';
503 getMetadata()->printAsOperand(OS, MST);
504 OS << '>';
505 break;
506 case MachineOperand::MO_MCSymbol:
507 OS << "<MCSym=" << *getMCSymbol() << '>';
508 break;
509 case MachineOperand::MO_CFIIndex:
510 OS << "<call frame instruction>";
511 break;
512 case MachineOperand::MO_IntrinsicID: {
513 Intrinsic::ID ID = getIntrinsicID();
514 if (ID < Intrinsic::num_intrinsics)
515 OS << "<intrinsic:@" << Intrinsic::getName(ID, None) << '>';
516 else if (IntrinsicInfo)
517 OS << "<intrinsic:@" << IntrinsicInfo->getName(ID) << '>';
518 else
519 OS << "<intrinsic:" << ID << '>';
520 break;
521 }
522 case MachineOperand::MO_Predicate: {
523 auto Pred = static_cast<CmpInst::Predicate>(getPredicate());
524 OS << '<' << (CmpInst::isIntPredicate(Pred) ? "intpred" : "floatpred")
525 << CmpInst::getPredicateName(Pred) << '>';
526 break;
527 }
528 }
529 if (unsigned TF = getTargetFlags())
530 OS << "[TF=" << TF << ']';
531}
532
533#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
534LLVM_DUMP_METHOD void MachineOperand::dump() const { dbgs() << *this << '\n'; }
535#endif
536
537//===----------------------------------------------------------------------===//
538// MachineMemOperand Implementation
539//===----------------------------------------------------------------------===//
540
541/// getAddrSpace - Return the LLVM IR address space number that this pointer
542/// points into.
Yaxun Liu49477042017-12-02 22:13:22 +0000543unsigned MachinePointerInfo::getAddrSpace() const { return AddrSpace; }
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000544
545/// isDereferenceable - Return true if V is always dereferenceable for
546/// Offset + Size byte.
547bool MachinePointerInfo::isDereferenceable(unsigned Size, LLVMContext &C,
548 const DataLayout &DL) const {
549 if (!V.is<const Value *>())
550 return false;
551
552 const Value *BasePtr = V.get<const Value *>();
553 if (BasePtr == nullptr)
554 return false;
555
556 return isDereferenceableAndAlignedPointer(
557 BasePtr, 1, APInt(DL.getPointerSizeInBits(), Offset + Size), DL);
558}
559
560/// getConstantPool - Return a MachinePointerInfo record that refers to the
561/// constant pool.
562MachinePointerInfo MachinePointerInfo::getConstantPool(MachineFunction &MF) {
563 return MachinePointerInfo(MF.getPSVManager().getConstantPool());
564}
565
566/// getFixedStack - Return a MachinePointerInfo record that refers to the
567/// the specified FrameIndex.
568MachinePointerInfo MachinePointerInfo::getFixedStack(MachineFunction &MF,
569 int FI, int64_t Offset) {
570 return MachinePointerInfo(MF.getPSVManager().getFixedStack(FI), Offset);
571}
572
573MachinePointerInfo MachinePointerInfo::getJumpTable(MachineFunction &MF) {
574 return MachinePointerInfo(MF.getPSVManager().getJumpTable());
575}
576
577MachinePointerInfo MachinePointerInfo::getGOT(MachineFunction &MF) {
578 return MachinePointerInfo(MF.getPSVManager().getGOT());
579}
580
581MachinePointerInfo MachinePointerInfo::getStack(MachineFunction &MF,
582 int64_t Offset, uint8_t ID) {
583 return MachinePointerInfo(MF.getPSVManager().getStack(), Offset, ID);
584}
585
Yaxun Liu49477042017-12-02 22:13:22 +0000586MachinePointerInfo MachinePointerInfo::getUnknownStack(MachineFunction &MF) {
587 return MachinePointerInfo(MF.getDataLayout().getAllocaAddrSpace());
588}
589
Francis Visoiu Mistrihaa739692017-11-28 17:58:43 +0000590MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, Flags f,
591 uint64_t s, unsigned int a,
592 const AAMDNodes &AAInfo,
593 const MDNode *Ranges, SyncScope::ID SSID,
594 AtomicOrdering Ordering,
595 AtomicOrdering FailureOrdering)
596 : PtrInfo(ptrinfo), Size(s), FlagVals(f), BaseAlignLog2(Log2_32(a) + 1),
597 AAInfo(AAInfo), Ranges(Ranges) {
598 assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue *>() ||
599 isa<PointerType>(PtrInfo.V.get<const Value *>()->getType())) &&
600 "invalid pointer value");
601 assert(getBaseAlignment() == a && "Alignment is not a power of 2!");
602 assert((isLoad() || isStore()) && "Not a load/store!");
603
604 AtomicInfo.SSID = static_cast<unsigned>(SSID);
605 assert(getSyncScopeID() == SSID && "Value truncated");
606 AtomicInfo.Ordering = static_cast<unsigned>(Ordering);
607 assert(getOrdering() == Ordering && "Value truncated");
608 AtomicInfo.FailureOrdering = static_cast<unsigned>(FailureOrdering);
609 assert(getFailureOrdering() == FailureOrdering && "Value truncated");
610}
611
612/// Profile - Gather unique data for the object.
613///
614void MachineMemOperand::Profile(FoldingSetNodeID &ID) const {
615 ID.AddInteger(getOffset());
616 ID.AddInteger(Size);
617 ID.AddPointer(getOpaqueValue());
618 ID.AddInteger(getFlags());
619 ID.AddInteger(getBaseAlignment());
620}
621
622void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) {
623 // The Value and Offset may differ due to CSE. But the flags and size
624 // should be the same.
625 assert(MMO->getFlags() == getFlags() && "Flags mismatch!");
626 assert(MMO->getSize() == getSize() && "Size mismatch!");
627
628 if (MMO->getBaseAlignment() >= getBaseAlignment()) {
629 // Update the alignment value.
630 BaseAlignLog2 = Log2_32(MMO->getBaseAlignment()) + 1;
631 // Also update the base and offset, because the new alignment may
632 // not be applicable with the old ones.
633 PtrInfo = MMO->PtrInfo;
634 }
635}
636
637/// getAlignment - Return the minimum known alignment in bytes of the
638/// actual memory reference.
639uint64_t MachineMemOperand::getAlignment() const {
640 return MinAlign(getBaseAlignment(), getOffset());
641}
642
643void MachineMemOperand::print(raw_ostream &OS) const {
644 ModuleSlotTracker DummyMST(nullptr);
645 print(OS, DummyMST);
646}
647void MachineMemOperand::print(raw_ostream &OS, ModuleSlotTracker &MST) const {
648 assert((isLoad() || isStore()) && "SV has to be a load, store or both.");
649
650 if (isVolatile())
651 OS << "Volatile ";
652
653 if (isLoad())
654 OS << "LD";
655 if (isStore())
656 OS << "ST";
657 OS << getSize();
658
659 // Print the address information.
660 OS << "[";
661 if (const Value *V = getValue())
662 V->printAsOperand(OS, /*PrintType=*/false, MST);
663 else if (const PseudoSourceValue *PSV = getPseudoValue())
664 PSV->printCustom(OS);
665 else
666 OS << "<unknown>";
667
668 unsigned AS = getAddrSpace();
669 if (AS != 0)
670 OS << "(addrspace=" << AS << ')';
671
672 // If the alignment of the memory reference itself differs from the alignment
673 // of the base pointer, print the base alignment explicitly, next to the base
674 // pointer.
675 if (getBaseAlignment() != getAlignment())
676 OS << "(align=" << getBaseAlignment() << ")";
677
678 if (getOffset() != 0)
679 OS << "+" << getOffset();
680 OS << "]";
681
682 // Print the alignment of the reference.
683 if (getBaseAlignment() != getAlignment() || getBaseAlignment() != getSize())
684 OS << "(align=" << getAlignment() << ")";
685
686 // Print TBAA info.
687 if (const MDNode *TBAAInfo = getAAInfo().TBAA) {
688 OS << "(tbaa=";
689 if (TBAAInfo->getNumOperands() > 0)
690 TBAAInfo->getOperand(0)->printAsOperand(OS, MST);
691 else
692 OS << "<unknown>";
693 OS << ")";
694 }
695
696 // Print AA scope info.
697 if (const MDNode *ScopeInfo = getAAInfo().Scope) {
698 OS << "(alias.scope=";
699 if (ScopeInfo->getNumOperands() > 0)
700 for (unsigned i = 0, ie = ScopeInfo->getNumOperands(); i != ie; ++i) {
701 ScopeInfo->getOperand(i)->printAsOperand(OS, MST);
702 if (i != ie - 1)
703 OS << ",";
704 }
705 else
706 OS << "<unknown>";
707 OS << ")";
708 }
709
710 // Print AA noalias scope info.
711 if (const MDNode *NoAliasInfo = getAAInfo().NoAlias) {
712 OS << "(noalias=";
713 if (NoAliasInfo->getNumOperands() > 0)
714 for (unsigned i = 0, ie = NoAliasInfo->getNumOperands(); i != ie; ++i) {
715 NoAliasInfo->getOperand(i)->printAsOperand(OS, MST);
716 if (i != ie - 1)
717 OS << ",";
718 }
719 else
720 OS << "<unknown>";
721 OS << ")";
722 }
723
724 if (const MDNode *Ranges = getRanges()) {
725 unsigned NumRanges = Ranges->getNumOperands();
726 if (NumRanges != 0) {
727 OS << "(ranges=";
728
729 for (unsigned I = 0; I != NumRanges; ++I) {
730 Ranges->getOperand(I)->printAsOperand(OS, MST);
731 if (I != NumRanges - 1)
732 OS << ',';
733 }
734
735 OS << ')';
736 }
737 }
738
739 if (isNonTemporal())
740 OS << "(nontemporal)";
741 if (isDereferenceable())
742 OS << "(dereferenceable)";
743 if (isInvariant())
744 OS << "(invariant)";
745 if (getFlags() & MOTargetFlag1)
746 OS << "(flag1)";
747 if (getFlags() & MOTargetFlag2)
748 OS << "(flag2)";
749 if (getFlags() & MOTargetFlag3)
750 OS << "(flag3)";
751}