2016-02-12 01:44:59 +08:00
|
|
|
//===-- llvm/CodeGen/GlobalISel/MachineIRBuilder.cpp - MIBuilder--*- C++ -*-==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// \file
|
|
|
|
/// This file implements the MachineIRBuidler class.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2016-09-09 19:46:34 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2017-01-27 07:39:14 +08:00
|
|
|
#include "llvm/IR/DebugInfo.h"
|
2016-02-12 01:44:59 +08:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2016-02-12 05:16:56 +08:00
|
|
|
#include "llvm/Target/TargetOpcodes.h"
|
2016-02-12 01:44:59 +08:00
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2016-03-12 01:27:51 +08:00
|
|
|
void MachineIRBuilder::setMF(MachineFunction &MF) {
|
2016-02-12 01:44:59 +08:00
|
|
|
this->MF = &MF;
|
|
|
|
this->MBB = nullptr;
|
2016-09-09 19:46:34 +08:00
|
|
|
this->MRI = &MF.getRegInfo();
|
2016-02-12 01:44:59 +08:00
|
|
|
this->TII = MF.getSubtarget().getInstrInfo();
|
|
|
|
this->DL = DebugLoc();
|
2016-12-08 05:05:38 +08:00
|
|
|
this->II = MachineBasicBlock::iterator();
|
2016-08-26 01:37:32 +08:00
|
|
|
this->InsertedInstr = nullptr;
|
2016-02-12 01:44:59 +08:00
|
|
|
}
|
|
|
|
|
2016-12-08 05:05:38 +08:00
|
|
|
void MachineIRBuilder::setMBB(MachineBasicBlock &MBB) {
|
2016-02-12 01:44:59 +08:00
|
|
|
this->MBB = &MBB;
|
2016-12-08 05:05:38 +08:00
|
|
|
this->II = MBB.end();
|
2016-02-12 01:44:59 +08:00
|
|
|
assert(&getMF() == MBB.getParent() &&
|
|
|
|
"Basic block is in a different function");
|
|
|
|
}
|
|
|
|
|
2016-12-08 05:05:38 +08:00
|
|
|
void MachineIRBuilder::setInstr(MachineInstr &MI) {
|
2016-02-12 01:44:59 +08:00
|
|
|
assert(MI.getParent() && "Instruction is not part of a basic block");
|
2016-03-12 01:27:47 +08:00
|
|
|
setMBB(*MI.getParent());
|
2016-12-08 05:05:38 +08:00
|
|
|
this->II = MI.getIterator();
|
2016-02-12 01:44:59 +08:00
|
|
|
}
|
|
|
|
|
2016-12-08 05:05:38 +08:00
|
|
|
void MachineIRBuilder::setInsertPt(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator II) {
|
|
|
|
assert(MBB.getParent() == &getMF() &&
|
|
|
|
"Basic block is in a different function");
|
|
|
|
this->MBB = &MBB;
|
|
|
|
this->II = II;
|
2016-02-12 01:44:59 +08:00
|
|
|
}
|
|
|
|
|
2016-08-26 01:37:32 +08:00
|
|
|
void MachineIRBuilder::recordInsertions(
|
|
|
|
std::function<void(MachineInstr *)> Inserted) {
|
2017-01-13 22:39:03 +08:00
|
|
|
InsertedInstr = std::move(Inserted);
|
2016-08-26 01:37:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MachineIRBuilder::stopRecordingInsertions() {
|
|
|
|
InsertedInstr = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-12 01:27:58 +08:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Build instruction variants.
|
|
|
|
//------------------------------------------------------------------------------
|
2016-07-27 00:45:26 +08:00
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildInstr(unsigned Opcode) {
|
2016-09-22 21:49:25 +08:00
|
|
|
return insertInstr(buildInstrNoInsert(Opcode));
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstrBuilder MachineIRBuilder::buildInstrNoInsert(unsigned Opcode) {
|
2016-07-30 01:43:52 +08:00
|
|
|
MachineInstrBuilder MIB = BuildMI(getMF(), DL, getTII().get(Opcode));
|
2016-09-22 21:49:25 +08:00
|
|
|
return MIB;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MachineInstrBuilder MachineIRBuilder::insertInstr(MachineInstrBuilder MIB) {
|
2016-07-30 01:43:52 +08:00
|
|
|
getMBB().insert(getInsertPt(), MIB);
|
2016-08-26 01:37:32 +08:00
|
|
|
if (InsertedInstr)
|
|
|
|
InsertedInstr(MIB);
|
2016-07-30 01:43:52 +08:00
|
|
|
return MIB;
|
2016-02-12 02:53:28 +08:00
|
|
|
}
|
|
|
|
|
2017-01-27 07:39:14 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildDirectDbgValue(
|
|
|
|
unsigned Reg, const MDNode *Variable, const MDNode *Expr) {
|
|
|
|
assert(isa<DILocalVariable>(Variable) && "not a variable");
|
|
|
|
assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
|
|
|
|
assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
|
|
|
|
"Expected inlined-at fields to agree");
|
|
|
|
return buildInstr(TargetOpcode::DBG_VALUE)
|
|
|
|
.addReg(Reg, RegState::Debug)
|
|
|
|
.addReg(0, RegState::Debug)
|
|
|
|
.addMetadata(Variable)
|
|
|
|
.addMetadata(Expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstrBuilder MachineIRBuilder::buildIndirectDbgValue(
|
|
|
|
unsigned Reg, unsigned Offset, const MDNode *Variable, const MDNode *Expr) {
|
|
|
|
assert(isa<DILocalVariable>(Variable) && "not a variable");
|
|
|
|
assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
|
|
|
|
assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
|
|
|
|
"Expected inlined-at fields to agree");
|
|
|
|
return buildInstr(TargetOpcode::DBG_VALUE)
|
|
|
|
.addReg(Reg, RegState::Debug)
|
|
|
|
.addImm(Offset)
|
|
|
|
.addMetadata(Variable)
|
|
|
|
.addMetadata(Expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstrBuilder MachineIRBuilder::buildFIDbgValue(int FI,
|
|
|
|
const MDNode *Variable,
|
|
|
|
const MDNode *Expr) {
|
|
|
|
assert(isa<DILocalVariable>(Variable) && "not a variable");
|
|
|
|
assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
|
|
|
|
assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
|
|
|
|
"Expected inlined-at fields to agree");
|
|
|
|
return buildInstr(TargetOpcode::DBG_VALUE)
|
|
|
|
.addFrameIndex(FI)
|
|
|
|
.addImm(0)
|
|
|
|
.addMetadata(Variable)
|
|
|
|
.addMetadata(Expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstrBuilder MachineIRBuilder::buildConstDbgValue(const Constant &C,
|
|
|
|
unsigned Offset,
|
|
|
|
const MDNode *Variable,
|
|
|
|
const MDNode *Expr) {
|
|
|
|
assert(isa<DILocalVariable>(Variable) && "not a variable");
|
|
|
|
assert(cast<DIExpression>(Expr)->isValid() && "not an expression");
|
|
|
|
assert(cast<DILocalVariable>(Variable)->isValidLocationForIntrinsic(DL) &&
|
|
|
|
"Expected inlined-at fields to agree");
|
|
|
|
auto MIB = buildInstr(TargetOpcode::DBG_VALUE);
|
|
|
|
if (auto *CI = dyn_cast<ConstantInt>(&C)) {
|
|
|
|
if (CI->getBitWidth() > 64)
|
|
|
|
MIB.addCImm(CI);
|
|
|
|
else
|
|
|
|
MIB.addImm(CI->getZExtValue());
|
|
|
|
} else
|
|
|
|
MIB.addFPImm(&cast<ConstantFP>(C));
|
|
|
|
|
|
|
|
return MIB.addImm(Offset).addMetadata(Variable).addMetadata(Expr);
|
|
|
|
}
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildFrameIndex(unsigned Res, int Idx) {
|
2016-09-09 19:46:58 +08:00
|
|
|
assert(MRI->getType(Res).isPointer() && "invalid operand type");
|
2016-09-09 19:46:34 +08:00
|
|
|
return buildInstr(TargetOpcode::G_FRAME_INDEX)
|
2016-07-30 01:43:52 +08:00
|
|
|
.addDef(Res)
|
|
|
|
.addFrameIndex(Idx);
|
2016-07-23 00:59:52 +08:00
|
|
|
}
|
2016-07-23 04:03:43 +08:00
|
|
|
|
2016-09-12 20:10:41 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildGlobalValue(unsigned Res,
|
|
|
|
const GlobalValue *GV) {
|
|
|
|
assert(MRI->getType(Res).isPointer() && "invalid operand type");
|
|
|
|
assert(MRI->getType(Res).getAddressSpace() ==
|
|
|
|
GV->getType()->getAddressSpace() &&
|
|
|
|
"address space mismatch");
|
|
|
|
|
|
|
|
return buildInstr(TargetOpcode::G_GLOBAL_VALUE)
|
|
|
|
.addDef(Res)
|
|
|
|
.addGlobalAddress(GV);
|
|
|
|
}
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildAdd(unsigned Res, unsigned Op0,
|
|
|
|
unsigned Op1) {
|
2016-09-09 19:46:58 +08:00
|
|
|
assert((MRI->getType(Res).isScalar() || MRI->getType(Res).isVector()) &&
|
|
|
|
"invalid operand type");
|
|
|
|
assert(MRI->getType(Res) == MRI->getType(Op0) &&
|
|
|
|
MRI->getType(Res) == MRI->getType(Op1) && "type mismatch");
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
return buildInstr(TargetOpcode::G_ADD)
|
2016-07-30 01:43:52 +08:00
|
|
|
.addDef(Res)
|
|
|
|
.addUse(Op0)
|
|
|
|
.addUse(Op1);
|
2016-07-23 04:03:43 +08:00
|
|
|
}
|
|
|
|
|
2016-09-12 19:20:22 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildGEP(unsigned Res, unsigned Op0,
|
|
|
|
unsigned Op1) {
|
|
|
|
assert(MRI->getType(Res).isPointer() &&
|
|
|
|
MRI->getType(Res) == MRI->getType(Op0) && "type mismatch");
|
|
|
|
assert(MRI->getType(Op1).isScalar() && "invalid offset type");
|
|
|
|
|
|
|
|
return buildInstr(TargetOpcode::G_GEP)
|
|
|
|
.addDef(Res)
|
|
|
|
.addUse(Op0)
|
|
|
|
.addUse(Op1);
|
|
|
|
}
|
|
|
|
|
2017-02-15 04:56:18 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildPtrMask(unsigned Res, unsigned Op0,
|
|
|
|
uint32_t NumBits) {
|
|
|
|
assert(MRI->getType(Res).isPointer() &&
|
|
|
|
MRI->getType(Res) == MRI->getType(Op0) && "type mismatch");
|
|
|
|
|
|
|
|
return buildInstr(TargetOpcode::G_PTR_MASK)
|
|
|
|
.addDef(Res)
|
|
|
|
.addUse(Op0)
|
|
|
|
.addImm(NumBits);
|
|
|
|
}
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildSub(unsigned Res, unsigned Op0,
|
|
|
|
unsigned Op1) {
|
2016-09-09 19:46:58 +08:00
|
|
|
assert((MRI->getType(Res).isScalar() || MRI->getType(Res).isVector()) &&
|
|
|
|
"invalid operand type");
|
|
|
|
assert(MRI->getType(Res) == MRI->getType(Op0) &&
|
|
|
|
MRI->getType(Res) == MRI->getType(Op1) && "type mismatch");
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
return buildInstr(TargetOpcode::G_SUB)
|
2016-08-27 01:46:13 +08:00
|
|
|
.addDef(Res)
|
|
|
|
.addUse(Op0)
|
|
|
|
.addUse(Op1);
|
|
|
|
}
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildMul(unsigned Res, unsigned Op0,
|
|
|
|
unsigned Op1) {
|
2016-09-09 19:46:58 +08:00
|
|
|
assert((MRI->getType(Res).isScalar() || MRI->getType(Res).isVector()) &&
|
|
|
|
"invalid operand type");
|
|
|
|
assert(MRI->getType(Res) == MRI->getType(Op0) &&
|
|
|
|
MRI->getType(Res) == MRI->getType(Op1) && "type mismatch");
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
return buildInstr(TargetOpcode::G_MUL)
|
2016-08-27 01:46:13 +08:00
|
|
|
.addDef(Res)
|
|
|
|
.addUse(Op0)
|
|
|
|
.addUse(Op1);
|
|
|
|
}
|
|
|
|
|
2017-02-04 02:22:45 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildAnd(unsigned Res, unsigned Op0,
|
|
|
|
unsigned Op1) {
|
|
|
|
assert((MRI->getType(Res).isScalar() || MRI->getType(Res).isVector()) &&
|
|
|
|
"invalid operand type");
|
|
|
|
assert(MRI->getType(Res) == MRI->getType(Op0) &&
|
|
|
|
MRI->getType(Res) == MRI->getType(Op1) && "type mismatch");
|
|
|
|
|
|
|
|
return buildInstr(TargetOpcode::G_AND)
|
|
|
|
.addDef(Res)
|
|
|
|
.addUse(Op0)
|
|
|
|
.addUse(Op1);
|
|
|
|
}
|
|
|
|
|
2016-07-30 01:43:52 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildBr(MachineBasicBlock &Dest) {
|
2016-09-09 19:46:34 +08:00
|
|
|
return buildInstr(TargetOpcode::G_BR).addMBB(&Dest);
|
2016-07-27 00:45:26 +08:00
|
|
|
}
|
|
|
|
|
2017-01-30 17:13:18 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildBrIndirect(unsigned Tgt) {
|
|
|
|
return buildInstr(TargetOpcode::G_BRINDIRECT).addUse(Tgt);
|
|
|
|
}
|
|
|
|
|
2016-07-30 01:43:52 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildCopy(unsigned Res, unsigned Op) {
|
|
|
|
return buildInstr(TargetOpcode::COPY).addDef(Res).addUse(Op);
|
2016-07-27 00:45:30 +08:00
|
|
|
}
|
|
|
|
|
2016-12-06 05:47:07 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildConstant(unsigned Res,
|
|
|
|
const ConstantInt &Val) {
|
|
|
|
LLT Ty = MRI->getType(Res);
|
|
|
|
|
2016-12-06 18:14:36 +08:00
|
|
|
assert((Ty.isScalar() || Ty.isPointer()) && "invalid operand type");
|
2016-12-06 05:47:07 +08:00
|
|
|
|
|
|
|
const ConstantInt *NewVal = &Val;
|
|
|
|
if (Ty.getSizeInBits() != Val.getBitWidth())
|
|
|
|
NewVal = ConstantInt::get(MF->getFunction()->getContext(),
|
|
|
|
Val.getValue().sextOrTrunc(Ty.getSizeInBits()));
|
|
|
|
|
|
|
|
return buildInstr(TargetOpcode::G_CONSTANT).addDef(Res).addCImm(NewVal);
|
|
|
|
}
|
2016-09-09 19:46:58 +08:00
|
|
|
|
2016-12-06 05:47:07 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildConstant(unsigned Res,
|
|
|
|
int64_t Val) {
|
|
|
|
auto IntN = IntegerType::get(MF->getFunction()->getContext(),
|
|
|
|
MRI->getType(Res).getSizeInBits());
|
|
|
|
ConstantInt *CI = ConstantInt::get(IntN, Val, true);
|
|
|
|
return buildConstant(Res, *CI);
|
2016-08-05 04:54:13 +08:00
|
|
|
}
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildFConstant(unsigned Res,
|
|
|
|
const ConstantFP &Val) {
|
2016-09-09 19:46:58 +08:00
|
|
|
assert(MRI->getType(Res).isScalar() && "invalid operand type");
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
return buildInstr(TargetOpcode::G_FCONSTANT).addDef(Res).addFPImm(&Val);
|
2016-08-20 04:09:15 +08:00
|
|
|
}
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildBrCond(unsigned Tst,
|
2016-07-30 01:58:00 +08:00
|
|
|
MachineBasicBlock &Dest) {
|
2016-09-09 19:46:58 +08:00
|
|
|
assert(MRI->getType(Tst).isScalar() && "invalid operand type");
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
return buildInstr(TargetOpcode::G_BRCOND).addUse(Tst).addMBB(&Dest);
|
2016-07-30 01:58:00 +08:00
|
|
|
}
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildLoad(unsigned Res, unsigned Addr,
|
|
|
|
MachineMemOperand &MMO) {
|
2016-09-09 19:46:58 +08:00
|
|
|
assert(MRI->getType(Res).isValid() && "invalid operand type");
|
|
|
|
assert(MRI->getType(Addr).isPointer() && "invalid operand type");
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
return buildInstr(TargetOpcode::G_LOAD)
|
2016-07-30 01:43:52 +08:00
|
|
|
.addDef(Res)
|
|
|
|
.addUse(Addr)
|
|
|
|
.addMemOperand(&MMO);
|
2016-07-27 04:23:26 +08:00
|
|
|
}
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildStore(unsigned Val, unsigned Addr,
|
|
|
|
MachineMemOperand &MMO) {
|
2016-09-09 19:46:58 +08:00
|
|
|
assert(MRI->getType(Val).isValid() && "invalid operand type");
|
|
|
|
assert(MRI->getType(Addr).isPointer() && "invalid operand type");
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
return buildInstr(TargetOpcode::G_STORE)
|
2016-07-30 01:43:52 +08:00
|
|
|
.addUse(Val)
|
|
|
|
.addUse(Addr)
|
|
|
|
.addMemOperand(&MMO);
|
2016-07-27 04:23:26 +08:00
|
|
|
}
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildUAdde(unsigned Res,
|
|
|
|
unsigned CarryOut,
|
|
|
|
unsigned Op0, unsigned Op1,
|
|
|
|
unsigned CarryIn) {
|
2016-09-09 19:46:58 +08:00
|
|
|
assert(MRI->getType(Res).isScalar() && "invalid operand type");
|
|
|
|
assert(MRI->getType(Res) == MRI->getType(Op0) &&
|
|
|
|
MRI->getType(Res) == MRI->getType(Op1) && "type mismatch");
|
|
|
|
assert(MRI->getType(CarryOut).isScalar() && "invalid operand type");
|
|
|
|
assert(MRI->getType(CarryOut) == MRI->getType(CarryIn) && "type mismatch");
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
return buildInstr(TargetOpcode::G_UADDE)
|
2016-08-05 04:54:13 +08:00
|
|
|
.addDef(Res)
|
|
|
|
.addDef(CarryOut)
|
|
|
|
.addUse(Op0)
|
|
|
|
.addUse(Op1)
|
|
|
|
.addUse(CarryIn);
|
|
|
|
}
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildAnyExt(unsigned Res, unsigned Op) {
|
|
|
|
validateTruncExt(Res, Op, true);
|
|
|
|
return buildInstr(TargetOpcode::G_ANYEXT).addDef(Res).addUse(Op);
|
2016-08-05 02:35:11 +08:00
|
|
|
}
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildSExt(unsigned Res, unsigned Op) {
|
|
|
|
validateTruncExt(Res, Op, true);
|
|
|
|
return buildInstr(TargetOpcode::G_SEXT).addDef(Res).addUse(Op);
|
2016-08-24 05:01:26 +08:00
|
|
|
}
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildZExt(unsigned Res, unsigned Op) {
|
|
|
|
validateTruncExt(Res, Op, true);
|
|
|
|
return buildInstr(TargetOpcode::G_ZEXT).addDef(Res).addUse(Op);
|
2016-08-24 05:01:26 +08:00
|
|
|
}
|
|
|
|
|
2016-09-12 19:20:22 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildSExtOrTrunc(unsigned Res,
|
|
|
|
unsigned Op) {
|
|
|
|
unsigned Opcode = TargetOpcode::COPY;
|
|
|
|
if (MRI->getType(Res).getSizeInBits() > MRI->getType(Op).getSizeInBits())
|
|
|
|
Opcode = TargetOpcode::G_SEXT;
|
|
|
|
else if (MRI->getType(Res).getSizeInBits() < MRI->getType(Op).getSizeInBits())
|
|
|
|
Opcode = TargetOpcode::G_TRUNC;
|
|
|
|
|
|
|
|
return buildInstr(Opcode).addDef(Res).addUse(Op);
|
|
|
|
}
|
|
|
|
|
2017-02-04 02:22:45 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildZExtOrTrunc(unsigned Res,
|
|
|
|
unsigned Op) {
|
|
|
|
unsigned Opcode = TargetOpcode::COPY;
|
|
|
|
if (MRI->getType(Res).getSizeInBits() > MRI->getType(Op).getSizeInBits())
|
|
|
|
Opcode = TargetOpcode::G_ZEXT;
|
|
|
|
else if (MRI->getType(Res).getSizeInBits() < MRI->getType(Op).getSizeInBits())
|
|
|
|
Opcode = TargetOpcode::G_TRUNC;
|
|
|
|
|
|
|
|
return buildInstr(Opcode).addDef(Res).addUse(Op);
|
|
|
|
}
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildExtract(ArrayRef<unsigned> Results,
|
2016-08-20 02:32:14 +08:00
|
|
|
ArrayRef<uint64_t> Indices,
|
2016-09-09 19:46:34 +08:00
|
|
|
unsigned Src) {
|
2016-09-09 19:46:58 +08:00
|
|
|
#ifndef NDEBUG
|
2016-09-09 19:46:34 +08:00
|
|
|
assert(Results.size() == Indices.size() && "inconsistent number of regs");
|
2016-08-20 02:32:14 +08:00
|
|
|
assert(!Results.empty() && "invalid trivial extract");
|
2016-08-31 04:51:25 +08:00
|
|
|
assert(std::is_sorted(Indices.begin(), Indices.end()) &&
|
|
|
|
"extract offsets must be in ascending order");
|
2016-08-20 02:32:14 +08:00
|
|
|
|
2016-09-09 19:46:58 +08:00
|
|
|
assert(MRI->getType(Src).isValid() && "invalid operand type");
|
|
|
|
for (auto Res : Results)
|
|
|
|
assert(MRI->getType(Res).isValid() && "invalid operand type");
|
|
|
|
#endif
|
|
|
|
|
2016-08-20 02:32:14 +08:00
|
|
|
auto MIB = BuildMI(getMF(), DL, getTII().get(TargetOpcode::G_EXTRACT));
|
2016-07-23 04:03:43 +08:00
|
|
|
for (auto Res : Results)
|
2016-07-30 01:43:52 +08:00
|
|
|
MIB.addDef(Res);
|
2016-07-23 04:03:43 +08:00
|
|
|
|
2016-07-30 01:43:52 +08:00
|
|
|
MIB.addUse(Src);
|
2016-07-23 04:03:43 +08:00
|
|
|
|
2016-08-20 02:32:14 +08:00
|
|
|
for (auto Idx : Indices)
|
2016-07-23 04:03:43 +08:00
|
|
|
MIB.addImm(Idx);
|
2016-08-20 02:32:14 +08:00
|
|
|
|
|
|
|
getMBB().insert(getInsertPt(), MIB);
|
2016-08-26 01:37:32 +08:00
|
|
|
if (InsertedInstr)
|
|
|
|
InsertedInstr(MIB);
|
2016-08-20 02:32:14 +08:00
|
|
|
|
2016-07-30 01:43:52 +08:00
|
|
|
return MIB;
|
2016-07-23 04:03:43 +08:00
|
|
|
}
|
|
|
|
|
2016-08-20 01:17:06 +08:00
|
|
|
MachineInstrBuilder
|
2016-09-09 19:46:34 +08:00
|
|
|
MachineIRBuilder::buildSequence(unsigned Res,
|
2016-08-20 01:17:06 +08:00
|
|
|
ArrayRef<unsigned> Ops,
|
2016-09-20 23:20:36 +08:00
|
|
|
ArrayRef<uint64_t> Indices) {
|
2016-09-09 19:46:58 +08:00
|
|
|
#ifndef NDEBUG
|
2016-09-09 19:46:34 +08:00
|
|
|
assert(Ops.size() == Indices.size() && "incompatible args");
|
2016-08-20 02:32:14 +08:00
|
|
|
assert(!Ops.empty() && "invalid trivial sequence");
|
2016-08-31 04:51:25 +08:00
|
|
|
assert(std::is_sorted(Indices.begin(), Indices.end()) &&
|
|
|
|
"sequence offsets must be in ascending order");
|
2016-08-20 01:17:06 +08:00
|
|
|
|
2016-09-09 19:46:58 +08:00
|
|
|
assert(MRI->getType(Res).isValid() && "invalid operand type");
|
|
|
|
for (auto Op : Ops)
|
|
|
|
assert(MRI->getType(Op).isValid() && "invalid operand type");
|
|
|
|
#endif
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
MachineInstrBuilder MIB = buildInstr(TargetOpcode::G_SEQUENCE);
|
2016-07-30 01:43:52 +08:00
|
|
|
MIB.addDef(Res);
|
2016-08-20 01:17:06 +08:00
|
|
|
for (unsigned i = 0; i < Ops.size(); ++i) {
|
|
|
|
MIB.addUse(Ops[i]);
|
2016-08-20 02:32:14 +08:00
|
|
|
MIB.addImm(Indices[i]);
|
2016-08-20 01:17:06 +08:00
|
|
|
}
|
2016-07-30 01:43:52 +08:00
|
|
|
return MIB;
|
2017-03-04 06:46:09 +08:00
|
|
|
}
|
|
|
|
|
2017-03-07 02:36:40 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildUndef(unsigned Res) {
|
|
|
|
return buildInstr(TargetOpcode::IMPLICIT_DEF).addDef(Res);
|
|
|
|
}
|
|
|
|
|
2017-03-04 06:46:09 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildMerge(unsigned Res,
|
|
|
|
ArrayRef<unsigned> Ops) {
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
assert(!Ops.empty() && "invalid trivial sequence");
|
|
|
|
LLT Ty = MRI->getType(Ops[0]);
|
|
|
|
for (auto Reg : Ops)
|
|
|
|
assert(MRI->getType(Reg) == Ty && "type mismatch in input list");
|
|
|
|
assert(Ops.size() * MRI->getType(Ops[0]).getSizeInBits() ==
|
|
|
|
MRI->getType(Res).getSizeInBits() &&
|
|
|
|
"input operands do not cover output register");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
MachineInstrBuilder MIB = buildInstr(TargetOpcode::G_MERGE_VALUES);
|
|
|
|
MIB.addDef(Res);
|
|
|
|
for (unsigned i = 0; i < Ops.size(); ++i)
|
|
|
|
MIB.addUse(Ops[i]);
|
|
|
|
return MIB;
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstrBuilder MachineIRBuilder::buildUnmerge(ArrayRef<unsigned> Res,
|
|
|
|
unsigned Op) {
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
assert(!Res.empty() && "invalid trivial sequence");
|
|
|
|
LLT Ty = MRI->getType(Res[0]);
|
|
|
|
for (auto Reg : Res)
|
|
|
|
assert(MRI->getType(Reg) == Ty && "type mismatch in input list");
|
|
|
|
assert(Res.size() * MRI->getType(Res[0]).getSizeInBits() ==
|
|
|
|
MRI->getType(Op).getSizeInBits() &&
|
|
|
|
"input operands do not cover output register");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
MachineInstrBuilder MIB = buildInstr(TargetOpcode::G_UNMERGE_VALUES);
|
|
|
|
for (unsigned i = 0; i < Res.size(); ++i)
|
|
|
|
MIB.addDef(Res[i]);
|
|
|
|
MIB.addUse(Op);
|
|
|
|
return MIB;
|
2016-07-23 04:03:43 +08:00
|
|
|
}
|
2016-07-30 06:32:36 +08:00
|
|
|
|
2017-03-04 07:05:47 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildInsert(unsigned Res, unsigned Src,
|
|
|
|
unsigned Op, unsigned Index) {
|
|
|
|
return buildInstr(TargetOpcode::G_INSERT)
|
|
|
|
.addDef(Res)
|
|
|
|
.addUse(Src)
|
|
|
|
.addUse(Op)
|
|
|
|
.addImm(Index);
|
|
|
|
}
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildIntrinsic(Intrinsic::ID ID,
|
2016-07-30 06:32:36 +08:00
|
|
|
unsigned Res,
|
|
|
|
bool HasSideEffects) {
|
|
|
|
auto MIB =
|
|
|
|
buildInstr(HasSideEffects ? TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS
|
2016-09-09 19:46:34 +08:00
|
|
|
: TargetOpcode::G_INTRINSIC);
|
2016-07-30 06:32:36 +08:00
|
|
|
if (Res)
|
|
|
|
MIB.addDef(Res);
|
|
|
|
MIB.addIntrinsicID(ID);
|
|
|
|
return MIB;
|
|
|
|
}
|
2016-08-05 02:35:11 +08:00
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildTrunc(unsigned Res, unsigned Op) {
|
|
|
|
validateTruncExt(Res, Op, false);
|
|
|
|
return buildInstr(TargetOpcode::G_TRUNC).addDef(Res).addUse(Op);
|
2016-08-05 02:35:11 +08:00
|
|
|
}
|
2016-08-18 04:25:25 +08:00
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildFPTrunc(unsigned Res, unsigned Op) {
|
|
|
|
validateTruncExt(Res, Op, false);
|
|
|
|
return buildInstr(TargetOpcode::G_FPTRUNC).addDef(Res).addUse(Op);
|
2016-08-20 06:40:08 +08:00
|
|
|
}
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildICmp(CmpInst::Predicate Pred,
|
2016-08-18 04:25:25 +08:00
|
|
|
unsigned Res, unsigned Op0,
|
|
|
|
unsigned Op1) {
|
2016-09-09 19:46:58 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
assert(MRI->getType(Op0) == MRI->getType(Op0) && "type mismatch");
|
|
|
|
assert(CmpInst::isIntPredicate(Pred) && "invalid predicate");
|
2016-09-15 18:40:38 +08:00
|
|
|
if (MRI->getType(Op0).isScalar() || MRI->getType(Op0).isPointer())
|
2016-09-09 19:46:58 +08:00
|
|
|
assert(MRI->getType(Res).isScalar() && "type mismatch");
|
|
|
|
else
|
|
|
|
assert(MRI->getType(Res).isVector() &&
|
|
|
|
MRI->getType(Res).getNumElements() ==
|
|
|
|
MRI->getType(Op0).getNumElements() &&
|
|
|
|
"type mismatch");
|
|
|
|
#endif
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
return buildInstr(TargetOpcode::G_ICMP)
|
2016-08-18 04:25:25 +08:00
|
|
|
.addDef(Res)
|
|
|
|
.addPredicate(Pred)
|
|
|
|
.addUse(Op0)
|
|
|
|
.addUse(Op1);
|
|
|
|
}
|
2016-08-20 04:09:07 +08:00
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildFCmp(CmpInst::Predicate Pred,
|
2016-08-20 04:48:16 +08:00
|
|
|
unsigned Res, unsigned Op0,
|
|
|
|
unsigned Op1) {
|
2016-09-09 19:46:58 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
assert((MRI->getType(Op0).isScalar() || MRI->getType(Op0).isVector()) &&
|
|
|
|
"invalid operand type");
|
|
|
|
assert(MRI->getType(Op0) == MRI->getType(Op1) && "type mismatch");
|
|
|
|
assert(CmpInst::isFPPredicate(Pred) && "invalid predicate");
|
|
|
|
if (MRI->getType(Op0).isScalar())
|
|
|
|
assert(MRI->getType(Res).isScalar() && "type mismatch");
|
|
|
|
else
|
|
|
|
assert(MRI->getType(Res).isVector() &&
|
|
|
|
MRI->getType(Res).getNumElements() ==
|
|
|
|
MRI->getType(Op0).getNumElements() &&
|
|
|
|
"type mismatch");
|
|
|
|
#endif
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
return buildInstr(TargetOpcode::G_FCMP)
|
2016-08-20 04:48:16 +08:00
|
|
|
.addDef(Res)
|
|
|
|
.addPredicate(Pred)
|
|
|
|
.addUse(Op0)
|
|
|
|
.addUse(Op1);
|
|
|
|
}
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
MachineInstrBuilder MachineIRBuilder::buildSelect(unsigned Res, unsigned Tst,
|
2016-08-20 04:09:07 +08:00
|
|
|
unsigned Op0, unsigned Op1) {
|
2016-09-09 19:46:58 +08:00
|
|
|
#ifndef NDEBUG
|
2016-12-07 02:38:34 +08:00
|
|
|
LLT ResTy = MRI->getType(Res);
|
|
|
|
assert((ResTy.isScalar() || ResTy.isVector() || ResTy.isPointer()) &&
|
2016-09-09 19:46:58 +08:00
|
|
|
"invalid operand type");
|
2016-12-07 02:38:34 +08:00
|
|
|
assert(ResTy == MRI->getType(Op0) && ResTy == MRI->getType(Op1) &&
|
|
|
|
"type mismatch");
|
|
|
|
if (ResTy.isScalar() || ResTy.isPointer())
|
2016-09-09 19:46:58 +08:00
|
|
|
assert(MRI->getType(Tst).isScalar() && "type mismatch");
|
|
|
|
else
|
|
|
|
assert(MRI->getType(Tst).isVector() &&
|
|
|
|
MRI->getType(Tst).getNumElements() ==
|
|
|
|
MRI->getType(Op0).getNumElements() &&
|
|
|
|
"type mismatch");
|
|
|
|
#endif
|
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
return buildInstr(TargetOpcode::G_SELECT)
|
2016-08-20 04:09:07 +08:00
|
|
|
.addDef(Res)
|
|
|
|
.addUse(Tst)
|
|
|
|
.addUse(Op0)
|
|
|
|
.addUse(Op1);
|
|
|
|
}
|
2016-08-24 05:01:33 +08:00
|
|
|
|
2016-09-09 19:46:34 +08:00
|
|
|
void MachineIRBuilder::validateTruncExt(unsigned Dst, unsigned Src,
|
|
|
|
bool IsExtend) {
|
2016-08-24 06:14:15 +08:00
|
|
|
#ifndef NDEBUG
|
2016-09-09 19:46:34 +08:00
|
|
|
LLT SrcTy = MRI->getType(Src);
|
|
|
|
LLT DstTy = MRI->getType(Dst);
|
2016-08-24 05:01:33 +08:00
|
|
|
|
|
|
|
if (DstTy.isVector()) {
|
|
|
|
assert(SrcTy.isVector() && "mismatched cast between vecot and non-vector");
|
|
|
|
assert(SrcTy.getNumElements() == DstTy.getNumElements() &&
|
|
|
|
"different number of elements in a trunc/ext");
|
|
|
|
} else
|
|
|
|
assert(DstTy.isScalar() && SrcTy.isScalar() && "invalid extend/trunc");
|
|
|
|
|
|
|
|
if (IsExtend)
|
|
|
|
assert(DstTy.getSizeInBits() > SrcTy.getSizeInBits() &&
|
|
|
|
"invalid narrowing extend");
|
|
|
|
else
|
|
|
|
assert(DstTy.getSizeInBits() < SrcTy.getSizeInBits() &&
|
|
|
|
"invalid widening trunc");
|
2016-08-24 06:14:15 +08:00
|
|
|
#endif
|
2016-08-24 05:01:33 +08:00
|
|
|
}
|