Index: llvm/test/TableGen/GlobalISelEmitterOverloadedPtr.td =================================================================== --- /dev/null +++ llvm/test/TableGen/GlobalISelEmitterOverloadedPtr.td @@ -0,0 +1,48 @@ +// RUN: llvm-tblgen -gen-global-isel -I %p/../../include %s -o - | FileCheck %s + +// Boilerplate code. +include "llvm/Target/Target.td" + +def MyTargetISA : InstrInfo; +def MyTarget : Target { let InstructionSet = MyTargetISA; } + +let TargetPrefix = "mytarget" in { + def int_mytarget_anyptr : Intrinsic<[llvm_i32_ty], [llvm_anyptr_ty]>; +} + +def R0 : Register<"r0"> { let Namespace = "MyTarget"; } +def GPR32 : RegisterClass<"MyTarget", [i32], 32, (add R0)>; +def GPR32Op : RegisterOperand; +def p0 : PtrValueType ; + +class I Pat> + : Instruction { + let Namespace = "MyTarget"; + let OutOperandList = OOps; + let InOperandList = IOps; + let Pattern = Pat; +} + +// Ensure that llvm_anyptr_ty on an intrinsic results in a +// GIM_CheckPointerToAny rather than a GIM_CheckType. +// +// CHECK: GIM_CheckCxxInsnPredicate, /*MI*/0, /*FnId*/GIPFP_MI_Predicate_frag_anyptr, +// CHECK-NEXT: GIM_CheckRegBankForClass, /*MI*/0, /*Op*/0, /*RC*/MyTarget::GPR32RegClassID, +// CHECK-NEXT: // MIs[0] src +// CHECK-NEXT: GIM_CheckPointerToAny, /*MI*/0, /*Op*/2, /*SizeInBits*/32, +// CHECK-NEXT: GIM_CheckRegBankForClass, /*MI*/0, /*Op*/2, /*RC*/MyTarget::GPR32RegClassID, +// CHECK-NEXT: // (intrinsic_w_chain:{ *:[i32] } 3821:{ *:[iPTR] }, GPR32:{ *:[i32] }:$src)<> => (ANYLOAD:{ *:[i32] } GPR32:{ *:[i32] }:$src) +// CHECK-NEXT: GIR_BuildMI, /*InsnID*/0, /*Opcode*/MyTarget::ANYLOAD, +let hasSideEffects = 1 in { + def ANYLOAD : I<(outs GPR32:$dst), (ins GPR32:$src1), + [(set GPR32:$dst, (load GPR32:$src1))]>; +} + +def frag_anyptr : PatFrag<(ops node:$src), + (int_mytarget_anyptr node:$src), + [{ return true; // C++ code }]> { + let GISelPredicateCode = [{ return true; // C++ code }]; +} + +def : Pat<(frag_anyptr GPR32:$src), + (p0 (ANYLOAD GPR32:$src))>; Index: llvm/utils/TableGen/GlobalISelEmitter.cpp =================================================================== --- llvm/utils/TableGen/GlobalISelEmitter.cpp +++ llvm/utils/TableGen/GlobalISelEmitter.cpp @@ -3263,6 +3263,23 @@ return Error::success(); } +// Return true if an intrinsic \p II's ParamIdx-th entry in its ParamTypes +// record has an iPTRAny type. If this is true, then that parameter should be +// treated as an iPTR. +static bool isParamAnyPtr(const CodeGenIntrinsic &II, unsigned ParamIdx) { + if (!II.isOverloaded) + return false; + Record *Def = II.TheDef; + ListInit *ParamTypes = Def->getValueAsListInit("ParamTypes"); + if (ParamIdx < ParamTypes->size()) { + Record *TyEl = ParamTypes->getElementAsRecord(ParamIdx); + MVT::SimpleValueType VT = getValueType(TyEl->getValueAsDef("VT")); + if (MVT(VT) == MVT::iPTRAny) + return true; + } + return false; +} + Expected GlobalISelEmitter::createAndImportSelDAGMatcher( RuleMatcher &Rule, InstructionMatcher &InsnMatcher, const TreePatternNode *Src, unsigned &TempOpIdx) { @@ -3474,6 +3491,13 @@ } // Match the used operands (i.e. the children of the operator). + bool IsIntrinsic = + SrcGIOrNull->TheDef->getName() == "G_INTRINSIC" || + SrcGIOrNull->TheDef->getName() == "G_INTRINSIC_W_SIDE_EFFECTS"; + const CodeGenIntrinsic *II = Src->getIntrinsicInfo(CGP); + if (IsIntrinsic && !II) + return failedImport("Expected IntInit containing instrinsic ID)"); + for (unsigned i = 0, e = Src->getNumChildren(); i != e; ++i) { TreePatternNode *SrcChild = Src->getChild(i); @@ -3484,19 +3508,20 @@ // For G_INTRINSIC/G_INTRINSIC_W_SIDE_EFFECTS, the operand immediately // following the defs is an intrinsic ID. - if ((SrcGIOrNull->TheDef->getName() == "G_INTRINSIC" || - SrcGIOrNull->TheDef->getName() == "G_INTRINSIC_W_SIDE_EFFECTS") && - i == 0) { - if (const CodeGenIntrinsic *II = Src->getIntrinsicInfo(CGP)) { - OperandMatcher &OM = - InsnMatcher.addOperand(OpIdx++, SrcChild->getName(), TempOpIdx); - OM.addPredicate(II); - continue; - } - - return failedImport("Expected IntInit containing instrinsic ID)"); + if (IsIntrinsic && i == 0) { + OperandMatcher &OM = + InsnMatcher.addOperand(OpIdx++, SrcChild->getName(), TempOpIdx); + OM.addPredicate(II); + continue; } + // We could have anyptr_ty operands on an overloaded intrinsic. These are + // pointers. + // Note that we have to look at the i-1th parameter, because the + // intrinsic ID is not present in the ParamTypes record. + if (IsIntrinsic) + OperandIsAPointer |= isParamAnyPtr(*II, i - 1); + if (auto Error = importChildMatcher(Rule, InsnMatcher, SrcChild, OperandIsAPointer, OpIdx++, TempOpIdx))