| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#ifndef LLVM_IR_INLINEASM_H |
| 16 |
#define LLVM_IR_INLINEASM_H |
| 17 |
|
| 18 |
#include "llvm/ADT/StringRef.h" |
| 19 |
#include "llvm/IR/Value.h" |
| 20 |
#include "llvm/Support/ErrorHandling.h" |
| 21 |
#include <cassert> |
| 22 |
#include <string> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
namespace llvm { |
| 26 |
|
| 27 |
class FunctionType; |
| 28 |
class PointerType; |
| 29 |
template <class ConstantClass> class ConstantUniqueMap; |
| 30 |
|
| 31 |
class InlineAsm final : public Value { |
| 32 |
public: |
| 33 |
enum AsmDialect { |
| 34 |
AD_ATT, |
| 35 |
AD_Intel |
| 36 |
}; |
| 37 |
|
| 38 |
private: |
| 39 |
friend struct InlineAsmKeyType; |
| 40 |
friend class ConstantUniqueMap<InlineAsm>; |
| 41 |
|
| 42 |
std::string AsmString, Constraints; |
| 43 |
FunctionType *FTy; |
| 44 |
bool HasSideEffects; |
| 45 |
bool IsAlignStack; |
| 46 |
AsmDialect Dialect; |
| 47 |
bool CanThrow; |
| 48 |
|
| 49 |
InlineAsm(FunctionType *Ty, const std::string &AsmString, |
| 50 |
const std::string &Constraints, bool hasSideEffects, |
| 51 |
bool isAlignStack, AsmDialect asmDialect, bool canThrow); |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
void destroyConstant(); |
| 56 |
|
| 57 |
public: |
| 58 |
InlineAsm(const InlineAsm &) = delete; |
| 59 |
InlineAsm &operator=(const InlineAsm &) = delete; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
static InlineAsm *get(FunctionType *Ty, StringRef AsmString, |
| 64 |
StringRef Constraints, bool hasSideEffects, |
| 65 |
bool isAlignStack = false, |
| 66 |
AsmDialect asmDialect = AD_ATT, bool canThrow = false); |
| 67 |
|
| 68 |
bool hasSideEffects() const { return HasSideEffects; } |
| 69 |
bool isAlignStack() const { return IsAlignStack; } |
| 70 |
AsmDialect getDialect() const { return Dialect; } |
| 71 |
bool canThrow() const { return CanThrow; } |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
PointerType *getType() const { |
| 76 |
return reinterpret_cast<PointerType*>(Value::getType()); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
FunctionType *getFunctionType() const; |
| 82 |
|
| 83 |
const std::string &getAsmString() const { return AsmString; } |
| 84 |
const std::string &getConstraintString() const { return Constraints; } |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
static bool Verify(FunctionType *Ty, StringRef Constraints); |
| 91 |
|
| 92 |
|
| 93 |
enum ConstraintPrefix { |
| 94 |
isInput, |
| 95 |
isOutput, |
| 96 |
isClobber |
| 97 |
}; |
| 98 |
|
| 99 |
using ConstraintCodeVector = std::vector<std::string>; |
| 100 |
|
| 101 |
struct SubConstraintInfo { |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
int MatchingInput = -1; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
ConstraintCodeVector Codes; |
| 111 |
|
| 112 |
|
| 113 |
SubConstraintInfo() = default; |
| 114 |
}; |
| 115 |
|
| 116 |
using SubConstraintInfoVector = std::vector<SubConstraintInfo>; |
| 117 |
struct ConstraintInfo; |
| 118 |
using ConstraintInfoVector = std::vector<ConstraintInfo>; |
| 119 |
|
| 120 |
struct ConstraintInfo { |
| 121 |
|
| 122 |
|
| 123 |
ConstraintPrefix Type = isInput; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
bool isEarlyClobber = false; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
int MatchingInput = -1; |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
bool hasMatchingInput() const { return MatchingInput != -1; } |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
bool isCommutative = false; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
bool isIndirect = false; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
ConstraintCodeVector Codes; |
| 152 |
|
| 153 |
|
| 154 |
bool isMultipleAlternative = false; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
SubConstraintInfoVector multipleAlternatives; |
| 159 |
|
| 160 |
|
| 161 |
unsigned currentAlternativeIndex = 0; |
| 162 |
|
| 163 |
|
| 164 |
ConstraintInfo() = default; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
bool Parse(StringRef Str, ConstraintInfoVector &ConstraintsSoFar); |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
void selectAlternative(unsigned index); |
| 174 |
|
| 175 |
|
| 176 |
bool hasArg() const { |
| 177 |
return Type == isInput || (Type == isOutput && isIndirect); |
| 178 |
} |
| 179 |
}; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
static ConstraintInfoVector ParseConstraints(StringRef ConstraintString); |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
ConstraintInfoVector ParseConstraints() const { |
| 189 |
return ParseConstraints(Constraints); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
static bool classof(const Value *V) { |
| 194 |
return V->getValueID() == Value::InlineAsmVal; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
enum : uint32_t { |
| 215 |
|
| 216 |
Op_InputChain = 0, |
| 217 |
Op_AsmString = 1, |
| 218 |
Op_MDNode = 2, |
| 219 |
Op_ExtraInfo = 3, |
| 220 |
Op_FirstOperand = 4, |
| 221 |
|
| 222 |
|
| 223 |
MIOp_AsmString = 0, |
| 224 |
MIOp_ExtraInfo = 1, |
| 225 |
MIOp_FirstOperand = 2, |
| 226 |
|
| 227 |
|
| 228 |
Extra_HasSideEffects = 1, |
| 229 |
Extra_IsAlignStack = 2, |
| 230 |
Extra_AsmDialect = 4, |
| 231 |
Extra_MayLoad = 8, |
| 232 |
Extra_MayStore = 16, |
| 233 |
Extra_IsConvergent = 32, |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
Kind_RegUse = 1, |
| 239 |
Kind_RegDef = 2, |
| 240 |
Kind_RegDefEarlyClobber = 3, |
| 241 |
Kind_Clobber = 4, |
| 242 |
Kind_Imm = 5, |
| 243 |
Kind_Mem = 6, |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
Constraint_Unknown = 0, |
| 250 |
Constraint_es, |
| 251 |
Constraint_i, |
| 252 |
Constraint_m, |
| 253 |
Constraint_o, |
| 254 |
Constraint_v, |
| 255 |
Constraint_A, |
| 256 |
Constraint_Q, |
| 257 |
Constraint_R, |
| 258 |
Constraint_S, |
| 259 |
Constraint_T, |
| 260 |
Constraint_Um, |
| 261 |
Constraint_Un, |
| 262 |
Constraint_Uq, |
| 263 |
Constraint_Us, |
| 264 |
Constraint_Ut, |
| 265 |
Constraint_Uv, |
| 266 |
Constraint_Uy, |
| 267 |
Constraint_X, |
| 268 |
Constraint_Z, |
| 269 |
Constraint_ZC, |
| 270 |
Constraint_Zy, |
| 271 |
Constraints_Max = Constraint_Zy, |
| 272 |
Constraints_ShiftAmount = 16, |
| 273 |
|
| 274 |
Flag_MatchingOperand = 0x80000000 |
| 275 |
}; |
| 276 |
|
| 277 |
static unsigned getFlagWord(unsigned Kind, unsigned NumOps) { |
| 278 |
assert(((NumOps << 3) & ~0xffff) == 0 && "Too many inline asm operands!"); |
| 279 |
assert(Kind >= Kind_RegUse && Kind <= Kind_Mem && "Invalid Kind"); |
| 280 |
return Kind | (NumOps << 3); |
| 281 |
} |
| 282 |
|
| 283 |
static bool isRegDefKind(unsigned Flag){ return getKind(Flag) == Kind_RegDef;} |
| 284 |
static bool isImmKind(unsigned Flag) { return getKind(Flag) == Kind_Imm; } |
| 285 |
static bool isMemKind(unsigned Flag) { return getKind(Flag) == Kind_Mem; } |
| 286 |
static bool isRegDefEarlyClobberKind(unsigned Flag) { |
| 287 |
return getKind(Flag) == Kind_RegDefEarlyClobber; |
| 288 |
} |
| 289 |
static bool isClobberKind(unsigned Flag) { |
| 290 |
return getKind(Flag) == Kind_Clobber; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
static unsigned getFlagWordForMatchingOp(unsigned InputFlag, |
| 297 |
unsigned MatchedOperandNo) { |
| 298 |
assert(MatchedOperandNo <= 0x7fff && "Too big matched operand"); |
| 299 |
assert((InputFlag & ~0xffff) == 0 && "High bits already contain data"); |
| 300 |
return InputFlag | Flag_MatchingOperand | (MatchedOperandNo << 16); |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
static unsigned getFlagWordForRegClass(unsigned InputFlag, unsigned RC) { |
| 309 |
|
| 310 |
++RC; |
| 311 |
assert(!isImmKind(InputFlag) && "Immediates cannot have a register class"); |
| 312 |
assert(!isMemKind(InputFlag) && "Memory operand cannot have a register class"); |
| 313 |
assert(RC <= 0x7fff && "Too large register class ID"); |
| 314 |
assert((InputFlag & ~0xffff) == 0 && "High bits already contain data"); |
| 315 |
return InputFlag | (RC << 16); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
static unsigned getFlagWordForMem(unsigned InputFlag, unsigned Constraint) { |
| 321 |
assert(isMemKind(InputFlag) && "InputFlag is not a memory constraint!"); |
| 322 |
assert(Constraint <= 0x7fff && "Too large a memory constraint ID"); |
| 323 |
assert(Constraint <= Constraints_Max && "Unknown constraint ID"); |
| 324 |
assert((InputFlag & ~0xffff) == 0 && "High bits already contain data"); |
| 325 |
return InputFlag | (Constraint << Constraints_ShiftAmount); |
| 326 |
} |
| 327 |
|
| 328 |
static unsigned convertMemFlagWordToMatchingFlagWord(unsigned InputFlag) { |
| 329 |
assert(isMemKind(InputFlag)); |
| 330 |
return InputFlag & ~(0x7fff << Constraints_ShiftAmount); |
| 331 |
} |
| 332 |
|
| 333 |
static unsigned getKind(unsigned Flags) { |
| 334 |
return Flags & 7; |
| 335 |
} |
| 336 |
|
| 337 |
static unsigned getMemoryConstraintID(unsigned Flag) { |
| 338 |
assert(isMemKind(Flag)); |
| 339 |
return (Flag >> Constraints_ShiftAmount) & 0x7fff; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
static unsigned getNumOperandRegisters(unsigned Flag) { |
| 345 |
return (Flag & 0xffff) >> 3; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
static bool isUseOperandTiedToDef(unsigned Flag, unsigned &Idx) { |
| 351 |
if ((Flag & Flag_MatchingOperand) == 0) |
| 352 |
return false; |
| 353 |
Idx = (Flag & ~Flag_MatchingOperand) >> 16; |
| 354 |
return true; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
static bool hasRegClassConstraint(unsigned Flag, unsigned &RC) { |
| 360 |
if (Flag & Flag_MatchingOperand) |
| 361 |
return false; |
| 362 |
unsigned High = Flag >> 16; |
| 363 |
|
| 364 |
|
| 365 |
if (!High) |
| 366 |
return false; |
| 367 |
RC = High - 1; |
| 368 |
return true; |
| 369 |
} |
| 370 |
|
| 371 |
static std::vector<StringRef> getExtraInfoNames(unsigned ExtraInfo) { |
| 372 |
std::vector<StringRef> Result; |
| 373 |
if (ExtraInfo & InlineAsm::Extra_HasSideEffects) |
| 374 |
Result.push_back("sideeffect"); |
| 375 |
if (ExtraInfo & InlineAsm::Extra_MayLoad) |
| 376 |
Result.push_back("mayload"); |
| 377 |
if (ExtraInfo & InlineAsm::Extra_MayStore) |
| 378 |
Result.push_back("maystore"); |
| 379 |
if (ExtraInfo & InlineAsm::Extra_IsConvergent) |
| 380 |
Result.push_back("isconvergent"); |
| 381 |
if (ExtraInfo & InlineAsm::Extra_IsAlignStack) |
| 382 |
Result.push_back("alignstack"); |
| 383 |
|
| 384 |
AsmDialect Dialect = |
| 385 |
InlineAsm::AsmDialect((ExtraInfo & InlineAsm::Extra_AsmDialect)); |
| 386 |
|
| 387 |
if (Dialect == InlineAsm::AD_ATT) |
| 388 |
Result.push_back("attdialect"); |
| 389 |
if (Dialect == InlineAsm::AD_Intel) |
| 390 |
Result.push_back("inteldialect"); |
| 391 |
|
| 392 |
return Result; |
| 393 |
} |
| 394 |
|
| 395 |
static StringRef getKindName(unsigned Kind) { |
| 396 |
switch (Kind) { |
| 397 |
case InlineAsm::Kind_RegUse: |
| 398 |
return "reguse"; |
| 399 |
case InlineAsm::Kind_RegDef: |
| 400 |
return "regdef"; |
| 401 |
case InlineAsm::Kind_RegDefEarlyClobber: |
| 402 |
return "regdef-ec"; |
| 403 |
case InlineAsm::Kind_Clobber: |
| 404 |
return "clobber"; |
| 405 |
case InlineAsm::Kind_Imm: |
| 406 |
return "imm"; |
| 407 |
case InlineAsm::Kind_Mem: |
| 408 |
return "mem"; |
| 409 |
default: |
| 410 |
llvm_unreachable("Unknown operand kind"); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
static StringRef getMemConstraintName(unsigned Constraint) { |
| 415 |
switch (Constraint) { |
| 416 |
case InlineAsm::Constraint_es: |
| 417 |
return "es"; |
| 418 |
case InlineAsm::Constraint_i: |
| 419 |
return "i"; |
| 420 |
case InlineAsm::Constraint_m: |
| 421 |
return "m"; |
| 422 |
case InlineAsm::Constraint_o: |
| 423 |
return "o"; |
| 424 |
case InlineAsm::Constraint_v: |
| 425 |
return "v"; |
| 426 |
case InlineAsm::Constraint_Q: |
| 427 |
return "Q"; |
| 428 |
case InlineAsm::Constraint_R: |
| 429 |
return "R"; |
| 430 |
case InlineAsm::Constraint_S: |
| 431 |
return "S"; |
| 432 |
case InlineAsm::Constraint_T: |
| 433 |
return "T"; |
| 434 |
case InlineAsm::Constraint_Um: |
| 435 |
return "Um"; |
| 436 |
case InlineAsm::Constraint_Un: |
| 437 |
return "Un"; |
| 438 |
case InlineAsm::Constraint_Uq: |
| 439 |
return "Uq"; |
| 440 |
case InlineAsm::Constraint_Us: |
| 441 |
return "Us"; |
| 442 |
case InlineAsm::Constraint_Ut: |
| 443 |
return "Ut"; |
| 444 |
case InlineAsm::Constraint_Uv: |
| 445 |
return "Uv"; |
| 446 |
case InlineAsm::Constraint_Uy: |
| 447 |
return "Uy"; |
| 448 |
case InlineAsm::Constraint_X: |
| 449 |
return "X"; |
| 450 |
case InlineAsm::Constraint_Z: |
| 451 |
return "Z"; |
| 452 |
case InlineAsm::Constraint_ZC: |
| 453 |
return "ZC"; |
| 454 |
case InlineAsm::Constraint_Zy: |
| 455 |
return "Zy"; |
| 456 |
default: |
| 457 |
llvm_unreachable("Unknown memory constraint"); |
| 458 |
} |
| 459 |
} |
| 460 |
}; |
| 461 |
|
| 462 |
} |
| 463 |
|
| 464 |
#endif |