| 1 | //===- SandboxVectorizerIR.cpp - Sandbox IR Specialization ----------------===// |
| 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 | |
| 9 | #include "llvm/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizerIR.h" |
| 10 | #include "llvm/SandboxIR/Type.h" |
| 11 | #include "llvm/Transforms/Vectorize/SandboxVectorizer/VecUtils.h" |
| 12 | |
| 13 | namespace llvm::sandboxir { |
| 14 | |
| 15 | Value *PackInst::create(ArrayRef<Value *> PackOps, InsertPosition InsertBefore, |
| 16 | SBVecContext &Ctx) { |
| 17 | auto &Builder = Instruction::setInsertPos(InsertBefore); |
| 18 | // TODO: Replace with actual instruction sequence! |
| 19 | auto *C = ConstantInt::get(Ty: Type::getInt32Ty(Ctx), V: 0); |
| 20 | auto *LLVMC = cast<llvm::Constant>(Val: C->Val); |
| 21 | auto *LLVMVecTy = llvm::FixedVectorType::get(ElementType: PackOps[0]->Val->getType(), NumElts: 2); |
| 22 | auto *LLVMPoison = llvm::PoisonValue::get(T: LLVMVecTy); |
| 23 | llvm::Value *NewV = |
| 24 | Builder.CreateInsertElement(Vec: LLVMPoison, NewElt: PackOps[0]->Val, Idx: LLVMC, Name: "TEST" ); |
| 25 | |
| 26 | if (auto *NewInsert = dyn_cast<llvm::InsertElementInst>(Val: NewV)) |
| 27 | return Ctx.createPackInst(PackInstrs: {NewInsert}); |
| 28 | assert(isa<llvm::Constant>(NewV) && "Expected constant" ); |
| 29 | return Ctx.getOrCreateConstant(LLVMC: cast<llvm::Constant>(Val: NewV)); |
| 30 | } |
| 31 | |
| 32 | bool PackInst::classof(const Value *From) { |
| 33 | return From->getSubclassID() == ClassID::Pack; |
| 34 | } |
| 35 | |
| 36 | PackInst * |
| 37 | SBVecContext::createPackInst(ArrayRef<llvm::Instruction *> PackInstrs) { |
| 38 | assert(all_of(PackInstrs, |
| 39 | [](llvm::Value *V) { |
| 40 | return isa<llvm::InsertElementInst>(V) || |
| 41 | isa<llvm::ExtractElementInst>(V); |
| 42 | }) && |
| 43 | "Expected inserts or extracts!" ); |
| 44 | auto NewPtr = std::unique_ptr<PackInst>(new PackInst(PackInstrs, *this)); |
| 45 | return cast<PackInst>(Val: registerValue(VPtr: std::move(NewPtr))); |
| 46 | } |
| 47 | |
| 48 | } // namespace llvm::sandboxir |
| 49 | |