LLVM  7.0.0svn
MachineIRBuilder.cpp
Go to the documentation of this file.
1 //===-- llvm/CodeGen/GlobalISel/MachineIRBuilder.cpp - MIBuilder--*- C++ -*-==//
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 /// \file
10 /// This file implements the MachineIRBuidler class.
11 //===----------------------------------------------------------------------===//
13 
21 #include "llvm/IR/DebugInfo.h"
22 
23 using namespace llvm;
24 
26  State.MF = &MF;
27  State.MBB = nullptr;
28  State.MRI = &MF.getRegInfo();
29  State.TII = MF.getSubtarget().getInstrInfo();
30  State.DL = DebugLoc();
32  State.InsertedInstr = nullptr;
33 }
34 
36  State.MBB = &MBB;
37  State.II = MBB.end();
38  assert(&getMF() == MBB.getParent() &&
39  "Basic block is in a different function");
40 }
41 
43  assert(MI.getParent() && "Instruction is not part of a basic block");
44  setMBB(*MI.getParent());
45  State.II = MI.getIterator();
46 }
47 
50  assert(MBB.getParent() == &getMF() &&
51  "Basic block is in a different function");
52  State.MBB = &MBB;
53  State.II = II;
54 }
55 
57  if (State.InsertedInstr)
58  State.InsertedInstr(InsertedInstr);
59 }
60 
62  std::function<void(MachineInstr *)> Inserted) {
63  State.InsertedInstr = std::move(Inserted);
64 }
65 
67  State.InsertedInstr = nullptr;
68 }
69 
70 //------------------------------------------------------------------------------
71 // Build instruction variants.
72 //------------------------------------------------------------------------------
73 
75  return insertInstr(buildInstrNoInsert(Opcode));
76 }
77 
79  MachineInstrBuilder MIB = BuildMI(getMF(), getDL(), getTII().get(Opcode));
80  return MIB;
81 }
82 
84  getMBB().insert(getInsertPt(), MIB);
85  recordInsertion(MIB);
86  return MIB;
87 }
88 
91  const MDNode *Expr) {
92  assert(isa<DILocalVariable>(Variable) && "not a variable");
93  assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
94  assert(
95  cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(getDL()) &&
96  "Expected inlined-at fields to agree");
97  return insertInstr(BuildMI(getMF(), getDL(),
98  getTII().get(TargetOpcode::DBG_VALUE),
99  /*IsIndirect*/ false, Reg, Variable, Expr));
100 }
101 
103  unsigned Reg, const MDNode *Variable, const MDNode *Expr) {
104  assert(isa<DILocalVariable>(Variable) && "not a variable");
105  assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
106  assert(
107  cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(getDL()) &&
108  "Expected inlined-at fields to agree");
109  return insertInstr(BuildMI(getMF(), getDL(),
110  getTII().get(TargetOpcode::DBG_VALUE),
111  /*IsIndirect*/ true, Reg, Variable, Expr));
112 }
113 
116  const MDNode *Expr) {
117  assert(isa<DILocalVariable>(Variable) && "not a variable");
118  assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
119  assert(
120  cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(getDL()) &&
121  "Expected inlined-at fields to agree");
122  return buildInstr(TargetOpcode::DBG_VALUE)
123  .addFrameIndex(FI)
124  .addImm(0)
125  .addMetadata(Variable)
126  .addMetadata(Expr);
127 }
128 
130  const Constant &C, const MDNode *Variable, const MDNode *Expr) {
131  assert(isa<DILocalVariable>(Variable) && "not a variable");
132  assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
133  assert(
134  cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(getDL()) &&
135  "Expected inlined-at fields to agree");
136  auto MIB = buildInstr(TargetOpcode::DBG_VALUE);
137  if (auto *CI = dyn_cast<ConstantInt>(&C)) {
138  if (CI->getBitWidth() > 64)
139  MIB.addCImm(CI);
140  else
141  MIB.addImm(CI->getZExtValue());
142  } else if (auto *CFP = dyn_cast<ConstantFP>(&C)) {
143  MIB.addFPImm(CFP);
144  } else {
145  // Insert %noreg if we didn't find a usable constant and had to drop it.
146  MIB.addReg(0U);
147  }
148 
149  return MIB.addImm(0).addMetadata(Variable).addMetadata(Expr);
150 }
151 
153  int Idx) {
154  assert(getMRI()->getType(Res).isPointer() && "invalid operand type");
155  return buildInstr(TargetOpcode::G_FRAME_INDEX)
156  .addDef(Res)
157  .addFrameIndex(Idx);
158 }
159 
162  assert(getMRI()->getType(Res).isPointer() && "invalid operand type");
163  assert(getMRI()->getType(Res).getAddressSpace() ==
164  GV->getType()->getAddressSpace() &&
165  "address space mismatch");
166 
167  return buildInstr(TargetOpcode::G_GLOBAL_VALUE)
168  .addDef(Res)
169  .addGlobalAddress(GV);
170 }
171 
172 void MachineIRBuilderBase::validateBinaryOp(unsigned Res, unsigned Op0,
173  unsigned Op1) {
174  assert((getMRI()->getType(Res).isScalar() ||
175  getMRI()->getType(Res).isVector()) &&
176  "invalid operand type");
177  assert(getMRI()->getType(Res) == getMRI()->getType(Op0) &&
178  getMRI()->getType(Res) == getMRI()->getType(Op1) && "type mismatch");
179 }
180 
182  unsigned Op1) {
183  assert(getMRI()->getType(Res).isPointer() &&
184  getMRI()->getType(Res) == getMRI()->getType(Op0) && "type mismatch");
185  assert(getMRI()->getType(Op1).isScalar() && "invalid offset type");
186 
187  return buildInstr(TargetOpcode::G_GEP)
188  .addDef(Res)
189  .addUse(Op0)
190  .addUse(Op1);
191 }
192 
194 MachineIRBuilderBase::materializeGEP(unsigned &Res, unsigned Op0,
195  const LLT &ValueTy, uint64_t Value) {
196  assert(Res == 0 && "Res is a result argument");
197  assert(ValueTy.isScalar() && "invalid offset type");
198 
199  if (Value == 0) {
200  Res = Op0;
201  return None;
202  }
203 
205  unsigned TmpReg = getMRI()->createGenericVirtualRegister(ValueTy);
206 
207  buildConstant(TmpReg, Value);
208  return buildGEP(Res, Op0, TmpReg);
209 }
210 
212  unsigned Op0,
213  uint32_t NumBits) {
214  assert(getMRI()->getType(Res).isPointer() &&
215  getMRI()->getType(Res) == getMRI()->getType(Op0) && "type mismatch");
216 
217  return buildInstr(TargetOpcode::G_PTR_MASK)
218  .addDef(Res)
219  .addUse(Op0)
220  .addImm(NumBits);
221 }
222 
224  return buildInstr(TargetOpcode::G_BR).addMBB(&Dest);
225 }
226 
228  assert(getMRI()->getType(Tgt).isPointer() && "invalid branch destination");
229  return buildInstr(TargetOpcode::G_BRINDIRECT).addUse(Tgt);
230 }
231 
233  assert(getMRI()->getType(Res) == LLT() || getMRI()->getType(Op) == LLT() ||
234  getMRI()->getType(Res) == getMRI()->getType(Op));
235  return buildInstr(TargetOpcode::COPY).addDef(Res).addUse(Op);
236 }
237 
240  LLT Ty = getMRI()->getType(Res);
241 
242  assert((Ty.isScalar() || Ty.isPointer()) && "invalid operand type");
243 
244  const ConstantInt *NewVal = &Val;
245  if (Ty.getSizeInBits() != Val.getBitWidth())
247  Val.getValue().sextOrTrunc(Ty.getSizeInBits()));
248 
249  return buildInstr(TargetOpcode::G_CONSTANT).addDef(Res).addCImm(NewVal);
250 }
251 
253  int64_t Val) {
254  auto IntN = IntegerType::get(getMF().getFunction().getContext(),
255  getMRI()->getType(Res).getSizeInBits());
256  ConstantInt *CI = ConstantInt::get(IntN, Val, true);
257  return buildConstant(Res, *CI);
258 }
259 
262  assert(getMRI()->getType(Res).isScalar() && "invalid operand type");
263 
264  return buildInstr(TargetOpcode::G_FCONSTANT).addDef(Res).addFPImm(&Val);
265 }
266 
268  double Val) {
269  LLT DstTy = getMRI()->getType(Res);
270  auto &Ctx = getMF().getFunction().getContext();
271  auto *CFP =
273  return buildFConstant(Res, *CFP);
274 }
275 
277  MachineBasicBlock &Dest) {
278  assert(getMRI()->getType(Tst).isScalar() && "invalid operand type");
279 
280  return buildInstr(TargetOpcode::G_BRCOND).addUse(Tst).addMBB(&Dest);
281 }
282 
284  MachineMemOperand &MMO) {
285  return buildLoadInstr(TargetOpcode::G_LOAD, Res, Addr, MMO);
286 }
287 
289 MachineIRBuilderBase::buildLoadInstr(unsigned Opcode, unsigned Res,
290  unsigned Addr, MachineMemOperand &MMO) {
291  assert(getMRI()->getType(Res).isValid() && "invalid operand type");
292  assert(getMRI()->getType(Addr).isPointer() && "invalid operand type");
293 
294  return buildInstr(Opcode)
295  .addDef(Res)
296  .addUse(Addr)
297  .addMemOperand(&MMO);
298 }
299 
301  unsigned Addr,
302  MachineMemOperand &MMO) {
303  assert(getMRI()->getType(Val).isValid() && "invalid operand type");
304  assert(getMRI()->getType(Addr).isPointer() && "invalid operand type");
305 
306  return buildInstr(TargetOpcode::G_STORE)
307  .addUse(Val)
308  .addUse(Addr)
309  .addMemOperand(&MMO);
310 }
311 
313  unsigned CarryOut,
314  unsigned Op0, unsigned Op1,
315  unsigned CarryIn) {
316  assert(getMRI()->getType(Res).isScalar() && "invalid operand type");
317  assert(getMRI()->getType(Res) == getMRI()->getType(Op0) &&
318  getMRI()->getType(Res) == getMRI()->getType(Op1) && "type mismatch");
319  assert(getMRI()->getType(CarryOut).isScalar() && "invalid operand type");
320  assert(getMRI()->getType(CarryOut) == getMRI()->getType(CarryIn) &&
321  "type mismatch");
322 
323  return buildInstr(TargetOpcode::G_UADDE)
324  .addDef(Res)
325  .addDef(CarryOut)
326  .addUse(Op0)
327  .addUse(Op1)
328  .addUse(CarryIn);
329 }
330 
332  unsigned Op) {
333  validateTruncExt(Res, Op, true);
334  return buildInstr(TargetOpcode::G_ANYEXT).addDef(Res).addUse(Op);
335 }
336 
338  validateTruncExt(Res, Op, true);
339  return buildInstr(TargetOpcode::G_SEXT).addDef(Res).addUse(Op);
340 }
341 
343  validateTruncExt(Res, Op, true);
344  return buildInstr(TargetOpcode::G_ZEXT).addDef(Res).addUse(Op);
345 }
346 
348  unsigned Res,
349  unsigned Op) {
350  assert((TargetOpcode::G_ANYEXT == ExtOpc || TargetOpcode::G_ZEXT == ExtOpc ||
351  TargetOpcode::G_SEXT == ExtOpc) &&
352  "Expecting Extending Opc");
353  assert(getMRI()->getType(Res).isScalar() ||
354  getMRI()->getType(Res).isVector());
355  assert(getMRI()->getType(Res).isScalar() == getMRI()->getType(Op).isScalar());
356 
357  unsigned Opcode = TargetOpcode::COPY;
358  if (getMRI()->getType(Res).getSizeInBits() >
359  getMRI()->getType(Op).getSizeInBits())
360  Opcode = ExtOpc;
361  else if (getMRI()->getType(Res).getSizeInBits() <
362  getMRI()->getType(Op).getSizeInBits())
363  Opcode = TargetOpcode::G_TRUNC;
364  else
365  assert(getMRI()->getType(Res) == getMRI()->getType(Op));
366 
367  return buildInstr(Opcode).addDef(Res).addUse(Op);
368 }
369 
371  unsigned Op) {
372  return buildExtOrTrunc(TargetOpcode::G_SEXT, Res, Op);
373 }
374 
376  unsigned Op) {
377  return buildExtOrTrunc(TargetOpcode::G_ZEXT, Res, Op);
378 }
379 
381  unsigned Op) {
382  return buildExtOrTrunc(TargetOpcode::G_ANYEXT, Res, Op);
383 }
384 
386  unsigned Src) {
387  LLT SrcTy = getMRI()->getType(Src);
388  LLT DstTy = getMRI()->getType(Dst);
389  if (SrcTy == DstTy)
390  return buildCopy(Dst, Src);
391 
392  unsigned Opcode;
393  if (SrcTy.isPointer() && DstTy.isScalar())
394  Opcode = TargetOpcode::G_PTRTOINT;
395  else if (DstTy.isPointer() && SrcTy.isScalar())
396  Opcode = TargetOpcode::G_INTTOPTR;
397  else {
398  assert(!SrcTy.isPointer() && !DstTy.isPointer() && "n G_ADDRCAST yet");
399  Opcode = TargetOpcode::G_BITCAST;
400  }
401 
402  return buildInstr(Opcode).addDef(Dst).addUse(Src);
403 }
404 
406 MachineIRBuilderBase::buildExtract(unsigned Res, unsigned Src, uint64_t Index) {
407 #ifndef NDEBUG
408  assert(getMRI()->getType(Src).isValid() && "invalid operand type");
409  assert(getMRI()->getType(Res).isValid() && "invalid operand type");
410  assert(Index + getMRI()->getType(Res).getSizeInBits() <=
411  getMRI()->getType(Src).getSizeInBits() &&
412  "extracting off end of register");
413 #endif
414 
415  if (getMRI()->getType(Res).getSizeInBits() ==
416  getMRI()->getType(Src).getSizeInBits()) {
417  assert(Index == 0 && "insertion past the end of a register");
418  return buildCast(Res, Src);
419  }
420 
421  return buildInstr(TargetOpcode::G_EXTRACT)
422  .addDef(Res)
423  .addUse(Src)
424  .addImm(Index);
425 }
426 
428  ArrayRef<uint64_t> Indices) {
429 #ifndef NDEBUG
430  assert(Ops.size() == Indices.size() && "incompatible args");
431  assert(!Ops.empty() && "invalid trivial sequence");
432  assert(std::is_sorted(Indices.begin(), Indices.end()) &&
433  "sequence offsets must be in ascending order");
434 
435  assert(getMRI()->getType(Res).isValid() && "invalid operand type");
436  for (auto Op : Ops)
437  assert(getMRI()->getType(Op).isValid() && "invalid operand type");
438 #endif
439 
440  LLT ResTy = getMRI()->getType(Res);
441  LLT OpTy = getMRI()->getType(Ops[0]);
442  unsigned OpSize = OpTy.getSizeInBits();
443  bool MaybeMerge = true;
444  for (unsigned i = 0; i < Ops.size(); ++i) {
445  if (getMRI()->getType(Ops[i]) != OpTy || Indices[i] != i * OpSize) {
446  MaybeMerge = false;
447  break;
448  }
449  }
450 
451  if (MaybeMerge && Ops.size() * OpSize == ResTy.getSizeInBits()) {
452  buildMerge(Res, Ops);
453  return;
454  }
455 
456  unsigned ResIn = getMRI()->createGenericVirtualRegister(ResTy);
457  buildUndef(ResIn);
458 
459  for (unsigned i = 0; i < Ops.size(); ++i) {
460  unsigned ResOut = i + 1 == Ops.size()
461  ? Res
463  buildInsert(ResOut, ResIn, Ops[i], Indices[i]);
464  ResIn = ResOut;
465  }
466 }
467 
469  return buildInstr(TargetOpcode::G_IMPLICIT_DEF).addDef(Res);
470 }
471 
473  ArrayRef<unsigned> Ops) {
474 
475 #ifndef NDEBUG
476  assert(!Ops.empty() && "invalid trivial sequence");
477  LLT Ty = getMRI()->getType(Ops[0]);
478  for (auto Reg : Ops)
479  assert(getMRI()->getType(Reg) == Ty && "type mismatch in input list");
480  assert(Ops.size() * getMRI()->getType(Ops[0]).getSizeInBits() ==
481  getMRI()->getType(Res).getSizeInBits() &&
482  "input operands do not cover output register");
483 #endif
484 
485  if (Ops.size() == 1)
486  return buildCast(Res, Ops[0]);
487 
488  MachineInstrBuilder MIB = buildInstr(TargetOpcode::G_MERGE_VALUES);
489  MIB.addDef(Res);
490  for (unsigned i = 0; i < Ops.size(); ++i)
491  MIB.addUse(Ops[i]);
492  return MIB;
493 }
494 
496  unsigned Op) {
497 
498 #ifndef NDEBUG
499  assert(!Res.empty() && "invalid trivial sequence");
500  LLT Ty = getMRI()->getType(Res[0]);
501  for (auto Reg : Res)
502  assert(getMRI()->getType(Reg) == Ty && "type mismatch in input list");
503  assert(Res.size() * getMRI()->getType(Res[0]).getSizeInBits() ==
504  getMRI()->getType(Op).getSizeInBits() &&
505  "input operands do not cover output register");
506 #endif
507 
508  MachineInstrBuilder MIB = buildInstr(TargetOpcode::G_UNMERGE_VALUES);
509  for (unsigned i = 0; i < Res.size(); ++i)
510  MIB.addDef(Res[i]);
511  MIB.addUse(Op);
512  return MIB;
513 }
514 
516  unsigned Src, unsigned Op,
517  unsigned Index) {
518  assert(Index + getMRI()->getType(Op).getSizeInBits() <=
519  getMRI()->getType(Res).getSizeInBits() &&
520  "insertion past the end of a register");
521 
522  if (getMRI()->getType(Res).getSizeInBits() ==
523  getMRI()->getType(Op).getSizeInBits()) {
524  return buildCast(Res, Op);
525  }
526 
527  return buildInstr(TargetOpcode::G_INSERT)
528  .addDef(Res)
529  .addUse(Src)
530  .addUse(Op)
531  .addImm(Index);
532 }
533 
535  unsigned Res,
536  bool HasSideEffects) {
537  auto MIB =
538  buildInstr(HasSideEffects ? TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS
539  : TargetOpcode::G_INTRINSIC);
540  if (Res)
541  MIB.addDef(Res);
542  MIB.addIntrinsicID(ID);
543  return MIB;
544 }
545 
547  unsigned Op) {
548  validateTruncExt(Res, Op, false);
549  return buildInstr(TargetOpcode::G_TRUNC).addDef(Res).addUse(Op);
550 }
551 
553  unsigned Op) {
554  validateTruncExt(Res, Op, false);
555  return buildInstr(TargetOpcode::G_FPTRUNC).addDef(Res).addUse(Op);
556 }
557 
559  unsigned Res, unsigned Op0,
560  unsigned Op1) {
561 #ifndef NDEBUG
562  assert(getMRI()->getType(Op0) == getMRI()->getType(Op0) && "type mismatch");
563  assert(CmpInst::isIntPredicate(Pred) && "invalid predicate");
564  if (getMRI()->getType(Op0).isScalar() || getMRI()->getType(Op0).isPointer())
565  assert(getMRI()->getType(Res).isScalar() && "type mismatch");
566  else
567  assert(getMRI()->getType(Res).isVector() &&
568  getMRI()->getType(Res).getNumElements() ==
569  getMRI()->getType(Op0).getNumElements() &&
570  "type mismatch");
571 #endif
572 
573  return buildInstr(TargetOpcode::G_ICMP)
574  .addDef(Res)
575  .addPredicate(Pred)
576  .addUse(Op0)
577  .addUse(Op1);
578 }
579 
581  unsigned Res, unsigned Op0,
582  unsigned Op1) {
583 #ifndef NDEBUG
584  assert((getMRI()->getType(Op0).isScalar() ||
585  getMRI()->getType(Op0).isVector()) &&
586  "invalid operand type");
587  assert(getMRI()->getType(Op0) == getMRI()->getType(Op1) && "type mismatch");
588  assert(CmpInst::isFPPredicate(Pred) && "invalid predicate");
589  if (getMRI()->getType(Op0).isScalar())
590  assert(getMRI()->getType(Res).isScalar() && "type mismatch");
591  else
592  assert(getMRI()->getType(Res).isVector() &&
593  getMRI()->getType(Res).getNumElements() ==
594  getMRI()->getType(Op0).getNumElements() &&
595  "type mismatch");
596 #endif
597 
598  return buildInstr(TargetOpcode::G_FCMP)
599  .addDef(Res)
600  .addPredicate(Pred)
601  .addUse(Op0)
602  .addUse(Op1);
603 }
604 
606  unsigned Tst,
607  unsigned Op0,
608  unsigned Op1) {
609 #ifndef NDEBUG
610  LLT ResTy = getMRI()->getType(Res);
611  assert((ResTy.isScalar() || ResTy.isVector() || ResTy.isPointer()) &&
612  "invalid operand type");
613  assert(ResTy == getMRI()->getType(Op0) && ResTy == getMRI()->getType(Op1) &&
614  "type mismatch");
615  if (ResTy.isScalar() || ResTy.isPointer())
616  assert(getMRI()->getType(Tst).isScalar() && "type mismatch");
617  else
618  assert((getMRI()->getType(Tst).isScalar() ||
619  (getMRI()->getType(Tst).isVector() &&
620  getMRI()->getType(Tst).getNumElements() ==
621  getMRI()->getType(Op0).getNumElements())) &&
622  "type mismatch");
623 #endif
624 
625  return buildInstr(TargetOpcode::G_SELECT)
626  .addDef(Res)
627  .addUse(Tst)
628  .addUse(Op0)
629  .addUse(Op1);
630 }
631 
634  unsigned Elt, unsigned Idx) {
635 #ifndef NDEBUG
636  LLT ResTy = getMRI()->getType(Res);
637  LLT ValTy = getMRI()->getType(Val);
638  LLT EltTy = getMRI()->getType(Elt);
639  LLT IdxTy = getMRI()->getType(Idx);
640  assert(ResTy.isVector() && ValTy.isVector() && "invalid operand type");
641  assert(IdxTy.isScalar() && "invalid operand type");
642  assert(ResTy.getNumElements() == ValTy.getNumElements() && "type mismatch");
643  assert(ResTy.getElementType() == EltTy && "type mismatch");
644 #endif
645 
646  return buildInstr(TargetOpcode::G_INSERT_VECTOR_ELT)
647  .addDef(Res)
648  .addUse(Val)
649  .addUse(Elt)
650  .addUse(Idx);
651 }
652 
655  unsigned Idx) {
656 #ifndef NDEBUG
657  LLT ResTy = getMRI()->getType(Res);
658  LLT ValTy = getMRI()->getType(Val);
659  LLT IdxTy = getMRI()->getType(Idx);
660  assert(ValTy.isVector() && "invalid operand type");
661  assert((ResTy.isScalar() || ResTy.isPointer()) && "invalid operand type");
662  assert(IdxTy.isScalar() && "invalid operand type");
663  assert(ValTy.getElementType() == ResTy && "type mismatch");
664 #endif
665 
666  return buildInstr(TargetOpcode::G_EXTRACT_VECTOR_ELT)
667  .addDef(Res)
668  .addUse(Val)
669  .addUse(Idx);
670 }
671 
673 MachineIRBuilderBase::buildAtomicCmpXchg(unsigned OldValRes, unsigned Addr,
674  unsigned CmpVal, unsigned NewVal,
675  MachineMemOperand &MMO) {
676 #ifndef NDEBUG
677  LLT OldValResTy = getMRI()->getType(OldValRes);
678  LLT AddrTy = getMRI()->getType(Addr);
679  LLT CmpValTy = getMRI()->getType(CmpVal);
680  LLT NewValTy = getMRI()->getType(NewVal);
681  assert(OldValResTy.isScalar() && "invalid operand type");
682  assert(AddrTy.isPointer() && "invalid operand type");
683  assert(CmpValTy.isValid() && "invalid operand type");
684  assert(NewValTy.isValid() && "invalid operand type");
685  assert(OldValResTy == CmpValTy && "type mismatch");
686  assert(OldValResTy == NewValTy && "type mismatch");
687 #endif
688 
689  return buildInstr(TargetOpcode::G_ATOMIC_CMPXCHG)
690  .addDef(OldValRes)
691  .addUse(Addr)
692  .addUse(CmpVal)
693  .addUse(NewVal)
694  .addMemOperand(&MMO);
695 }
696 
697 void MachineIRBuilderBase::validateTruncExt(unsigned Dst, unsigned Src,
698  bool IsExtend) {
699 #ifndef NDEBUG
700  LLT SrcTy = getMRI()->getType(Src);
701  LLT DstTy = getMRI()->getType(Dst);
702 
703  if (DstTy.isVector()) {
704  assert(SrcTy.isVector() && "mismatched cast between vecot and non-vector");
705  assert(SrcTy.getNumElements() == DstTy.getNumElements() &&
706  "different number of elements in a trunc/ext");
707  } else
708  assert(DstTy.isScalar() && SrcTy.isScalar() && "invalid extend/trunc");
709 
710  if (IsExtend)
711  assert(DstTy.getSizeInBits() > SrcTy.getSizeInBits() &&
712  "invalid narrowing extend");
713  else
714  assert(DstTy.getSizeInBits() < SrcTy.getSizeInBits() &&
715  "invalid widening trunc");
716 #endif
717 }
bool isFPPredicate() const
Definition: InstrTypes.h:976
MachineInstrBuilder buildCopy(unsigned Res, unsigned Op)
Build and insert Res = COPY Op.
uint64_t CallInst * C
const MachineInstrBuilder & addMetadata(const MDNode *MD) const
MachineInstrBuilder buildIndirectDbgValue(unsigned Reg, const MDNode *Variable, const MDNode *Expr)
Build and insert a DBG_VALUE instruction expressing the fact that the associated Variable lives in me...
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
MachineInstrBuilder buildGEP(unsigned Res, unsigned Op0, unsigned Op1)
Build and insert Res = G_GEP Op0, Op1.
iterator begin() const
Definition: ArrayRef.h:137
MachineInstrBuilder buildInsertVectorElement(unsigned Res, unsigned Val, unsigned Elt, unsigned Idx)
Build and insert Res = G_INSERT_VECTOR_ELT Val, Elt, Idx.
MachineInstrBuilder buildStore(unsigned Val, unsigned Addr, MachineMemOperand &MMO)
Build and insert G_STORE Val, Addr, MMO.
bool isScalar() const
MachineInstrBuilder buildFConstant(DstType &&Res, const ConstantFP &Val)
Build and insert Res = G_FCONSTANT Val.
MachineInstrBuilder buildUndef(DstType &&Res)
Build and insert Res = IMPLICIT_DEF.
MachineInstrBuilder buildSExt(DstType &&Res, ArgType &&Arg)
Build and insert Res = G_SEXT Op.
MachineInstrBuilder buildUnmerge(ArrayRef< unsigned > Res, unsigned Op)
Build and insert Res0, ...
unsigned Reg
MachineInstrBuilder buildFPTrunc(DstType &&Res, SrcType &&Src)
Build and insert Res = G_FPTRUNC Op.
LLT getType(unsigned Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register...
const MachineInstrBuilder & addPredicate(CmpInst::Predicate Pred) const
MachineInstrBuilder buildAnyExt(unsigned Res, unsigned Op)
Build and insert Res = G_ANYEXT Op0.
A debug info location.
Definition: DebugLoc.h:34
Metadata node.
Definition: Metadata.h:862
const MachineInstrBuilder & addGlobalAddress(const GlobalValue *GV, int64_t Offset=0, unsigned char TargetFlags=0) const
void setMBB(MachineBasicBlock &MBB)
Set the insertion point to the end of MBB.
unsigned getBitWidth() const
getBitWidth - Return the bitwidth of this constant.
Definition: Constants.h:143
MachineInstrBuilder buildAtomicCmpXchg(unsigned OldValRes, unsigned Addr, unsigned CmpVal, unsigned NewVal, MachineMemOperand &MMO)
Build and insert OldValRes = G_ATOMIC_CMPXCHG Addr, CmpVal, NewVal, MMO.
MachineInstrBuilder buildInstr(unsigned Opcode)
Build and insert <empty> = Opcode <empty>.
bool isVector() const
void recordInsertion(MachineInstr *InsertedInstr) const
A description of a memory reference used in the backend.
std::function< void(MachineInstr *)> InsertedInstr
void setMF(MachineFunction &)
const MachineInstrBuilder & addUse(unsigned RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register use operand.
MachineInstrBuilder buildBrIndirect(unsigned Tgt)
Build and insert G_BRINDIRECT Tgt.
MachineBasicBlock::iterator II
MachineInstrBuilder buildInstrNoInsert(unsigned Opcode)
Build but don&#39;t insert <empty> = Opcode <empty>.
LLT getElementType() const
Returns the vector&#39;s element type. Only valid for vector types.
APFloat getAPFloatFromSize(double Val, unsigned Size)
Returns an APFloat from Val converted to the appropriate size.
Definition: Utils.cpp:224
MachineInstrBuilder buildConstDbgValue(const Constant &C, const MDNode *Variable, const MDNode *Expr)
Build and insert a DBG_VALUE instructions specifying that Variable is given by C (suitably modified b...
MachineInstrBuilder buildAnyExtOrTrunc(DstTy &&Dst, UseArgTy &&Use)
Res = COPY Op depending on the differing sizes of Res and Op.
MachineInstrBuilder buildPtrMask(unsigned Res, unsigned Op0, uint32_t NumBits)
Build and insert Res = G_PTR_MASK Op0, NumBits.
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:890
const MachineInstrBuilder & addFPImm(const ConstantFP *Val) const
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:138
virtual const TargetInstrInfo * getInstrInfo() const
MachineInstrBuilder buildZExt(DstType &&Res, ArgType &&Arg)
Build and insert Res = G_ZEXT Op.
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
MachineInstrBuilder buildGlobalValue(unsigned Res, const GlobalValue *GV)
Build and insert Res = G_GLOBAL_VALUE GV.
MachineInstrBuilder buildFIDbgValue(int FI, const MDNode *Variable, const MDNode *Expr)
Build and insert a DBG_VALUE instruction expressing the fact that the associated Variable lives in th...
MachineInstrBuilder buildIntrinsic(Intrinsic::ID ID, unsigned Res, bool HasSideEffects)
Build and insert either a G_INTRINSIC (if HasSideEffects is false) or G_INTRINSIC_W_SIDE_EFFECTS inst...
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
MachineInstrBundleIterator< MachineInstr > iterator
MachineInstrBuilder buildZExtOrTrunc(DstTy &&Dst, UseArgTy &&Use)
Build and insert Res = G_ZEXT Op, Res = G_TRUNC Op, or Res = COPY Op depending on the differing sizes...
const TargetInstrInfo * TII
Information used to access the description of the opcodes.
const MachineInstrBuilder & addCImm(const ConstantInt *Val) const
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
This is an important base class in LLVM.
Definition: Constant.h:42
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:264
MachineInstrBuilder buildUAdde(unsigned Res, unsigned CarryOut, unsigned Op0, unsigned Op1, unsigned CarryIn)
Build and insert Res, CarryOut = G_UADDE Op0, Op1, CarryIn.
MachineInstrBuilder buildExtract(unsigned Res, unsigned Src, uint64_t Index)
Build and insert `Res0, ...
MachineInstrBuilder buildDirectDbgValue(unsigned Reg, const MDNode *Variable, const MDNode *Expr)
Build and insert a DBG_VALUE instruction expressing the fact that the associated Variable lives in Re...
bool isValid() const
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:885
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:495
DebugLoc DL
Debug location to be set to any instruction we create.
void setInstr(MachineInstr &MI)
Set the insertion point to before MI.
MachineInstrBuilder buildExtractVectorElement(unsigned Res, unsigned Val, unsigned Idx)
Build and insert Res = G_EXTRACT_VECTOR_ELT Val, Idx.
const DebugLoc & getDL()
Getter for DebugLoc.
self_iterator getIterator()
Definition: ilist_node.h:82
const MachineInstrBuilder & addFrameIndex(int Idx) const
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:193
MachineBasicBlock::iterator getInsertPt()
Current insertion point for new instructions.
static wasm::ValType getType(const TargetRegisterClass *RC)
unsigned createGenericVirtualRegister(LLT Ty, StringRef Name="")
Create and return a new generic virtual register with low-level type Ty.
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:240
MachineInstrBuilder buildSExtOrTrunc(DstTy &&Dst, UseArgTy &&Use)
Build and insert Res = G_SEXT Op, Res = G_TRUNC Op, or Res = COPY Op depending on the differing sizes...
MachineInstrBuilder buildBr(MachineBasicBlock &BB)
Build and insert G_BR Dest.
void setInsertPt(MachineBasicBlock &MBB, MachineBasicBlock::iterator II)
Set the insertion point before the specified position.
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
void validateBinaryOp(unsigned Dst, unsigned Src0, unsigned Src1)
bool isVector(MCInstrInfo const &MCII, MCInst const &MCI)
iterator end() const
Definition: ArrayRef.h:138
unsigned getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
MachineInstrBuilder buildFrameIndex(unsigned Res, int Idx)
Build and insert Res = G_FRAME_INDEX Idx.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:611
LegalityPredicate isScalar(unsigned TypeIdx)
True iff the specified type index is a scalar.
static Constant * get(Type *Ty, double V)
This returns a ConstantFP, or a vector containing a splat of a ConstantFP, for the specified value in...
Definition: Constants.cpp:674
MachineInstrBuilder buildSelect(unsigned Res, unsigned Tst, unsigned Op0, unsigned Op1)
Build and insert a Res = G_SELECT Tst, Op0, Op1.
MachineInstrBuilder buildTrunc(unsigned Res, unsigned Op)
Build and insert Res = G_TRUNC Op.
const Function & getFunction() const
Return the LLVM function that this machine code represents.
This file declares the MachineIRBuilder class.
bool isIntPredicate() const
Definition: InstrTypes.h:977
MachineInstrBuilder insertInstr(MachineInstrBuilder MIB)
Insert an existing instruction at the insertion point.
void recordInsertions(std::function< void(MachineInstr *)> InsertedInstr)
bool isPointer() const
MachineInstrBuilder buildConstant(unsigned Res, const ConstantInt &Val)
Build and insert Res = G_CONSTANT Val.
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:156
MachineInstrBuilder buildMerge(unsigned Res, ArrayRef< unsigned > Ops)
Build and insert Res = G_MERGE_VALUES Op0, ...
Representation of each machine instruction.
Definition: MachineInstr.h:60
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
MachineRegisterInfo * getMRI()
Getter for MRI.
MachineInstrBuilder buildLoadInstr(unsigned Opcode, unsigned Res, unsigned Addr, MachineMemOperand &MMO)
Build and insert Res = <opcode> Addr, MMO.
MachineInstrBuilder buildLoad(unsigned Res, unsigned Addr, MachineMemOperand &MMO)
Build and insert Res = G_LOAD Addr, MMO.
MachineFunction & getMF()
Getter for the function we currently build.
Optional< MachineInstrBuilder > materializeGEP(unsigned &Res, unsigned Op0, const LLT &ValueTy, uint64_t Value)
Materialize and insert Res = G_GEP Op0, (G_CONSTANT Value)
MachineInstrBuilder buildCast(DstType &&Res, ArgType &&Arg)
Build and insert an appropriate cast between two registers of equal size.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
unsigned getSizeInBits(unsigned Reg, const MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI) const
Get the size in bits of Reg.
uint16_t getNumElements() const
Returns the number of elements in a vector LLT.
MachineInstrBuilder buildBrCond(unsigned Tst, MachineBasicBlock &BB)
Build and insert G_BRCOND Tst, Dest.
MachineBasicBlock & getMBB()
Getter for the basic block we currently build.
print Print MemDeps of function
IRTranslator LLVM IR MI
MachineInstrBuilder buildFCmp(CmpInst::Predicate Pred, unsigned Res, unsigned Op0, unsigned Op1)
Build and insert a Res = G_FCMP PredOp0, Op1.
const MachineInstrBuilder & addDef(unsigned RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register definition operand.
MachineFunction * MF
MachineFunction under construction.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned char TargetFlags=0) const
MachineInstrBuilder buildInsert(unsigned Res, unsigned Src, unsigned Op, unsigned Index)
void buildSequence(unsigned Res, ArrayRef< unsigned > Ops, ArrayRef< uint64_t > Indices)
Build and insert instructions to put Ops together at the specified p Indices to form a larger registe...
MachineInstrBuilder buildExtOrTrunc(unsigned ExtOpc, unsigned Res, unsigned Op)
Build and insert Res = ExtOpc, Res = G_TRUNC Op, or Res = COPY Op depending on the differing sizes of...
MachineInstrBuilder buildICmp(CmpInst::Predicate Pred, unsigned Res, unsigned Op0, unsigned Op1)
Build and insert a Res = G_ICMP Pred, Op0, Op1.
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:273
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:144
MachineRegisterInfo * MRI
Information used to verify types are consistent and to create virtual registers.