llvm.org GIT mirror llvm / 24e64df
Compute isFunctionLocal in MDNode ctor or via argument in new function getWhenValsUnresolved(). Document PFS argument to ParseValID() and ConvertGlobalOrMetadataValIDToValue(). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@93108 91177308-0d34-0410-b5e6-96231b3b80d8 Victor Hernandez 10 years ago
7 changed file(s) with 80 addition(s) and 29 deletion(s). Raw diff Collapse all Expand all
110110 enum MetadataCodes {
111111 METADATA_STRING = 1, // MDSTRING: [values]
112112 METADATA_NODE = 2, // MDNODE: [n x (type num, value num)]
113 METADATA_NAME = 3, // STRING: [values]
114 METADATA_NAMED_NODE = 4, // NAMEDMDNODE: [n x mdnodes]
115 METADATA_KIND = 5, // [n x [id, name]]
116 METADATA_ATTACHMENT = 6 // [m x [value, [n x [id, mdnode]]]
113 METADATA_FN_NODE = 3, // FN_MDNODE: [n x (type num, value num)]
114 METADATA_NAME = 4, // STRING: [values]
115 METADATA_NAMED_NODE = 5, // NAMEDMDNODE: [n x mdnodes]
116 METADATA_KIND = 6, // [n x [id, name]]
117 METADATA_ATTACHMENT = 7 // [m x [value, [n x [id, mdnode]]]
117118 };
118119 // The constants block (CONSTANTS_BLOCK_ID) describes emission for each
119120 // constant and maintains an implicit current type value.
111111 DestroyFlag = 1 << 2
112112 };
113113
114 // FunctionLocal enums.
115 enum FunctionLocalness {
116 FL_Unknown = -1,
117 FL_No = 0,
118 FL_Yes = 1
119 };
120
114121 // Replace each instance of F from the operand list of this node with T.
115122 void replaceOperand(MDNodeOperand *Op, Value *NewVal);
116123 ~MDNode();
118125 protected:
119126 explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
120127 bool isFunctionLocal);
128
129 static MDNode *getMDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
130 FunctionLocalness FL);
121131 public:
122132 // Constructors and destructors.
123 static MDNode *get(LLVMContext &Context, Value *const *Vals, unsigned NumVals,
124 bool isFunctionLocal = false);
133 static MDNode *get(LLVMContext &Context, Value *const *Vals,
134 unsigned NumVals);
135 // getWhenValsUnresolved - Construct MDNode determining function-localness
136 // from isFunctionLocal argument, not by analyzing Vals.
137 static MDNode *getWhenValsUnresolved(LLVMContext &Context, Value *const *Vals,
138 unsigned NumVals, bool isFunctionLocal);
125139
126140 /// getOperand - Return specified operand.
127141 Value *getOperand(unsigned i) const;
547547 ParseType(Ty, TyLoc) ||
548548 ParseToken(lltok::exclaim, "Expected '!' here") ||
549549 ParseToken(lltok::lbrace, "Expected '{' here") ||
550 ParseMDNodeVector(Elts, NULL, NULL) ||
550 ParseMDNodeVector(Elts, NULL) ||
551551 ParseToken(lltok::rbrace, "expected end of metadata node"))
552552 return true;
553553
18831883 /// ParseValID - Parse an abstract value that doesn't necessarily have a
18841884 /// type implied. For example, if we parse "4" we don't know what integer type
18851885 /// it has. The value will later be combined with its type and checked for
1886 /// sanity.
1886 /// sanity. PFS is used to convert function-local operands of metadata (since
1887 /// metadata operands are not just parsed here but also converted to values).
1888 /// PFS can be null when we are not parsing metadata values inside a function.
18871889 bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
18881890 ID.Loc = Lex.getLoc();
18891891 switch (Lex.getKind()) {
19101912
19111913 if (EatIfPresent(lltok::lbrace)) {
19121914 SmallVector Elts;
1913 bool isFunctionLocal = false;
1914 if (ParseMDNodeVector(Elts, PFS, &isFunctionLocal) ||
1915 if (ParseMDNodeVector(Elts, PFS) ||
19151916 ParseToken(lltok::rbrace, "expected end of metadata node"))
19161917 return true;
19171918
1918 ID.MDNodeVal = MDNode::get(Context, Elts.data(), Elts.size(),
1919 isFunctionLocal);
1919 ID.MDNodeVal = MDNode::get(Context, Elts.data(), Elts.size());
19201920 ID.Kind = ValID::t_MDNode;
19211921 return false;
19221922 }
24452445 }
24462446
24472447 /// ConvertGlobalOrMetadataValIDToValue - Apply a type to a ValID to get a fully
2448 /// resolved constant, metadata, or function-local value
2448 /// resolved constant, metadata, or function-local value. PFS is used to
2449 /// convert a function-local ValID and can be null when parsing a global or a
2450 /// non-function-local metadata ValID.
2451
24492452 bool LLParser::ConvertGlobalOrMetadataValIDToValue(const Type *Ty, ValID &ID,
24502453 Value *&V,
2451 PerFunctionState *PFS,
2452 bool *isFunctionLocal) {
2454 PerFunctionState *PFS) {
24532455 switch (ID.Kind) {
24542456 case ValID::t_MDNode:
24552457 if (!Ty->isMetadataTy())
24632465 return false;
24642466 case ValID::t_LocalID:
24652467 case ValID::t_LocalName:
2466 if (!PFS || !isFunctionLocal)
2468 if (!PFS)
24672469 return Error(ID.Loc, "invalid use of function-local name");
24682470 if (ConvertValIDToValue(Ty, ID, V, *PFS)) return true;
2469 *isFunctionLocal = true;
24702471 return false;
24712472 default:
24722473 Constant *C;
25262527 return false;
25272528 }
25282529 default:
2529 return ConvertGlobalOrMetadataValIDToValue(Ty, ID, V, &PFS, NULL);
2530 return ConvertGlobalOrMetadataValIDToValue(Ty, ID, V, &PFS);
25302531 }
25312532
25322533 return V == 0;
38573858 /// Element
38583859 /// ::= 'null' | TypeAndValue
38593860 bool LLParser::ParseMDNodeVector(SmallVectorImpl &Elts,
3860 PerFunctionState *PFS, bool *isFunctionLocal) {
3861 PerFunctionState *PFS) {
38613862 do {
38623863 // Null is a special case since it is typeless.
38633864 if (EatIfPresent(lltok::kw_null)) {
38693870 PATypeHolder Ty(Type::getVoidTy(Context));
38703871 ValID ID;
38713872 if (ParseType(Ty) || ParseValID(ID, PFS) ||
3872 ConvertGlobalOrMetadataValIDToValue(Ty, ID, V, PFS, isFunctionLocal))
3873 ConvertGlobalOrMetadataValIDToValue(Ty, ID, V, PFS))
38733874 return true;
38743875
38753876 Elts.push_back(V);
293293 bool ParseValID(ValID &ID, PerFunctionState *PFS = NULL);
294294 bool ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, Constant *&V);
295295 bool ConvertGlobalOrMetadataValIDToValue(const Type *Ty, ValID &ID,
296 Value *&V, PerFunctionState *PFS,
297 bool *isFunctionLocal);
296 Value *&V, PerFunctionState *PFS);
298297 bool ParseGlobalValue(const Type *Ty, Constant *&V);
299298 bool ParseGlobalTypeAndValue(Constant *&V);
300299 bool ParseGlobalValueVector(SmallVectorImpl &Elts);
301 bool ParseMDNodeVector(SmallVectorImpl &, PerFunctionState *PFS,
302 bool *isFunctionLocal);
300 bool ParseMDNodeVector(SmallVectorImpl &, PerFunctionState *PFS);
303301
304302 // Function Parsing.
305303 struct ArgInfo {
765765 continue;
766766 }
767767
768 bool IsFunctionLocal = false;
768769 // Read a record.
769770 Record.clear();
770771 switch (Stream.ReadRecord(Code, Record)) {
803804 MDValueList.AssignValue(V, NextValueNo++);
804805 break;
805806 }
807 case bitc::METADATA_FN_NODE:
808 IsFunctionLocal = true;
809 // fall-through
806810 case bitc::METADATA_NODE: {
807811 if (Record.empty() || Record.size() % 2 == 1)
808812 return Error("Invalid METADATA_NODE record");
818822 else
819823 Elts.push_back(NULL);
820824 }
821 Value *V = MDNode::get(Context, &Elts[0], Elts.size());
825 Value *V = MDNode::getWhenValsUnresolved(Context, &Elts[0], Elts.size(),
826 IsFunctionLocal);
827 IsFunctionLocal = false;
822828 MDValueList.AssignValue(V, NextValueNo++);
823829 break;
824830 }
483483 Record.push_back(0);
484484 }
485485 }
486 Stream.EmitRecord(bitc::METADATA_NODE, Record, 0);
486 unsigned MDCode = N->isFunctionLocal() ? bitc::METADATA_FN_NODE :
487 bitc::METADATA_NODE;
488 Stream.EmitRecord(MDCode, Record, 0);
487489 Record.clear();
488490 }
489491
127127 free(this);
128128 }
129129
130
131 MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals,
132 bool isFunctionLocal) {
130 MDNode *MDNode::getMDNode(LLVMContext &Context, Value *const *Vals,
131 unsigned NumVals, FunctionLocalness FL) {
133132 LLVMContextImpl *pImpl = Context.pImpl;
134133 FoldingSetNodeID ID;
135134 for (unsigned i = 0; i != NumVals; ++i)
138137 void *InsertPoint;
139138 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
140139 if (!N) {
140 bool isFunctionLocal = false;
141 switch (FL) {
142 case FL_Unknown:
143 for (unsigned i = 0; i != NumVals; ++i) {
144 Value *V = Vals[i];
145 if (!V) continue;
146 if (isa(V) || isa(V) || isa(V) ||
147 isa(V) && cast(V)->isFunctionLocal()) {
148 isFunctionLocal = true;
149 break;
150 }
151 }
152 break;
153 case FL_No:
154 isFunctionLocal = false;
155 break;
156 case FL_Yes:
157 isFunctionLocal = true;
158 break;
159 }
160
141161 // Coallocate space for the node and Operands together, then placement new.
142162 void *Ptr = malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
143163 N = new (Ptr) MDNode(Context, Vals, NumVals, isFunctionLocal);
146166 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
147167 }
148168 return N;
169 }
170
171 MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
172 return getMDNode(Context, Vals, NumVals, FL_Unknown);
173 }
174
175 MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, Value*const* Vals,
176 unsigned NumVals, bool isFunctionLocal) {
177 return getMDNode(Context, Vals, NumVals, isFunctionLocal ? FL_Yes : FL_No);
149178 }
150179
151180 /// getOperand - Return specified operand.