Commit 7c5a207b authored by Wei Pan's avatar Wei Pan Committed by gbsbuild

Legalize certain bitcast instructions

Change-Id: I267c8df4bd8dcbb1518af3051a9090f6dcd73e4a
parent c866bf7e
......@@ -379,6 +379,44 @@ bool InstPromoter::visitZExtInst(ZExtInst &I) {
return true;
}
bool InstPromoter::visitBitCastInst(BitCastInst &I) {
ValueSeq *ValSeq;
LegalizeAction ValAct;
std::tie(ValSeq, ValAct) = TL->getLegalizedValues(I.getOperand(0));
Value *Val = I.getOperand(0);
if (ValAct != Legal && ValSeq != nullptr)
Val = ValSeq->front();
TypeSeq *TySeq;
LegalizeAction Act;
std::tie(TySeq, Act) = TL->getLegalizedTypes(I.getDestTy());
assert(TySeq->size() == 1);
// Promote bitcast <3 x i8> %0 to i24
// %1 = shufflevector <3 x i8> %0, <3 x i8> zeroinitializer, <i32 0, i32
// 1, i32 2, i32 3> %2 = bitcast <4 x i8> %1 to i32 bail out for other
// cases for now.
if (Act == Promote && Val->getType()->isVectorTy() &&
I.getType()->isIntegerTy()) {
Type *PromotedTy = TySeq->front();
unsigned N = PromotedTy->getScalarSizeInBits() /
Val->getType()->getScalarSizeInBits();
std::vector<Constant *> Vals;
for (unsigned i = 0; i < N; i++)
Vals.push_back(IRB->getInt32(i));
Value *Mask = ConstantVector::get(Vals);
Value *Zero = Constant::getNullValue(Val->getType());
Value *NewVal = IRB->CreateShuffleVector(Val, Zero, Mask,
Twine(I.getName(), getSuffix()));
Promoted = IRB->CreateBitCast(NewVal, PromotedTy);
return true;
}
// Unsupported legalization action.
return false;
}
/// Other operators
bool InstPromoter::visitGenIntrinsicInst(GenIntrinsicInst &I) {
......
......@@ -82,6 +82,7 @@ namespace Legalizer {
bool visitTruncInst(TruncInst &I);
bool visitZExtInst(ZExtInst &I);
bool visitBitCastInst(BitCastInst &I);
/// Other operators
......
......@@ -143,7 +143,7 @@ TypeLegalizer::getTypeLegalizeAction(Type *Ty) const {
// Vector type!
if (Ty->isVectorTy())
return Scalarize;
return getTypeLegalizeAction(Ty->getVectorElementType());
// Floating point types always need native support; otherwise, they needs
// software emulation.
......@@ -334,19 +334,21 @@ TypeLegalizer::getLegalizedValues(Value *V, bool isSigned) {
if (Act == Legal)
return std::make_pair(nullptr, Legal);
if (isa<Constant>(V))
ValueMap.erase(V);
auto C = dyn_cast<Constant>(V);
if (C != nullptr)
ValueMap.erase(V);
ValueMapTy::iterator VMI; bool New;
std::tie(VMI, New) = ValueMap.insert(std::make_pair(V, ValueSeq()));
if (!New)
return std::make_pair(&VMI->second, Act);
return std::make_pair(&VMI->second, Act);
if (!C)
return std::make_pair(nullptr, Act);
// We visit instructions in topological order and we handle phis and
// arguments specially, so we shouldn't see a use before a def here except
// constants.
Constant *C = cast<Constant>(V);
TypeSeq *TySeq;
std::tie(TySeq, Act) = getLegalizedTypes(C->getType());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment