blob: 0228aeb31f5de51eb79a7554324ab91d3554e28f [file] [log] [blame]
Devang Patel0a9f7b92009-07-28 21:49:47 +00001//===-- Metadata.cpp - Implement Metadata classes -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Metadata classes.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000014#include "llvm/IR/Metadata.h"
Chris Lattner37da0ad2009-12-28 08:24:16 +000015#include "LLVMContextImpl.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "SymbolTableListTraitsImpl.h"
17#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/SmallString.h"
20#include "llvm/ADT/StringMap.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000021#include "llvm/IR/Instruction.h"
22#include "llvm/IR/LLVMContext.h"
23#include "llvm/IR/Module.h"
Hal Finkel7b4ff932012-06-16 20:33:37 +000024#include "llvm/Support/ConstantRange.h"
Dan Gohman489b29b2010-08-20 22:02:26 +000025#include "llvm/Support/LeakDetector.h"
Chris Lattner37da0ad2009-12-28 08:24:16 +000026#include "llvm/Support/ValueHandle.h"
Devang Patel0a9f7b92009-07-28 21:49:47 +000027using namespace llvm;
28
29//===----------------------------------------------------------------------===//
Chris Lattnerb2a33b462009-10-19 07:10:59 +000030// MDString implementation.
Owen Anderson647e3012009-07-31 21:35:40 +000031//
Chris Lattner5e9cd432009-12-28 08:30:43 +000032
David Blaikie2d24e2a2011-12-20 02:50:00 +000033void MDString::anchor() { }
34
Bill Wendling3ecb4472012-04-10 20:12:16 +000035MDString::MDString(LLVMContext &C)
36 : Value(Type::getMetadataTy(C), Value::MDStringVal) {}
Chris Lattner5e9cd432009-12-28 08:30:43 +000037
Devang Patel49b63a12009-10-22 00:10:15 +000038MDString *MDString::get(LLVMContext &Context, StringRef Str) {
Owen Anderson647e3012009-07-31 21:35:40 +000039 LLVMContextImpl *pImpl = Context.pImpl;
Bill Wendling3ecb4472012-04-10 20:12:16 +000040 StringMapEntry<Value*> &Entry =
Owen Anderson647e3012009-07-31 21:35:40 +000041 pImpl->MDStringCache.GetOrCreateValue(Str);
Bill Wendling3ecb4472012-04-10 20:12:16 +000042 Value *&S = Entry.getValue();
43 if (!S) S = new MDString(Context);
44 S->setValueName(&Entry);
45 return cast<MDString>(S);
Owen Anderson647e3012009-07-31 21:35:40 +000046}
47
48//===----------------------------------------------------------------------===//
Chris Lattner5d0cacd2009-12-31 01:22:29 +000049// MDNodeOperand implementation.
Chris Lattnerc5e08a92009-12-28 07:41:54 +000050//
51
Chris Lattner5d0cacd2009-12-31 01:22:29 +000052// Use CallbackVH to hold MDNode operands.
Chris Lattnerc5e08a92009-12-28 07:41:54 +000053namespace llvm {
Chris Lattner5d0cacd2009-12-31 01:22:29 +000054class MDNodeOperand : public CallbackVH {
Bill Wendling69b2c712012-04-08 10:20:49 +000055 MDNode *getParent() {
56 MDNodeOperand *Cur = this;
57
58 while (Cur->getValPtrInt() != 1)
59 --Cur;
60
61 assert(Cur->getValPtrInt() == 1 &&
62 "Couldn't find the beginning of the operand list!");
63 return reinterpret_cast<MDNode*>(Cur) - 1;
64 }
65
Chris Lattnerc5e08a92009-12-28 07:41:54 +000066public:
Bill Wendling69b2c712012-04-08 10:20:49 +000067 MDNodeOperand(Value *V) : CallbackVH(V) {}
Chris Lattner5d0cacd2009-12-31 01:22:29 +000068 ~MDNodeOperand() {}
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +000069
Bill Wendling48663632012-04-26 00:38:42 +000070 void set(Value *V) {
71 unsigned IsFirst = this->getValPtrInt();
72 this->setValPtr(V);
73 this->setAsFirstOperand(IsFirst);
74 }
Bill Wendling69b2c712012-04-08 10:20:49 +000075
76 /// setAsFirstOperand - Accessor method to mark the operand as the first in
77 /// the list.
78 void setAsFirstOperand(unsigned V) { this->setValPtrInt(V); }
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +000079
Chris Lattnerc5e08a92009-12-28 07:41:54 +000080 virtual void deleted();
81 virtual void allUsesReplacedWith(Value *NV);
82};
83} // end namespace llvm.
84
85
Chris Lattner5d0cacd2009-12-31 01:22:29 +000086void MDNodeOperand::deleted() {
Bill Wendling69b2c712012-04-08 10:20:49 +000087 getParent()->replaceOperand(this, 0);
Chris Lattnerc5e08a92009-12-28 07:41:54 +000088}
89
Chris Lattner5d0cacd2009-12-31 01:22:29 +000090void MDNodeOperand::allUsesReplacedWith(Value *NV) {
Bill Wendling69b2c712012-04-08 10:20:49 +000091 getParent()->replaceOperand(this, NV);
Chris Lattnerc5e08a92009-12-28 07:41:54 +000092}
93
Chris Lattnerc5e08a92009-12-28 07:41:54 +000094//===----------------------------------------------------------------------===//
Chris Lattnerb2a33b462009-10-19 07:10:59 +000095// MDNode implementation.
Devang Patel0a9f7b92009-07-28 21:49:47 +000096//
Chris Lattnerc5e08a92009-12-28 07:41:54 +000097
Chris Lattner5d0cacd2009-12-31 01:22:29 +000098/// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on
Chris Lattnerb76359e2009-12-31 01:05:46 +000099/// the end of the MDNode.
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000100static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
Dan Gohmanac809752010-07-13 19:33:27 +0000101 // Use <= instead of < to permit a one-past-the-end address.
102 assert(Op <= N->getNumOperands() && "Invalid operand number");
Bill Wendling48663632012-04-26 00:38:42 +0000103 return reinterpret_cast<MDNodeOperand*>(N + 1) + Op;
Chris Lattnerf2410182009-12-28 09:12:35 +0000104}
105
Eric Christopherf32a9602012-02-15 09:09:29 +0000106void MDNode::replaceOperandWith(unsigned i, Value *Val) {
107 MDNodeOperand *Op = getOperandPtr(this, i);
108 replaceOperand(Op, Val);
109}
110
Jay Foadec9186b2011-04-21 19:59:31 +0000111MDNode::MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal)
Devang Patelbc5201f2010-01-22 22:52:10 +0000112: Value(Type::getMetadataTy(C), Value::MDNodeVal) {
Jay Foadec9186b2011-04-21 19:59:31 +0000113 NumOperands = Vals.size();
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000114
Victor Hernandez5d301622009-12-18 20:09:14 +0000115 if (isFunctionLocal)
Chris Lattnercafe9bb2009-12-29 02:14:09 +0000116 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
Chris Lattnerb76359e2009-12-31 01:05:46 +0000117
118 // Initialize the operand list, which is co-allocated on the end of the node.
Jay Foadec9186b2011-04-21 19:59:31 +0000119 unsigned i = 0;
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000120 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Bill Wendling69b2c712012-04-08 10:20:49 +0000121 Op != E; ++Op, ++i) {
122 new (Op) MDNodeOperand(Vals[i]);
123
124 // Mark the first MDNodeOperand as being the first in the list of operands.
125 if (i == 0)
126 Op->setAsFirstOperand(1);
127 }
Devang Patel0a9f7b92009-07-28 21:49:47 +0000128}
129
Chris Lattnerb76359e2009-12-31 01:05:46 +0000130/// ~MDNode - Destroy MDNode.
131MDNode::~MDNode() {
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000132 assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
Chris Lattnerb76359e2009-12-31 01:05:46 +0000133 "Not being destroyed through destroy()?");
Jeffrey Yasskin6f555ca2010-03-13 01:26:15 +0000134 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
135 if (isNotUniqued()) {
136 pImpl->NonUniquedMDNodes.erase(this);
137 } else {
Chris Lattnerb76359e2009-12-31 01:05:46 +0000138 pImpl->MDNodeSet.RemoveNode(this);
139 }
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000140
Chris Lattnerb76359e2009-12-31 01:05:46 +0000141 // Destroy the operands.
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000142 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Chris Lattnerb76359e2009-12-31 01:05:46 +0000143 Op != E; ++Op)
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000144 Op->~MDNodeOperand();
Chris Lattnerb76359e2009-12-31 01:05:46 +0000145}
146
Victor Hernandez8fffff52010-01-20 04:45:57 +0000147static const Function *getFunctionForValue(Value *V) {
148 if (!V) return NULL;
Duncan Sands203f7cb2010-05-04 12:43:36 +0000149 if (Instruction *I = dyn_cast<Instruction>(V)) {
150 BasicBlock *BB = I->getParent();
151 return BB ? BB->getParent() : 0;
152 }
Chris Lattnerd3a6d902010-01-21 21:01:47 +0000153 if (Argument *A = dyn_cast<Argument>(V))
154 return A->getParent();
Duncan Sands203f7cb2010-05-04 12:43:36 +0000155 if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
156 return BB->getParent();
157 if (MDNode *MD = dyn_cast<MDNode>(V))
158 return MD->getFunction();
Victor Hernandez8fffff52010-01-20 04:45:57 +0000159 return NULL;
160}
161
Victor Hernandeze05f66e2010-01-14 20:12:34 +0000162#ifndef NDEBUG
Victor Hernandez8fffff52010-01-20 04:45:57 +0000163static const Function *assertLocalFunction(const MDNode *N) {
Chris Lattnerd3a6d902010-01-21 21:01:47 +0000164 if (!N->isFunctionLocal()) return 0;
Victor Hernandeze05f66e2010-01-14 20:12:34 +0000165
Devang Patel6db23892010-07-06 21:05:17 +0000166 // FIXME: This does not handle cyclic function local metadata.
Chris Lattnerd3a6d902010-01-21 21:01:47 +0000167 const Function *F = 0, *NewF = 0;
Victor Hernandezc7650b42010-01-14 01:45:14 +0000168 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Victor Hernandez8fffff52010-01-20 04:45:57 +0000169 if (Value *V = N->getOperand(i)) {
Chris Lattnerd3a6d902010-01-21 21:01:47 +0000170 if (MDNode *MD = dyn_cast<MDNode>(V))
171 NewF = assertLocalFunction(MD);
172 else
173 NewF = getFunctionForValue(V);
Victor Hernandez54630e12010-01-18 20:36:54 +0000174 }
Chris Lattnerd3a6d902010-01-21 21:01:47 +0000175 if (F == 0)
176 F = NewF;
177 else
178 assert((NewF == 0 || F == NewF) &&"inconsistent function-local metadata");
Victor Hernandezc7650b42010-01-14 01:45:14 +0000179 }
Victor Hernandezc7650b42010-01-14 01:45:14 +0000180 return F;
181}
Victor Hernandez54630e12010-01-18 20:36:54 +0000182#endif
Victor Hernandezc7650b42010-01-14 01:45:14 +0000183
184// getFunction - If this metadata is function-local and recursively has a
185// function-local operand, return the first such operand's parent function.
Victor Hernandez6cead782010-01-18 22:55:08 +0000186// Otherwise, return null. getFunction() should not be used for performance-
187// critical code because it recursively visits all the MDNode's operands.
Victor Hernandez8fffff52010-01-20 04:45:57 +0000188const Function *MDNode::getFunction() const {
Victor Hernandez54630e12010-01-18 20:36:54 +0000189#ifndef NDEBUG
190 return assertLocalFunction(this);
David Blaikie4d6ccb52012-01-20 21:51:11 +0000191#else
Victor Hernandezc7650b42010-01-14 01:45:14 +0000192 if (!isFunctionLocal()) return NULL;
Duncan Sandsd29f5282010-05-04 14:25:42 +0000193 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
194 if (const Function *F = getFunctionForValue(getOperand(i)))
195 return F;
Victor Hernandez54630e12010-01-18 20:36:54 +0000196 return NULL;
David Blaikie4d6ccb52012-01-20 21:51:11 +0000197#endif
Victor Hernandezc7650b42010-01-14 01:45:14 +0000198}
199
Chris Lattnerb76359e2009-12-31 01:05:46 +0000200// destroy - Delete this node. Only when there are no uses.
201void MDNode::destroy() {
202 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
Eric Christophera61d3a92012-08-14 01:09:10 +0000203 // Placement delete, then free the memory.
Chris Lattnerb76359e2009-12-31 01:05:46 +0000204 this->~MDNode();
205 free(this);
206}
207
Chris Lattnerf3f325b2010-04-28 20:16:12 +0000208/// isFunctionLocalValue - Return true if this is a value that would require a
209/// function-local MDNode.
210static bool isFunctionLocalValue(Value *V) {
211 return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
212 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal());
213}
214
Jay Foadec9186b2011-04-21 19:59:31 +0000215MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> Vals,
216 FunctionLocalness FL, bool Insert) {
Devang Patel5f4ac842009-09-03 01:39:20 +0000217 LLVMContextImpl *pImpl = Context.pImpl;
Dan Gohman7548fb32010-08-24 23:21:12 +0000218
219 // Add all the operand pointers. Note that we don't have to add the
220 // isFunctionLocal bit because that's implied by the operands.
Dan Gohman56b092e2010-08-30 21:18:41 +0000221 // Note that if the operands are later nulled out, the node will be
222 // removed from the uniquing map.
Dan Gohman7548fb32010-08-24 23:21:12 +0000223 FoldingSetNodeID ID;
Jay Foadec9186b2011-04-21 19:59:31 +0000224 for (unsigned i = 0; i != Vals.size(); ++i)
Dan Gohman7548fb32010-08-24 23:21:12 +0000225 ID.AddPointer(Vals[i]);
226
227 void *InsertPoint;
Duncan Sands4000afe2012-03-31 08:20:11 +0000228 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
229
230 if (N || !Insert)
Dan Gohman7548fb32010-08-24 23:21:12 +0000231 return N;
Duncan Sands4000afe2012-03-31 08:20:11 +0000232
Victor Hernandeze685f232010-01-26 02:36:35 +0000233 bool isFunctionLocal = false;
234 switch (FL) {
235 case FL_Unknown:
Jay Foadec9186b2011-04-21 19:59:31 +0000236 for (unsigned i = 0; i != Vals.size(); ++i) {
Victor Hernandeze685f232010-01-26 02:36:35 +0000237 Value *V = Vals[i];
238 if (!V) continue;
Chris Lattnerf3f325b2010-04-28 20:16:12 +0000239 if (isFunctionLocalValue(V)) {
Victor Hernandeze685f232010-01-26 02:36:35 +0000240 isFunctionLocal = true;
241 break;
Victor Hernandez24e64df2010-01-10 07:14:18 +0000242 }
Victor Hernandez24e64df2010-01-10 07:14:18 +0000243 }
Victor Hernandeze685f232010-01-26 02:36:35 +0000244 break;
245 case FL_No:
246 isFunctionLocal = false;
247 break;
248 case FL_Yes:
249 isFunctionLocal = true;
250 break;
Devang Patel5f4ac842009-09-03 01:39:20 +0000251 }
Victor Hernandeze685f232010-01-26 02:36:35 +0000252
253 // Coallocate space for the node and Operands together, then placement new.
Bill Wendling48663632012-04-26 00:38:42 +0000254 void *Ptr = malloc(sizeof(MDNode) + Vals.size() * sizeof(MDNodeOperand));
Jay Foadec9186b2011-04-21 19:59:31 +0000255 N = new (Ptr) MDNode(Context, Vals, isFunctionLocal);
Victor Hernandeze685f232010-01-26 02:36:35 +0000256
Benjamin Kramer611afc02012-04-11 14:06:54 +0000257 // Cache the operand hash.
258 N->Hash = ID.ComputeHash();
259
Victor Hernandeze685f232010-01-26 02:36:35 +0000260 // InsertPoint will have been set by the FindNodeOrInsertPos call.
261 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
262
Devang Patel5f4ac842009-09-03 01:39:20 +0000263 return N;
Owen Anderson647e3012009-07-31 21:35:40 +0000264}
265
Devang Patel566bd122011-03-04 01:20:33 +0000266MDNode *MDNode::get(LLVMContext &Context, ArrayRef<Value*> Vals) {
Jay Foadec9186b2011-04-21 19:59:31 +0000267 return getMDNode(Context, Vals, FL_Unknown);
Victor Hernandez24e64df2010-01-10 07:14:18 +0000268}
269
Jay Foadec9186b2011-04-21 19:59:31 +0000270MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context,
271 ArrayRef<Value*> Vals,
272 bool isFunctionLocal) {
273 return getMDNode(Context, Vals, isFunctionLocal ? FL_Yes : FL_No);
Victor Hernandez24e64df2010-01-10 07:14:18 +0000274}
275
Jay Foadec9186b2011-04-21 19:59:31 +0000276MDNode *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals) {
277 return getMDNode(Context, Vals, FL_Unknown, false);
Victor Hernandeze685f232010-01-26 02:36:35 +0000278}
279
Jay Foadec9186b2011-04-21 19:59:31 +0000280MDNode *MDNode::getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals) {
281 MDNode *N =
Bill Wendling48663632012-04-26 00:38:42 +0000282 (MDNode *)malloc(sizeof(MDNode) + Vals.size() * sizeof(MDNodeOperand));
Jay Foadec9186b2011-04-21 19:59:31 +0000283 N = new (N) MDNode(Context, Vals, FL_No);
Dan Gohman489b29b2010-08-20 22:02:26 +0000284 N->setValueSubclassData(N->getSubclassDataFromValue() |
285 NotUniquedBit);
286 LeakDetector::addGarbageObject(N);
287 return N;
288}
289
290void MDNode::deleteTemporary(MDNode *N) {
291 assert(N->use_empty() && "Temporary MDNode has uses!");
Dan Gohman990bdd52010-08-21 02:52:29 +0000292 assert(!N->getContext().pImpl->MDNodeSet.RemoveNode(N) &&
Dan Gohman97cfa7d2010-08-23 22:32:05 +0000293 "Deleting a non-temporary uniqued node!");
294 assert(!N->getContext().pImpl->NonUniquedMDNodes.erase(N) &&
295 "Deleting a non-temporary non-uniqued node!");
Dan Gohman489b29b2010-08-20 22:02:26 +0000296 assert((N->getSubclassDataFromValue() & NotUniquedBit) &&
297 "Temporary MDNode does not have NotUniquedBit set!");
298 assert((N->getSubclassDataFromValue() & DestroyFlag) == 0 &&
Dan Gohman990bdd52010-08-21 02:52:29 +0000299 "Temporary MDNode has DestroyFlag set!");
Dan Gohman489b29b2010-08-20 22:02:26 +0000300 LeakDetector::removeGarbageObject(N);
Benjamin Kramer9dd3e922010-08-21 15:07:23 +0000301 N->destroy();
Dan Gohman489b29b2010-08-20 22:02:26 +0000302}
303
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000304/// getOperand - Return specified operand.
305Value *MDNode::getOperand(unsigned i) const {
David Blaikieeda774a2013-03-08 21:08:23 +0000306 assert(i < getNumOperands() && "Invalid operand number");
Chris Lattnerb76359e2009-12-31 01:05:46 +0000307 return *getOperandPtr(const_cast<MDNode*>(this), i);
308}
309
Chris Lattnerf2410182009-12-28 09:12:35 +0000310void MDNode::Profile(FoldingSetNodeID &ID) const {
Dan Gohman7548fb32010-08-24 23:21:12 +0000311 // Add all the operand pointers. Note that we don't have to add the
312 // isFunctionLocal bit because that's implied by the operands.
Dan Gohman56b092e2010-08-30 21:18:41 +0000313 // Note that if the operands are later nulled out, the node will be
314 // removed from the uniquing map.
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000315 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
316 ID.AddPointer(getOperand(i));
Devang Patel4771e162009-08-03 22:51:10 +0000317}
Devang Patelab67e702009-08-11 18:01:24 +0000318
Jeffrey Yasskin6f555ca2010-03-13 01:26:15 +0000319void MDNode::setIsNotUniqued() {
320 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
321 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
322 pImpl->NonUniquedMDNodes.insert(this);
Devang Patel06fdacc2010-02-18 20:53:16 +0000323}
Chris Lattnerf2410182009-12-28 09:12:35 +0000324
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000325// Replace value from this node's operand list.
326void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
Chris Lattnerdf583892009-12-28 09:32:10 +0000327 Value *From = *Op;
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000328
Chris Lattnerf3f325b2010-04-28 20:16:12 +0000329 // If is possible that someone did GV->RAUW(inst), replacing a global variable
330 // with an instruction or some other function-local object. If this is a
331 // non-function-local MDNode, it can't point to a function-local object.
332 // Handle this case by implicitly dropping the MDNode reference to null.
Duncan Sands203f7cb2010-05-04 12:43:36 +0000333 // Likewise if the MDNode is function-local but for a different function.
334 if (To && isFunctionLocalValue(To)) {
335 if (!isFunctionLocal())
336 To = 0;
337 else {
338 const Function *F = getFunction();
339 const Function *FV = getFunctionForValue(To);
340 // Metadata can be function-local without having an associated function.
341 // So only consider functions to have changed if non-null.
342 if (F && FV && F != FV)
343 To = 0;
344 }
345 }
Chris Lattnerf3f325b2010-04-28 20:16:12 +0000346
Chris Lattnerdf583892009-12-28 09:32:10 +0000347 if (From == To)
Devang Patel5f4ac842009-09-03 01:39:20 +0000348 return;
Devang Patel5f4ac842009-09-03 01:39:20 +0000349
Chris Lattner54a1f9f2009-12-30 21:42:11 +0000350 // Update the operand.
Chris Lattnerb76359e2009-12-31 01:05:46 +0000351 Op->set(To);
Chris Lattner54a1f9f2009-12-30 21:42:11 +0000352
353 // If this node is already not being uniqued (because one of the operands
354 // already went to null), then there is nothing else to do here.
355 if (isNotUniqued()) return;
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000356
Chris Lattner49642572009-12-28 09:24:53 +0000357 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
358
359 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
360 // this node to remove it, so we don't care what state the operands are in.
361 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattnerdf583892009-12-28 09:32:10 +0000362
Chris Lattner54a1f9f2009-12-30 21:42:11 +0000363 // If we are dropping an argument to null, we choose to not unique the MDNode
364 // anymore. This commonly occurs during destruction, and uniquing these
Dan Gohman56b092e2010-08-30 21:18:41 +0000365 // brings little reuse. Also, this means we don't need to include
366 // isFunctionLocal bits in FoldingSetNodeIDs for MDNodes.
Chris Lattner54a1f9f2009-12-30 21:42:11 +0000367 if (To == 0) {
368 setIsNotUniqued();
369 return;
370 }
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000371
Chris Lattner54a1f9f2009-12-30 21:42:11 +0000372 // Now that the node is out of the folding set, get ready to reinsert it.
373 // First, check to see if another node with the same operands already exists
Dan Gohmanf53458f2010-09-28 22:07:19 +0000374 // in the set. If so, then this node is redundant.
Devang Patel5f4ac842009-09-03 01:39:20 +0000375 FoldingSetNodeID ID;
376 Profile(ID);
Devang Patel5f4ac842009-09-03 01:39:20 +0000377 void *InsertPoint;
Dan Gohmanf375ebe2010-09-28 21:02:55 +0000378 if (MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)) {
Dan Gohmanf53458f2010-09-28 22:07:19 +0000379 replaceAllUsesWith(N);
380 destroy();
381 return;
Devang Patel5f4ac842009-09-03 01:39:20 +0000382 }
383
Benjamin Kramer611afc02012-04-11 14:06:54 +0000384 // Cache the operand hash.
385 Hash = ID.ComputeHash();
Chris Lattner49642572009-12-28 09:24:53 +0000386 // InsertPoint will have been set by the FindNodeOrInsertPos call.
387 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Dan Gohman5781f3e2010-09-14 01:37:57 +0000388
389 // If this MDValue was previously function-local but no longer is, clear
390 // its function-local flag.
391 if (isFunctionLocal() && !isFunctionLocalValue(To)) {
392 bool isStillFunctionLocal = false;
393 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
394 Value *V = getOperand(i);
395 if (!V) continue;
396 if (isFunctionLocalValue(V)) {
397 isStillFunctionLocal = true;
398 break;
399 }
400 }
401 if (!isStillFunctionLocal)
402 setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit);
403 }
Devang Patel5f4ac842009-09-03 01:39:20 +0000404}
405
Hal Finkel7b4ff932012-06-16 20:33:37 +0000406MDNode *MDNode::getMostGenericTBAA(MDNode *A, MDNode *B) {
407 if (!A || !B)
408 return NULL;
409
410 if (A == B)
411 return A;
412
413 SmallVector<MDNode *, 4> PathA;
414 MDNode *T = A;
415 while (T) {
416 PathA.push_back(T);
417 T = T->getNumOperands() >= 2 ? cast_or_null<MDNode>(T->getOperand(1)) : 0;
418 }
419
420 SmallVector<MDNode *, 4> PathB;
421 T = B;
422 while (T) {
423 PathB.push_back(T);
424 T = T->getNumOperands() >= 2 ? cast_or_null<MDNode>(T->getOperand(1)) : 0;
425 }
426
427 int IA = PathA.size() - 1;
428 int IB = PathB.size() - 1;
429
430 MDNode *Ret = 0;
431 while (IA >= 0 && IB >=0) {
432 if (PathA[IA] == PathB[IB])
433 Ret = PathA[IA];
434 else
435 break;
436 --IA;
437 --IB;
438 }
439 return Ret;
440}
441
442MDNode *MDNode::getMostGenericFPMath(MDNode *A, MDNode *B) {
443 if (!A || !B)
444 return NULL;
445
446 APFloat AVal = cast<ConstantFP>(A->getOperand(0))->getValueAPF();
447 APFloat BVal = cast<ConstantFP>(B->getOperand(0))->getValueAPF();
448 if (AVal.compare(BVal) == APFloat::cmpLessThan)
449 return A;
450 return B;
451}
452
453static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
454 return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
455}
456
457static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) {
458 return !A.intersectWith(B).isEmptySet() || isContiguous(A, B);
459}
460
461static bool tryMergeRange(SmallVector<Value*, 4> &EndPoints, ConstantInt *Low,
462 ConstantInt *High) {
463 ConstantRange NewRange(Low->getValue(), High->getValue());
464 unsigned Size = EndPoints.size();
465 APInt LB = cast<ConstantInt>(EndPoints[Size - 2])->getValue();
466 APInt LE = cast<ConstantInt>(EndPoints[Size - 1])->getValue();
467 ConstantRange LastRange(LB, LE);
468 if (canBeMerged(NewRange, LastRange)) {
469 ConstantRange Union = LastRange.unionWith(NewRange);
470 Type *Ty = High->getType();
471 EndPoints[Size - 2] = ConstantInt::get(Ty, Union.getLower());
472 EndPoints[Size - 1] = ConstantInt::get(Ty, Union.getUpper());
473 return true;
474 }
475 return false;
476}
477
478static void addRange(SmallVector<Value*, 4> &EndPoints, ConstantInt *Low,
479 ConstantInt *High) {
480 if (!EndPoints.empty())
481 if (tryMergeRange(EndPoints, Low, High))
482 return;
483
484 EndPoints.push_back(Low);
485 EndPoints.push_back(High);
486}
487
488MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) {
489 // Given two ranges, we want to compute the union of the ranges. This
490 // is slightly complitade by having to combine the intervals and merge
491 // the ones that overlap.
492
493 if (!A || !B)
494 return NULL;
495
496 if (A == B)
497 return A;
498
499 // First, walk both lists in older of the lower boundary of each interval.
500 // At each step, try to merge the new interval to the last one we adedd.
501 SmallVector<Value*, 4> EndPoints;
502 int AI = 0;
503 int BI = 0;
504 int AN = A->getNumOperands() / 2;
505 int BN = B->getNumOperands() / 2;
506 while (AI < AN && BI < BN) {
507 ConstantInt *ALow = cast<ConstantInt>(A->getOperand(2 * AI));
508 ConstantInt *BLow = cast<ConstantInt>(B->getOperand(2 * BI));
509
510 if (ALow->getValue().slt(BLow->getValue())) {
511 addRange(EndPoints, ALow, cast<ConstantInt>(A->getOperand(2 * AI + 1)));
512 ++AI;
513 } else {
514 addRange(EndPoints, BLow, cast<ConstantInt>(B->getOperand(2 * BI + 1)));
515 ++BI;
516 }
517 }
518 while (AI < AN) {
519 addRange(EndPoints, cast<ConstantInt>(A->getOperand(2 * AI)),
520 cast<ConstantInt>(A->getOperand(2 * AI + 1)));
521 ++AI;
522 }
523 while (BI < BN) {
524 addRange(EndPoints, cast<ConstantInt>(B->getOperand(2 * BI)),
525 cast<ConstantInt>(B->getOperand(2 * BI + 1)));
526 ++BI;
527 }
528
529 // If we have more than 2 ranges (4 endpoints) we have to try to merge
530 // the last and first ones.
531 unsigned Size = EndPoints.size();
532 if (Size > 4) {
533 ConstantInt *FB = cast<ConstantInt>(EndPoints[0]);
534 ConstantInt *FE = cast<ConstantInt>(EndPoints[1]);
535 if (tryMergeRange(EndPoints, FB, FE)) {
536 for (unsigned i = 0; i < Size - 2; ++i) {
537 EndPoints[i] = EndPoints[i + 2];
538 }
539 EndPoints.resize(Size - 2);
540 }
541 }
542
543 // If in the end we have a single range, it is possible that it is now the
544 // full range. Just drop the metadata in that case.
545 if (EndPoints.size() == 2) {
546 ConstantRange Range(cast<ConstantInt>(EndPoints[0])->getValue(),
547 cast<ConstantInt>(EndPoints[1])->getValue());
548 if (Range.isFullSet())
549 return NULL;
550 }
551
552 return MDNode::get(A->getContext(), EndPoints);
553}
554
Devang Patelf457d132009-07-29 00:33:07 +0000555//===----------------------------------------------------------------------===//
Chris Lattnerb2a33b462009-10-19 07:10:59 +0000556// NamedMDNode implementation.
Devang Patelf457d132009-07-29 00:33:07 +0000557//
Devang Patel26028f22010-01-12 18:34:06 +0000558
Dan Gohmand9c86dc2010-07-21 18:01:42 +0000559static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
560 return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands;
Chris Lattner57109692009-12-28 08:07:14 +0000561}
562
Dan Gohman17aa92c2010-07-21 23:38:33 +0000563NamedMDNode::NamedMDNode(const Twine &N)
564 : Name(N.str()), Parent(0),
565 Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {
Devang Patelab67e702009-08-11 18:01:24 +0000566}
567
Chris Lattner57109692009-12-28 08:07:14 +0000568NamedMDNode::~NamedMDNode() {
569 dropAllReferences();
570 delete &getNMDOps(Operands);
571}
572
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000573/// getNumOperands - Return number of NamedMDNode operands.
574unsigned NamedMDNode::getNumOperands() const {
Chris Lattner57109692009-12-28 08:07:14 +0000575 return (unsigned)getNMDOps(Operands).size();
576}
577
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000578/// getOperand - Return specified operand.
Devang Patel3e30c2a2010-01-05 20:41:31 +0000579MDNode *NamedMDNode::getOperand(unsigned i) const {
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000580 assert(i < getNumOperands() && "Invalid Operand number!");
Dan Gohman872814a2010-07-21 18:54:18 +0000581 return dyn_cast<MDNode>(&*getNMDOps(Operands)[i]);
Chris Lattner57109692009-12-28 08:07:14 +0000582}
583
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000584/// addOperand - Add metadata Operand.
Devang Patel3e30c2a2010-01-05 20:41:31 +0000585void NamedMDNode::addOperand(MDNode *M) {
Dan Gohman5781f3e2010-09-14 01:37:57 +0000586 assert(!M->isFunctionLocal() &&
587 "NamedMDNode operands must not be function-local!");
Dan Gohmand9c86dc2010-07-21 18:01:42 +0000588 getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
Chris Lattner57109692009-12-28 08:07:14 +0000589}
590
Devang Patela82f8832009-08-03 06:19:01 +0000591/// eraseFromParent - Drop all references and remove the node from parent
592/// module.
593void NamedMDNode::eraseFromParent() {
Dan Gohman17aa92c2010-07-21 23:38:33 +0000594 getParent()->eraseNamedMetadata(this);
Devang Patela82f8832009-08-03 06:19:01 +0000595}
596
597/// dropAllReferences - Remove all uses and clear node vector.
598void NamedMDNode::dropAllReferences() {
Chris Lattner57109692009-12-28 08:07:14 +0000599 getNMDOps(Operands).clear();
Devang Patela82f8832009-08-03 06:19:01 +0000600}
601
Devang Patel0386f012010-01-07 19:39:36 +0000602/// getName - Return a constant reference to this named metadata's name.
603StringRef NamedMDNode::getName() const {
604 return StringRef(Name);
605}
Devang Patel937b1e92009-09-16 18:09:00 +0000606
607//===----------------------------------------------------------------------===//
Chris Lattner3990b122009-12-28 23:41:32 +0000608// Instruction Metadata method implementations.
609//
610
Benjamin Kramer85dadec2011-12-06 11:50:26 +0000611void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
Chris Lattner3990b122009-12-28 23:41:32 +0000612 if (Node == 0 && !hasMetadata()) return;
Chris Lattner08113472009-12-29 09:01:33 +0000613 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner3990b122009-12-28 23:41:32 +0000614}
615
Benjamin Kramer85dadec2011-12-06 11:50:26 +0000616MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
Chris Lattner08113472009-12-29 09:01:33 +0000617 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner3990b122009-12-28 23:41:32 +0000618}
619
620/// setMetadata - Set the metadata of of the specified kind to the specified
621/// node. This updates/replaces metadata if already present, or removes it if
622/// Node is null.
623void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
624 if (Node == 0 && !hasMetadata()) return;
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000625
Chris Lattnerec39f092010-03-30 23:03:27 +0000626 // Handle 'dbg' as a special case since it is not stored in the hash table.
627 if (KindID == LLVMContext::MD_dbg) {
Chris Lattner84e679b2010-04-02 20:21:22 +0000628 DbgLoc = DebugLoc::getFromDILocation(Node);
Chris Lattnerec39f092010-03-30 23:03:27 +0000629 return;
630 }
631
Chris Lattner08113472009-12-29 09:01:33 +0000632 // Handle the case when we're adding/updating metadata on an instruction.
633 if (Node) {
634 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerec39f092010-03-30 23:03:27 +0000635 assert(!Info.empty() == hasMetadataHashEntry() &&
636 "HasMetadata bit is wonked");
Chris Lattner08113472009-12-29 09:01:33 +0000637 if (Info.empty()) {
Chris Lattnerec39f092010-03-30 23:03:27 +0000638 setHasMetadataHashEntry(true);
Chris Lattner08113472009-12-29 09:01:33 +0000639 } else {
640 // Handle replacement of an existing value.
641 for (unsigned i = 0, e = Info.size(); i != e; ++i)
642 if (Info[i].first == KindID) {
643 Info[i].second = Node;
644 return;
645 }
646 }
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000647
Chris Lattner08113472009-12-29 09:01:33 +0000648 // No replacement, just add it to the list.
649 Info.push_back(std::make_pair(KindID, Node));
650 return;
651 }
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000652
Chris Lattner08113472009-12-29 09:01:33 +0000653 // Otherwise, we're removing metadata from an instruction.
Nick Lewyckyda32cc62011-12-27 01:17:40 +0000654 assert((hasMetadataHashEntry() ==
655 getContext().pImpl->MetadataStore.count(this)) &&
Chris Lattner08113472009-12-29 09:01:33 +0000656 "HasMetadata bit out of date!");
Nick Lewyckyda32cc62011-12-27 01:17:40 +0000657 if (!hasMetadataHashEntry())
658 return; // Nothing to remove!
Chris Lattner08113472009-12-29 09:01:33 +0000659 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000660
Chris Lattner08113472009-12-29 09:01:33 +0000661 // Common case is removing the only entry.
662 if (Info.size() == 1 && Info[0].first == KindID) {
663 getContext().pImpl->MetadataStore.erase(this);
Chris Lattnerec39f092010-03-30 23:03:27 +0000664 setHasMetadataHashEntry(false);
Chris Lattner08113472009-12-29 09:01:33 +0000665 return;
666 }
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000667
Chris Lattnerec39f092010-03-30 23:03:27 +0000668 // Handle removal of an existing value.
Chris Lattner08113472009-12-29 09:01:33 +0000669 for (unsigned i = 0, e = Info.size(); i != e; ++i)
670 if (Info[i].first == KindID) {
671 Info[i] = Info.back();
672 Info.pop_back();
673 assert(!Info.empty() && "Removing last entry should be handled above");
674 return;
675 }
676 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner3990b122009-12-28 23:41:32 +0000677}
678
679MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnerec39f092010-03-30 23:03:27 +0000680 // Handle 'dbg' as a special case since it is not stored in the hash table.
681 if (KindID == LLVMContext::MD_dbg)
Chris Lattner61336ae2010-04-01 05:23:13 +0000682 return DbgLoc.getAsMDNode(getContext());
Chris Lattnerec39f092010-03-30 23:03:27 +0000683
684 if (!hasMetadataHashEntry()) return 0;
685
Chris Lattner08113472009-12-29 09:01:33 +0000686 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerec39f092010-03-30 23:03:27 +0000687 assert(!Info.empty() && "bit out of sync with hash table");
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000688
Chris Lattner08113472009-12-29 09:01:33 +0000689 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
690 I != E; ++I)
691 if (I->first == KindID)
692 return I->second;
693 return 0;
Chris Lattner3990b122009-12-28 23:41:32 +0000694}
695
696void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
Chris Lattnerec39f092010-03-30 23:03:27 +0000697 MDNode*> > &Result) const {
698 Result.clear();
699
700 // Handle 'dbg' as a special case since it is not stored in the hash table.
Chris Lattner61336ae2010-04-01 05:23:13 +0000701 if (!DbgLoc.isUnknown()) {
702 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
703 DbgLoc.getAsMDNode(getContext())));
Chris Lattnerec39f092010-03-30 23:03:27 +0000704 if (!hasMetadataHashEntry()) return;
705 }
706
707 assert(hasMetadataHashEntry() &&
708 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattner08113472009-12-29 09:01:33 +0000709 "Shouldn't have called this");
710 const LLVMContextImpl::MDMapTy &Info =
711 getContext().pImpl->MetadataStore.find(this)->second;
712 assert(!Info.empty() && "Shouldn't have called this");
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000713
Chris Lattner08113472009-12-29 09:01:33 +0000714 Result.append(Info.begin(), Info.end());
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000715
Chris Lattner08113472009-12-29 09:01:33 +0000716 // Sort the resulting array so it is stable.
717 if (Result.size() > 1)
718 array_pod_sort(Result.begin(), Result.end());
Chris Lattner3990b122009-12-28 23:41:32 +0000719}
720
Chris Lattner61336ae2010-04-01 05:23:13 +0000721void Instruction::
722getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
723 MDNode*> > &Result) const {
724 Result.clear();
725 assert(hasMetadataHashEntry() &&
726 getContext().pImpl->MetadataStore.count(this) &&
727 "Shouldn't have called this");
728 const LLVMContextImpl::MDMapTy &Info =
Bill Wendlingf563fc32012-04-03 10:50:09 +0000729 getContext().pImpl->MetadataStore.find(this)->second;
Chris Lattner61336ae2010-04-01 05:23:13 +0000730 assert(!Info.empty() && "Shouldn't have called this");
Chris Lattner61336ae2010-04-01 05:23:13 +0000731 Result.append(Info.begin(), Info.end());
Bill Wendlingf563fc32012-04-03 10:50:09 +0000732
Chris Lattner61336ae2010-04-01 05:23:13 +0000733 // Sort the resulting array so it is stable.
734 if (Result.size() > 1)
735 array_pod_sort(Result.begin(), Result.end());
736}
737
Dan Gohman4f1be4a2010-07-20 22:25:04 +0000738/// clearMetadataHashEntries - Clear all hashtable-based metadata from
739/// this instruction.
740void Instruction::clearMetadataHashEntries() {
741 assert(hasMetadataHashEntry() && "Caller should check");
742 getContext().pImpl->MetadataStore.erase(this);
743 setHasMetadataHashEntry(false);
Chris Lattner508b19a2009-12-29 07:44:16 +0000744}
745