blob: 5f074b33a2d401bfc55c7df8f32d43aebd41b0db [file] [log] [blame]
Justin Holewinski49683f32012-05-04 20:18:50 +00001//===-- NVPTXutil.cpp - Functions exported to CodeGen --*- C++ -*-===//
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 the functions that can be used in CodeGen.
11//
12//===----------------------------------------------------------------------===//
13
14#include "NVPTXutil.h"
15#include "NVPTX.h"
16
17using namespace llvm;
18
19namespace llvm {
20
Justin Holewinski3639ce22013-03-30 14:29:21 +000021bool isParamLoad(const MachineInstr *MI) {
Justin Holewinski49683f32012-05-04 20:18:50 +000022 if ((MI->getOpcode() != NVPTX::LD_i32_avar) &&
23 (MI->getOpcode() != NVPTX::LD_i64_avar))
24 return false;
25 if (MI->getOperand(2).isImm() == false)
26 return false;
27 if (MI->getOperand(2).getImm() != NVPTX::PTXLdStInstCode::PARAM)
28 return false;
29 return true;
30}
31
Justin Holewinski3639ce22013-03-30 14:29:21 +000032#define DATA_MASK 0x7f
33#define DIGIT_WIDTH 7
34#define MORE_BYTES 0x80
Justin Holewinski49683f32012-05-04 20:18:50 +000035
Justin Holewinski3639ce22013-03-30 14:29:21 +000036static int encode_leb128(uint64_t val, int *nbytes, char *space, int splen) {
Justin Holewinski49683f32012-05-04 20:18:50 +000037 char *a;
38 char *end = space + splen;
39
40 a = space;
41 do {
42 unsigned char uc;
43
44 if (a >= end)
45 return 1;
46 uc = val & DATA_MASK;
47 val >>= DIGIT_WIDTH;
48 if (val != 0)
49 uc |= MORE_BYTES;
50 *a = uc;
51 a++;
52 } while (val);
53 *nbytes = a - space;
54 return 0;
55}
56
57#undef DATA_MASK
58#undef DIGIT_WIDTH
59#undef MORE_BYTES
60
Justin Holewinski3639ce22013-03-30 14:29:21 +000061uint64_t encode_leb128(const char *str) {
62 union {
63 uint64_t x;
64 char a[8];
65 } temp64;
Justin Holewinski49683f32012-05-04 20:18:50 +000066
67 temp64.x = 0;
68
Justin Holewinski3639ce22013-03-30 14:29:21 +000069 for (unsigned i = 0, e = strlen(str); i != e; ++i)
70 temp64.a[i] = str[e - 1 - i];
Justin Holewinski49683f32012-05-04 20:18:50 +000071
72 char encoded[16];
73 int nbytes;
74
75 int retval = encode_leb128(temp64.x, &nbytes, encoded, 16);
76
Justin Holewinski3639ce22013-03-30 14:29:21 +000077 (void) retval;
78 assert(retval == 0 && "Encoding to leb128 failed");
Justin Holewinski49683f32012-05-04 20:18:50 +000079
80 assert(nbytes <= 8 &&
81 "Cannot support register names with leb128 encoding > 8 bytes");
82
83 temp64.x = 0;
Justin Holewinski3639ce22013-03-30 14:29:21 +000084 for (int i = 0; i < nbytes; ++i)
Justin Holewinski49683f32012-05-04 20:18:50 +000085 temp64.a[i] = encoded[i];
86
87 return temp64.x;
88}
89
90} // end namespace llvm