1 //===-- llvm/CodeGen/GlobalISel/MachineIRBuilder.cpp - MIBuilder--*- C++ -*-==//
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 /// \file
9 /// This file implements the MachineIRBuidler class.
10 //===----------------------------------------------------------------------===//
11 #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
12 #include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
13 
14 #include "llvm/CodeGen/MachineFunction.h"
15 #include "llvm/CodeGen/MachineInstr.h"
16 #include "llvm/CodeGen/MachineInstrBuilder.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/CodeGen/TargetInstrInfo.h"
19 #include "llvm/CodeGen/TargetLowering.h"
20 #include "llvm/CodeGen/TargetOpcodes.h"
21 #include "llvm/CodeGen/TargetSubtargetInfo.h"
22 #include "llvm/IR/DebugInfo.h"
23 
24 using namespace llvm;
25 
26 void MachineIRBuilder::setMF(MachineFunction &MF) {
27   State.MF = &MF;
28   State.MBB = nullptr;
29   State.MRI = &MF.getRegInfo();
30   State.TII = MF.getSubtarget().getInstrInfo();
31   State.DL = DebugLoc();
32   State.II = MachineBasicBlock::iterator();
33   State.Observer = nullptr;
34 }
35 
36 void MachineIRBuilder::setMBB(MachineBasicBlock &MBB) {
37   State.MBB = &MBB;
38   State.II = MBB.end();
39   assert(&getMF() == MBB.getParent() &&
40          "Basic block is in a different function");
41 }
42 
43 void MachineIRBuilder::setInstr(MachineInstr &MI) {
44   assert(MI.getParent() && "Instruction is not part of a basic block");
45   setMBB(*MI.getParent());
46   State.II = MI.getIterator();
47 }
48 
49 void MachineIRBuilder::setCSEInfo(GISelCSEInfo *Info) { State.CSEInfo = Info; }
50 
51 void MachineIRBuilder::setInsertPt(MachineBasicBlock &MBB,
52                                    MachineBasicBlock::iterator II) {
53   assert(MBB.getParent() == &getMF() &&
54          "Basic block is in a different function");
55   State.MBB = &MBB;
56   State.II = II;
57 }
58 
59 void MachineIRBuilder::recordInsertion(MachineInstr *InsertedInstr) const {
60   if (State.Observer)
61     State.Observer->createdInstr(*InsertedInstr);
62 }
63 
64 void MachineIRBuilder::setChangeObserver(GISelChangeObserver &Observer) {
65   State.Observer = &Observer;
66 }
67 
68 void MachineIRBuilder::stopObservingChanges() { State.Observer = nullptr; }
69 
70 //------------------------------------------------------------------------------
71 // Build instruction variants.
72 //------------------------------------------------------------------------------
73 
74 MachineInstrBuilder MachineIRBuilder::buildInstr(unsigned Opcode) {
75   return insertInstr(buildInstrNoInsert(Opcode));
76 }
77 
78 MachineInstrBuilder MachineIRBuilder::buildInstrNoInsert(unsigned Opcode) {
79   MachineInstrBuilder MIB = BuildMI(getMF(), getDL(), getTII().get(Opcode));
80   return MIB;
81 }
82 
83 MachineInstrBuilder MachineIRBuilder::insertInstr(MachineInstrBuilder MIB) {
84   getMBB().insert(getInsertPt(), MIB);
85   recordInsertion(MIB);
86   return MIB;
87 }
88 
89 MachineInstrBuilder
90 MachineIRBuilder::buildDirectDbgValue(unsigned Reg, const MDNode *Variable,
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 
102 MachineInstrBuilder
103 MachineIRBuilder::buildIndirectDbgValue(unsigned Reg, const MDNode *Variable,
104                                         const MDNode *Expr) {
105   assert(isa<DILocalVariable>(Variable) && "not a variable");
106   assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
107   assert(
108       cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(getDL()) &&
109       "Expected inlined-at fields to agree");
110   return insertInstr(BuildMI(getMF(), getDL(),
111                              getTII().get(TargetOpcode::DBG_VALUE),
112                              /*IsIndirect*/ true, Reg, Variable, Expr));
113 }
114 
115 MachineInstrBuilder MachineIRBuilder::buildFIDbgValue(int FI,
116                                                       const MDNode *Variable,
117                                                       const MDNode *Expr) {
118   assert(isa<DILocalVariable>(Variable) && "not a variable");
119   assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
120   assert(
121       cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(getDL()) &&
122       "Expected inlined-at fields to agree");
123   return buildInstr(TargetOpcode::DBG_VALUE)
124       .addFrameIndex(FI)
125       .addImm(0)
126       .addMetadata(Variable)
127       .addMetadata(Expr);
128 }
129 
130 MachineInstrBuilder MachineIRBuilder::buildConstDbgValue(const Constant &C,
131                                                          const MDNode *Variable,
132                                                          const MDNode *Expr) {
133   assert(isa<DILocalVariable>(Variable) && "not a variable");
134   assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
135   assert(
136       cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(getDL()) &&
137       "Expected inlined-at fields to agree");
138   auto MIB = buildInstr(TargetOpcode::DBG_VALUE);
139   if (auto *CI = dyn_cast<ConstantInt>(&C)) {
140     if (CI->getBitWidth() > 64)
141       MIB.addCImm(CI);
142     else
143       MIB.addImm(CI->getZExtValue());
144   } else if (auto *CFP = dyn_cast<ConstantFP>(&C)) {
145     MIB.addFPImm(CFP);
146   } else {
147     // Insert %noreg if we didn't find a usable constant and had to drop it.
148     MIB.addReg(0U);
149   }
150 
151   return MIB.addImm(0).addMetadata(Variable).addMetadata(Expr);
152 }
153 
154 MachineInstrBuilder MachineIRBuilder::buildDbgLabel(const MDNode *Label) {
155   assert(isa<DILabel>(Label) && "not a label");
156   assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(State.DL) &&
157          "Expected inlined-at fields to agree");
158   auto MIB = buildInstr(TargetOpcode::DBG_LABEL);
159 
160   return MIB.addMetadata(Label);
161 }
162 
163 MachineInstrBuilder MachineIRBuilder::buildFrameIndex(unsigned Res, int Idx) {
164   assert(getMRI()->getType(Res).isPointer() && "invalid operand type");
165   return buildInstr(TargetOpcode::G_FRAME_INDEX)
166       .addDef(Res)
167       .addFrameIndex(Idx);
168 }
169 
170 MachineInstrBuilder MachineIRBuilder::buildGlobalValue(unsigned Res,
171                                                        const GlobalValue *GV) {
172   assert(getMRI()->getType(Res).isPointer() && "invalid operand type");
173   assert(getMRI()->getType(Res).getAddressSpace() ==
174              GV->getType()->getAddressSpace() &&
175          "address space mismatch");
176 
177   return buildInstr(TargetOpcode::G_GLOBAL_VALUE)
178       .addDef(Res)
179       .addGlobalAddress(GV);
180 }
181 
182 void MachineIRBuilder::validateBinaryOp(const LLT &Res, const LLT &Op0,
183                                         const LLT &Op1) {
184   assert((Res.isScalar() || Res.isVector()) && "invalid operand type");
185   assert((Res == Op0 && Res == Op1) && "type mismatch");
186 }
187 
188 void MachineIRBuilder::validateShiftOp(const LLT &Res, const LLT &Op0,
189                                        const LLT &Op1) {
190   assert((Res.isScalar() || Res.isVector()) && "invalid operand type");
191   assert((Res == Op0) && "type mismatch");
192 }
193 
194 MachineInstrBuilder MachineIRBuilder::buildGEP(unsigned Res, unsigned Op0,
195                                                unsigned Op1) {
196   assert(getMRI()->getType(Res).isPointer() &&
197          getMRI()->getType(Res) == getMRI()->getType(Op0) && "type mismatch");
198   assert(getMRI()->getType(Op1).isScalar() && "invalid offset type");
199 
200   return buildInstr(TargetOpcode::G_GEP)
201       .addDef(Res)
202       .addUse(Op0)
203       .addUse(Op1);
204 }
205 
206 Optional<MachineInstrBuilder>
207 MachineIRBuilder::materializeGEP(unsigned &Res, unsigned Op0,
208                                  const LLT &ValueTy, uint64_t Value) {
209   assert(Res == 0 && "Res is a result argument");
210   assert(ValueTy.isScalar()  && "invalid offset type");
211 
212   if (Value == 0) {
213     Res = Op0;
214     return None;
215   }
216 
217   Res = getMRI()->createGenericVirtualRegister(getMRI()->getType(Op0));
218   unsigned TmpReg = getMRI()->createGenericVirtualRegister(ValueTy);
219 
220   buildConstant(TmpReg, Value);
221   return buildGEP(Res, Op0, TmpReg);
222 }
223 
224 MachineInstrBuilder MachineIRBuilder::buildPtrMask(unsigned Res, unsigned Op0,
225                                                    uint32_t NumBits) {
226   assert(getMRI()->getType(Res).isPointer() &&
227          getMRI()->getType(Res) == getMRI()->getType(Op0) && "type mismatch");
228 
229   return buildInstr(TargetOpcode::G_PTR_MASK)
230       .addDef(Res)
231       .addUse(Op0)
232       .addImm(NumBits);
233 }
234 
235 MachineInstrBuilder MachineIRBuilder::buildBr(MachineBasicBlock &Dest) {
236   return buildInstr(TargetOpcode::G_BR).addMBB(&Dest);
237 }
238 
239 MachineInstrBuilder MachineIRBuilder::buildBrIndirect(unsigned Tgt) {
240   assert(getMRI()->getType(Tgt).isPointer() && "invalid branch destination");
241   return buildInstr(TargetOpcode::G_BRINDIRECT).addUse(Tgt);
242 }
243 
244 MachineInstrBuilder MachineIRBuilder::buildCopy(const DstOp &Res,
245                                                 const SrcOp &Op) {
246   return buildInstr(TargetOpcode::COPY, Res, Op);
247 }
248 
249 MachineInstrBuilder MachineIRBuilder::buildConstant(const DstOp &Res,
250                                                     const ConstantInt &Val) {
251   LLT Ty = Res.getLLTTy(*getMRI());
252   LLT EltTy = Ty.getScalarType();
253   assert(EltTy.getScalarSizeInBits() == Val.getBitWidth() &&
254          "creating constant with the wrong size");
255 
256   if (Ty.isVector()) {
257     auto Const = buildInstr(TargetOpcode::G_CONSTANT)
258     .addDef(getMRI()->createGenericVirtualRegister(EltTy))
259     .addCImm(&Val);
260     return buildSplatVector(Res, Const);
261   }
262 
263   auto Const = buildInstr(TargetOpcode::G_CONSTANT);
264   Res.addDefToMIB(*getMRI(), Const);
265   Const.addCImm(&Val);
266   return Const;
267 }
268 
269 MachineInstrBuilder MachineIRBuilder::buildConstant(const DstOp &Res,
270                                                     int64_t Val) {
271   auto IntN = IntegerType::get(getMF().getFunction().getContext(),
272                                Res.getLLTTy(*getMRI()).getScalarSizeInBits());
273   ConstantInt *CI = ConstantInt::get(IntN, Val, true);
274   return buildConstant(Res, *CI);
275 }
276 
277 MachineInstrBuilder MachineIRBuilder::buildFConstant(const DstOp &Res,
278                                                      const ConstantFP &Val) {
279   LLT Ty = Res.getLLTTy(*getMRI());
280   LLT EltTy = Ty.getScalarType();
281 
282   assert(APFloat::getSizeInBits(Val.getValueAPF().getSemantics())
283          == EltTy.getSizeInBits() &&
284          "creating fconstant with the wrong size");
285 
286   assert(!Ty.isPointer() && "invalid operand type");
287 
288   if (Ty.isVector()) {
289     auto Const = buildInstr(TargetOpcode::G_FCONSTANT)
290     .addDef(getMRI()->createGenericVirtualRegister(EltTy))
291     .addFPImm(&Val);
292 
293     return buildSplatVector(Res, Const);
294   }
295 
296   auto Const = buildInstr(TargetOpcode::G_FCONSTANT);
297   Res.addDefToMIB(*getMRI(), Const);
298   Const.addFPImm(&Val);
299   return Const;
300 }
301 
302 MachineInstrBuilder MachineIRBuilder::buildConstant(const DstOp &Res,
303                                                     const APInt &Val) {
304   ConstantInt *CI = ConstantInt::get(getMF().getFunction().getContext(), Val);
305   return buildConstant(Res, *CI);
306 }
307 
308 MachineInstrBuilder MachineIRBuilder::buildFConstant(const DstOp &Res,
309                                                      double Val) {
310   LLT DstTy = Res.getLLTTy(*getMRI());
311   auto &Ctx = getMF().getFunction().getContext();
312   auto *CFP =
313       ConstantFP::get(Ctx, getAPFloatFromSize(Val, DstTy.getScalarSizeInBits()));
314   return buildFConstant(Res, *CFP);
315 }
316 
317 MachineInstrBuilder MachineIRBuilder::buildBrCond(unsigned Tst,
318                                                   MachineBasicBlock &Dest) {
319   assert(getMRI()->getType(Tst).isScalar() && "invalid operand type");
320 
321   return buildInstr(TargetOpcode::G_BRCOND).addUse(Tst).addMBB(&Dest);
322 }
323 
324 MachineInstrBuilder MachineIRBuilder::buildLoad(unsigned Res, unsigned Addr,
325                                                 MachineMemOperand &MMO) {
326   return buildLoadInstr(TargetOpcode::G_LOAD, Res, Addr, MMO);
327 }
328 
329 MachineInstrBuilder MachineIRBuilder::buildLoadInstr(unsigned Opcode,
330                                                      unsigned Res,
331                                                      unsigned Addr,
332                                                      MachineMemOperand &MMO) {
333   assert(getMRI()->getType(Res).isValid() && "invalid operand type");
334   assert(getMRI()->getType(Addr).isPointer() && "invalid operand type");
335 
336   return buildInstr(Opcode)
337       .addDef(Res)
338       .addUse(Addr)
339       .addMemOperand(&MMO);
340 }
341 
342 MachineInstrBuilder MachineIRBuilder::buildStore(unsigned Val, unsigned Addr,
343                                                  MachineMemOperand &MMO) {
344   assert(getMRI()->getType(Val).isValid() && "invalid operand type");
345   assert(getMRI()->getType(Addr).isPointer() && "invalid operand type");
346 
347   return buildInstr(TargetOpcode::G_STORE)
348       .addUse(Val)
349       .addUse(Addr)
350       .addMemOperand(&MMO);
351 }
352 
353 MachineInstrBuilder MachineIRBuilder::buildUAddo(const DstOp &Res,
354                                                  const DstOp &CarryOut,
355                                                  const SrcOp &Op0,
356                                                  const SrcOp &Op1) {
357   return buildInstr(TargetOpcode::G_UADDO, {Res, CarryOut}, {Op0, Op1});
358 }
359 
360 MachineInstrBuilder MachineIRBuilder::buildUAdde(const DstOp &Res,
361                                                  const DstOp &CarryOut,
362                                                  const SrcOp &Op0,
363                                                  const SrcOp &Op1,
364                                                  const SrcOp &CarryIn) {
365   return buildInstr(TargetOpcode::G_UADDE, {Res, CarryOut},
366                     {Op0, Op1, CarryIn});
367 }
368 
369 MachineInstrBuilder MachineIRBuilder::buildAnyExt(const DstOp &Res,
370                                                   const SrcOp &Op) {
371   return buildInstr(TargetOpcode::G_ANYEXT, Res, Op);
372 }
373 
374 MachineInstrBuilder MachineIRBuilder::buildSExt(const DstOp &Res,
375                                                 const SrcOp &Op) {
376   return buildInstr(TargetOpcode::G_SEXT, Res, Op);
377 }
378 
379 MachineInstrBuilder MachineIRBuilder::buildZExt(const DstOp &Res,
380                                                 const SrcOp &Op) {
381   return buildInstr(TargetOpcode::G_ZEXT, Res, Op);
382 }
383 
384 unsigned MachineIRBuilder::getBoolExtOp(bool IsVec, bool IsFP) const {
385   const auto *TLI = getMF().getSubtarget().getTargetLowering();
386   switch (TLI->getBooleanContents(IsVec, IsFP)) {
387   case TargetLoweringBase::ZeroOrNegativeOneBooleanContent:
388     return TargetOpcode::G_SEXT;
389   case TargetLoweringBase::ZeroOrOneBooleanContent:
390     return TargetOpcode::G_ZEXT;
391   default:
392     return TargetOpcode::G_ANYEXT;
393   }
394 }
395 
396 MachineInstrBuilder MachineIRBuilder::buildBoolExt(const DstOp &Res,
397                                                    const SrcOp &Op,
398                                                    bool IsFP) {
399   unsigned ExtOp = getBoolExtOp(getMRI()->getType(Op.getReg()).isVector(), IsFP);
400   return buildInstr(ExtOp, Res, Op);
401 }
402 
403 MachineInstrBuilder MachineIRBuilder::buildExtOrTrunc(unsigned ExtOpc,
404                                                       const DstOp &Res,
405                                                       const SrcOp &Op) {
406   assert((TargetOpcode::G_ANYEXT == ExtOpc || TargetOpcode::G_ZEXT == ExtOpc ||
407           TargetOpcode::G_SEXT == ExtOpc) &&
408          "Expecting Extending Opc");
409   assert(Res.getLLTTy(*getMRI()).isScalar() ||
410          Res.getLLTTy(*getMRI()).isVector());
411   assert(Res.getLLTTy(*getMRI()).isScalar() ==
412          Op.getLLTTy(*getMRI()).isScalar());
413 
414   unsigned Opcode = TargetOpcode::COPY;
415   if (Res.getLLTTy(*getMRI()).getSizeInBits() >
416       Op.getLLTTy(*getMRI()).getSizeInBits())
417     Opcode = ExtOpc;
418   else if (Res.getLLTTy(*getMRI()).getSizeInBits() <
419            Op.getLLTTy(*getMRI()).getSizeInBits())
420     Opcode = TargetOpcode::G_TRUNC;
421   else
422     assert(Res.getLLTTy(*getMRI()) == Op.getLLTTy(*getMRI()));
423 
424   return buildInstr(Opcode, Res, Op);
425 }
426 
427 MachineInstrBuilder MachineIRBuilder::buildSExtOrTrunc(const DstOp &Res,
428                                                        const SrcOp &Op) {
429   return buildExtOrTrunc(TargetOpcode::G_SEXT, Res, Op);
430 }
431 
432 MachineInstrBuilder MachineIRBuilder::buildZExtOrTrunc(const DstOp &Res,
433                                                        const SrcOp &Op) {
434   return buildExtOrTrunc(TargetOpcode::G_ZEXT, Res, Op);
435 }
436 
437 MachineInstrBuilder MachineIRBuilder::buildAnyExtOrTrunc(const DstOp &Res,
438                                                          const SrcOp &Op) {
439   return buildExtOrTrunc(TargetOpcode::G_ANYEXT, Res, Op);
440 }
441 
442 MachineInstrBuilder MachineIRBuilder::buildCast(const DstOp &Dst,
443                                                 const SrcOp &Src) {
444   LLT SrcTy = Src.getLLTTy(*getMRI());
445   LLT DstTy = Dst.getLLTTy(*getMRI());
446   if (SrcTy == DstTy)
447     return buildCopy(Dst, Src);
448 
449   unsigned Opcode;
450   if (SrcTy.isPointer() && DstTy.isScalar())
451     Opcode = TargetOpcode::G_PTRTOINT;
452   else if (DstTy.isPointer() && SrcTy.isScalar())
453     Opcode = TargetOpcode::G_INTTOPTR;
454   else {
455     assert(!SrcTy.isPointer() && !DstTy.isPointer() && "n G_ADDRCAST yet");
456     Opcode = TargetOpcode::G_BITCAST;
457   }
458 
459   return buildInstr(Opcode, Dst, Src);
460 }
461 
462 MachineInstrBuilder MachineIRBuilder::buildExtract(const DstOp &Dst,
463                                                    const SrcOp &Src,
464                                                    uint64_t Index) {
465   LLT SrcTy = Src.getLLTTy(*getMRI());
466   LLT DstTy = Dst.getLLTTy(*getMRI());
467 
468 #ifndef NDEBUG
469   assert(SrcTy.isValid() && "invalid operand type");
470   assert(DstTy.isValid() && "invalid operand type");
471   assert(Index + DstTy.getSizeInBits() <= SrcTy.getSizeInBits() &&
472          "extracting off end of register");
473 #endif
474 
475   if (DstTy.getSizeInBits() == SrcTy.getSizeInBits()) {
476     assert(Index == 0 && "insertion past the end of a register");
477     return buildCast(Dst, Src);
478   }
479 
480   auto Extract = buildInstr(TargetOpcode::G_EXTRACT);
481   Dst.addDefToMIB(*getMRI(), Extract);
482   Src.addSrcToMIB(Extract);
483   Extract.addImm(Index);
484   return Extract;
485 }
486 
487 void MachineIRBuilder::buildSequence(unsigned Res, ArrayRef<unsigned> Ops,
488                                      ArrayRef<uint64_t> Indices) {
489 #ifndef NDEBUG
490   assert(Ops.size() == Indices.size() && "incompatible args");
491   assert(!Ops.empty() && "invalid trivial sequence");
492   assert(std::is_sorted(Indices.begin(), Indices.end()) &&
493          "sequence offsets must be in ascending order");
494 
495   assert(getMRI()->getType(Res).isValid() && "invalid operand type");
496   for (auto Op : Ops)
497     assert(getMRI()->getType(Op).isValid() && "invalid operand type");
498 #endif
499 
500   LLT ResTy = getMRI()->getType(Res);
501   LLT OpTy = getMRI()->getType(Ops[0]);
502   unsigned OpSize = OpTy.getSizeInBits();
503   bool MaybeMerge = true;
504   for (unsigned i = 0; i < Ops.size(); ++i) {
505     if (getMRI()->getType(Ops[i]) != OpTy || Indices[i] != i * OpSize) {
506       MaybeMerge = false;
507       break;
508     }
509   }
510 
511   if (MaybeMerge && Ops.size() * OpSize == ResTy.getSizeInBits()) {
512     buildMerge(Res, Ops);
513     return;
514   }
515 
516   unsigned ResIn = getMRI()->createGenericVirtualRegister(ResTy);
517   buildUndef(ResIn);
518 
519   for (unsigned i = 0; i < Ops.size(); ++i) {
520     unsigned ResOut = i + 1 == Ops.size()
521                           ? Res
522                           : getMRI()->createGenericVirtualRegister(ResTy);
523     buildInsert(ResOut, ResIn, Ops[i], Indices[i]);
524     ResIn = ResOut;
525   }
526 }
527 
528 MachineInstrBuilder MachineIRBuilder::buildUndef(const DstOp &Res) {
529   return buildInstr(TargetOpcode::G_IMPLICIT_DEF, {Res}, {});
530 }
531 
532 MachineInstrBuilder MachineIRBuilder::buildMerge(const DstOp &Res,
533                                                  ArrayRef<unsigned> Ops) {
534   // Unfortunately to convert from ArrayRef<LLT> to ArrayRef<SrcOp>,
535   // we need some temporary storage for the DstOp objects. Here we use a
536   // sufficiently large SmallVector to not go through the heap.
537   SmallVector<SrcOp, 8> TmpVec(Ops.begin(), Ops.end());
538   return buildInstr(TargetOpcode::G_MERGE_VALUES, Res, TmpVec);
539 }
540 
541 MachineInstrBuilder MachineIRBuilder::buildUnmerge(ArrayRef<LLT> Res,
542                                                    const SrcOp &Op) {
543   // Unfortunately to convert from ArrayRef<LLT> to ArrayRef<DstOp>,
544   // we need some temporary storage for the DstOp objects. Here we use a
545   // sufficiently large SmallVector to not go through the heap.
546   SmallVector<DstOp, 8> TmpVec(Res.begin(), Res.end());
547   return buildInstr(TargetOpcode::G_UNMERGE_VALUES, TmpVec, Op);
548 }
549 
550 MachineInstrBuilder MachineIRBuilder::buildUnmerge(ArrayRef<unsigned> Res,
551                                                    const SrcOp &Op) {
552   // Unfortunately to convert from ArrayRef<unsigned> to ArrayRef<DstOp>,
553   // we need some temporary storage for the DstOp objects. Here we use a
554   // sufficiently large SmallVector to not go through the heap.
555   SmallVector<DstOp, 8> TmpVec(Res.begin(), Res.end());
556   return buildInstr(TargetOpcode::G_UNMERGE_VALUES, TmpVec, Op);
557 }
558 
559 MachineInstrBuilder MachineIRBuilder::buildBuildVector(const DstOp &Res,
560                                                        ArrayRef<unsigned> Ops) {
561   // Unfortunately to convert from ArrayRef<unsigned> to ArrayRef<SrcOp>,
562   // we need some temporary storage for the DstOp objects. Here we use a
563   // sufficiently large SmallVector to not go through the heap.
564   SmallVector<SrcOp, 8> TmpVec(Ops.begin(), Ops.end());
565   return buildInstr(TargetOpcode::G_BUILD_VECTOR, Res, TmpVec);
566 }
567 
568 MachineInstrBuilder MachineIRBuilder::buildSplatVector(const DstOp &Res,
569                                                        const SrcOp &Src) {
570   SmallVector<SrcOp, 8> TmpVec(Res.getLLTTy(*getMRI()).getNumElements(), Src);
571   return buildInstr(TargetOpcode::G_BUILD_VECTOR, Res, TmpVec);
572 }
573 
574 MachineInstrBuilder
575 MachineIRBuilder::buildBuildVectorTrunc(const DstOp &Res,
576                                         ArrayRef<unsigned> Ops) {
577   // Unfortunately to convert from ArrayRef<unsigned> to ArrayRef<SrcOp>,
578   // we need some temporary storage for the DstOp objects. Here we use a
579   // sufficiently large SmallVector to not go through the heap.
580   SmallVector<SrcOp, 8> TmpVec(Ops.begin(), Ops.end());
581   return buildInstr(TargetOpcode::G_BUILD_VECTOR_TRUNC, Res, TmpVec);
582 }
583 
584 MachineInstrBuilder
585 MachineIRBuilder::buildConcatVectors(const DstOp &Res, ArrayRef<unsigned> Ops) {
586   // Unfortunately to convert from ArrayRef<unsigned> to ArrayRef<SrcOp>,
587   // we need some temporary storage for the DstOp objects. Here we use a
588   // sufficiently large SmallVector to not go through the heap.
589   SmallVector<SrcOp, 8> TmpVec(Ops.begin(), Ops.end());
590   return buildInstr(TargetOpcode::G_CONCAT_VECTORS, Res, TmpVec);
591 }
592 
593 MachineInstrBuilder MachineIRBuilder::buildInsert(unsigned Res, unsigned Src,
594                                                   unsigned Op, unsigned Index) {
595   assert(Index + getMRI()->getType(Op).getSizeInBits() <=
596              getMRI()->getType(Res).getSizeInBits() &&
597          "insertion past the end of a register");
598 
599   if (getMRI()->getType(Res).getSizeInBits() ==
600       getMRI()->getType(Op).getSizeInBits()) {
601     return buildCast(Res, Op);
602   }
603 
604   return buildInstr(TargetOpcode::G_INSERT)
605       .addDef(Res)
606       .addUse(Src)
607       .addUse(Op)
608       .addImm(Index);
609 }
610 
611 MachineInstrBuilder MachineIRBuilder::buildIntrinsic(Intrinsic::ID ID,
612                                                      unsigned Res,
613                                                      bool HasSideEffects) {
614   auto MIB =
615       buildInstr(HasSideEffects ? TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS
616                                 : TargetOpcode::G_INTRINSIC);
617   if (Res)
618     MIB.addDef(Res);
619   MIB.addIntrinsicID(ID);
620   return MIB;
621 }
622 
623 MachineInstrBuilder MachineIRBuilder::buildTrunc(const DstOp &Res,
624                                                  const SrcOp &Op) {
625   return buildInstr(TargetOpcode::G_TRUNC, Res, Op);
626 }
627 
628 MachineInstrBuilder MachineIRBuilder::buildFPTrunc(const DstOp &Res,
629                                                    const SrcOp &Op) {
630   return buildInstr(TargetOpcode::G_FPTRUNC, Res, Op);
631 }
632 
633 MachineInstrBuilder MachineIRBuilder::buildICmp(CmpInst::Predicate Pred,
634                                                 const DstOp &Res,
635                                                 const SrcOp &Op0,
636                                                 const SrcOp &Op1) {
637   return buildInstr(TargetOpcode::G_ICMP, Res, {Pred, Op0, Op1});
638 }
639 
640 MachineInstrBuilder MachineIRBuilder::buildFCmp(CmpInst::Predicate Pred,
641                                                 const DstOp &Res,
642                                                 const SrcOp &Op0,
643                                                 const SrcOp &Op1) {
644 
645   return buildInstr(TargetOpcode::G_FCMP, Res, {Pred, Op0, Op1});
646 }
647 
648 MachineInstrBuilder MachineIRBuilder::buildSelect(const DstOp &Res,
649                                                   const SrcOp &Tst,
650                                                   const SrcOp &Op0,
651                                                   const SrcOp &Op1) {
652 
653   return buildInstr(TargetOpcode::G_SELECT, {Res}, {Tst, Op0, Op1});
654 }
655 
656 MachineInstrBuilder
657 MachineIRBuilder::buildInsertVectorElement(const DstOp &Res, const SrcOp &Val,
658                                            const SrcOp &Elt, const SrcOp &Idx) {
659   return buildInstr(TargetOpcode::G_INSERT_VECTOR_ELT, Res, {Val, Elt, Idx});
660 }
661 
662 MachineInstrBuilder
663 MachineIRBuilder::buildExtractVectorElement(const DstOp &Res, const SrcOp &Val,
664                                             const SrcOp &Idx) {
665   return buildInstr(TargetOpcode::G_EXTRACT_VECTOR_ELT, Res, {Val, Idx});
666 }
667 
668 MachineInstrBuilder MachineIRBuilder::buildAtomicCmpXchgWithSuccess(
669     unsigned OldValRes, unsigned SuccessRes, unsigned Addr, unsigned CmpVal,
670     unsigned NewVal, MachineMemOperand &MMO) {
671 #ifndef NDEBUG
672   LLT OldValResTy = getMRI()->getType(OldValRes);
673   LLT SuccessResTy = getMRI()->getType(SuccessRes);
674   LLT AddrTy = getMRI()->getType(Addr);
675   LLT CmpValTy = getMRI()->getType(CmpVal);
676   LLT NewValTy = getMRI()->getType(NewVal);
677   assert(OldValResTy.isScalar() && "invalid operand type");
678   assert(SuccessResTy.isScalar() && "invalid operand type");
679   assert(AddrTy.isPointer() && "invalid operand type");
680   assert(CmpValTy.isValid() && "invalid operand type");
681   assert(NewValTy.isValid() && "invalid operand type");
682   assert(OldValResTy == CmpValTy && "type mismatch");
683   assert(OldValResTy == NewValTy && "type mismatch");
684 #endif
685 
686   return buildInstr(TargetOpcode::G_ATOMIC_CMPXCHG_WITH_SUCCESS)
687       .addDef(OldValRes)
688       .addDef(SuccessRes)
689       .addUse(Addr)
690       .addUse(CmpVal)
691       .addUse(NewVal)
692       .addMemOperand(&MMO);
693 }
694 
695 MachineInstrBuilder
696 MachineIRBuilder::buildAtomicCmpXchg(unsigned OldValRes, unsigned Addr,
697                                      unsigned CmpVal, unsigned NewVal,
698                                      MachineMemOperand &MMO) {
699 #ifndef NDEBUG
700   LLT OldValResTy = getMRI()->getType(OldValRes);
701   LLT AddrTy = getMRI()->getType(Addr);
702   LLT CmpValTy = getMRI()->getType(CmpVal);
703   LLT NewValTy = getMRI()->getType(NewVal);
704   assert(OldValResTy.isScalar() && "invalid operand type");
705   assert(AddrTy.isPointer() && "invalid operand type");
706   assert(CmpValTy.isValid() && "invalid operand type");
707   assert(NewValTy.isValid() && "invalid operand type");
708   assert(OldValResTy == CmpValTy && "type mismatch");
709   assert(OldValResTy == NewValTy && "type mismatch");
710 #endif
711 
712   return buildInstr(TargetOpcode::G_ATOMIC_CMPXCHG)
713       .addDef(OldValRes)
714       .addUse(Addr)
715       .addUse(CmpVal)
716       .addUse(NewVal)
717       .addMemOperand(&MMO);
718 }
719 
720 MachineInstrBuilder MachineIRBuilder::buildAtomicRMW(unsigned Opcode,
721                                                      unsigned OldValRes,
722                                                      unsigned Addr,
723                                                      unsigned Val,
724                                                      MachineMemOperand &MMO) {
725 #ifndef NDEBUG
726   LLT OldValResTy = getMRI()->getType(OldValRes);
727   LLT AddrTy = getMRI()->getType(Addr);
728   LLT ValTy = getMRI()->getType(Val);
729   assert(OldValResTy.isScalar() && "invalid operand type");
730   assert(AddrTy.isPointer() && "invalid operand type");
731   assert(ValTy.isValid() && "invalid operand type");
732   assert(OldValResTy == ValTy && "type mismatch");
733 #endif
734 
735   return buildInstr(Opcode)
736       .addDef(OldValRes)
737       .addUse(Addr)
738       .addUse(Val)
739       .addMemOperand(&MMO);
740 }
741 
742 MachineInstrBuilder
743 MachineIRBuilder::buildAtomicRMWXchg(unsigned OldValRes, unsigned Addr,
744                                      unsigned Val, MachineMemOperand &MMO) {
745   return buildAtomicRMW(TargetOpcode::G_ATOMICRMW_XCHG, OldValRes, Addr, Val,
746                         MMO);
747 }
748 MachineInstrBuilder
749 MachineIRBuilder::buildAtomicRMWAdd(unsigned OldValRes, unsigned Addr,
750                                     unsigned Val, MachineMemOperand &MMO) {
751   return buildAtomicRMW(TargetOpcode::G_ATOMICRMW_ADD, OldValRes, Addr, Val,
752                         MMO);
753 }
754 MachineInstrBuilder
755 MachineIRBuilder::buildAtomicRMWSub(unsigned OldValRes, unsigned Addr,
756                                     unsigned Val, MachineMemOperand &MMO) {
757   return buildAtomicRMW(TargetOpcode::G_ATOMICRMW_SUB, OldValRes, Addr, Val,
758                         MMO);
759 }
760 MachineInstrBuilder
761 MachineIRBuilder::buildAtomicRMWAnd(unsigned OldValRes, unsigned Addr,
762                                     unsigned Val, MachineMemOperand &MMO) {
763   return buildAtomicRMW(TargetOpcode::G_ATOMICRMW_AND, OldValRes, Addr, Val,
764                         MMO);
765 }
766 MachineInstrBuilder
767 MachineIRBuilder::buildAtomicRMWNand(unsigned OldValRes, unsigned Addr,
768                                      unsigned Val, MachineMemOperand &MMO) {
769   return buildAtomicRMW(TargetOpcode::G_ATOMICRMW_NAND, OldValRes, Addr, Val,
770                         MMO);
771 }
772 MachineInstrBuilder MachineIRBuilder::buildAtomicRMWOr(unsigned OldValRes,
773                                                        unsigned Addr,
774                                                        unsigned Val,
775                                                        MachineMemOperand &MMO) {
776   return buildAtomicRMW(TargetOpcode::G_ATOMICRMW_OR, OldValRes, Addr, Val,
777                         MMO);
778 }
779 MachineInstrBuilder
780 MachineIRBuilder::buildAtomicRMWXor(unsigned OldValRes, unsigned Addr,
781                                     unsigned Val, MachineMemOperand &MMO) {
782   return buildAtomicRMW(TargetOpcode::G_ATOMICRMW_XOR, OldValRes, Addr, Val,
783                         MMO);
784 }
785 MachineInstrBuilder
786 MachineIRBuilder::buildAtomicRMWMax(unsigned OldValRes, unsigned Addr,
787                                     unsigned Val, MachineMemOperand &MMO) {
788   return buildAtomicRMW(TargetOpcode::G_ATOMICRMW_MAX, OldValRes, Addr, Val,
789                         MMO);
790 }
791 MachineInstrBuilder
792 MachineIRBuilder::buildAtomicRMWMin(unsigned OldValRes, unsigned Addr,
793                                     unsigned Val, MachineMemOperand &MMO) {
794   return buildAtomicRMW(TargetOpcode::G_ATOMICRMW_MIN, OldValRes, Addr, Val,
795                         MMO);
796 }
797 MachineInstrBuilder
798 MachineIRBuilder::buildAtomicRMWUmax(unsigned OldValRes, unsigned Addr,
799                                      unsigned Val, MachineMemOperand &MMO) {
800   return buildAtomicRMW(TargetOpcode::G_ATOMICRMW_UMAX, OldValRes, Addr, Val,
801                         MMO);
802 }
803 MachineInstrBuilder
804 MachineIRBuilder::buildAtomicRMWUmin(unsigned OldValRes, unsigned Addr,
805                                      unsigned Val, MachineMemOperand &MMO) {
806   return buildAtomicRMW(TargetOpcode::G_ATOMICRMW_UMIN, OldValRes, Addr, Val,
807                         MMO);
808 }
809 
810 MachineInstrBuilder
811 MachineIRBuilder::buildBlockAddress(unsigned Res, const BlockAddress *BA) {
812 #ifndef NDEBUG
813   assert(getMRI()->getType(Res).isPointer() && "invalid res type");
814 #endif
815 
816   return buildInstr(TargetOpcode::G_BLOCK_ADDR).addDef(Res).addBlockAddress(BA);
817 }
818 
819 void MachineIRBuilder::validateTruncExt(const LLT &DstTy, const LLT &SrcTy,
820                                         bool IsExtend) {
821 #ifndef NDEBUG
822   if (DstTy.isVector()) {
823     assert(SrcTy.isVector() && "mismatched cast between vector and non-vector");
824     assert(SrcTy.getNumElements() == DstTy.getNumElements() &&
825            "different number of elements in a trunc/ext");
826   } else
827     assert(DstTy.isScalar() && SrcTy.isScalar() && "invalid extend/trunc");
828 
829   if (IsExtend)
830     assert(DstTy.getSizeInBits() > SrcTy.getSizeInBits() &&
831            "invalid narrowing extend");
832   else
833     assert(DstTy.getSizeInBits() < SrcTy.getSizeInBits() &&
834            "invalid widening trunc");
835 #endif
836 }
837 
838 void MachineIRBuilder::validateSelectOp(const LLT &ResTy, const LLT &TstTy,
839                                         const LLT &Op0Ty, const LLT &Op1Ty) {
840 #ifndef NDEBUG
841   assert((ResTy.isScalar() || ResTy.isVector() || ResTy.isPointer()) &&
842          "invalid operand type");
843   assert((ResTy == Op0Ty && ResTy == Op1Ty) && "type mismatch");
844   if (ResTy.isScalar() || ResTy.isPointer())
845     assert(TstTy.isScalar() && "type mismatch");
846   else
847     assert((TstTy.isScalar() ||
848             (TstTy.isVector() &&
849              TstTy.getNumElements() == Op0Ty.getNumElements())) &&
850            "type mismatch");
851 #endif
852 }
853 
854 MachineInstrBuilder MachineIRBuilder::buildInstr(unsigned Opc,
855                                                  ArrayRef<DstOp> DstOps,
856                                                  ArrayRef<SrcOp> SrcOps,
857                                                  Optional<unsigned> Flags) {
858   switch (Opc) {
859   default:
860     break;
861   case TargetOpcode::G_SELECT: {
862     assert(DstOps.size() == 1 && "Invalid select");
863     assert(SrcOps.size() == 3 && "Invalid select");
864     validateSelectOp(
865         DstOps[0].getLLTTy(*getMRI()), SrcOps[0].getLLTTy(*getMRI()),
866         SrcOps[1].getLLTTy(*getMRI()), SrcOps[2].getLLTTy(*getMRI()));
867     break;
868   }
869   case TargetOpcode::G_ADD:
870   case TargetOpcode::G_AND:
871   case TargetOpcode::G_MUL:
872   case TargetOpcode::G_OR:
873   case TargetOpcode::G_SUB:
874   case TargetOpcode::G_XOR:
875   case TargetOpcode::G_UDIV:
876   case TargetOpcode::G_SDIV:
877   case TargetOpcode::G_UREM:
878   case TargetOpcode::G_SREM: {
879     // All these are binary ops.
880     assert(DstOps.size() == 1 && "Invalid Dst");
881     assert(SrcOps.size() == 2 && "Invalid Srcs");
882     validateBinaryOp(DstOps[0].getLLTTy(*getMRI()),
883                      SrcOps[0].getLLTTy(*getMRI()),
884                      SrcOps[1].getLLTTy(*getMRI()));
885     break;
886   }
887   case TargetOpcode::G_SHL:
888   case TargetOpcode::G_ASHR:
889   case TargetOpcode::G_LSHR: {
890     assert(DstOps.size() == 1 && "Invalid Dst");
891     assert(SrcOps.size() == 2 && "Invalid Srcs");
892     validateShiftOp(DstOps[0].getLLTTy(*getMRI()),
893                     SrcOps[0].getLLTTy(*getMRI()),
894                     SrcOps[1].getLLTTy(*getMRI()));
895     break;
896   }
897   case TargetOpcode::G_SEXT:
898   case TargetOpcode::G_ZEXT:
899   case TargetOpcode::G_ANYEXT:
900     assert(DstOps.size() == 1 && "Invalid Dst");
901     assert(SrcOps.size() == 1 && "Invalid Srcs");
902     validateTruncExt(DstOps[0].getLLTTy(*getMRI()),
903                      SrcOps[0].getLLTTy(*getMRI()), true);
904     break;
905   case TargetOpcode::G_TRUNC:
906   case TargetOpcode::G_FPTRUNC: {
907     assert(DstOps.size() == 1 && "Invalid Dst");
908     assert(SrcOps.size() == 1 && "Invalid Srcs");
909     validateTruncExt(DstOps[0].getLLTTy(*getMRI()),
910                      SrcOps[0].getLLTTy(*getMRI()), false);
911     break;
912   }
913   case TargetOpcode::COPY:
914     assert(DstOps.size() == 1 && "Invalid Dst");
915     assert(SrcOps.size() == 1 && "Invalid Srcs");
916     assert(DstOps[0].getLLTTy(*getMRI()) == LLT() ||
917            SrcOps[0].getLLTTy(*getMRI()) == LLT() ||
918            DstOps[0].getLLTTy(*getMRI()) == SrcOps[0].getLLTTy(*getMRI()));
919     break;
920   case TargetOpcode::G_FCMP:
921   case TargetOpcode::G_ICMP: {
922     assert(DstOps.size() == 1 && "Invalid Dst Operands");
923     assert(SrcOps.size() == 3 && "Invalid Src Operands");
924     // For F/ICMP, the first src operand is the predicate, followed by
925     // the two comparands.
926     assert(SrcOps[0].getSrcOpKind() == SrcOp::SrcType::Ty_Predicate &&
927            "Expecting predicate");
928     assert([&]() -> bool {
929       CmpInst::Predicate Pred = SrcOps[0].getPredicate();
930       return Opc == TargetOpcode::G_ICMP ? CmpInst::isIntPredicate(Pred)
931                                          : CmpInst::isFPPredicate(Pred);
932     }() && "Invalid predicate");
933     assert(SrcOps[1].getLLTTy(*getMRI()) == SrcOps[2].getLLTTy(*getMRI()) &&
934            "Type mismatch");
935     assert([&]() -> bool {
936       LLT Op0Ty = SrcOps[1].getLLTTy(*getMRI());
937       LLT DstTy = DstOps[0].getLLTTy(*getMRI());
938       if (Op0Ty.isScalar() || Op0Ty.isPointer())
939         return DstTy.isScalar();
940       else
941         return DstTy.isVector() &&
942                DstTy.getNumElements() == Op0Ty.getNumElements();
943     }() && "Type Mismatch");
944     break;
945   }
946   case TargetOpcode::G_UNMERGE_VALUES: {
947     assert(!DstOps.empty() && "Invalid trivial sequence");
948     assert(SrcOps.size() == 1 && "Invalid src for Unmerge");
949     assert(std::all_of(DstOps.begin(), DstOps.end(),
950                        [&, this](const DstOp &Op) {
951                          return Op.getLLTTy(*getMRI()) ==
952                                 DstOps[0].getLLTTy(*getMRI());
953                        }) &&
954            "type mismatch in output list");
955     assert(DstOps.size() * DstOps[0].getLLTTy(*getMRI()).getSizeInBits() ==
956                SrcOps[0].getLLTTy(*getMRI()).getSizeInBits() &&
957            "input operands do not cover output register");
958     break;
959   }
960   case TargetOpcode::G_MERGE_VALUES: {
961     assert(!SrcOps.empty() && "invalid trivial sequence");
962     assert(DstOps.size() == 1 && "Invalid Dst");
963     assert(std::all_of(SrcOps.begin(), SrcOps.end(),
964                        [&, this](const SrcOp &Op) {
965                          return Op.getLLTTy(*getMRI()) ==
966                                 SrcOps[0].getLLTTy(*getMRI());
967                        }) &&
968            "type mismatch in input list");
969     assert(SrcOps.size() * SrcOps[0].getLLTTy(*getMRI()).getSizeInBits() ==
970                DstOps[0].getLLTTy(*getMRI()).getSizeInBits() &&
971            "input operands do not cover output register");
972     if (SrcOps.size() == 1)
973       return buildCast(DstOps[0], SrcOps[0]);
974     if (DstOps[0].getLLTTy(*getMRI()).isVector())
975       return buildInstr(TargetOpcode::G_CONCAT_VECTORS, DstOps, SrcOps);
976     break;
977   }
978   case TargetOpcode::G_EXTRACT_VECTOR_ELT: {
979     assert(DstOps.size() == 1 && "Invalid Dst size");
980     assert(SrcOps.size() == 2 && "Invalid Src size");
981     assert(SrcOps[0].getLLTTy(*getMRI()).isVector() && "Invalid operand type");
982     assert((DstOps[0].getLLTTy(*getMRI()).isScalar() ||
983             DstOps[0].getLLTTy(*getMRI()).isPointer()) &&
984            "Invalid operand type");
985     assert(SrcOps[1].getLLTTy(*getMRI()).isScalar() && "Invalid operand type");
986     assert(SrcOps[0].getLLTTy(*getMRI()).getElementType() ==
987                DstOps[0].getLLTTy(*getMRI()) &&
988            "Type mismatch");
989     break;
990   }
991   case TargetOpcode::G_INSERT_VECTOR_ELT: {
992     assert(DstOps.size() == 1 && "Invalid dst size");
993     assert(SrcOps.size() == 3 && "Invalid src size");
994     assert(DstOps[0].getLLTTy(*getMRI()).isVector() &&
995            SrcOps[0].getLLTTy(*getMRI()).isVector() && "Invalid operand type");
996     assert(DstOps[0].getLLTTy(*getMRI()).getElementType() ==
997                SrcOps[1].getLLTTy(*getMRI()) &&
998            "Type mismatch");
999     assert(SrcOps[2].getLLTTy(*getMRI()).isScalar() && "Invalid index");
1000     assert(DstOps[0].getLLTTy(*getMRI()).getNumElements() ==
1001                SrcOps[0].getLLTTy(*getMRI()).getNumElements() &&
1002            "Type mismatch");
1003     break;
1004   }
1005   case TargetOpcode::G_BUILD_VECTOR: {
1006     assert((!SrcOps.empty() || SrcOps.size() < 2) &&
1007            "Must have at least 2 operands");
1008     assert(DstOps.size() == 1 && "Invalid DstOps");
1009     assert(DstOps[0].getLLTTy(*getMRI()).isVector() &&
1010            "Res type must be a vector");
1011     assert(std::all_of(SrcOps.begin(), SrcOps.end(),
1012                        [&, this](const SrcOp &Op) {
1013                          return Op.getLLTTy(*getMRI()) ==
1014                                 SrcOps[0].getLLTTy(*getMRI());
1015                        }) &&
1016            "type mismatch in input list");
1017     assert(SrcOps.size() * SrcOps[0].getLLTTy(*getMRI()).getSizeInBits() ==
1018                DstOps[0].getLLTTy(*getMRI()).getSizeInBits() &&
1019            "input scalars do not exactly cover the output vector register");
1020     break;
1021   }
1022   case TargetOpcode::G_BUILD_VECTOR_TRUNC: {
1023     assert((!SrcOps.empty() || SrcOps.size() < 2) &&
1024            "Must have at least 2 operands");
1025     assert(DstOps.size() == 1 && "Invalid DstOps");
1026     assert(DstOps[0].getLLTTy(*getMRI()).isVector() &&
1027            "Res type must be a vector");
1028     assert(std::all_of(SrcOps.begin(), SrcOps.end(),
1029                        [&, this](const SrcOp &Op) {
1030                          return Op.getLLTTy(*getMRI()) ==
1031                                 SrcOps[0].getLLTTy(*getMRI());
1032                        }) &&
1033            "type mismatch in input list");
1034     if (SrcOps[0].getLLTTy(*getMRI()).getSizeInBits() ==
1035         DstOps[0].getLLTTy(*getMRI()).getElementType().getSizeInBits())
1036       return buildInstr(TargetOpcode::G_BUILD_VECTOR, DstOps, SrcOps);
1037     break;
1038   }
1039   case TargetOpcode::G_CONCAT_VECTORS: {
1040     assert(DstOps.size() == 1 && "Invalid DstOps");
1041     assert((!SrcOps.empty() || SrcOps.size() < 2) &&
1042            "Must have at least 2 operands");
1043     assert(std::all_of(SrcOps.begin(), SrcOps.end(),
1044                        [&, this](const SrcOp &Op) {
1045                          return (Op.getLLTTy(*getMRI()).isVector() &&
1046                                  Op.getLLTTy(*getMRI()) ==
1047                                      SrcOps[0].getLLTTy(*getMRI()));
1048                        }) &&
1049            "type mismatch in input list");
1050     assert(SrcOps.size() * SrcOps[0].getLLTTy(*getMRI()).getSizeInBits() ==
1051                DstOps[0].getLLTTy(*getMRI()).getSizeInBits() &&
1052            "input vectors do not exactly cover the output vector register");
1053     break;
1054   }
1055   case TargetOpcode::G_UADDE: {
1056     assert(DstOps.size() == 2 && "Invalid no of dst operands");
1057     assert(SrcOps.size() == 3 && "Invalid no of src operands");
1058     assert(DstOps[0].getLLTTy(*getMRI()).isScalar() && "Invalid operand");
1059     assert((DstOps[0].getLLTTy(*getMRI()) == SrcOps[0].getLLTTy(*getMRI())) &&
1060            (DstOps[0].getLLTTy(*getMRI()) == SrcOps[1].getLLTTy(*getMRI())) &&
1061            "Invalid operand");
1062     assert(DstOps[1].getLLTTy(*getMRI()).isScalar() && "Invalid operand");
1063     assert(DstOps[1].getLLTTy(*getMRI()) == SrcOps[2].getLLTTy(*getMRI()) &&
1064            "type mismatch");
1065     break;
1066   }
1067   }
1068 
1069   auto MIB = buildInstr(Opc);
1070   for (const DstOp &Op : DstOps)
1071     Op.addDefToMIB(*getMRI(), MIB);
1072   for (const SrcOp &Op : SrcOps)
1073     Op.addSrcToMIB(MIB);
1074   if (Flags)
1075     MIB->setFlags(*Flags);
1076   return MIB;
1077 }
1078