blob: c293058ffc61dd8db95283a7b8be10f3879be428 [file] [log] [blame]
Justin Holewinskiae556d32012-05-04 20:18:50 +00001//===- NVPTXUtilities.cpp - Utility Functions -----------------------------===//
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 contains miscellaneous utility functions
11//===----------------------------------------------------------------------===//
12
13#include "NVPTXUtilities.h"
14#include "NVPTX.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Constants.h"
16#include "llvm/IR/Function.h"
17#include "llvm/IR/GlobalVariable.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000018#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Module.h"
20#include "llvm/IR/Operator.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000021#include "llvm/Support/ManagedStatic.h"
22#include "llvm/Support/MutexGuard.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000023#include <algorithm>
24#include <cstring>
25#include <map>
26#include <string>
27#include <vector>
Justin Holewinskiae556d32012-05-04 20:18:50 +000028
29using namespace llvm;
30
31typedef std::map<std::string, std::vector<unsigned> > key_val_pair_t;
32typedef std::map<const GlobalValue *, key_val_pair_t> global_val_annot_t;
33typedef std::map<const Module *, global_val_annot_t> per_module_annot_t;
34
35ManagedStatic<per_module_annot_t> annotationCache;
Justin Holewinski59596952014-04-09 15:38:52 +000036static sys::Mutex Lock;
37
38void llvm::clearAnnotationCache(const llvm::Module *Mod) {
39 MutexGuard Guard(Lock);
40 annotationCache->erase(Mod);
41}
Justin Holewinskiae556d32012-05-04 20:18:50 +000042
Justin Holewinskiae556d32012-05-04 20:18:50 +000043static void cacheAnnotationFromMD(const MDNode *md, key_val_pair_t &retval) {
Justin Holewinski59596952014-04-09 15:38:52 +000044 MutexGuard Guard(Lock);
Justin Holewinskiae556d32012-05-04 20:18:50 +000045 assert(md && "Invalid mdnode for annotation");
46 assert((md->getNumOperands() % 2) == 1 && "Invalid number of operands");
47 // start index = 1, to skip the global variable key
48 // increment = 2, to skip the value for each property-value pairs
49 for (unsigned i = 1, e = md->getNumOperands(); i != e; i += 2) {
50 // property
51 const MDString *prop = dyn_cast<MDString>(md->getOperand(i));
52 assert(prop && "Annotation property not a string");
53
54 // value
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000055 ConstantInt *Val = mdconst::dyn_extract<ConstantInt>(md->getOperand(i + 1));
Justin Holewinskiae556d32012-05-04 20:18:50 +000056 assert(Val && "Value operand not a constant int");
57
58 std::string keyname = prop->getString().str();
59 if (retval.find(keyname) != retval.end())
60 retval[keyname].push_back(Val->getZExtValue());
61 else {
62 std::vector<unsigned> tmp;
63 tmp.push_back(Val->getZExtValue());
64 retval[keyname] = tmp;
65 }
66 }
67}
68
69static void cacheAnnotationFromMD(const Module *m, const GlobalValue *gv) {
Justin Holewinski59596952014-04-09 15:38:52 +000070 MutexGuard Guard(Lock);
Justin Holewinskiae556d32012-05-04 20:18:50 +000071 NamedMDNode *NMD = m->getNamedMetadata(llvm::NamedMDForAnnotations);
72 if (!NMD)
73 return;
74 key_val_pair_t tmp;
75 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +000076 const MDNode *elem = NMD->getOperand(i);
Justin Holewinskiae556d32012-05-04 20:18:50 +000077
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000078 GlobalValue *entity =
79 mdconst::dyn_extract_or_null<GlobalValue>(elem->getOperand(0));
Justin Holewinskiae556d32012-05-04 20:18:50 +000080 // entity may be null due to DCE
81 if (!entity)
82 continue;
83 if (entity != gv)
84 continue;
85
86 // accumulate annotations for entity in tmp
87 cacheAnnotationFromMD(elem, tmp);
88 }
89
90 if (tmp.empty()) // no annotations for this gv
91 return;
92
93 if ((*annotationCache).find(m) != (*annotationCache).end())
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +000094 (*annotationCache)[m][gv] = std::move(tmp);
Justin Holewinskiae556d32012-05-04 20:18:50 +000095 else {
96 global_val_annot_t tmp1;
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +000097 tmp1[gv] = std::move(tmp);
98 (*annotationCache)[m] = std::move(tmp1);
Justin Holewinskiae556d32012-05-04 20:18:50 +000099 }
100}
101
102bool llvm::findOneNVVMAnnotation(const GlobalValue *gv, std::string prop,
103 unsigned &retval) {
Justin Holewinski59596952014-04-09 15:38:52 +0000104 MutexGuard Guard(Lock);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000105 const Module *m = gv->getParent();
106 if ((*annotationCache).find(m) == (*annotationCache).end())
107 cacheAnnotationFromMD(m, gv);
108 else if ((*annotationCache)[m].find(gv) == (*annotationCache)[m].end())
109 cacheAnnotationFromMD(m, gv);
110 if ((*annotationCache)[m][gv].find(prop) == (*annotationCache)[m][gv].end())
111 return false;
112 retval = (*annotationCache)[m][gv][prop][0];
113 return true;
114}
115
116bool llvm::findAllNVVMAnnotation(const GlobalValue *gv, std::string prop,
117 std::vector<unsigned> &retval) {
Justin Holewinski59596952014-04-09 15:38:52 +0000118 MutexGuard Guard(Lock);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000119 const Module *m = gv->getParent();
120 if ((*annotationCache).find(m) == (*annotationCache).end())
121 cacheAnnotationFromMD(m, gv);
122 else if ((*annotationCache)[m].find(gv) == (*annotationCache)[m].end())
123 cacheAnnotationFromMD(m, gv);
124 if ((*annotationCache)[m][gv].find(prop) == (*annotationCache)[m][gv].end())
125 return false;
126 retval = (*annotationCache)[m][gv][prop];
127 return true;
128}
129
130bool llvm::isTexture(const llvm::Value &val) {
131 if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
132 unsigned annot;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000133 if (llvm::findOneNVVMAnnotation(
134 gv, llvm::PropertyAnnotationNames[llvm::PROPERTY_ISTEXTURE],
135 annot)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000136 assert((annot == 1) && "Unexpected annotation on a texture symbol");
137 return true;
138 }
139 }
140 return false;
141}
142
143bool llvm::isSurface(const llvm::Value &val) {
144 if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
145 unsigned annot;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000146 if (llvm::findOneNVVMAnnotation(
147 gv, llvm::PropertyAnnotationNames[llvm::PROPERTY_ISSURFACE],
148 annot)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000149 assert((annot == 1) && "Unexpected annotation on a surface symbol");
150 return true;
151 }
152 }
153 return false;
154}
155
156bool llvm::isSampler(const llvm::Value &val) {
157 if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
158 unsigned annot;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000159 if (llvm::findOneNVVMAnnotation(
160 gv, llvm::PropertyAnnotationNames[llvm::PROPERTY_ISSAMPLER],
161 annot)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000162 assert((annot == 1) && "Unexpected annotation on a sampler symbol");
163 return true;
164 }
165 }
166 if (const Argument *arg = dyn_cast<Argument>(&val)) {
167 const Function *func = arg->getParent();
168 std::vector<unsigned> annot;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000169 if (llvm::findAllNVVMAnnotation(
170 func, llvm::PropertyAnnotationNames[llvm::PROPERTY_ISSAMPLER],
171 annot)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000172 if (std::find(annot.begin(), annot.end(), arg->getArgNo()) != annot.end())
173 return true;
174 }
175 }
176 return false;
177}
178
179bool llvm::isImageReadOnly(const llvm::Value &val) {
180 if (const Argument *arg = dyn_cast<Argument>(&val)) {
181 const Function *func = arg->getParent();
182 std::vector<unsigned> annot;
183 if (llvm::findAllNVVMAnnotation(func,
Justin Holewinski0497ab12013-03-30 14:29:21 +0000184 llvm::PropertyAnnotationNames[
185 llvm::PROPERTY_ISREADONLY_IMAGE_PARAM],
186 annot)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000187 if (std::find(annot.begin(), annot.end(), arg->getArgNo()) != annot.end())
188 return true;
189 }
190 }
191 return false;
192}
193
194bool llvm::isImageWriteOnly(const llvm::Value &val) {
195 if (const Argument *arg = dyn_cast<Argument>(&val)) {
196 const Function *func = arg->getParent();
197 std::vector<unsigned> annot;
198 if (llvm::findAllNVVMAnnotation(func,
Justin Holewinski0497ab12013-03-30 14:29:21 +0000199 llvm::PropertyAnnotationNames[
200 llvm::PROPERTY_ISWRITEONLY_IMAGE_PARAM],
201 annot)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000202 if (std::find(annot.begin(), annot.end(), arg->getArgNo()) != annot.end())
203 return true;
204 }
205 }
206 return false;
207}
208
Justin Holewinski59596952014-04-09 15:38:52 +0000209bool llvm::isImageReadWrite(const llvm::Value &val) {
210 if (const Argument *arg = dyn_cast<Argument>(&val)) {
211 const Function *func = arg->getParent();
212 std::vector<unsigned> annot;
213 if (llvm::findAllNVVMAnnotation(func,
214 llvm::PropertyAnnotationNames[
215 llvm::PROPERTY_ISREADWRITE_IMAGE_PARAM],
216 annot)) {
217 if (std::find(annot.begin(), annot.end(), arg->getArgNo()) != annot.end())
218 return true;
219 }
220 }
221 return false;
222}
223
Justin Holewinskiae556d32012-05-04 20:18:50 +0000224bool llvm::isImage(const llvm::Value &val) {
Justin Holewinski59596952014-04-09 15:38:52 +0000225 return llvm::isImageReadOnly(val) || llvm::isImageWriteOnly(val) ||
226 llvm::isImageReadWrite(val);
227}
228
229bool llvm::isManaged(const llvm::Value &val) {
230 if(const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) {
231 unsigned annot;
232 if(llvm::findOneNVVMAnnotation(gv,
233 llvm::PropertyAnnotationNames[llvm::PROPERTY_MANAGED],
234 annot)) {
235 assert((annot == 1) && "Unexpected annotation on a managed symbol");
236 return true;
237 }
238 }
239 return false;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000240}
241
242std::string llvm::getTextureName(const llvm::Value &val) {
243 assert(val.hasName() && "Found texture variable with no name");
244 return val.getName();
245}
246
247std::string llvm::getSurfaceName(const llvm::Value &val) {
248 assert(val.hasName() && "Found surface variable with no name");
249 return val.getName();
250}
251
252std::string llvm::getSamplerName(const llvm::Value &val) {
253 assert(val.hasName() && "Found sampler variable with no name");
254 return val.getName();
255}
256
257bool llvm::getMaxNTIDx(const Function &F, unsigned &x) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000258 return (llvm::findOneNVVMAnnotation(
259 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_MAXNTID_X], x));
Justin Holewinskiae556d32012-05-04 20:18:50 +0000260}
261
262bool llvm::getMaxNTIDy(const Function &F, unsigned &y) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000263 return (llvm::findOneNVVMAnnotation(
264 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_MAXNTID_Y], y));
Justin Holewinskiae556d32012-05-04 20:18:50 +0000265}
266
267bool llvm::getMaxNTIDz(const Function &F, unsigned &z) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000268 return (llvm::findOneNVVMAnnotation(
269 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_MAXNTID_Z], z));
Justin Holewinskiae556d32012-05-04 20:18:50 +0000270}
271
272bool llvm::getReqNTIDx(const Function &F, unsigned &x) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000273 return (llvm::findOneNVVMAnnotation(
274 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_REQNTID_X], x));
Justin Holewinskiae556d32012-05-04 20:18:50 +0000275}
276
277bool llvm::getReqNTIDy(const Function &F, unsigned &y) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000278 return (llvm::findOneNVVMAnnotation(
279 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_REQNTID_Y], y));
Justin Holewinskiae556d32012-05-04 20:18:50 +0000280}
281
282bool llvm::getReqNTIDz(const Function &F, unsigned &z) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000283 return (llvm::findOneNVVMAnnotation(
284 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_REQNTID_Z], z));
Justin Holewinskiae556d32012-05-04 20:18:50 +0000285}
286
287bool llvm::getMinCTASm(const Function &F, unsigned &x) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000288 return (llvm::findOneNVVMAnnotation(
289 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_MINNCTAPERSM], x));
Justin Holewinskiae556d32012-05-04 20:18:50 +0000290}
291
292bool llvm::isKernelFunction(const Function &F) {
293 unsigned x = 0;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000294 bool retval = llvm::findOneNVVMAnnotation(
295 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_ISKERNEL_FUNCTION], x);
Eli Bendersky3e840192015-03-23 16:26:23 +0000296 if (!retval) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000297 // There is no NVVM metadata, check the calling convention
Eli Bendersky3e840192015-03-23 16:26:23 +0000298 return F.getCallingConv() == llvm::CallingConv::PTX_Kernel;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000299 }
Justin Holewinski0497ab12013-03-30 14:29:21 +0000300 return (x == 1);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000301}
302
303bool llvm::getAlign(const Function &F, unsigned index, unsigned &align) {
304 std::vector<unsigned> Vs;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000305 bool retval = llvm::findAllNVVMAnnotation(
306 &F, llvm::PropertyAnnotationNames[llvm::PROPERTY_ALIGN], Vs);
Eli Bendersky3e840192015-03-23 16:26:23 +0000307 if (!retval)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000308 return false;
Justin Holewinski0497ab12013-03-30 14:29:21 +0000309 for (int i = 0, e = Vs.size(); i < e; i++) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000310 unsigned v = Vs[i];
Justin Holewinski0497ab12013-03-30 14:29:21 +0000311 if ((v >> 16) == index) {
312 align = v & 0xFFFF;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000313 return true;
314 }
315 }
316 return false;
317}
318
319bool llvm::getAlign(const CallInst &I, unsigned index, unsigned &align) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000320 if (MDNode *alignNode = I.getMetadata("callalign")) {
Justin Holewinski0497ab12013-03-30 14:29:21 +0000321 for (int i = 0, n = alignNode->getNumOperands(); i < n; i++) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000322 if (const ConstantInt *CI =
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000323 mdconst::dyn_extract<ConstantInt>(alignNode->getOperand(i))) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000324 unsigned v = CI->getZExtValue();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000325 if ((v >> 16) == index) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000326 align = v & 0xFFFF;
327 return true;
328 }
Justin Holewinski0497ab12013-03-30 14:29:21 +0000329 if ((v >> 16) > index) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000330 return false;
331 }
332 }
333 }
334 }
335 return false;
336}
337
338bool llvm::isBarrierIntrinsic(Intrinsic::ID id) {
Jingyue Wu4be014a2015-07-31 05:09:47 +0000339 return (id == Intrinsic::nvvm_barrier0) ||
340 (id == Intrinsic::nvvm_barrier0_popc) ||
341 (id == Intrinsic::nvvm_barrier0_and) ||
342 (id == Intrinsic::nvvm_barrier0_or) ||
343 (id == Intrinsic::cuda_syncthreads);
Justin Holewinskiae556d32012-05-04 20:18:50 +0000344}
345
346// Interface for checking all memory space transfer related intrinsics
347bool llvm::isMemorySpaceTransferIntrinsic(Intrinsic::ID id) {
Jingyue Wu4be014a2015-07-31 05:09:47 +0000348 return id == Intrinsic::nvvm_ptr_local_to_gen ||
Justin Holewinskiae556d32012-05-04 20:18:50 +0000349 id == Intrinsic::nvvm_ptr_shared_to_gen ||
350 id == Intrinsic::nvvm_ptr_global_to_gen ||
351 id == Intrinsic::nvvm_ptr_constant_to_gen ||
352 id == Intrinsic::nvvm_ptr_gen_to_global ||
353 id == Intrinsic::nvvm_ptr_gen_to_shared ||
354 id == Intrinsic::nvvm_ptr_gen_to_local ||
355 id == Intrinsic::nvvm_ptr_gen_to_constant ||
Jingyue Wu4be014a2015-07-31 05:09:47 +0000356 id == Intrinsic::nvvm_ptr_gen_to_param;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000357}
358
359// consider several special intrinsics in striping pointer casts, and
Bruce Mitchenere9ffb452015-09-12 01:17:08 +0000360// provide an option to ignore GEP indices for find out the base address only
361// which could be used in simple alias disambiguation.
Justin Holewinski0497ab12013-03-30 14:29:21 +0000362const Value *
363llvm::skipPointerTransfer(const Value *V, bool ignore_GEP_indices) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000364 V = V->stripPointerCasts();
365 while (true) {
366 if (const IntrinsicInst *IS = dyn_cast<IntrinsicInst>(V)) {
367 if (isMemorySpaceTransferIntrinsic(IS->getIntrinsicID())) {
368 V = IS->getArgOperand(0)->stripPointerCasts();
369 continue;
370 }
371 } else if (ignore_GEP_indices)
372 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
373 V = GEP->getPointerOperand()->stripPointerCasts();
374 continue;
375 }
376 break;
377 }
378 return V;
379}
380
381// consider several special intrinsics in striping pointer casts, and
Bruce Mitchenere9ffb452015-09-12 01:17:08 +0000382// - ignore GEP indices for find out the base address only, and
Justin Holewinskiae556d32012-05-04 20:18:50 +0000383// - tracking PHINode
Bruce Mitchenere9ffb452015-09-12 01:17:08 +0000384// which could be used in simple alias disambiguation.
Justin Holewinski0497ab12013-03-30 14:29:21 +0000385const Value *
386llvm::skipPointerTransfer(const Value *V, std::set<const Value *> &processed) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000387 if (processed.find(V) != processed.end())
Craig Topper062a2ba2014-04-25 05:30:21 +0000388 return nullptr;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000389 processed.insert(V);
390
391 const Value *V2 = V->stripPointerCasts();
392 if (V2 != V && processed.find(V2) != processed.end())
Craig Topper062a2ba2014-04-25 05:30:21 +0000393 return nullptr;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000394 processed.insert(V2);
395
396 V = V2;
397
398 while (true) {
399 if (const IntrinsicInst *IS = dyn_cast<IntrinsicInst>(V)) {
400 if (isMemorySpaceTransferIntrinsic(IS->getIntrinsicID())) {
401 V = IS->getArgOperand(0)->stripPointerCasts();
402 continue;
403 }
404 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
405 V = GEP->getPointerOperand()->stripPointerCasts();
406 continue;
407 } else if (const PHINode *PN = dyn_cast<PHINode>(V)) {
408 if (V != V2 && processed.find(V) != processed.end())
Craig Topper062a2ba2014-04-25 05:30:21 +0000409 return nullptr;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000410 processed.insert(PN);
Craig Topper062a2ba2014-04-25 05:30:21 +0000411 const Value *common = nullptr;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000412 for (unsigned i = 0; i != PN->getNumIncomingValues(); ++i) {
413 const Value *pv = PN->getIncomingValue(i);
414 const Value *base = skipPointerTransfer(pv, processed);
415 if (base) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000416 if (!common)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000417 common = base;
418 else if (common != base)
419 return PN;
420 }
421 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000422 if (!common)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000423 return PN;
424 V = common;
425 }
426 break;
427 }
428 return V;
429}
430
Bruce Mitchenere9ffb452015-09-12 01:17:08 +0000431// The following are some useful utilities for debugging
Justin Holewinskiae556d32012-05-04 20:18:50 +0000432
433BasicBlock *llvm::getParentBlock(Value *v) {
434 if (BasicBlock *B = dyn_cast<BasicBlock>(v))
435 return B;
436
437 if (Instruction *I = dyn_cast<Instruction>(v))
438 return I->getParent();
439
Craig Topper062a2ba2014-04-25 05:30:21 +0000440 return nullptr;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000441}
442
443Function *llvm::getParentFunction(Value *v) {
444 if (Function *F = dyn_cast<Function>(v))
445 return F;
446
447 if (Instruction *I = dyn_cast<Instruction>(v))
448 return I->getParent()->getParent();
449
450 if (BasicBlock *B = dyn_cast<BasicBlock>(v))
451 return B->getParent();
452
Craig Topper062a2ba2014-04-25 05:30:21 +0000453 return nullptr;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000454}
455
456// Dump a block by name
457void llvm::dumpBlock(Value *v, char *blockName) {
458 Function *F = getParentFunction(v);
Craig Topper062a2ba2014-04-25 05:30:21 +0000459 if (!F)
Justin Holewinskiae556d32012-05-04 20:18:50 +0000460 return;
461
462 for (Function::iterator it = F->begin(), ie = F->end(); it != ie; ++it) {
463 BasicBlock *B = it;
464 if (strcmp(B->getName().data(), blockName) == 0) {
465 B->dump();
466 return;
467 }
468 }
469}
470
471// Find an instruction by name
472Instruction *llvm::getInst(Value *base, char *instName) {
473 Function *F = getParentFunction(base);
Craig Topper062a2ba2014-04-25 05:30:21 +0000474 if (!F)
475 return nullptr;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000476
477 for (inst_iterator it = inst_begin(F), ie = inst_end(F); it != ie; ++it) {
478 Instruction *I = &*it;
479 if (strcmp(I->getName().data(), instName) == 0) {
480 return I;
481 }
482 }
483
Craig Topper062a2ba2014-04-25 05:30:21 +0000484 return nullptr;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000485}
486
Bruce Mitchenere9ffb452015-09-12 01:17:08 +0000487// Dump an instruction by name
Justin Holewinskiae556d32012-05-04 20:18:50 +0000488void llvm::dumpInst(Value *base, char *instName) {
489 Instruction *I = getInst(base, instName);
490 if (I)
491 I->dump();
492}
493
494// Dump an instruction and all dependent instructions
495void llvm::dumpInstRec(Value *v, std::set<Instruction *> *visited) {
496 if (Instruction *I = dyn_cast<Instruction>(v)) {
497
498 if (visited->find(I) != visited->end())
499 return;
500
501 visited->insert(I);
502
503 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
504 dumpInstRec(I->getOperand(i), visited);
505
506 I->dump();
507 }
508}
509
510// Dump an instruction and all dependent instructions
511void llvm::dumpInstRec(Value *v) {
512 std::set<Instruction *> visited;
513
514 //BasicBlock *B = getParentBlock(v);
515
516 dumpInstRec(v, &visited);
517}
518
519// Dump the parent for Instruction, block or function
520void llvm::dumpParent(Value *v) {
521 if (Instruction *I = dyn_cast<Instruction>(v)) {
522 I->getParent()->dump();
523 return;
524 }
525
526 if (BasicBlock *B = dyn_cast<BasicBlock>(v)) {
527 B->getParent()->dump();
528 return;
529 }
530
531 if (Function *F = dyn_cast<Function>(v)) {
532 F->getParent()->dump();
533 return;
534 }
535}