bool FilterChooser::emitPredicateMatch(raw_ostream &o, unsigned &Indentation, unsigned Opc) { ListInit *Predicates = AllInstructions[Opc]->TheDef->getValueAsListInit("Predicates"); for (unsigned i = 0; i < Predicates->getSize(); ++i) { Record *Pred = Predicates->getElementAsRecord(i); if (!Pred->getValue("AssemblerMatcherPredicate")) continue; std::string P = Pred->getValueAsString("AssemblerCondString"); if (!P.length()) continue; if (i != 0) o << " && "; StringRef SR(P); std::pair<StringRef, StringRef> pairs = SR.split(','); while (pairs.second.size()) { emitSinglePredicateMatch(o, pairs.first, Emitter->PredicateNamespace); o << " && "; pairs = pairs.second.split(','); } emitSinglePredicateMatch(o, pairs.first, Emitter->PredicateNamespace); } return Predicates->getSize() > 0; }
void processRecord(raw_ostream& os, Record& rec, string arch) { if(!rec.getValue("GCCBuiltinName")) return; string builtinName = rec.getValueAsString("GCCBuiltinName"); string name = rec.getName(); if(name.substr(0, 4) != "int_" || name.find(arch) == string::npos) return; name = name.substr(4); replace(name.begin(), name.end(), '_', '.'); name = string("llvm.") + name; ListInit* paramsList = rec.getValueAsListInit("ParamTypes"); vector<string> params; for(unsigned int i = 0; i < paramsList->getSize(); i++) { string t = dtype(paramsList->getElementAsRecord(i)); if(t == "") return; params.push_back(t); } ListInit* retList = rec.getValueAsListInit("RetTypes"); string ret; if(retList->getSize() == 0) ret = "void"; else if(retList->getSize() == 1) { ret = dtype(retList->getElementAsRecord(0)); if(ret == "") return; } else return; os << "pragma(LDC_intrinsic, \"" + name + "\")\n "; os << ret + " " + builtinName + "("; if(params.size()) os << params[0]; for(size_t i = 1; i < params.size(); i++) os << ", " << params[i]; os << ")" + attributes(rec.getValueAsListInit("Properties")) + ";\n\n"; }
/// ReadNodeTypes - Read in all of the node types in the current RecordKeeper, /// turning them into the more accessible NodeTypes data structure. /// void InstrSelectorEmitter::ReadNodeTypes() { std::vector<Record*> Nodes = Records.getAllDerivedDefinitions("DagNode"); DEBUG(std::cerr << "Getting node types: "); for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { Record *Node = Nodes[i]; // Translate the return type... NodeType::ArgResultTypes RetTy = NodeType::Translate(Node->getValueAsDef("RetType")); // Translate the arguments... ListInit *Args = Node->getValueAsListInit("ArgTypes"); std::vector<NodeType::ArgResultTypes> ArgTypes; for (unsigned a = 0, e = Args->getSize(); a != e; ++a) { if (DefInit *DI = dynamic_cast<DefInit*>(Args->getElement(a))) ArgTypes.push_back(NodeType::Translate(DI->getDef())); else throw "In node " + Node->getName() + ", argument is not a Def!"; if (a == 0 && ArgTypes.back() == NodeType::Arg0) throw "In node " + Node->getName() + ", arg 0 cannot have type 'arg0'!"; if (a == 1 && ArgTypes.back() == NodeType::Arg1) throw "In node " + Node->getName() + ", arg 1 cannot have type 'arg1'!"; } if ((RetTy == NodeType::Arg0 && Args->getSize() == 0) || (RetTy == NodeType::Arg1 && Args->getSize() < 2)) throw "In node " + Node->getName() + ", invalid return type for node with this many operands!"; // Add the node type mapping now... NodeTypes[Node] = NodeType(RetTy, ArgTypes); DEBUG(std::cerr << Node->getName() << ", "); } DEBUG(std::cerr << "DONE!\n"); }
void CallingConvEmitter::EmitCallingConv(Record *CC, std::ostream &O) { ListInit *CCActions = CC->getValueAsListInit("Actions"); Counter = 0; O << "\n\nstatic bool " << CC->getName() << "(unsigned ValNo, MVT ValVT,\n" << std::string(CC->getName().size()+13, ' ') << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n" << std::string(CC->getName().size()+13, ' ') << "ISD::ArgFlagsTy ArgFlags, CCState &State) {\n"; // Emit all of the actions, in order. for (unsigned i = 0, e = CCActions->getSize(); i != e; ++i) { O << "\n"; EmitAction(CCActions->getElementAsRecord(i), 2, O); } O << "\n return true; // CC didn't match.\n"; O << "}\n"; }
static const std::vector<StringRef> getValueAsListOfStrings(Record &R, StringRef FieldName) { ListInit *List = R.getValueAsListInit(FieldName); assert (List && "Got a null ListInit"); std::vector<StringRef> Strings; Strings.reserve(List->getSize()); for (ListInit::iterator i = List->begin(), e = List->end(); i != e; ++i) { assert(*i && "Got a null element in a ListInit"); if (StringInit *S = dynamic_cast<StringInit *>(*i)) Strings.push_back(S->getValue()); else if (CodeInit *C = dynamic_cast<CodeInit *>(*i)) Strings.push_back(C->getValue()); else assert(false && "Got a non-string, non-code element in a ListInit"); } return Strings; }
void CallingConvEmitter::EmitAction(Record *Action, unsigned Indent, std::ostream &O) { std::string IndentStr = std::string(Indent, ' '); if (Action->isSubClassOf("CCPredicateAction")) { O << IndentStr << "if ("; if (Action->isSubClassOf("CCIfType")) { ListInit *VTs = Action->getValueAsListInit("VTs"); for (unsigned i = 0, e = VTs->getSize(); i != e; ++i) { Record *VT = VTs->getElementAsRecord(i); if (i != 0) O << " ||\n " << IndentStr; O << "LocVT == " << getEnumName(getValueType(VT)); } } else if (Action->isSubClassOf("CCIf")) { O << Action->getValueAsString("Predicate"); } else { Action->dump(); throw "Unknown CCPredicateAction!"; } O << ") {\n"; EmitAction(Action->getValueAsDef("SubAction"), Indent+2, O); O << IndentStr << "}\n"; } else { if (Action->isSubClassOf("CCDelegateTo")) { Record *CC = Action->getValueAsDef("CC"); O << IndentStr << "if (!" << CC->getName() << "(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))\n" << IndentStr << " return false;\n"; } else if (Action->isSubClassOf("CCAssignToReg")) { ListInit *RegList = Action->getValueAsListInit("RegList"); if (RegList->getSize() == 1) { O << IndentStr << "if (unsigned Reg = State.AllocateReg("; O << getQualifiedName(RegList->getElementAsRecord(0)) << ")) {\n"; } else { O << IndentStr << "static const unsigned RegList" << ++Counter << "[] = {\n"; O << IndentStr << " "; for (unsigned i = 0, e = RegList->getSize(); i != e; ++i) { if (i != 0) O << ", "; O << getQualifiedName(RegList->getElementAsRecord(i)); } O << "\n" << IndentStr << "};\n"; O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList" << Counter << ", " << RegList->getSize() << ")) {\n"; } O << IndentStr << " State.addLoc(CCValAssign::getReg(ValNo, ValVT, " << "Reg, LocVT, LocInfo));\n"; O << IndentStr << " return false;\n"; O << IndentStr << "}\n"; } else if (Action->isSubClassOf("CCAssignToRegWithShadow")) { ListInit *RegList = Action->getValueAsListInit("RegList"); ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList"); if (ShadowRegList->getSize() >0 && ShadowRegList->getSize() != RegList->getSize()) throw "Invalid length of list of shadowed registers"; if (RegList->getSize() == 1) { O << IndentStr << "if (unsigned Reg = State.AllocateReg("; O << getQualifiedName(RegList->getElementAsRecord(0)); O << ", " << getQualifiedName(ShadowRegList->getElementAsRecord(0)); O << ")) {\n"; } else { unsigned RegListNumber = ++Counter; unsigned ShadowRegListNumber = ++Counter; O << IndentStr << "static const unsigned RegList" << RegListNumber << "[] = {\n"; O << IndentStr << " "; for (unsigned i = 0, e = RegList->getSize(); i != e; ++i) { if (i != 0) O << ", "; O << getQualifiedName(RegList->getElementAsRecord(i)); } O << "\n" << IndentStr << "};\n"; O << IndentStr << "static const unsigned RegList" << ShadowRegListNumber << "[] = {\n"; O << IndentStr << " "; for (unsigned i = 0, e = ShadowRegList->getSize(); i != e; ++i) { if (i != 0) O << ", "; O << getQualifiedName(ShadowRegList->getElementAsRecord(i)); } O << "\n" << IndentStr << "};\n"; O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList" << RegListNumber << ", " << "RegList" << ShadowRegListNumber << ", " << RegList->getSize() << ")) {\n"; } O << IndentStr << " State.addLoc(CCValAssign::getReg(ValNo, ValVT, " << "Reg, LocVT, LocInfo));\n"; O << IndentStr << " return false;\n"; O << IndentStr << "}\n"; } else if (Action->isSubClassOf("CCAssignToStack")) { int Size = Action->getValueAsInt("Size"); int Align = Action->getValueAsInt("Align"); O << IndentStr << "unsigned Offset" << ++Counter << " = State.AllocateStack("; if (Size) O << Size << ", "; else O << "\n" << IndentStr << " State.getTarget().getTargetData()" "->getTypePaddedSize(LocVT.getTypeForMVT()), "; if (Align) O << Align; else O << "\n" << IndentStr << " State.getTarget().getTargetData()" "->getABITypeAlignment(LocVT.getTypeForMVT())"; O << ");\n" << IndentStr << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset" << Counter << ", LocVT, LocInfo));\n"; O << IndentStr << "return false;\n"; } else if (Action->isSubClassOf("CCPromoteToType")) { Record *DestTy = Action->getValueAsDef("DestTy"); O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n"; O << IndentStr << "if (ArgFlags.isSExt())\n" << IndentStr << IndentStr << "LocInfo = CCValAssign::SExt;\n" << IndentStr << "else if (ArgFlags.isZExt())\n" << IndentStr << IndentStr << "LocInfo = CCValAssign::ZExt;\n" << IndentStr << "else\n" << IndentStr << IndentStr << "LocInfo = CCValAssign::AExt;\n"; } else if (Action->isSubClassOf("CCPassByVal")) { int Size = Action->getValueAsInt("Size"); int Align = Action->getValueAsInt("Align"); O << IndentStr << "State.HandleByVal(ValNo, ValVT, LocVT, LocInfo, " << Size << ", " << Align << ", ArgFlags);\n"; O << IndentStr << "return false;\n"; } else { Action->dump(); throw "Unknown CCAction!"; } } }
CodeGenIntrinsic::CodeGenIntrinsic(Record *R, CodeGenTarget *CGT) { TheDef = R; std::string DefName = R->getName(); ModRef = WriteMem; isOverloaded = false; if (DefName.size() <= 4 || std::string(DefName.begin(), DefName.begin()+4) != "int_") throw "Intrinsic '" + DefName + "' does not start with 'int_'!"; EnumName = std::string(DefName.begin()+4, DefName.end()); if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field. GCCBuiltinName = R->getValueAsString("GCCBuiltinName"); TargetPrefix = R->getValueAsString("TargetPrefix"); Name = R->getValueAsString("LLVMName"); if (Name == "") { // If an explicit name isn't specified, derive one from the DefName. Name = "llvm."; for (unsigned i = 0, e = EnumName.size(); i != e; ++i) if (EnumName[i] == '_') Name += '.'; else Name += EnumName[i]; } else { // Verify it starts with "llvm.". if (Name.size() <= 5 || std::string(Name.begin(), Name.begin()+5) != "llvm.") throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!"; } // If TargetPrefix is specified, make sure that Name starts with // "llvm.<targetprefix>.". if (!TargetPrefix.empty()) { if (Name.size() < 6+TargetPrefix.size() || std::string(Name.begin()+5, Name.begin()+6+TargetPrefix.size()) != (TargetPrefix+".")) throw "Intrinsic '" + DefName + "' does not start with 'llvm." + TargetPrefix + ".'!"; } // Parse the list of argument types. ListInit *TypeList = R->getValueAsListInit("Types"); for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) { Record *TyEl = TypeList->getElementAsRecord(i); assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!"); ArgTypes.push_back(TyEl->getValueAsString("TypeVal")); MVT::ValueType VT = getValueType(TyEl->getValueAsDef("VT"), CGT); isOverloaded |= VT == MVT::iAny; ArgVTs.push_back(VT); ArgTypeDefs.push_back(TyEl); } if (ArgTypes.size() == 0) throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!"; // Parse the intrinsic properties. ListInit *PropList = R->getValueAsListInit("Properties"); for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) { Record *Property = PropList->getElementAsRecord(i); assert(Property->isSubClassOf("IntrinsicProperty") && "Expected a property!"); if (Property->getName() == "IntrNoMem") ModRef = NoMem; else if (Property->getName() == "IntrReadArgMem") ModRef = ReadArgMem; else if (Property->getName() == "IntrReadMem") ModRef = ReadMem; else if (Property->getName() == "IntrWriteArgMem") ModRef = WriteArgMem; else if (Property->getName() == "IntrWriteMem") ModRef = WriteMem; else assert(0 && "Unknown property!"); } }
CodeGenIntrinsic::CodeGenIntrinsic(Record *R) { TheDef = R; std::string DefName = R->getName(); ModRef = ReadWriteMem; isOverloaded = false; isCommutative = false; canThrow = false; isNoReturn = false; if (DefName.size() <= 4 || std::string(DefName.begin(), DefName.begin() + 4) != "int_") PrintFatalError("Intrinsic '" + DefName + "' does not start with 'int_'!"); EnumName = std::string(DefName.begin()+4, DefName.end()); if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field. GCCBuiltinName = R->getValueAsString("GCCBuiltinName"); TargetPrefix = R->getValueAsString("TargetPrefix"); Name = R->getValueAsString("LLVMName"); if (Name == "") { // If an explicit name isn't specified, derive one from the DefName. Name = "llvm."; for (unsigned i = 0, e = EnumName.size(); i != e; ++i) Name += (EnumName[i] == '_') ? '.' : EnumName[i]; } else { // Verify it starts with "llvm.". if (Name.size() <= 5 || std::string(Name.begin(), Name.begin() + 5) != "llvm.") PrintFatalError("Intrinsic '" + DefName + "'s name does not start with 'llvm.'!"); } // If TargetPrefix is specified, make sure that Name starts with // "llvm.<targetprefix>.". if (!TargetPrefix.empty()) { if (Name.size() < 6+TargetPrefix.size() || std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size()) != (TargetPrefix + ".")) PrintFatalError("Intrinsic '" + DefName + "' does not start with 'llvm." + TargetPrefix + ".'!"); } // Parse the list of return types. std::vector<MVT::SimpleValueType> OverloadedVTs; ListInit *TypeList = R->getValueAsListInit("RetTypes"); for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) { Record *TyEl = TypeList->getElementAsRecord(i); assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!"); MVT::SimpleValueType VT; if (TyEl->isSubClassOf("LLVMMatchType")) { unsigned MatchTy = TyEl->getValueAsInt("Number"); assert(MatchTy < OverloadedVTs.size() && "Invalid matching number!"); VT = OverloadedVTs[MatchTy]; // It only makes sense to use the extended and truncated vector element // variants with iAny types; otherwise, if the intrinsic is not // overloaded, all the types can be specified directly. assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") && !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) || VT == MVT::iAny || VT == MVT::vAny) && "Expected iAny or vAny type"); } else { VT = getValueType(TyEl->getValueAsDef("VT")); } if (EVT(VT).isOverloaded()) { OverloadedVTs.push_back(VT); isOverloaded = true; } // Reject invalid types. if (VT == MVT::isVoid) PrintFatalError("Intrinsic '" + DefName + " has void in result type list!"); IS.RetVTs.push_back(VT); IS.RetTypeDefs.push_back(TyEl); } // Parse the list of parameter types. TypeList = R->getValueAsListInit("ParamTypes"); for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) { Record *TyEl = TypeList->getElementAsRecord(i); assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!"); MVT::SimpleValueType VT; if (TyEl->isSubClassOf("LLVMMatchType")) { unsigned MatchTy = TyEl->getValueAsInt("Number"); assert(MatchTy < OverloadedVTs.size() && "Invalid matching number!"); VT = OverloadedVTs[MatchTy]; // It only makes sense to use the extended and truncated vector element // variants with iAny types; otherwise, if the intrinsic is not // overloaded, all the types can be specified directly. assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") && !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) || VT == MVT::iAny || VT == MVT::vAny) && "Expected iAny or vAny type"); } else VT = getValueType(TyEl->getValueAsDef("VT")); if (EVT(VT).isOverloaded()) { OverloadedVTs.push_back(VT); isOverloaded = true; } // Reject invalid types. if (VT == MVT::isVoid && i != e-1 /*void at end means varargs*/) PrintFatalError("Intrinsic '" + DefName + " has void in result type list!"); IS.ParamVTs.push_back(VT); IS.ParamTypeDefs.push_back(TyEl); } // Parse the intrinsic properties. ListInit *PropList = R->getValueAsListInit("Properties"); for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) { Record *Property = PropList->getElementAsRecord(i); assert(Property->isSubClassOf("IntrinsicProperty") && "Expected a property!"); if (Property->getName() == "IntrNoMem") ModRef = NoMem; else if (Property->getName() == "IntrReadArgMem") ModRef = ReadArgMem; else if (Property->getName() == "IntrReadMem") ModRef = ReadMem; else if (Property->getName() == "IntrReadWriteArgMem") ModRef = ReadWriteArgMem; else if (Property->getName() == "Commutative") isCommutative = true; else if (Property->getName() == "Throws") canThrow = true; else if (Property->getName() == "IntrNoReturn") isNoReturn = true; else if (Property->isSubClassOf("NoCapture")) { unsigned ArgNo = Property->getValueAsInt("ArgNo"); ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture)); } else llvm_unreachable("Unknown property!"); } // Sort the argument attributes for later benefit. std::sort(ArgumentAttributes.begin(), ArgumentAttributes.end()); }
void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num, Record *InstrInfo, std::map<std::vector<Record*>, unsigned> &EmittedLists, std::map<Record*, unsigned> &BarriersMap, const OperandInfoMapTy &OpInfo, raw_ostream &OS) { int MinOperands = 0; if (!Inst.OperandList.empty()) // Each logical operand can be multiple MI operands. MinOperands = Inst.OperandList.back().MIOperandNo + Inst.OperandList.back().MINumOperands; OS << " { "; OS << Num << ",\t" << MinOperands << ",\t" << Inst.NumDefs << ",\t" << getItinClassNumber(Inst.TheDef) << ",\t\"" << Inst.TheDef->getName() << "\", 0"; // Emit all of the target indepedent flags... if (Inst.isReturn) OS << "|(1<<TID::Return)"; if (Inst.isBranch) OS << "|(1<<TID::Branch)"; if (Inst.isIndirectBranch) OS << "|(1<<TID::IndirectBranch)"; if (Inst.isBarrier) OS << "|(1<<TID::Barrier)"; if (Inst.hasDelaySlot) OS << "|(1<<TID::DelaySlot)"; if (Inst.isCall) OS << "|(1<<TID::Call)"; if (Inst.canFoldAsLoad) OS << "|(1<<TID::FoldableAsLoad)"; if (Inst.mayLoad) OS << "|(1<<TID::MayLoad)"; if (Inst.mayStore) OS << "|(1<<TID::MayStore)"; if (Inst.isPredicable) OS << "|(1<<TID::Predicable)"; if (Inst.isConvertibleToThreeAddress) OS << "|(1<<TID::ConvertibleTo3Addr)"; if (Inst.isCommutable) OS << "|(1<<TID::Commutable)"; if (Inst.isTerminator) OS << "|(1<<TID::Terminator)"; if (Inst.isReMaterializable) OS << "|(1<<TID::Rematerializable)"; if (Inst.isNotDuplicable) OS << "|(1<<TID::NotDuplicable)"; if (Inst.hasOptionalDef) OS << "|(1<<TID::HasOptionalDef)"; if (Inst.usesCustomDAGSchedInserter) OS << "|(1<<TID::UsesCustomDAGSchedInserter)"; if (Inst.isVariadic) OS << "|(1<<TID::Variadic)"; if (Inst.hasSideEffects) OS << "|(1<<TID::UnmodeledSideEffects)"; if (Inst.isAsCheapAsAMove) OS << "|(1<<TID::CheapAsAMove)"; OS << ", 0"; // Emit all of the target-specific flags... ListInit *LI = InstrInfo->getValueAsListInit("TSFlagsFields"); ListInit *Shift = InstrInfo->getValueAsListInit("TSFlagsShifts"); if (LI->getSize() != Shift->getSize()) throw "Lengths of " + InstrInfo->getName() + ":(TargetInfoFields, TargetInfoPositions) must be equal!"; for (unsigned i = 0, e = LI->getSize(); i != e; ++i) emitShiftedValue(Inst.TheDef, dynamic_cast<StringInit*>(LI->getElement(i)), dynamic_cast<IntInit*>(Shift->getElement(i)), OS); OS << ", "; // Emit the implicit uses and defs lists... std::vector<Record*> UseList = Inst.TheDef->getValueAsListOfDefs("Uses"); if (UseList.empty()) OS << "NULL, "; else OS << "ImplicitList" << EmittedLists[UseList] << ", "; std::vector<Record*> DefList = Inst.TheDef->getValueAsListOfDefs("Defs"); if (DefList.empty()) OS << "NULL, "; else OS << "ImplicitList" << EmittedLists[DefList] << ", "; std::map<Record*, unsigned>::iterator BI = BarriersMap.find(Inst.TheDef); if (BI == BarriersMap.end()) OS << "NULL, "; else OS << "Barriers" << BI->second << ", "; // Emit the operand info. std::vector<std::string> OperandInfo = GetOperandInfo(Inst); if (OperandInfo.empty()) OS << "0"; else OS << "OperandInfo" << OpInfo.find(OperandInfo)->second; OS << " }, // Inst #" << Num << " = " << Inst.TheDef->getName() << "\n"; }
void CallingConvEmitter::EmitAction(Record *Action, unsigned Indent, std::ostream &O) { std::string IndentStr = std::string(Indent, ' '); if (Action->isSubClassOf("CCPredicateAction")) { O << IndentStr << "if ("; if (Action->isSubClassOf("CCIfType")) { ListInit *VTs = Action->getValueAsListInit("VTs"); for (unsigned i = 0, e = VTs->getSize(); i != e; ++i) { Record *VT = VTs->getElementAsRecord(i); if (i != 0) O << " ||\n " << IndentStr; O << "LocVT == " << getEnumName(getValueType(VT)); } } else if (Action->isSubClassOf("CCIf")) { O << Action->getValueAsString("Predicate"); } else { Action->dump(); throw "Unknown CCPredicateAction!"; } O << ") {\n"; EmitAction(Action->getValueAsDef("SubAction"), Indent+2, O); O << IndentStr << "}\n"; } else { if (Action->isSubClassOf("CCDelegateTo")) { Record *CC = Action->getValueAsDef("CC"); O << IndentStr << "if (!" << CC->getName() << "(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))\n" << IndentStr << " return false;\n"; } else if (Action->isSubClassOf("CCAssignToReg")) { ListInit *RegList = Action->getValueAsListInit("RegList"); if (RegList->getSize() == 1) { O << IndentStr << "if (unsigned Reg = State.AllocateReg("; O << getQualifiedName(RegList->getElementAsRecord(0)) << ")) {\n"; } else { O << IndentStr << "static const unsigned RegList" << ++Counter << "[] = {\n"; O << IndentStr << " "; for (unsigned i = 0, e = RegList->getSize(); i != e; ++i) { if (i != 0) O << ", "; O << getQualifiedName(RegList->getElementAsRecord(i)); } O << "\n" << IndentStr << "};\n"; O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList" << Counter << ", " << RegList->getSize() << ")) {\n"; } O << IndentStr << " State.addLoc(CCValAssign::getReg(ValNo, ValVT, " << "Reg, LocVT, LocInfo));\n"; O << IndentStr << " return false;\n"; O << IndentStr << "}\n"; } else if (Action->isSubClassOf("CCAssignToStack")) { int Size = Action->getValueAsInt("Size"); int Align = Action->getValueAsInt("Align"); O << IndentStr << "unsigned Offset" << ++Counter << " = State.AllocateStack(" << Size << ", " << Align << ");\n"; O << IndentStr << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset" << Counter << ", LocVT, LocInfo));\n"; O << IndentStr << "return false;\n"; } else if (Action->isSubClassOf("CCPromoteToType")) { Record *DestTy = Action->getValueAsDef("DestTy"); O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n"; O << IndentStr << "if (ArgFlags & ISD::ParamFlags::SExt)\n" << IndentStr << IndentStr << "LocInfo = CCValAssign::SExt;\n" << IndentStr << "else if (ArgFlags & ISD::ParamFlags::ZExt)\n" << IndentStr << IndentStr << "LocInfo = CCValAssign::ZExt;\n" << IndentStr << "else\n" << IndentStr << IndentStr << "LocInfo = CCValAssign::AExt;\n"; } else { Action->dump(); throw "Unknown CCAction!"; } } }
void InstrSelectorEmitter::run(std::ostream &OS) { // Type-check all of the node types to ensure we "understand" them. ReadNodeTypes(); // Read in all of the nonterminals, instructions, and expanders... ReadNonterminals(); ReadInstructionPatterns(); ReadExpanderPatterns(); // Instantiate any unresolved nonterminals with information from the context // that they are used in. InstantiateNonterminals(); // Clear InstantiatedNTs, we don't need it anymore... InstantiatedNTs.clear(); DEBUG(std::cerr << "Patterns acquired:\n"); for (std::map<Record*, Pattern*>::iterator I = Patterns.begin(), E = Patterns.end(); I != E; ++I) if (I->second->isResolved()) DEBUG(std::cerr << " " << *I->second << "\n"); CalculateComputableValues(); OS << "#include \"llvm/CodeGen/MachineInstrBuilder.h\"\n"; EmitSourceFileHeader("Instruction Selector for the " + Target.getName() + " target", OS); // Output the slot number enums... OS << "\nenum { // Slot numbers...\n" << " LastBuiltinSlot = ISD::NumBuiltinSlots-1, // Start numbering here\n"; for (PatternOrganizer::iterator I = ComputableValues.begin(), E = ComputableValues.end(); I != E; ++I) OS << " " << I->first << "_Slot,\n"; OS << " NumSlots\n};\n\n// Reduction value typedefs...\n"; // Output the reduction value typedefs... for (PatternOrganizer::iterator I = ComputableValues.begin(), E = ComputableValues.end(); I != E; ++I) { OS << "typedef ReducedValue<unsigned, " << I->first << "_Slot> ReducedValue_" << I->first << ";\n"; } // Output the pattern enums... OS << "\n\n" << "enum { // Patterns...\n" << " NotComputed = 0,\n" << " NoMatchPattern, \n"; for (PatternOrganizer::iterator I = ComputableValues.begin(), E = ComputableValues.end(); I != E; ++I) { OS << " // " << I->first << " patterns...\n"; for (PatternOrganizer::NodesForSlot::iterator J = I->second.begin(), E = I->second.end(); J != E; ++J) for (unsigned i = 0, e = J->second.size(); i != e; ++i) OS << " " << J->second[i]->getRecord()->getName() << "_Pattern,\n"; } OS << "};\n\n"; //===--------------------------------------------------------------------===// // Emit the class definition... // OS << "namespace {\n" << " class " << Target.getName() << "ISel {\n" << " SelectionDAG &DAG;\n" << " public:\n" << " " << Target.getName () << "ISel(SelectionDAG &D) : DAG(D) {}\n" << " void generateCode();\n" << " private:\n" << " unsigned makeAnotherReg(const TargetRegisterClass *RC) {\n" << " return DAG.getMachineFunction().getSSARegMap()->createVirt" "ualRegister(RC);\n" << " }\n\n" << " // DAG matching methods for classes... all of these methods" " return the cost\n" << " // of producing a value of the specified class and type, which" " also gets\n" << " // added to the DAG node.\n"; // Output all of the matching prototypes for slots... for (PatternOrganizer::iterator I = ComputableValues.begin(), E = ComputableValues.end(); I != E; ++I) OS << " unsigned Match_" << I->first << "(SelectionDAGNode *N);\n"; OS << "\n // DAG matching methods for DAG nodes...\n"; // Output all of the matching prototypes for slot/node pairs for (PatternOrganizer::iterator I = ComputableValues.begin(), E = ComputableValues.end(); I != E; ++I) for (PatternOrganizer::NodesForSlot::iterator J = I->second.begin(), E = I->second.end(); J != E; ++J) OS << " unsigned Match_" << I->first << "_" << getNodeName(J->first) << "(SelectionDAGNode *N);\n"; // Output all of the dag reduction methods prototypes... OS << "\n // DAG reduction methods...\n"; for (PatternOrganizer::iterator I = ComputableValues.begin(), E = ComputableValues.end(); I != E; ++I) OS << " ReducedValue_" << I->first << " *Reduce_" << I->first << "(SelectionDAGNode *N,\n" << std::string(27+2*I->first.size(), ' ') << "MachineBasicBlock *MBB);\n"; OS << " };\n}\n\n"; // Emit the generateCode entry-point... OS << "void " << Target.getName () << "ISel::generateCode() {\n" << " SelectionDAGNode *Root = DAG.getRoot();\n" << " assert(Root->getValueType() == MVT::isVoid && " "\"Root of DAG produces value??\");\n\n" << " std::cerr << \"\\n\";\n" << " unsigned Cost = Match_Void_void(Root);\n" << " if (Cost >= ~0U >> 1) {\n" << " std::cerr << \"Match failed!\\n\";\n" << " Root->dump();\n" << " abort();\n" << " }\n\n" << " std::cerr << \"Total DAG Cost: \" << Cost << \"\\n\\n\";\n\n" << " Reduce_Void_void(Root, 0);\n" << "}\n\n" << "//===" << std::string(70, '-') << "===//\n" << "// Matching methods...\n" << "//\n\n"; //===--------------------------------------------------------------------===// // Emit all of the matcher methods... // for (PatternOrganizer::iterator I = ComputableValues.begin(), E = ComputableValues.end(); I != E; ++I) { const std::string &SlotName = I->first; OS << "unsigned " << Target.getName() << "ISel::Match_" << SlotName << "(SelectionDAGNode *N) {\n" << " assert(N->getValueType() == MVT::" << getEnumName((*I->second.begin()).second[0]->getTree()->getType()) << ");\n" << " // If we already have a cost available for " << SlotName << " use it!\n" << " if (N->getPatternFor(" << SlotName << "_Slot))\n" << " return N->getCostFor(" << SlotName << "_Slot);\n\n" << " unsigned Cost;\n" << " switch (N->getNodeType()) {\n" << " default: Cost = ~0U >> 1; // Match failed\n" << " N->setPatternCostFor(" << SlotName << "_Slot, NoMatchPattern, Cost, NumSlots);\n" << " break;\n"; for (PatternOrganizer::NodesForSlot::iterator J = I->second.begin(), E = I->second.end(); J != E; ++J) if (!J->first->isSubClassOf("Nonterminal")) OS << " case ISD::" << getNodeName(J->first) << ":\tCost = Match_" << SlotName << "_" << getNodeName(J->first) << "(N); break;\n"; OS << " }\n"; // End of the switch statement // Emit any patterns which have a nonterminal leaf as the RHS. These may // match multiple root nodes, so they cannot be handled with the switch... for (PatternOrganizer::NodesForSlot::iterator J = I->second.begin(), E = I->second.end(); J != E; ++J) if (J->first->isSubClassOf("Nonterminal")) { OS << " unsigned " << J->first->getName() << "_Cost = Match_" << getNodeName(J->first) << "(N);\n" << " if (" << getNodeName(J->first) << "_Cost < Cost) Cost = " << getNodeName(J->first) << "_Cost;\n"; } OS << " return Cost;\n}\n\n"; for (PatternOrganizer::NodesForSlot::iterator J = I->second.begin(), E = I->second.end(); J != E; ++J) { Record *Operator = J->first; bool isNonterm = Operator->isSubClassOf("Nonterminal"); if (!isNonterm) { OS << "unsigned " << Target.getName() << "ISel::Match_"; if (!isNonterm) OS << SlotName << "_"; OS << getNodeName(Operator) << "(SelectionDAGNode *N) {\n" << " unsigned Pattern = NoMatchPattern;\n" << " unsigned MinCost = ~0U >> 1;\n"; std::vector<std::pair<Pattern*, TreePatternNode*> > Patterns; for (unsigned i = 0, e = J->second.size(); i != e; ++i) Patterns.push_back(std::make_pair(J->second[i], J->second[i]->getTree())); EmitMatchCosters(OS, Patterns, "N", 2); OS << "\n N->setPatternCostFor(" << SlotName << "_Slot, Pattern, MinCost, NumSlots);\n" << " return MinCost;\n" << "}\n"; } } } //===--------------------------------------------------------------------===// // Emit all of the reducer methods... // OS << "\n\n//===" << std::string(70, '-') << "===//\n" << "// Reducer methods...\n" << "//\n"; for (PatternOrganizer::iterator I = ComputableValues.begin(), E = ComputableValues.end(); I != E; ++I) { const std::string &SlotName = I->first; OS << "ReducedValue_" << SlotName << " *" << Target.getName() << "ISel::Reduce_" << SlotName << "(SelectionDAGNode *N, MachineBasicBlock *MBB) {\n" << " ReducedValue_" << SlotName << " *Val = N->hasValue<ReducedValue_" << SlotName << ">(" << SlotName << "_Slot);\n" << " if (Val) return Val;\n" << " if (N->getBB()) MBB = N->getBB();\n\n" << " switch (N->getPatternFor(" << SlotName << "_Slot)) {\n"; // Loop over all of the patterns that can produce a value for this slot... PatternOrganizer::NodesForSlot &NodesForSlot = I->second; for (PatternOrganizer::NodesForSlot::iterator J = NodesForSlot.begin(), E = NodesForSlot.end(); J != E; ++J) for (unsigned i = 0, e = J->second.size(); i != e; ++i) { Pattern *P = J->second[i]; OS << " case " << P->getRecord()->getName() << "_Pattern: {\n" << " // " << *P << "\n"; // Loop over the operands, reducing them... std::vector<std::pair<TreePatternNode*, std::string> > Operands; ReduceAllOperands(P->getTree(), "N", Operands, OS); // Now that we have reduced all of our operands, and have the values // that reduction produces, perform the reduction action for this // pattern. std::string Result; // If the pattern produces a register result, generate a new register // now. if (Record *R = P->getResult()) { assert(R->isSubClassOf("RegisterClass") && "Only handle register class results so far!"); OS << " unsigned NewReg = makeAnotherReg(" << Target.getName() << "::" << R->getName() << "RegisterClass);\n"; Result = "NewReg"; DEBUG(OS << " std::cerr << \"%reg\" << NewReg << \" =\t\";\n"); } else { DEBUG(OS << " std::cerr << \"\t\t\";\n"); Result = "0"; } // Print out the pattern that matched... DEBUG(OS << " std::cerr << \" " << P->getRecord()->getName() <<'"'); DEBUG(for (unsigned i = 0, e = Operands.size(); i != e; ++i) if (Operands[i].first->isLeaf()) { Record *RV = Operands[i].first->getValueRecord(); assert(RV->isSubClassOf("RegisterClass") && "Only handles registers here so far!"); OS << " << \" %reg\" << " << Operands[i].second << "->Val"; } else { OS << " << ' ' << " << Operands[i].second << "->Val"; }); DEBUG(OS << " << \"\\n\";\n"); // Generate the reduction code appropriate to the particular type of // pattern that this is... switch (P->getPatternType()) { case Pattern::Instruction: // Instruction patterns just emit a single MachineInstr, using BuildMI OS << " BuildMI(MBB, " << Target.getName() << "::" << P->getRecord()->getName() << ", " << Operands.size(); if (P->getResult()) OS << ", NewReg"; OS << ")"; for (unsigned i = 0, e = Operands.size(); i != e; ++i) { TreePatternNode *Op = Operands[i].first; if (Op->isLeaf()) { Record *RV = Op->getValueRecord(); assert(RV->isSubClassOf("RegisterClass") && "Only handles registers here so far!"); OS << ".addReg(" << Operands[i].second << "->Val)"; } else if (Op->getOperator()->getName() == "imm") { OS << ".addZImm(" << Operands[i].second << "->Val)"; } else if (Op->getOperator()->getName() == "basicblock") { OS << ".addMBB(" << Operands[i].second << "->Val)"; } else { assert(0 && "Unknown value type!"); } } OS << ";\n"; break; case Pattern::Expander: { // Expander patterns emit one machine instr for each instruction in // the list of instructions expanded to. ListInit *Insts = P->getRecord()->getValueAsListInit("Result"); for (unsigned IN = 0, e = Insts->getSize(); IN != e; ++IN) { DagInit *DIInst = dynamic_cast<DagInit*>(Insts->getElement(IN)); if (!DIInst) P->error("Result list must contain instructions!"); Record *InstRec = DIInst->getNodeType(); Pattern *InstPat = getPattern(InstRec); if (!InstPat || InstPat->getPatternType() != Pattern::Instruction) P->error("Instruction list must contain Instruction patterns!"); bool hasResult = InstPat->getResult() != 0; if (InstPat->getNumArgs() != DIInst->getNumArgs()-hasResult) { P->error("Incorrect number of arguments specified for inst '" + InstPat->getRecord()->getName() + "' in result list!"); } // Start emission of the instruction... OS << " BuildMI(MBB, " << Target.getName() << "::" << InstRec->getName() << ", " << DIInst->getNumArgs()-hasResult; // Emit register result if necessary.. if (hasResult) { std::string ArgNameVal = getArgName(P, DIInst->getArgName(0), Operands); PrintExpanderOperand(DIInst->getArg(0), ArgNameVal, InstPat->getResultNode(), P, false, OS << ", "); } OS << ")"; for (unsigned i = hasResult, e = DIInst->getNumArgs(); i != e; ++i){ std::string ArgNameVal = getArgName(P, DIInst->getArgName(i), Operands); PrintExpanderOperand(DIInst->getArg(i), ArgNameVal, InstPat->getArg(i-hasResult), P, true, OS); } OS << ";\n"; } break; } default: assert(0 && "Reduction of this type of pattern not implemented!"); } OS << " Val = new ReducedValue_" << SlotName << "(" << Result<<");\n" << " break;\n" << " }\n"; } OS << " default: assert(0 && \"Unknown " << SlotName << " pattern!\");\n" << " }\n\n N->addValue(Val); // Do not ever recalculate this\n" << " return Val;\n}\n\n"; }
CodeGenIntrinsic::CodeGenIntrinsic(Record *R) { TheDef = R; std::string DefName = R->getName(); ModRef = WriteMem; isOverloaded = false; isCommutative = false; if (DefName.size() <= 4 || std::string(DefName.begin(), DefName.begin() + 4) != "int_") throw "Intrinsic '" + DefName + "' does not start with 'int_'!"; EnumName = std::string(DefName.begin()+4, DefName.end()); if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field. GCCBuiltinName = R->getValueAsString("GCCBuiltinName"); TargetPrefix = R->getValueAsString("TargetPrefix"); Name = R->getValueAsString("LLVMName"); if (Name == "") { // If an explicit name isn't specified, derive one from the DefName. Name = "llvm."; for (unsigned i = 0, e = EnumName.size(); i != e; ++i) Name += (EnumName[i] == '_') ? '.' : EnumName[i]; } else { // Verify it starts with "llvm.". if (Name.size() <= 5 || std::string(Name.begin(), Name.begin() + 5) != "llvm.") throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!"; } // If TargetPrefix is specified, make sure that Name starts with // "llvm.<targetprefix>.". if (!TargetPrefix.empty()) { if (Name.size() < 6+TargetPrefix.size() || std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size()) != (TargetPrefix + ".")) throw "Intrinsic '" + DefName + "' does not start with 'llvm." + TargetPrefix + ".'!"; } // Parse the list of return types. ListInit *TypeList = R->getValueAsListInit("RetTypes"); for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) { Record *TyEl = TypeList->getElementAsRecord(i); assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!"); MVT::SimpleValueType VT; if (TyEl->isSubClassOf("LLVMMatchType")) { VT = IS.RetVTs[TyEl->getValueAsInt("Number")]; // It only makes sense to use the extended and truncated vector element // variants with iAny types; otherwise, if the intrinsic is not // overloaded, all the types can be specified directly. assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") && !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) || VT == MVT::iAny) && "Expected iAny type"); } else VT = getValueType(TyEl->getValueAsDef("VT")); isOverloaded |= VT == MVT::iAny || VT == MVT::fAny || VT == MVT::iPTRAny; IS.RetVTs.push_back(VT); IS.RetTypeDefs.push_back(TyEl); } if (IS.RetVTs.size() == 0) throw "Intrinsic '"+DefName+"' needs at least a type for the ret value!"; // Parse the list of parameter types. TypeList = R->getValueAsListInit("ParamTypes"); for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) { Record *TyEl = TypeList->getElementAsRecord(i); assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!"); MVT::SimpleValueType VT; if (TyEl->isSubClassOf("LLVMMatchType")) { unsigned MatchTy = TyEl->getValueAsInt("Number"); if (MatchTy < IS.RetVTs.size()) VT = IS.RetVTs[MatchTy]; else VT = IS.ParamVTs[MatchTy - IS.RetVTs.size()]; // It only makes sense to use the extended and truncated vector element // variants with iAny types; otherwise, if the intrinsic is not // overloaded, all the types can be specified directly. assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") && !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) || VT == MVT::iAny) && "Expected iAny type"); } else VT = getValueType(TyEl->getValueAsDef("VT")); isOverloaded |= VT == MVT::iAny || VT == MVT::fAny || VT == MVT::iPTRAny; IS.ParamVTs.push_back(VT); IS.ParamTypeDefs.push_back(TyEl); } // Parse the intrinsic properties. ListInit *PropList = R->getValueAsListInit("Properties"); for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) { Record *Property = PropList->getElementAsRecord(i); assert(Property->isSubClassOf("IntrinsicProperty") && "Expected a property!"); if (Property->getName() == "IntrNoMem") ModRef = NoMem; else if (Property->getName() == "IntrReadArgMem") ModRef = ReadArgMem; else if (Property->getName() == "IntrReadMem") ModRef = ReadMem; else if (Property->getName() == "IntrWriteArgMem") ModRef = WriteArgMem; else if (Property->getName() == "IntrWriteMem") ModRef = WriteMem; else if (Property->getName() == "Commutative") isCommutative = true; else if (Property->isSubClassOf("NoCapture")) { unsigned ArgNo = Property->getValueAsInt("ArgNo"); ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture)); } else assert(0 && "Unknown property!"); } }