LLVM  4.0.0
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 
22 using namespace llvm;
23 
25  this->MF = &MF;
26  this->MBB = nullptr;
27  this->MRI = &MF.getRegInfo();
28  this->TII = MF.getSubtarget().getInstrInfo();
29  this->DL = DebugLoc();
30  this->II = MachineBasicBlock::iterator();
31  this->InsertedInstr = nullptr;
32 }
33 
35  this->MBB = &MBB;
36  this->II = MBB.end();
37  assert(&getMF() == MBB.getParent() &&
38  "Basic block is in a different function");
39 }
40 
42  assert(MI.getParent() && "Instruction is not part of a basic block");
43  setMBB(*MI.getParent());
44  this->II = MI.getIterator();
45 }
46 
49  assert(MBB.getParent() == &getMF() &&
50  "Basic block is in a different function");
51  this->MBB = &MBB;
52  this->II = II;
53 }
54 
56  std::function<void(MachineInstr *)> Inserted) {
57  InsertedInstr = Inserted;
58 }
59 
61  InsertedInstr = nullptr;
62 }
63 
64 //------------------------------------------------------------------------------
65 // Build instruction variants.
66 //------------------------------------------------------------------------------
67 
69  return insertInstr(buildInstrNoInsert(Opcode));
70 }
71 
73  MachineInstrBuilder MIB = BuildMI(getMF(), DL, getTII().get(Opcode));
74  return MIB;
75 }
76 
77 
79  getMBB().insert(getInsertPt(), MIB);
80  if (InsertedInstr)
81  InsertedInstr(MIB);
82  return MIB;
83 }
84 
86  assert(MRI->getType(Res).isPointer() && "invalid operand type");
87  return buildInstr(TargetOpcode::G_FRAME_INDEX)
88  .addDef(Res)
89  .addFrameIndex(Idx);
90 }
91 
93  const GlobalValue *GV) {
94  assert(MRI->getType(Res).isPointer() && "invalid operand type");
95  assert(MRI->getType(Res).getAddressSpace() ==
96  GV->getType()->getAddressSpace() &&
97  "address space mismatch");
98 
99  return buildInstr(TargetOpcode::G_GLOBAL_VALUE)
100  .addDef(Res)
101  .addGlobalAddress(GV);
102 }
103 
105  unsigned Op1) {
106  assert((MRI->getType(Res).isScalar() || MRI->getType(Res).isVector()) &&
107  "invalid operand type");
108  assert(MRI->getType(Res) == MRI->getType(Op0) &&
109  MRI->getType(Res) == MRI->getType(Op1) && "type mismatch");
110 
111  return buildInstr(TargetOpcode::G_ADD)
112  .addDef(Res)
113  .addUse(Op0)
114  .addUse(Op1);
115 }
116 
118  unsigned Op1) {
119  assert(MRI->getType(Res).isPointer() &&
120  MRI->getType(Res) == MRI->getType(Op0) && "type mismatch");
121  assert(MRI->getType(Op1).isScalar() && "invalid offset type");
122 
123  return buildInstr(TargetOpcode::G_GEP)
124  .addDef(Res)
125  .addUse(Op0)
126  .addUse(Op1);
127 }
128 
130  unsigned Op1) {
131  assert((MRI->getType(Res).isScalar() || MRI->getType(Res).isVector()) &&
132  "invalid operand type");
133  assert(MRI->getType(Res) == MRI->getType(Op0) &&
134  MRI->getType(Res) == MRI->getType(Op1) && "type mismatch");
135 
136  return buildInstr(TargetOpcode::G_SUB)
137  .addDef(Res)
138  .addUse(Op0)
139  .addUse(Op1);
140 }
141 
143  unsigned Op1) {
144  assert((MRI->getType(Res).isScalar() || MRI->getType(Res).isVector()) &&
145  "invalid operand type");
146  assert(MRI->getType(Res) == MRI->getType(Op0) &&
147  MRI->getType(Res) == MRI->getType(Op1) && "type mismatch");
148 
149  return buildInstr(TargetOpcode::G_MUL)
150  .addDef(Res)
151  .addUse(Op0)
152  .addUse(Op1);
153 }
154 
156  return buildInstr(TargetOpcode::G_BR).addMBB(&Dest);
157 }
158 
160  return buildInstr(TargetOpcode::COPY).addDef(Res).addUse(Op);
161 }
162 
164  const ConstantInt &Val) {
165  LLT Ty = MRI->getType(Res);
166 
167  assert((Ty.isScalar() || Ty.isPointer()) && "invalid operand type");
168 
169  const ConstantInt *NewVal = &Val;
170  if (Ty.getSizeInBits() != Val.getBitWidth())
171  NewVal = ConstantInt::get(MF->getFunction()->getContext(),
172  Val.getValue().sextOrTrunc(Ty.getSizeInBits()));
173 
174  return buildInstr(TargetOpcode::G_CONSTANT).addDef(Res).addCImm(NewVal);
175 }
176 
178  int64_t Val) {
179  auto IntN = IntegerType::get(MF->getFunction()->getContext(),
180  MRI->getType(Res).getSizeInBits());
181  ConstantInt *CI = ConstantInt::get(IntN, Val, true);
182  return buildConstant(Res, *CI);
183 }
184 
186  const ConstantFP &Val) {
187  assert(MRI->getType(Res).isScalar() && "invalid operand type");
188 
189  return buildInstr(TargetOpcode::G_FCONSTANT).addDef(Res).addFPImm(&Val);
190 }
191 
193  MachineBasicBlock &Dest) {
194  assert(MRI->getType(Tst).isScalar() && "invalid operand type");
195 
196  return buildInstr(TargetOpcode::G_BRCOND).addUse(Tst).addMBB(&Dest);
197 }
198 
200  MachineMemOperand &MMO) {
201  assert(MRI->getType(Res).isValid() && "invalid operand type");
202  assert(MRI->getType(Addr).isPointer() && "invalid operand type");
203 
204  return buildInstr(TargetOpcode::G_LOAD)
205  .addDef(Res)
206  .addUse(Addr)
207  .addMemOperand(&MMO);
208 }
209 
211  MachineMemOperand &MMO) {
212  assert(MRI->getType(Val).isValid() && "invalid operand type");
213  assert(MRI->getType(Addr).isPointer() && "invalid operand type");
214 
215  return buildInstr(TargetOpcode::G_STORE)
216  .addUse(Val)
217  .addUse(Addr)
218  .addMemOperand(&MMO);
219 }
220 
222  unsigned CarryOut,
223  unsigned Op0, unsigned Op1,
224  unsigned CarryIn) {
225  assert(MRI->getType(Res).isScalar() && "invalid operand type");
226  assert(MRI->getType(Res) == MRI->getType(Op0) &&
227  MRI->getType(Res) == MRI->getType(Op1) && "type mismatch");
228  assert(MRI->getType(CarryOut).isScalar() && "invalid operand type");
229  assert(MRI->getType(CarryOut) == MRI->getType(CarryIn) && "type mismatch");
230 
231  return buildInstr(TargetOpcode::G_UADDE)
232  .addDef(Res)
233  .addDef(CarryOut)
234  .addUse(Op0)
235  .addUse(Op1)
236  .addUse(CarryIn);
237 }
238 
240  validateTruncExt(Res, Op, true);
241  return buildInstr(TargetOpcode::G_ANYEXT).addDef(Res).addUse(Op);
242 }
243 
245  validateTruncExt(Res, Op, true);
246  return buildInstr(TargetOpcode::G_SEXT).addDef(Res).addUse(Op);
247 }
248 
250  validateTruncExt(Res, Op, true);
251  return buildInstr(TargetOpcode::G_ZEXT).addDef(Res).addUse(Op);
252 }
253 
255  unsigned Op) {
256  unsigned Opcode = TargetOpcode::COPY;
257  if (MRI->getType(Res).getSizeInBits() > MRI->getType(Op).getSizeInBits())
258  Opcode = TargetOpcode::G_SEXT;
259  else if (MRI->getType(Res).getSizeInBits() < MRI->getType(Op).getSizeInBits())
260  Opcode = TargetOpcode::G_TRUNC;
261 
262  return buildInstr(Opcode).addDef(Res).addUse(Op);
263 }
264 
266  ArrayRef<uint64_t> Indices,
267  unsigned Src) {
268 #ifndef NDEBUG
269  assert(Results.size() == Indices.size() && "inconsistent number of regs");
270  assert(!Results.empty() && "invalid trivial extract");
271  assert(std::is_sorted(Indices.begin(), Indices.end()) &&
272  "extract offsets must be in ascending order");
273 
274  assert(MRI->getType(Src).isValid() && "invalid operand type");
275  for (auto Res : Results)
276  assert(MRI->getType(Res).isValid() && "invalid operand type");
277 #endif
278 
279  auto MIB = BuildMI(getMF(), DL, getTII().get(TargetOpcode::G_EXTRACT));
280  for (auto Res : Results)
281  MIB.addDef(Res);
282 
283  MIB.addUse(Src);
284 
285  for (auto Idx : Indices)
286  MIB.addImm(Idx);
287 
288  getMBB().insert(getInsertPt(), MIB);
289  if (InsertedInstr)
290  InsertedInstr(MIB);
291 
292  return MIB;
293 }
294 
297  ArrayRef<unsigned> Ops,
298  ArrayRef<uint64_t> Indices) {
299 #ifndef NDEBUG
300  assert(Ops.size() == Indices.size() && "incompatible args");
301  assert(!Ops.empty() && "invalid trivial sequence");
302  assert(std::is_sorted(Indices.begin(), Indices.end()) &&
303  "sequence offsets must be in ascending order");
304 
305  assert(MRI->getType(Res).isValid() && "invalid operand type");
306  for (auto Op : Ops)
307  assert(MRI->getType(Op).isValid() && "invalid operand type");
308 #endif
309 
310  MachineInstrBuilder MIB = buildInstr(TargetOpcode::G_SEQUENCE);
311  MIB.addDef(Res);
312  for (unsigned i = 0; i < Ops.size(); ++i) {
313  MIB.addUse(Ops[i]);
314  MIB.addImm(Indices[i]);
315  }
316  return MIB;
317 }
318 
320  unsigned Res,
321  bool HasSideEffects) {
322  auto MIB =
323  buildInstr(HasSideEffects ? TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS
324  : TargetOpcode::G_INTRINSIC);
325  if (Res)
326  MIB.addDef(Res);
327  MIB.addIntrinsicID(ID);
328  return MIB;
329 }
330 
332  validateTruncExt(Res, Op, false);
333  return buildInstr(TargetOpcode::G_TRUNC).addDef(Res).addUse(Op);
334 }
335 
337  validateTruncExt(Res, Op, false);
338  return buildInstr(TargetOpcode::G_FPTRUNC).addDef(Res).addUse(Op);
339 }
340 
342  unsigned Res, unsigned Op0,
343  unsigned Op1) {
344 #ifndef NDEBUG
345  assert(MRI->getType(Op0) == MRI->getType(Op0) && "type mismatch");
346  assert(CmpInst::isIntPredicate(Pred) && "invalid predicate");
347  if (MRI->getType(Op0).isScalar() || MRI->getType(Op0).isPointer())
348  assert(MRI->getType(Res).isScalar() && "type mismatch");
349  else
350  assert(MRI->getType(Res).isVector() &&
351  MRI->getType(Res).getNumElements() ==
352  MRI->getType(Op0).getNumElements() &&
353  "type mismatch");
354 #endif
355 
356  return buildInstr(TargetOpcode::G_ICMP)
357  .addDef(Res)
358  .addPredicate(Pred)
359  .addUse(Op0)
360  .addUse(Op1);
361 }
362 
364  unsigned Res, unsigned Op0,
365  unsigned Op1) {
366 #ifndef NDEBUG
367  assert((MRI->getType(Op0).isScalar() || MRI->getType(Op0).isVector()) &&
368  "invalid operand type");
369  assert(MRI->getType(Op0) == MRI->getType(Op1) && "type mismatch");
370  assert(CmpInst::isFPPredicate(Pred) && "invalid predicate");
371  if (MRI->getType(Op0).isScalar())
372  assert(MRI->getType(Res).isScalar() && "type mismatch");
373  else
374  assert(MRI->getType(Res).isVector() &&
375  MRI->getType(Res).getNumElements() ==
376  MRI->getType(Op0).getNumElements() &&
377  "type mismatch");
378 #endif
379 
380  return buildInstr(TargetOpcode::G_FCMP)
381  .addDef(Res)
382  .addPredicate(Pred)
383  .addUse(Op0)
384  .addUse(Op1);
385 }
386 
388  unsigned Op0, unsigned Op1) {
389 #ifndef NDEBUG
390  LLT ResTy = MRI->getType(Res);
391  assert((ResTy.isScalar() || ResTy.isVector() || ResTy.isPointer()) &&
392  "invalid operand type");
393  assert(ResTy == MRI->getType(Op0) && ResTy == MRI->getType(Op1) &&
394  "type mismatch");
395  if (ResTy.isScalar() || ResTy.isPointer())
396  assert(MRI->getType(Tst).isScalar() && "type mismatch");
397  else
398  assert(MRI->getType(Tst).isVector() &&
399  MRI->getType(Tst).getNumElements() ==
400  MRI->getType(Op0).getNumElements() &&
401  "type mismatch");
402 #endif
403 
404  return buildInstr(TargetOpcode::G_SELECT)
405  .addDef(Res)
406  .addUse(Tst)
407  .addUse(Op0)
408  .addUse(Op1);
409 }
410 
411 void MachineIRBuilder::validateTruncExt(unsigned Dst, unsigned Src,
412  bool IsExtend) {
413 #ifndef NDEBUG
414  LLT SrcTy = MRI->getType(Src);
415  LLT DstTy = MRI->getType(Dst);
416 
417  if (DstTy.isVector()) {
418  assert(SrcTy.isVector() && "mismatched cast between vecot and non-vector");
419  assert(SrcTy.getNumElements() == DstTy.getNumElements() &&
420  "different number of elements in a trunc/ext");
421  } else
422  assert(DstTy.isScalar() && SrcTy.isScalar() && "invalid extend/trunc");
423 
424  if (IsExtend)
425  assert(DstTy.getSizeInBits() > SrcTy.getSizeInBits() &&
426  "invalid narrowing extend");
427  else
428  assert(DstTy.getSizeInBits() < SrcTy.getSizeInBits() &&
429  "invalid widening trunc");
430 #endif
431 }
MachineBasicBlock & getMBB()
Getter for the basic block we currently build.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:226
MachineInstrBuilder buildGEP(unsigned Res, unsigned Op0, unsigned Op1)
Build and insert Res<def> = G_GEP Op0, Op1.
LLT getType(unsigned VReg) const
Get the low-level type of VReg or LLT{} if VReg is not a generic (target independent) virtual registe...
size_t i
MachineInstrBuilder buildZExt(unsigned Res, unsigned Op)
Build and insert Res<def> = G_ZEXT Op.
unsigned getBitWidth() const
getBitWidth - Return the bitwidth of this constant.
Definition: Constants.h:148
iterator end() const
Definition: ArrayRef.h:130
Function Alias Analysis Results
MachineInstrBuilder buildTrunc(unsigned Res, unsigned Op)
Build and insert Res<def> = G_TRUNC Op.
MachineInstrBundleIterator< MachineInstr > iterator
A debug info location.
Definition: DebugLoc.h:34
bool isPointer() const
Definition: LowLevelType.h:92
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
MachineInstrBuilder buildSub(unsigned Res, unsigned Op0, unsigned Op1)
Build and insert Res<def> = G_SUB Op0, Op1.
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:471
MachineInstrBuilder buildAnyExt(unsigned Res, unsigned Op)
Build and insert Res<def> = G_ANYEXT Op0.
const MachineInstrBuilder & addDef(unsigned RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register definition operand.
MachineInstrBuilder buildFPTrunc(unsigned Res, unsigned Op)
Build and insert Res<def> = G_FPTRUNC Op.
MachineInstrBuilder buildSelect(unsigned Res, unsigned Tst, unsigned Op0, unsigned Op1)
Build and insert a Res = G_SELECT Tst, Op0, Op1.
MachineInstrBuilder buildFCmp(CmpInst::Predicate Pred, unsigned Res, unsigned Op0, unsigned Op1)
Build and insert a Res = G_FCMP PredOp0, Op1.
MachineInstrBuilder buildStore(unsigned Val, unsigned Addr, MachineMemOperand &MMO)
Build and insert G_STORE Val, Addr, MMO.
A description of a memory reference used in the backend.
void setInsertPt(MachineBasicBlock &MBB, MachineBasicBlock::iterator II)
Set the insertion point before the specified position.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
unsigned getAddressSpace() const
Definition: LowLevelType.h:114
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:143
MachineInstrBuilder buildFConstant(unsigned Res, const ConstantFP &Val)
Build and insert Res = G_FCONSTANT Val.
MachineInstrBuilder buildExtract(ArrayRef< unsigned > Results, ArrayRef< uint64_t > Indices, unsigned Src)
Build and insert `Res0<def>, ...
MachineBasicBlock * MBB
MachineInstrBuilder buildInstrNoInsert(unsigned Opcode)
Build but don't insert <empty> = Opcode <empty>.
MachineFunction & getMF()
Getter for the function we currently build.
void recordInsertions(std::function< void(MachineInstr *)> InsertedInstr)
Control where instructions we create are recorded (typically for visiting again later during legaliza...
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:141
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:131
MachineBasicBlock::iterator getInsertPt()
Current insertion point for new instructions.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
MachineInstrBuilder buildBr(MachineBasicBlock &BB)
Build and insert G_BR Dest.
MachineInstrBuilder buildInstr(unsigned Opcode)
Build and insert <empty> = Opcode <empty>.
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:269
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...
bool isIntPredicate() const
Definition: InstrTypes.h:978
MachineInstrBuilder buildICmp(CmpInst::Predicate Pred, unsigned Res, unsigned Op0, unsigned Op1)
Build and insert a Res = G_ICMP Pred, Op0, Op1.
void setInstr(MachineInstr &MI)
Set the insertion point to before MI.
const MachineInstrBuilder & addPredicate(CmpInst::Predicate Pred) const
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:880
iterator begin() const
Definition: ArrayRef.h:129
self_iterator getIterator()
Definition: ilist_node.h:81
void setMF(MachineFunction &)
Setters for the insertion point.
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:136
MachineInstrBuilder buildSExtOrTrunc(unsigned Res, unsigned Op)
Build and insert Res<def> = G_SEXT Op, Res = G_TRUNC Op, or Res = COPY Op depending on the differing ...
MachineInstrBuilder buildUAdde(unsigned Res, unsigned CarryOut, unsigned Op0, unsigned Op1, unsigned CarryIn)
Build and insert Res<def>, CarryOut<def> = G_UADDE Op0, Op1, CarryIn.
uint16_t getNumElements() const
Returns the number of elements in a vector LLT.
Definition: LowLevelType.h:98
MachineInstrBuilder buildFrameIndex(unsigned Res, int Idx)
Build and insert Res<def> = G_FRAME_INDEX Idx.
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:234
This is the shared class of boolean and integer constants.
Definition: Constants.h:88
MachineInstrBuilder buildCopy(unsigned Res, unsigned Op)
Build and insert Res<def> = COPY Op.
const MachineInstrBuilder & addCImm(const ConstantInt *Val) const
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:558
const MachineInstrBuilder & addFrameIndex(int Idx) const
This file declares the MachineIRBuilder class.
MachineInstrBuilder buildSExt(unsigned Res, unsigned Op)
Build and insert Res<def> = G_SEXT Op.
bool isScalar() const
Definition: LowLevelType.h:90
bool isFPPredicate() const
Definition: InstrTypes.h:977
const MachineInstrBuilder & addGlobalAddress(const GlobalValue *GV, int64_t Offset=0, unsigned char TargetFlags=0) const
MachineInstrBuilder buildSequence(unsigned Res, ArrayRef< unsigned > Ops, ArrayRef< uint64_t > Indices)
Build and insert Res<def> = G_SEQUENCE Op0, Idx0...
Representation of each machine instruction.
Definition: MachineInstr.h:52
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:259
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
MachineInstrBuilder buildAdd(unsigned Res, unsigned Op0, unsigned Op1)
Build and insert Res<def> = G_ADD Op0, Op1.
void setMBB(MachineBasicBlock &MBB)
Set the insertion point to the end of MBB.
MachineInstrBuilder buildConstant(unsigned Res, const ConstantInt &Val)
Build and insert Res = G_CONSTANT Val.
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
MachineInstrBuilder buildMul(unsigned Res, unsigned Op0, unsigned Op1)
Build and insert Res<def> = G_MUL Op0, Op1.
bool isValid() const
Definition: LowLevelType.h:88
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
const MachineInstrBuilder & addFPImm(const ConstantFP *Val) const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const MachineInstrBuilder & addUse(unsigned RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register use operand.
virtual const TargetInstrInfo * getInstrInfo() const
MachineInstrBuilder insertInstr(MachineInstrBuilder MIB)
Insert an existing instruction at the insertion point.
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:1007
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned char TargetFlags=0) const
MachineInstrBuilder buildLoad(unsigned Res, unsigned Addr, MachineMemOperand &MMO)
Build and insert Res<def> = G_LOAD Addr, MMO.
print Print MemDeps of function
IRTranslator LLVM IR MI
unsigned getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
Definition: LowLevelType.h:104
bool isVector() const
Definition: LowLevelType.h:94
MachineInstrBuilder buildGlobalValue(unsigned Res, const GlobalValue *GV)
Build and insert Res<def> = G_GLOBAL_VALUE GV.
MachineInstrBuilder buildBrCond(unsigned Tst, MachineBasicBlock &BB)
Build and insert G_BRCOND Tst, Dest.