llvm.org GIT mirror llvm / ec9186b
PR9214: Convert Metadata API to use ArrayRef. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@129932 91177308-0d34-0410-b5e6-96231b3b80d8 Jay Foad 9 years ago
10 changed file(s) with 65 addition(s) and 73 deletion(s). Raw diff Collapse all Expand all
110110 void replaceOperand(MDNodeOperand *Op, Value *NewVal);
111111 ~MDNode();
112112
113 MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
114 bool isFunctionLocal);
115
116 static MDNode *getMDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
113 MDNode(LLVMContext &C, ArrayRef Vals, bool isFunctionLocal);
114
115 static MDNode *getMDNode(LLVMContext &C, ArrayRef Vals,
117116 FunctionLocalness FL, bool Insert = true);
118117 public:
119118 // Constructors and destructors.
120 static MDNode *get(LLVMContext &Context, ArrayRef V);
121 // FIXME: Eliminate this constructor form.
122 static MDNode *get(LLVMContext &Context, Value *const *Vals,
123 unsigned NumVals);
119 static MDNode *get(LLVMContext &Context, ArrayRef Vals);
124120 // getWhenValsUnresolved - Construct MDNode determining function-localness
125121 // from isFunctionLocal argument, not by analyzing Vals.
126 static MDNode *getWhenValsUnresolved(LLVMContext &Context, Value *const *Vals,
127 unsigned NumVals, bool isFunctionLocal);
122 static MDNode *getWhenValsUnresolved(LLVMContext &Context,
123 ArrayRef Vals,
124 bool isFunctionLocal);
128125
129 static MDNode *getIfExists(LLVMContext &Context, Value *const *Vals,
130 unsigned NumVals);
126 static MDNode *getIfExists(LLVMContext &Context, ArrayRef Vals);
131127
132128 /// getTemporary - Return a temporary MDNode, for use in constructing
133129 /// cyclic MDNode structures. A temporary MDNode is not uniqued,
134130 /// may be RAUW'd, and must be manually deleted with deleteTemporary.
135 static MDNode *getTemporary(LLVMContext &Context, Value *const *Vals,
136 unsigned NumVals);
131 static MDNode *getTemporary(LLVMContext &Context, ArrayRef Vals);
137132
138133 /// deleteTemporary - Deallocate a node created by getTemporary. The
139134 /// node must not have any users.
517517 // Give the temporary MDNode a tag. It doesn't matter what tag we
518518 // use here as long as DIType accepts it.
519519 Value *Elts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
520 MDNode *Node = MDNode::getTemporary(VMContext, Elts, 1);
520 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
521521 return DIType(Node);
522522 }
523523
531531 NULL,
532532 F
533533 };
534 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
534 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
535535 return DIType(Node);
536536 }
537537
538538 /// getOrCreateArray - Get a DIArray, create one if required.
539 DIArray DIBuilder::getOrCreateArray(Value *const *Elements, unsigned NumElements) {
539 DIArray DIBuilder::getOrCreateArray(Value *const *Elements,
540 unsigned NumElements) {
540541 if (NumElements == 0) {
541542 Value *Null = llvm::Constant::getNullValue(Type::getInt32Ty(VMContext));
542 return DIArray(MDNode::get(VMContext, &Null, 1));
543 return DIArray(MDNode::get(VMContext, Null));
543544 }
544 return DIArray(MDNode::get(VMContext, Elements, NumElements));
545 return DIArray(MDNode::get(VMContext,
546 ArrayRef(Elements, NumElements)));
545547 }
546548
547549 /// getOrCreateSubrange - Create a descriptor for a value range. This
777779 if (!DeclareFn)
778780 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
779781
780 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1), VarInfo };
782 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
781783 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
782784 }
783785
789791 if (!DeclareFn)
790792 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
791793
792 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1), VarInfo };
794 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
793795
794796 // If this block already has a terminator then insert this intrinsic
795797 // before the terminator.
808810 if (!ValueFn)
809811 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
810812
811 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
813 Value *Args[] = { MDNode::get(V->getContext(), V),
812814 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
813815 VarInfo };
814816 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
823825 if (!ValueFn)
824826 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
825827
826 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
828 Value *Args[] = { MDNode::get(V->getContext(), V),
827829 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
828830 VarInfo };
829831 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
513513 if (Result) return false;
514514
515515 // Otherwise, create MDNode forward reference.
516 MDNode *FwdNode = MDNode::getTemporary(Context, 0, 0);
516 MDNode *FwdNode = MDNode::getTemporary(Context, ArrayRef());
517517 ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc());
518518
519519 if (NumberedMetadata.size() <= MID)
571571 ParseToken(lltok::rbrace, "expected end of metadata node"))
572572 return true;
573573
574 MDNode *Init = MDNode::get(Context, Elts.data(), Elts.size());
574 MDNode *Init = MDNode::get(Context, Elts);
575575
576576 // See if this was forward referenced, if so, handle it.
577577 std::map, LocTy> >::iterator
24972497 ParseToken(lltok::rbrace, "expected end of metadata node"))
24982498 return true;
24992499
2500 ID.MDNodeVal = MDNode::get(Context, Elts.data(), Elts.size());
2500 ID.MDNodeVal = MDNode::get(Context, Elts);
25012501 ID.Kind = ValID::t_MDNode;
25022502 return false;
25032503 }
348348 }
349349
350350 // Create and return a placeholder, which will later be RAUW'd.
351 Value *V = MDNode::getTemporary(Context, 0, 0);
351 Value *V = MDNode::getTemporary(Context, ArrayRef());
352352 MDValuePtrs[Idx] = V;
353353 return V;
354354 }
842842 else
843843 Elts.push_back(NULL);
844844 }
845 Value *V = MDNode::getWhenValsUnresolved(Context,
846 Elts.data(), Elts.size(),
847 IsFunctionLocal);
845 Value *V = MDNode::getWhenValsUnresolved(Context, Elts, IsFunctionLocal);
848846 IsFunctionLocal = false;
849847 MDValueList.AssignValue(V, NextMDValueNo++);
850848 break;
103103 /// FindAllocaDbgDeclare - Finds the llvm.dbg.declare intrinsic describing the
104104 /// alloca 'V', if any.
105105 static DbgDeclareInst *FindAllocaDbgDeclare(Value *V) {
106 if (MDNode *DebugNode = MDNode::getIfExists(V->getContext(), &V, 1))
106 if (MDNode *DebugNode = MDNode::getIfExists(V->getContext(), V))
107107 for (Value::use_iterator UI = DebugNode->use_begin(),
108108 E = DebugNode->use_end(); UI != E; ++UI)
109109 if (DbgDeclareInst *DDI = dyn_cast(*UI))
3838 return VM[V] = const_cast(V);
3939
4040 // Create a dummy node in case we have a metadata cycle.
41 MDNode *Dummy = MDNode::getTemporary(V->getContext(), 0, 0);
41 MDNode *Dummy = MDNode::getTemporary(V->getContext(), ArrayRef());
4242 VM[V] = Dummy;
4343
4444 // Check all operands to see if any need to be remapped.
5353 Value *Op = MD->getOperand(i);
5454 Elts.push_back(Op ? MapValue(Op, VM, Flags) : 0);
5555 }
56 MDNode *NewMD = MDNode::get(V->getContext(), Elts.data(), Elts.size());
56 MDNode *NewMD = MDNode::get(V->getContext(), Elts);
5757 Dummy->replaceAllUsesWith(NewMD);
5858 VM[V] = NewMD;
5959 MDNode::deleteTemporary(Dummy);
542542
543543 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
544544 unsigned Count) {
545 return wrap(MDNode::get(*unwrap(C), unwrap(Vals, Count), Count));
545 return wrap(MDNode::get(*unwrap(C),
546 ArrayRef(unwrap(Vals, Count), Count)));
546547 }
547548
548549 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
108108 ConstantInt::get(Int32, getLine()), ConstantInt::get(Int32, getCol()),
109109 Scope, IA
110110 };
111 return MDNode::get(Ctx2, &Elts[0], 4);
111 return MDNode::get(Ctx2, Elts);
112112 }
113113
114114 /// getFromDILocation - Translate the DILocation quad into a DebugLoc.
8383 return reinterpret_cast(N+1)+Op;
8484 }
8585
86 MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
87 bool isFunctionLocal)
86 MDNode::MDNode(LLVMContext &C, ArrayRef Vals, bool isFunctionLocal)
8887 : Value(Type::getMetadataTy(C), Value::MDNodeVal) {
89 NumOperands = NumVals;
88 NumOperands = Vals.size();
9089
9190 if (isFunctionLocal)
9291 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
9392
9493 // Initialize the operand list, which is co-allocated on the end of the node.
94 unsigned i = 0;
9595 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
96 Op != E; ++Op, ++Vals)
97 new (Op) MDNodeOperand(*Vals, this);
96 Op != E; ++Op, ++i)
97 new (Op) MDNodeOperand(Vals[i], this);
9898 }
9999
100100
182182 (isa(V) && cast(V)->isFunctionLocal());
183183 }
184184
185 MDNode *MDNode::getMDNode(LLVMContext &Context, Value *const *Vals,
186 unsigned NumVals, FunctionLocalness FL,
187 bool Insert) {
185 MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef Vals,
186 FunctionLocalness FL, bool Insert) {
188187 LLVMContextImpl *pImpl = Context.pImpl;
189188
190189 // Add all the operand pointers. Note that we don't have to add the
192191 // Note that if the operands are later nulled out, the node will be
193192 // removed from the uniquing map.
194193 FoldingSetNodeID ID;
195 for (unsigned i = 0; i != NumVals; ++i)
194 for (unsigned i = 0; i != Vals.size(); ++i)
196195 ID.AddPointer(Vals[i]);
197196
198197 void *InsertPoint;
204203 bool isFunctionLocal = false;
205204 switch (FL) {
206205 case FL_Unknown:
207 for (unsigned i = 0; i != NumVals; ++i) {
206 for (unsigned i = 0; i != Vals.size(); ++i) {
208207 Value *V = Vals[i];
209208 if (!V) continue;
210209 if (isFunctionLocalValue(V)) {
222221 }
223222
224223 // Coallocate space for the node and Operands together, then placement new.
225 void *Ptr = malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
226 N = new (Ptr) MDNode(Context, Vals, NumVals, isFunctionLocal);
224 void *Ptr = malloc(sizeof(MDNode)+Vals.size()*sizeof(MDNodeOperand));
225 N = new (Ptr) MDNode(Context, Vals, isFunctionLocal);
227226
228227 // InsertPoint will have been set by the FindNodeOrInsertPos call.
229228 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
232231 }
233232
234233 MDNode *MDNode::get(LLVMContext &Context, ArrayRef Vals) {
235 return getMDNode(Context, Vals.data(), Vals.size(), FL_Unknown);
236 }
237 MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
238 return getMDNode(Context, Vals, NumVals, FL_Unknown);
239 }
240
241 MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, Value *const *Vals,
242 unsigned NumVals, bool isFunctionLocal) {
243 return getMDNode(Context, Vals, NumVals, isFunctionLocal ? FL_Yes : FL_No);
244 }
245
246 MDNode *MDNode::getIfExists(LLVMContext &Context, Value *const *Vals,
247 unsigned NumVals) {
248 return getMDNode(Context, Vals, NumVals, FL_Unknown, false);
249 }
250
251 MDNode *MDNode::getTemporary(LLVMContext &Context, Value *const *Vals,
252 unsigned NumVals) {
253 MDNode *N = (MDNode *)malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
254 N = new (N) MDNode(Context, Vals, NumVals, FL_No);
234 return getMDNode(Context, Vals, FL_Unknown);
235 }
236
237 MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context,
238 ArrayRef Vals,
239 bool isFunctionLocal) {
240 return getMDNode(Context, Vals, isFunctionLocal ? FL_Yes : FL_No);
241 }
242
243 MDNode *MDNode::getIfExists(LLVMContext &Context, ArrayRef Vals) {
244 return getMDNode(Context, Vals, FL_Unknown, false);
245 }
246
247 MDNode *MDNode::getTemporary(LLVMContext &Context, ArrayRef Vals) {
248 MDNode *N =
249 (MDNode *)malloc(sizeof(MDNode)+Vals.size()*sizeof(MDNodeOperand));
250 N = new (N) MDNode(Context, Vals, FL_No);
255251 N->setValueSubclassData(N->getSubclassDataFromValue() |
256252 NotUniquedBit);
257253 LeakDetector::addGarbageObject(N);
8686 V.push_back(CI);
8787 V.push_back(s2);
8888
89 MDNode *n1 = MDNode::get(Context, &V[0], 3);
89 MDNode *n1 = MDNode::get(Context, V);
9090 Value *const c1 = n1;
91 MDNode *n2 = MDNode::get(Context, &c1, 1);
92 MDNode *n3 = MDNode::get(Context, &V[0], 3);
91 MDNode *n2 = MDNode::get(Context, c1);
92 MDNode *n3 = MDNode::get(Context, V);
9393 EXPECT_NE(n1, n2);
9494 #ifdef ENABLE_MDNODE_UNIQUING
9595 EXPECT_EQ(n1, n3);
111111 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
112112
113113 Value *const V = I;
114 MDNode *n = MDNode::get(Context, &V, 1);
114 MDNode *n = MDNode::get(Context, V);
115115 WeakVH wvh = n;
116116
117117 EXPECT_EQ(n, wvh);
126126
127127 Value *const V = C;
128128 Value *const V2 = C2;
129 MDNode *n = MDNode::get(Context, &V, 1);
130 MDNode *n2 = MDNode::get(Context, &V2, 1);
129 MDNode *n = MDNode::get(Context, V);
130 MDNode *n2 = MDNode::get(Context, V2);
131131
132132 Module M("MyModule", Context);
133133 const char *Name = "llvm.NMD1";