2
0

VectorUtils2.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //===----------- VectorUtils2.cpp - findScalarElement function -----------===//
  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. //
  10. // This file defines vectorizer utility function findScalarElement.
  11. // Splitting this function from VectorUtils.cpp into a separate file
  12. // makes dxilconv.dll 121kB smaller (x86 release, compiler optimization for size).
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/Analysis/VectorUtils.h"
  16. #include "llvm/IR/PatternMatch.h"
  17. #include "llvm/IR/Value.h"
  18. /// \brief Given a vector and an element number, see if the scalar value is
  19. /// already around as a register, for example if it were inserted then extracted
  20. /// from the vector.
  21. llvm::Value *llvm::findScalarElement(llvm::Value *V, unsigned EltNo) {
  22. assert(V->getType()->isVectorTy() && "Not looking at a vector?");
  23. VectorType *VTy = cast<VectorType>(V->getType());
  24. unsigned Width = VTy->getNumElements();
  25. if (EltNo >= Width) // Out of range access.
  26. return UndefValue::get(VTy->getElementType());
  27. if (Constant *C = dyn_cast<Constant>(V))
  28. return C->getAggregateElement(EltNo);
  29. if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) {
  30. // If this is an insert to a variable element, we don't know what it is.
  31. if (!isa<ConstantInt>(III->getOperand(2)))
  32. return nullptr;
  33. unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue();
  34. // If this is an insert to the element we are looking for, return the
  35. // inserted value.
  36. if (EltNo == IIElt)
  37. return III->getOperand(1);
  38. // Otherwise, the insertelement doesn't modify the value, recurse on its
  39. // vector input.
  40. return findScalarElement(III->getOperand(0), EltNo);
  41. }
  42. if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) {
  43. unsigned LHSWidth = SVI->getOperand(0)->getType()->getVectorNumElements();
  44. int InEl = SVI->getMaskValue(EltNo);
  45. if (InEl < 0)
  46. return UndefValue::get(VTy->getElementType());
  47. if (InEl < (int)LHSWidth)
  48. return findScalarElement(SVI->getOperand(0), InEl);
  49. return findScalarElement(SVI->getOperand(1), InEl - LHSWidth);
  50. }
  51. // Extract a value from a vector add operation with a constant zero.
  52. Value *Val = nullptr; Constant *Con = nullptr;
  53. if (match(V,
  54. llvm::PatternMatch::m_Add(llvm::PatternMatch::m_Value(Val),
  55. llvm::PatternMatch::m_Constant(Con)))) {
  56. if (Constant *Elt = Con->getAggregateElement(EltNo))
  57. if (Elt->isNullValue())
  58. return findScalarElement(Val, EltNo);
  59. }
  60. // Otherwise, we don't know.
  61. return nullptr;
  62. }