Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1 | //===- 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 Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 15 | #include "llvm/IR/Constants.h" |
| 16 | #include "llvm/IR/Function.h" |
| 17 | #include "llvm/IR/GlobalVariable.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 18 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Module.h" |
| 20 | #include "llvm/IR/Operator.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ManagedStatic.h" |
| 22 | #include "llvm/Support/MutexGuard.h" |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 23 | #include <algorithm> |
| 24 | #include <cstring> |
| 25 | #include <map> |
| 26 | #include <string> |
| 27 | #include <vector> |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 28 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 29 | namespace llvm { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 30 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 31 | namespace { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 32 | typedef std::map<std::string, std::vector<unsigned> > key_val_pair_t; |
| 33 | typedef std::map<const GlobalValue *, key_val_pair_t> global_val_annot_t; |
| 34 | typedef std::map<const Module *, global_val_annot_t> per_module_annot_t; |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 35 | } // anonymous namespace |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 36 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 37 | static ManagedStatic<per_module_annot_t> annotationCache; |
Justin Holewinski | 5959695 | 2014-04-09 15:38:52 +0000 | [diff] [blame] | 38 | static sys::Mutex Lock; |
| 39 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 40 | void clearAnnotationCache(const Module *Mod) { |
Justin Holewinski | 5959695 | 2014-04-09 15:38:52 +0000 | [diff] [blame] | 41 | MutexGuard Guard(Lock); |
| 42 | annotationCache->erase(Mod); |
| 43 | } |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 44 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 45 | static void cacheAnnotationFromMD(const MDNode *md, key_val_pair_t &retval) { |
Justin Holewinski | 5959695 | 2014-04-09 15:38:52 +0000 | [diff] [blame] | 46 | MutexGuard Guard(Lock); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 47 | assert(md && "Invalid mdnode for annotation"); |
| 48 | assert((md->getNumOperands() % 2) == 1 && "Invalid number of operands"); |
| 49 | // start index = 1, to skip the global variable key |
| 50 | // increment = 2, to skip the value for each property-value pairs |
| 51 | for (unsigned i = 1, e = md->getNumOperands(); i != e; i += 2) { |
| 52 | // property |
| 53 | const MDString *prop = dyn_cast<MDString>(md->getOperand(i)); |
| 54 | assert(prop && "Annotation property not a string"); |
| 55 | |
| 56 | // value |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 57 | ConstantInt *Val = mdconst::dyn_extract<ConstantInt>(md->getOperand(i + 1)); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 58 | assert(Val && "Value operand not a constant int"); |
| 59 | |
| 60 | std::string keyname = prop->getString().str(); |
| 61 | if (retval.find(keyname) != retval.end()) |
| 62 | retval[keyname].push_back(Val->getZExtValue()); |
| 63 | else { |
| 64 | std::vector<unsigned> tmp; |
| 65 | tmp.push_back(Val->getZExtValue()); |
| 66 | retval[keyname] = tmp; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static void cacheAnnotationFromMD(const Module *m, const GlobalValue *gv) { |
Justin Holewinski | 5959695 | 2014-04-09 15:38:52 +0000 | [diff] [blame] | 72 | MutexGuard Guard(Lock); |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 73 | NamedMDNode *NMD = m->getNamedMetadata("nvvm.annotations"); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 74 | if (!NMD) |
| 75 | return; |
| 76 | key_val_pair_t tmp; |
| 77 | for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) { |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 78 | const MDNode *elem = NMD->getOperand(i); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 79 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 80 | GlobalValue *entity = |
| 81 | mdconst::dyn_extract_or_null<GlobalValue>(elem->getOperand(0)); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 82 | // entity may be null due to DCE |
| 83 | if (!entity) |
| 84 | continue; |
| 85 | if (entity != gv) |
| 86 | continue; |
| 87 | |
| 88 | // accumulate annotations for entity in tmp |
| 89 | cacheAnnotationFromMD(elem, tmp); |
| 90 | } |
| 91 | |
| 92 | if (tmp.empty()) // no annotations for this gv |
| 93 | return; |
| 94 | |
| 95 | if ((*annotationCache).find(m) != (*annotationCache).end()) |
Benjamin Kramer | c6cc58e | 2014-10-04 16:55:56 +0000 | [diff] [blame] | 96 | (*annotationCache)[m][gv] = std::move(tmp); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 97 | else { |
| 98 | global_val_annot_t tmp1; |
Benjamin Kramer | c6cc58e | 2014-10-04 16:55:56 +0000 | [diff] [blame] | 99 | tmp1[gv] = std::move(tmp); |
| 100 | (*annotationCache)[m] = std::move(tmp1); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 104 | bool findOneNVVMAnnotation(const GlobalValue *gv, const std::string &prop, |
| 105 | unsigned &retval) { |
Justin Holewinski | 5959695 | 2014-04-09 15:38:52 +0000 | [diff] [blame] | 106 | MutexGuard Guard(Lock); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 107 | const Module *m = gv->getParent(); |
| 108 | if ((*annotationCache).find(m) == (*annotationCache).end()) |
| 109 | cacheAnnotationFromMD(m, gv); |
| 110 | else if ((*annotationCache)[m].find(gv) == (*annotationCache)[m].end()) |
| 111 | cacheAnnotationFromMD(m, gv); |
| 112 | if ((*annotationCache)[m][gv].find(prop) == (*annotationCache)[m][gv].end()) |
| 113 | return false; |
| 114 | retval = (*annotationCache)[m][gv][prop][0]; |
| 115 | return true; |
| 116 | } |
| 117 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 118 | bool findAllNVVMAnnotation(const GlobalValue *gv, const std::string &prop, |
| 119 | std::vector<unsigned> &retval) { |
Justin Holewinski | 5959695 | 2014-04-09 15:38:52 +0000 | [diff] [blame] | 120 | MutexGuard Guard(Lock); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 121 | const Module *m = gv->getParent(); |
| 122 | if ((*annotationCache).find(m) == (*annotationCache).end()) |
| 123 | cacheAnnotationFromMD(m, gv); |
| 124 | else if ((*annotationCache)[m].find(gv) == (*annotationCache)[m].end()) |
| 125 | cacheAnnotationFromMD(m, gv); |
| 126 | if ((*annotationCache)[m][gv].find(prop) == (*annotationCache)[m][gv].end()) |
| 127 | return false; |
| 128 | retval = (*annotationCache)[m][gv][prop]; |
| 129 | return true; |
| 130 | } |
| 131 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 132 | bool isTexture(const Value &val) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 133 | if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) { |
| 134 | unsigned annot; |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 135 | if (findOneNVVMAnnotation(gv, "texture", annot)) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 136 | assert((annot == 1) && "Unexpected annotation on a texture symbol"); |
| 137 | return true; |
| 138 | } |
| 139 | } |
| 140 | return false; |
| 141 | } |
| 142 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 143 | bool isSurface(const Value &val) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 144 | if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) { |
| 145 | unsigned annot; |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 146 | if (findOneNVVMAnnotation(gv, "surface", annot)) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 147 | assert((annot == 1) && "Unexpected annotation on a surface symbol"); |
| 148 | return true; |
| 149 | } |
| 150 | } |
| 151 | return false; |
| 152 | } |
| 153 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 154 | bool isSampler(const Value &val) { |
| 155 | const char *AnnotationName = "sampler"; |
| 156 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 157 | if (const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) { |
| 158 | unsigned annot; |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 159 | if (findOneNVVMAnnotation(gv, AnnotationName, annot)) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 160 | assert((annot == 1) && "Unexpected annotation on a sampler symbol"); |
| 161 | return true; |
| 162 | } |
| 163 | } |
| 164 | if (const Argument *arg = dyn_cast<Argument>(&val)) { |
| 165 | const Function *func = arg->getParent(); |
| 166 | std::vector<unsigned> annot; |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 167 | if (findAllNVVMAnnotation(func, AnnotationName, annot)) { |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 168 | if (is_contained(annot, arg->getArgNo())) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 169 | return true; |
| 170 | } |
| 171 | } |
| 172 | return false; |
| 173 | } |
| 174 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 175 | bool isImageReadOnly(const Value &val) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 176 | if (const Argument *arg = dyn_cast<Argument>(&val)) { |
| 177 | const Function *func = arg->getParent(); |
| 178 | std::vector<unsigned> annot; |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 179 | if (findAllNVVMAnnotation(func, "rdoimage", annot)) { |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 180 | if (is_contained(annot, arg->getArgNo())) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 181 | return true; |
| 182 | } |
| 183 | } |
| 184 | return false; |
| 185 | } |
| 186 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 187 | bool isImageWriteOnly(const Value &val) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 188 | if (const Argument *arg = dyn_cast<Argument>(&val)) { |
| 189 | const Function *func = arg->getParent(); |
| 190 | std::vector<unsigned> annot; |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 191 | if (findAllNVVMAnnotation(func, "wroimage", annot)) { |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 192 | if (is_contained(annot, arg->getArgNo())) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 193 | return true; |
| 194 | } |
| 195 | } |
| 196 | return false; |
| 197 | } |
| 198 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 199 | bool isImageReadWrite(const Value &val) { |
Justin Holewinski | 5959695 | 2014-04-09 15:38:52 +0000 | [diff] [blame] | 200 | if (const Argument *arg = dyn_cast<Argument>(&val)) { |
| 201 | const Function *func = arg->getParent(); |
| 202 | std::vector<unsigned> annot; |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 203 | if (findAllNVVMAnnotation(func, "rdwrimage", annot)) { |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 204 | if (is_contained(annot, arg->getArgNo())) |
Justin Holewinski | 5959695 | 2014-04-09 15:38:52 +0000 | [diff] [blame] | 205 | return true; |
| 206 | } |
| 207 | } |
| 208 | return false; |
| 209 | } |
| 210 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 211 | bool isImage(const Value &val) { |
| 212 | return isImageReadOnly(val) || isImageWriteOnly(val) || isImageReadWrite(val); |
Justin Holewinski | 5959695 | 2014-04-09 15:38:52 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 215 | bool isManaged(const Value &val) { |
Justin Holewinski | 5959695 | 2014-04-09 15:38:52 +0000 | [diff] [blame] | 216 | if(const GlobalValue *gv = dyn_cast<GlobalValue>(&val)) { |
| 217 | unsigned annot; |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 218 | if (findOneNVVMAnnotation(gv, "managed", annot)) { |
Justin Holewinski | 5959695 | 2014-04-09 15:38:52 +0000 | [diff] [blame] | 219 | assert((annot == 1) && "Unexpected annotation on a managed symbol"); |
| 220 | return true; |
| 221 | } |
| 222 | } |
| 223 | return false; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 226 | std::string getTextureName(const Value &val) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 227 | assert(val.hasName() && "Found texture variable with no name"); |
| 228 | return val.getName(); |
| 229 | } |
| 230 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 231 | std::string getSurfaceName(const Value &val) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 232 | assert(val.hasName() && "Found surface variable with no name"); |
| 233 | return val.getName(); |
| 234 | } |
| 235 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 236 | std::string getSamplerName(const Value &val) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 237 | assert(val.hasName() && "Found sampler variable with no name"); |
| 238 | return val.getName(); |
| 239 | } |
| 240 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 241 | bool getMaxNTIDx(const Function &F, unsigned &x) { |
| 242 | return findOneNVVMAnnotation(&F, "maxntidx", x); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 245 | bool getMaxNTIDy(const Function &F, unsigned &y) { |
| 246 | return findOneNVVMAnnotation(&F, "maxntidy", y); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 249 | bool getMaxNTIDz(const Function &F, unsigned &z) { |
| 250 | return findOneNVVMAnnotation(&F, "maxntidz", z); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 253 | bool getReqNTIDx(const Function &F, unsigned &x) { |
| 254 | return findOneNVVMAnnotation(&F, "reqntidx", x); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 257 | bool getReqNTIDy(const Function &F, unsigned &y) { |
| 258 | return findOneNVVMAnnotation(&F, "reqntidy", y); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 261 | bool getReqNTIDz(const Function &F, unsigned &z) { |
| 262 | return findOneNVVMAnnotation(&F, "reqntidz", z); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 265 | bool getMinCTASm(const Function &F, unsigned &x) { |
| 266 | return findOneNVVMAnnotation(&F, "minctasm", x); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 269 | bool isKernelFunction(const Function &F) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 270 | unsigned x = 0; |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 271 | bool retval = findOneNVVMAnnotation(&F, "kernel", x); |
Eli Bendersky | 3e84019 | 2015-03-23 16:26:23 +0000 | [diff] [blame] | 272 | if (!retval) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 273 | // There is no NVVM metadata, check the calling convention |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 274 | return F.getCallingConv() == CallingConv::PTX_Kernel; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 275 | } |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 276 | return (x == 1); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 279 | bool getAlign(const Function &F, unsigned index, unsigned &align) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 280 | std::vector<unsigned> Vs; |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 281 | bool retval = findAllNVVMAnnotation(&F, "align", Vs); |
Eli Bendersky | 3e84019 | 2015-03-23 16:26:23 +0000 | [diff] [blame] | 282 | if (!retval) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 283 | return false; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 284 | for (int i = 0, e = Vs.size(); i < e; i++) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 285 | unsigned v = Vs[i]; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 286 | if ((v >> 16) == index) { |
| 287 | align = v & 0xFFFF; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 288 | return true; |
| 289 | } |
| 290 | } |
| 291 | return false; |
| 292 | } |
| 293 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 294 | bool getAlign(const CallInst &I, unsigned index, unsigned &align) { |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 295 | if (MDNode *alignNode = I.getMetadata("callalign")) { |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 296 | for (int i = 0, n = alignNode->getNumOperands(); i < n; i++) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 297 | if (const ConstantInt *CI = |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 298 | mdconst::dyn_extract<ConstantInt>(alignNode->getOperand(i))) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 299 | unsigned v = CI->getZExtValue(); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 300 | if ((v >> 16) == index) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 301 | align = v & 0xFFFF; |
| 302 | return true; |
| 303 | } |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 304 | if ((v >> 16) > index) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 305 | return false; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | return false; |
| 311 | } |
| 312 | |
Bruce Mitchener | e9ffb45 | 2015-09-12 01:17:08 +0000 | [diff] [blame] | 313 | // The following are some useful utilities for debugging |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 314 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 315 | BasicBlock *getParentBlock(Value *v) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 316 | if (BasicBlock *B = dyn_cast<BasicBlock>(v)) |
| 317 | return B; |
| 318 | |
| 319 | if (Instruction *I = dyn_cast<Instruction>(v)) |
| 320 | return I->getParent(); |
| 321 | |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 322 | return nullptr; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 325 | Function *getParentFunction(Value *v) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 326 | if (Function *F = dyn_cast<Function>(v)) |
| 327 | return F; |
| 328 | |
| 329 | if (Instruction *I = dyn_cast<Instruction>(v)) |
| 330 | return I->getParent()->getParent(); |
| 331 | |
| 332 | if (BasicBlock *B = dyn_cast<BasicBlock>(v)) |
| 333 | return B->getParent(); |
| 334 | |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 335 | return nullptr; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | // Dump a block by name |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 339 | void dumpBlock(Value *v, char *blockName) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 340 | Function *F = getParentFunction(v); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 341 | if (!F) |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 342 | return; |
| 343 | |
| 344 | for (Function::iterator it = F->begin(), ie = F->end(); it != ie; ++it) { |
Duncan P. N. Exon Smith | 61149b8 | 2015-10-20 00:54:09 +0000 | [diff] [blame] | 345 | BasicBlock *B = &*it; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 346 | if (strcmp(B->getName().data(), blockName) == 0) { |
| 347 | B->dump(); |
| 348 | return; |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | // Find an instruction by name |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 354 | Instruction *getInst(Value *base, char *instName) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 355 | Function *F = getParentFunction(base); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 356 | if (!F) |
| 357 | return nullptr; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 358 | |
| 359 | for (inst_iterator it = inst_begin(F), ie = inst_end(F); it != ie; ++it) { |
| 360 | Instruction *I = &*it; |
| 361 | if (strcmp(I->getName().data(), instName) == 0) { |
| 362 | return I; |
| 363 | } |
| 364 | } |
| 365 | |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 366 | return nullptr; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Bruce Mitchener | e9ffb45 | 2015-09-12 01:17:08 +0000 | [diff] [blame] | 369 | // Dump an instruction by name |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 370 | void dumpInst(Value *base, char *instName) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 371 | Instruction *I = getInst(base, instName); |
| 372 | if (I) |
| 373 | I->dump(); |
| 374 | } |
| 375 | |
| 376 | // Dump an instruction and all dependent instructions |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 377 | void dumpInstRec(Value *v, std::set<Instruction *> *visited) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 378 | if (Instruction *I = dyn_cast<Instruction>(v)) { |
| 379 | |
| 380 | if (visited->find(I) != visited->end()) |
| 381 | return; |
| 382 | |
| 383 | visited->insert(I); |
| 384 | |
| 385 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 386 | dumpInstRec(I->getOperand(i), visited); |
| 387 | |
| 388 | I->dump(); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | // Dump an instruction and all dependent instructions |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 393 | void dumpInstRec(Value *v) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 394 | std::set<Instruction *> visited; |
| 395 | |
| 396 | //BasicBlock *B = getParentBlock(v); |
| 397 | |
| 398 | dumpInstRec(v, &visited); |
| 399 | } |
| 400 | |
| 401 | // Dump the parent for Instruction, block or function |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 402 | void dumpParent(Value *v) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 403 | if (Instruction *I = dyn_cast<Instruction>(v)) { |
| 404 | I->getParent()->dump(); |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | if (BasicBlock *B = dyn_cast<BasicBlock>(v)) { |
| 409 | B->getParent()->dump(); |
| 410 | return; |
| 411 | } |
| 412 | |
| 413 | if (Function *F = dyn_cast<Function>(v)) { |
| 414 | F->getParent()->dump(); |
| 415 | return; |
| 416 | } |
| 417 | } |
Justin Lebar | e686708 | 2016-12-14 22:32:44 +0000 | [diff] [blame^] | 418 | |
| 419 | } // namespace llvm |