LLVM  4.0.0
SITypeRewriter.cpp
Go to the documentation of this file.
1 //===-- SITypeRewriter.cpp - Remove unwanted types ------------------------===//
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 /// \file
11 /// This pass removes performs the following type substitution on all
12 /// non-compute shaders:
13 ///
14 /// v16i8 => i128
15 /// - v16i8 is used for constant memory resource descriptors. This type is
16 /// legal for some compute APIs, and we don't want to declare it as legal
17 /// in the backend, because we want the legalizer to expand all v16i8
18 /// operations.
19 /// v1* => *
20 /// - Having v1* types complicates the legalizer and we can easily replace
21 /// - them with the element type.
22 //===----------------------------------------------------------------------===//
23 
24 #include "AMDGPU.h"
25 #include "Utils/AMDGPUBaseInfo.h"
26 #include "llvm/IR/IRBuilder.h"
27 #include "llvm/IR/InstVisitor.h"
28 
29 using namespace llvm;
30 
31 namespace {
32 
33 class SITypeRewriter : public FunctionPass,
34  public InstVisitor<SITypeRewriter> {
35 
36  static char ID;
37  Module *Mod;
38  Type *v16i8;
39  Type *v4i32;
40 
41 public:
42  SITypeRewriter() : FunctionPass(ID) { }
43  bool doInitialization(Module &M) override;
44  bool runOnFunction(Function &F) override;
45  StringRef getPassName() const override { return "SI Type Rewriter"; }
46  void visitLoadInst(LoadInst &I);
47  void visitCallInst(CallInst &I);
48  void visitBitCast(BitCastInst &I);
49 };
50 
51 } // End anonymous namespace
52 
53 char SITypeRewriter::ID = 0;
54 
55 bool SITypeRewriter::doInitialization(Module &M) {
56  Mod = &M;
57  v16i8 = VectorType::get(Type::getInt8Ty(M.getContext()), 16);
59  return false;
60 }
61 
62 bool SITypeRewriter::runOnFunction(Function &F) {
64  return false;
65 
66  visit(F);
67  visit(F);
68 
69  return false;
70 }
71 
72 void SITypeRewriter::visitLoadInst(LoadInst &I) {
74  Type *PtrTy = Ptr->getType();
75  Type *ElemTy = PtrTy->getPointerElementType();
76  IRBuilder<> Builder(&I);
77  if (ElemTy == v16i8) {
78  Value *BitCast = Builder.CreateBitCast(Ptr,
80  LoadInst *Load = Builder.CreateLoad(BitCast);
83  for (unsigned i = 0, e = MD.size(); i != e; ++i) {
84  Load->setMetadata(MD[i].first, MD[i].second);
85  }
86  Value *BitCastLoad = Builder.CreateBitCast(Load, I.getType());
87  I.replaceAllUsesWith(BitCastLoad);
88  I.eraseFromParent();
89  }
90 }
91 
92 void SITypeRewriter::visitCallInst(CallInst &I) {
93  IRBuilder<> Builder(&I);
94 
97  bool NeedToReplace = false;
98  Function *F = I.getCalledFunction();
99  if (!F)
100  return;
101 
102  std::string Name = F->getName();
103  for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
104  Value *Arg = I.getArgOperand(i);
105  if (Arg->getType() == v16i8) {
106  Args.push_back(Builder.CreateBitCast(Arg, v4i32));
107  Types.push_back(v4i32);
108  NeedToReplace = true;
109  Name = Name + ".v4i32";
110  } else if (Arg->getType()->isVectorTy() &&
111  Arg->getType()->getVectorNumElements() == 1 &&
112  Arg->getType()->getVectorElementType() ==
114  Type *ElementTy = Arg->getType()->getVectorElementType();
115  std::string TypeName = "i32";
116  InsertElementInst *Def = cast<InsertElementInst>(Arg);
117  Args.push_back(Def->getOperand(1));
118  Types.push_back(ElementTy);
119  std::string VecTypeName = "v1" + TypeName;
120  Name = Name.replace(Name.find(VecTypeName), VecTypeName.length(), TypeName);
121  NeedToReplace = true;
122  } else {
123  Args.push_back(Arg);
124  Types.push_back(Arg->getType());
125  }
126  }
127 
128  if (!NeedToReplace) {
129  return;
130  }
131  Function *NewF = Mod->getFunction(Name);
132  if (!NewF) {
133  NewF = Function::Create(FunctionType::get(F->getReturnType(), Types, false), GlobalValue::ExternalLinkage, Name, Mod);
134  NewF->setAttributes(F->getAttributes());
135  }
136  I.replaceAllUsesWith(Builder.CreateCall(NewF, Args));
137  I.eraseFromParent();
138 }
139 
140 void SITypeRewriter::visitBitCast(BitCastInst &I) {
141  IRBuilder<> Builder(&I);
142  if (I.getDestTy() != v4i32) {
143  return;
144  }
145 
146  if (BitCastInst *Op = dyn_cast<BitCastInst>(I.getOperand(0))) {
147  if (Op->getSrcTy() == v4i32) {
148  I.replaceAllUsesWith(Op->getOperand(0));
149  I.eraseFromParent();
150  }
151  }
152 }
153 
155  return new SITypeRewriter();
156 }
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:76
void push_back(const T &Elt)
Definition: SmallVector.h:211
Base class for instruction visitors.
Definition: InstVisitor.h:81
size_t i
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
This class represents a function call, abstracting a target machine's calling convention.
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space...
Definition: Type.cpp:655
Externally visible function.
Definition: GlobalValue.h:49
Type * getReturnType() const
Returns the type of the ret val.
Definition: Function.cpp:238
An instruction for reading from memory.
Definition: Instructions.h:164
Type * getPointerElementType() const
Definition: Type.h:358
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:165
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:191
unsigned getNumArgOperands() const
Return the number of call arguments.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:588
Type * getVectorElementType() const
Definition: Type.h:353
#define F(x, y, z)
Definition: MD5.cpp:51
FunctionPass * createSITypeRewriter()
This class represents a no-op cast from one type to another.
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:291
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:401
This instruction inserts a single (scalar) element into a VectorType value.
bool isShader(CallingConv::ID cc)
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
void getAllMetadataOtherThanDebugLoc(SmallVectorImpl< std::pair< unsigned, MDNode * >> &MDs) const
This does the same thing as getAllMetadata, except that it filters out the debug location.
Definition: Instruction.h:198
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:219
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
Value * getOperand(unsigned i) const
Definition: User.h:145
Value * getPointerOperand()
Definition: Instructions.h:270
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:654
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1183
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:843
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:230
Type * getDestTy() const
Return the destination type, as a convenience.
Definition: InstrTypes.h:847
Function * getCalledFunction() const
Return the function called, or null if this is an indirect function invocation.
AttributeSet getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:176
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
unsigned getVectorNumElements() const
Definition: DerivedTypes.h:438
void setAttributes(AttributeSet Attrs)
Set the attribute list for this Function.
Definition: Function.h:179
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:169
#define I(x, y, z)
Definition: MD5.cpp:54
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
LLVM Value Representation.
Definition: Value.h:71
static VectorType * get(Type *ElementType, unsigned NumElements)
This static method is the primary way to construct an VectorType.
Definition: Type.cpp:631
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, const Twine &N="", Module *M=nullptr)
Definition: Function.h:117
int * Ptr
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: DerivedTypes.h:479
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:167
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:222