34 bool runOnModule(
Module &M);
39 Value *remapConstantVectorOrConstantAggregate(
Module *M, Function *
F,
42 Value *remapConstantExpr(
Module *M, Function *
F, ConstantExpr *
C,
45 typedef ValueMap<GlobalVariable *, GlobalVariable *> GVMapTy;
46 typedef ValueMap<Constant *, Value *> ConstantToValueMapTy;
48 ConstantToValueMapTy ConstantToValueMap;
52bool GenericToNVVM::runOnModule(
Module &M) {
61 !GV.getName().starts_with(
"llvm.")) {
62 GlobalVariable *NewGV =
new GlobalVariable(
63 M, GV.getValueType(), GV.isConstant(), GV.getLinkage(),
64 GV.hasInitializer() ? GV.getInitializer() :
nullptr,
"", &GV,
81 for (Function &
F : M) {
82 if (
F.isDeclaration()) {
85 IRBuilder<> Builder(&*
F.getEntryBlock().getFirstNonPHIOrDbg());
86 for (BasicBlock &BB :
F) {
87 for (Instruction &
II : BB) {
88 for (
unsigned i = 0, e =
II.getNumOperands(); i < e; ++i) {
89 Value *Operand =
II.getOperand(i);
97 ConstantToValueMap.
clear();
103 VM[
I->first] =
I->second;
114 for (
auto [GV, NewGV] : GVs) {
122 GV->replaceAllUsesWith(BitCastNewGV);
123 std::string
Name = std::string(GV->getName());
124 GV->eraseFromParent();
127 assert(GVMap.
empty() &&
"Expected it to be empty by now");
132Value *GenericToNVVM::remapConstant(
Module *M, Function *
F, Constant *
C,
136 ConstantToValueMapTy::iterator CTII = ConstantToValueMap.
find(
C);
137 if (CTII != ConstantToValueMap.
end()) {
149 if (
I != GVMap.
end()) {
150 GlobalVariable *GV =
I->second;
158 NewValue = remapConstantVectorOrConstantAggregate(M,
F,
C, Builder);
166 ConstantToValueMap[
C] = NewValue;
170Value *GenericToNVVM::remapConstantVectorOrConstantAggregate(
172 bool OperandChanged =
false;
173 SmallVector<Value *, 4> NewOperands;
174 unsigned NumOperands =
C->getNumOperands();
178 for (
unsigned i = 0; i < NumOperands; ++i) {
179 Value *Operand =
C->getOperand(i);
181 OperandChanged |= Operand != NewOperand;
186 if (!OperandChanged) {
195 for (
unsigned i = 0; i < NumOperands; ++i) {
196 Value *Idx = ConstantInt::get(Type::getInt32Ty(
M->getContext()), i);
200 for (
unsigned i = 0; i < NumOperands; ++i) {
209Value *GenericToNVVM::remapConstantExpr(
Module *M, Function *
F, ConstantExpr *
C,
211 bool OperandChanged =
false;
212 SmallVector<Value *, 4> NewOperands;
213 unsigned NumOperands =
C->getNumOperands();
217 for (
unsigned i = 0; i < NumOperands; ++i) {
218 Value *Operand =
C->getOperand(i);
220 OperandChanged |= Operand != NewOperand;
225 if (!OperandChanged) {
231 unsigned Opcode =
C->getOpcode();
233 case Instruction::ExtractElement:
236 case Instruction::InsertElement:
240 case Instruction::ShuffleVector:
244 case Instruction::GetElementPtr:
248 ArrayRef(&NewOperands[1], NumOperands - 1),
"",
250 case Instruction::Select:
252 return Builder.
CreateSelect(NewOperands[0], NewOperands[1], NewOperands[2]);
257 NewOperands[0], NewOperands[1]);
262 NewOperands[0],
C->getType());
269class GenericToNVVMLegacyPass :
public ModulePass {
273 GenericToNVVMLegacyPass() : ModulePass(
ID) {}
275 bool runOnModule(
Module &M)
override;
279char GenericToNVVMLegacyPass::ID = 0;
282 return new GenericToNVVMLegacyPass();
286 GenericToNVVMLegacyPass,
"generic-to-nvvm",
287 "Ensure that the global variables are in the global address space",
false,
290bool GenericToNVVMLegacyPass::runOnModule(
Module &M) {
291 return GenericToNVVM().runOnModule(M);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Module.h This file contains the declarations for the Module class.
Machine Check Debug Module
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static LLVM_ABI Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
LLVM_ABI void copyMetadata(const GlobalObject *Src, unsigned Offset)
Copy metadata from Src, adjusting offsets by Offset.
LLVM_ABI void copyAttributesFrom(const GlobalVariable *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a GlobalVariable) fro...
Value * CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx, const Twine &Name="")
Value * CreateInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &Name="")
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
LLVM_ABI Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="", MDNode *FPMathTag=nullptr, FMFSource FMFSource={})
Value * CreateGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="", GEPNoWrapFlags NW=GEPNoWrapFlags::none())
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Value * CreateAddrSpaceCast(Value *V, Type *DestTy, const Twine &Name="")
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
A Module instance is used to store all the information related to an LLVM module.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
void push_back(const T &Elt)
iterator find(const KeyT &Val)
bool erase(const KeyT &Val)
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
LLVMContext & getContext() const
All values hold a context through their type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
ModulePass * createGenericToNVVMLegacyPass()
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
ArrayRef(const T &OneElt) -> ArrayRef< T >
ValueMap< const Value *, WeakTrackingVH > ValueToValueMapTy
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
PTXOpaqueType getPTXOpaqueType(const GlobalVariable &GV)
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)