1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2021 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
//
// Utility break Constant functions for the GenX backend.
//
//===----------------------------------------------------------------------===//
#include "vc/Utils/GenX/BreakConst.h"
#include <llvm/ADT/PostOrderIterator.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/Dominators.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/IntrinsicInst.h>
#include <llvm/IR/Intrinsics.h>
#include <llvm/IR/Metadata.h>
#include <llvm/IR/Module.h>
#include <llvm/Support/Casting.h>
#include "Probe/Assertion.h"
#include "llvmWrapper/IR/DerivedTypes.h"
#include "llvmWrapper/IR/InstrTypes.h"
#include "llvmWrapper/IR/Instructions.h"
#include "vc/Utils/GenX/Region.h"
#include "vc/Utils/GenX/TypeSize.h"
#include <cstddef>
#include <iterator>
using namespace llvm;
using namespace vc;
static Value *undefVector(Type *ScalarType, unsigned ElNum) {
IGC_ASSERT(!ScalarType->isVectorTy());
return UndefValue::get(IGCLLVM::FixedVectorType::get(ScalarType, ElNum));
}
static Value *buildNonLegalizedConstantVector(ArrayRef<Value *> Vals,
Instruction *InsertPt,
const DebugLoc &DbgLoc) {
IGC_ASSERT(!Vals.empty());
Value *Result = undefVector(Vals.front()->getType(), Vals.size());
IRBuilder<> Builder(InsertPt);
Builder.SetCurrentDebugLocation(DbgLoc);
for (unsigned j = 0, N = Vals.size(); j < N; ++j)
Result = Builder.CreateInsertElement(Result, Vals[j], j);
return Result;
}
static Value *buildLegalizedConstantVector(ArrayRef<Value *> Vals,
Instruction *InsertPt,
const DebugLoc &DbgLoc) {
IGC_ASSERT(!Vals.empty());
const auto& DL = InsertPt->getModule()->getDataLayout();
Type *ScalarType = Vals.front()->getType();
Value *Result = undefVector(ScalarType, Vals.size());
IntegerType *I16Ty = Type::getInt16Ty(InsertPt->getContext());
unsigned ElBytes = vc::getTypeSize(ScalarType, &DL).inBytes();
for (unsigned j = 0, N = Vals.size(); j < N; ++j) {
CMRegion R(Vals[j], &DL);
R.Indirect = ConstantInt::get(I16Ty, ElBytes * j);
Result = R.createWrRegion(Result, Vals[j], ".vconst", InsertPt, DbgLoc);
}
return Result;
}
Value *vc::breakConstantVector(ConstantVector *CV, Instruction *CurInst,
Instruction *InsertPt,
vc::LegalizationStage LegalizationStage) {
IGC_ASSERT(CurInst);
IGC_ASSERT(InsertPt);
if (!CV)
return nullptr;
const auto &DbgLoc = CurInst->getDebugLoc();
// Splat case.
if (auto *S = dyn_cast_or_null<ConstantExpr>(CV->getSplatValue())) {
// Turn element into an instruction
auto Inst = S->getAsInstruction();
Inst->setDebugLoc(DbgLoc);
Inst->insertBefore(InsertPt);
Value *Result = nullptr;
// Splat the value.
if (LegalizationStage == vc::LegalizationStage::Legalized) {
const auto &DL = InsertPt->getModule()->getDataLayout();
Result = CMRegion::createRdVectorSplat(DL, CV->getNumOperands(), Inst,
".splat", InsertPt, DbgLoc);
} else {
IRBuilder<> Builder(InsertPt);
Builder.SetCurrentDebugLocation(DbgLoc);
Result = Builder.CreateVectorSplat(CV->getNumOperands(), Inst);
}
// Update i-th operand with newly created splat.
CurInst->replaceUsesOfWith(CV, Result);
return Result;
}
SmallVector<Value *, 8> Vals;
bool HasConstExpr = false;
for (unsigned j = 0, N = CV->getNumOperands(); j < N; ++j) {
Value *Elt = CV->getOperand(j);
if (auto CE = dyn_cast<ConstantExpr>(Elt)) {
auto Inst = CE->getAsInstruction();
Inst->setDebugLoc(DbgLoc);
Inst->insertBefore(InsertPt);
Vals.push_back(Inst);
HasConstExpr = true;
} else
Vals.push_back(Elt);
}
if (!HasConstExpr)
return nullptr;
Value *Val = (LegalizationStage == vc::LegalizationStage::Legalized)
? buildLegalizedConstantVector(Vals, InsertPt, DbgLoc)
: buildNonLegalizedConstantVector(Vals, InsertPt, DbgLoc);
CurInst->replaceUsesOfWith(CV, Val);
return Val;
}
bool vc::breakConstantExprs(Instruction *I,
vc::LegalizationStage LegalizationStage) {
if (!I)
return false;
bool Modified = false;
std::list<Instruction *> Worklist = {I};
while (!Worklist.empty()) {
auto *CurInst = Worklist.front();
Worklist.pop_front();
PHINode *PN = dyn_cast<PHINode>(CurInst);
for (unsigned i = 0, e = CurInst->getNumOperands(); i < e; ++i) {
auto *InsertPt = PN ? PN->getIncomingBlock(i)->getTerminator() : CurInst;
Value *Op = CurInst->getOperand(i);
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) {
Instruction *NewInst = CE->getAsInstruction();
NewInst->setDebugLoc(CurInst->getDebugLoc());
NewInst->insertBefore(CurInst);
CurInst->setOperand(i, NewInst);
Worklist.push_back(NewInst);
Modified = true;
} else if (auto *CV = dyn_cast<ConstantVector>(Op)) {
if (auto *CVInst =
breakConstantVector(CV, CurInst, InsertPt, LegalizationStage)) {
Worklist.push_back(cast<Instruction>(CVInst));
Modified = true;
}
}
}
}
return Modified;
}
bool vc::breakConstantExprs(Function *F,
vc::LegalizationStage LegalizationStage) {
bool Modified = false;
for (po_iterator<BasicBlock *> i = po_begin(&F->getEntryBlock()),
e = po_end(&F->getEntryBlock());
i != e; ++i) {
BasicBlock *BB = *i;
// The effect of this loop is that we process the instructions in reverse
// order, and we re-process anything inserted before the instruction
// being processed.
for (Instruction *CurInst = BB->getTerminator(); CurInst;) {
Modified |= breakConstantExprs(CurInst, LegalizationStage);
CurInst = CurInst == &BB->front() ? nullptr : CurInst->getPrevNode();
}
}
return Modified;
}
|