LLVM  14.0.0git
MemoryBuffer.cpp
Go to the documentation of this file.
1 //===--- MemoryBuffer.cpp - Memory Buffer implementation ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the MemoryBuffer interface.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/Config/config.h"
17 #include "llvm/Support/Errc.h"
18 #include "llvm/Support/Errno.h"
21 #include "llvm/Support/Path.h"
22 #include "llvm/Support/Process.h"
23 #include "llvm/Support/Program.h"
25 #include <cassert>
26 #include <cerrno>
27 #include <cstring>
28 #include <new>
29 #include <sys/types.h>
30 #include <system_error>
31 #if !defined(_MSC_VER) && !defined(__MINGW32__)
32 #include <unistd.h>
33 #else
34 #include <io.h>
35 #endif
36 using namespace llvm;
37 
38 //===----------------------------------------------------------------------===//
39 // MemoryBuffer implementation itself.
40 //===----------------------------------------------------------------------===//
41 
43 
44 /// init - Initialize this MemoryBuffer as a reference to externally allocated
45 /// memory, memory that we know is already null terminated.
46 void MemoryBuffer::init(const char *BufStart, const char *BufEnd,
47  bool RequiresNullTerminator) {
48  assert((!RequiresNullTerminator || BufEnd[0] == 0) &&
49  "Buffer is not null terminated!");
50  BufferStart = BufStart;
51  BufferEnd = BufEnd;
52 }
53 
54 //===----------------------------------------------------------------------===//
55 // MemoryBufferMem implementation.
56 //===----------------------------------------------------------------------===//
57 
58 /// CopyStringRef - Copies contents of a StringRef into a block of memory and
59 /// null-terminates it.
60 static void CopyStringRef(char *Memory, StringRef Data) {
61  if (!Data.empty())
62  memcpy(Memory, Data.data(), Data.size());
63  Memory[Data.size()] = 0; // Null terminate string.
64 }
65 
66 namespace {
67 struct NamedBufferAlloc {
68  const Twine &Name;
69  NamedBufferAlloc(const Twine &Name) : Name(Name) {}
70 };
71 } // namespace
72 
73 void *operator new(size_t N, const NamedBufferAlloc &Alloc) {
74  SmallString<256> NameBuf;
75  StringRef NameRef = Alloc.Name.toStringRef(NameBuf);
76 
77  char *Mem = static_cast<char *>(operator new(N + NameRef.size() + 1));
78  CopyStringRef(Mem + N, NameRef);
79  return Mem;
80 }
81 
82 namespace {
83 /// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
84 template<typename MB>
85 class MemoryBufferMem : public MB {
86 public:
87  MemoryBufferMem(StringRef InputData, bool RequiresNullTerminator) {
88  MemoryBuffer::init(InputData.begin(), InputData.end(),
89  RequiresNullTerminator);
90  }
91 
92  /// Disable sized deallocation for MemoryBufferMem, because it has
93  /// tail-allocated data.
94  void operator delete(void *p) { ::operator delete(p); }
95 
96  StringRef getBufferIdentifier() const override {
97  // The name is stored after the class itself.
98  return StringRef(reinterpret_cast<const char *>(this + 1));
99  }
100 
101  MemoryBuffer::BufferKind getBufferKind() const override {
103  }
104 };
105 } // namespace
106 
107 template <typename MB>
109 getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
110  bool IsText, bool RequiresNullTerminator, bool IsVolatile);
111 
112 std::unique_ptr<MemoryBuffer>
114  bool RequiresNullTerminator) {
115  auto *Ret = new (NamedBufferAlloc(BufferName))
116  MemoryBufferMem<MemoryBuffer>(InputData, RequiresNullTerminator);
117  return std::unique_ptr<MemoryBuffer>(Ret);
118 }
119 
120 std::unique_ptr<MemoryBuffer>
121 MemoryBuffer::getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator) {
122  return std::unique_ptr<MemoryBuffer>(getMemBuffer(
123  Ref.getBuffer(), Ref.getBufferIdentifier(), RequiresNullTerminator));
124 }
125 
127 getMemBufferCopyImpl(StringRef InputData, const Twine &BufferName) {
128  auto Buf = WritableMemoryBuffer::getNewUninitMemBuffer(InputData.size(), BufferName);
129  if (!Buf)
131  memcpy(Buf->getBufferStart(), InputData.data(), InputData.size());
132  return std::move(Buf);
133 }
134 
135 std::unique_ptr<MemoryBuffer>
136 MemoryBuffer::getMemBufferCopy(StringRef InputData, const Twine &BufferName) {
137  auto Buf = getMemBufferCopyImpl(InputData, BufferName);
138  if (Buf)
139  return std::move(*Buf);
140  return nullptr;
141 }
142 
144 MemoryBuffer::getFileOrSTDIN(const Twine &Filename, bool IsText,
145  bool RequiresNullTerminator) {
146  SmallString<256> NameBuf;
147  StringRef NameRef = Filename.toStringRef(NameBuf);
148 
149  if (NameRef == "-")
150  return getSTDIN();
151  return getFile(Filename, IsText, RequiresNullTerminator,
152  /*IsVolatile=*/false);
153 }
154 
156 MemoryBuffer::getFileSlice(const Twine &FilePath, uint64_t MapSize,
157  uint64_t Offset, bool IsVolatile) {
158  return getFileAux<MemoryBuffer>(FilePath, MapSize, Offset, /*IsText=*/false,
159  /*RequiresNullTerminator=*/false, IsVolatile);
160 }
161 
162 //===----------------------------------------------------------------------===//
163 // MemoryBuffer::getFile implementation.
164 //===----------------------------------------------------------------------===//
165 
166 namespace {
167 
168 template <typename MB>
169 constexpr sys::fs::mapped_file_region::mapmode Mapmode =
171 template <>
172 constexpr sys::fs::mapped_file_region::mapmode Mapmode<MemoryBuffer> =
174 template <>
175 constexpr sys::fs::mapped_file_region::mapmode Mapmode<WritableMemoryBuffer> =
177 template <>
179  Mapmode<WriteThroughMemoryBuffer> = sys::fs::mapped_file_region::readwrite;
180 
181 /// Memory maps a file descriptor using sys::fs::mapped_file_region.
182 ///
183 /// This handles converting the offset into a legal offset on the platform.
184 template<typename MB>
185 class MemoryBufferMMapFile : public MB {
187 
188  static uint64_t getLegalMapOffset(uint64_t Offset) {
190  }
191 
192  static uint64_t getLegalMapSize(uint64_t Len, uint64_t Offset) {
193  return Len + (Offset - getLegalMapOffset(Offset));
194  }
195 
196  const char *getStart(uint64_t Len, uint64_t Offset) {
197  return MFR.const_data() + (Offset - getLegalMapOffset(Offset));
198  }
199 
200 public:
201  MemoryBufferMMapFile(bool RequiresNullTerminator, sys::fs::file_t FD, uint64_t Len,
202  uint64_t Offset, std::error_code &EC)
203  : MFR(FD, Mapmode<MB>, getLegalMapSize(Len, Offset),
204  getLegalMapOffset(Offset), EC) {
205  if (!EC) {
206  const char *Start = getStart(Len, Offset);
207  MemoryBuffer::init(Start, Start + Len, RequiresNullTerminator);
208  }
209  }
210 
211  /// Disable sized deallocation for MemoryBufferMMapFile, because it has
212  /// tail-allocated data.
213  void operator delete(void *p) { ::operator delete(p); }
214 
215  StringRef getBufferIdentifier() const override {
216  // The name is stored after the class itself.
217  return StringRef(reinterpret_cast<const char *>(this + 1));
218  }
219 
220  MemoryBuffer::BufferKind getBufferKind() const override {
222  }
223 };
224 } // namespace
225 
228  const ssize_t ChunkSize = 4096*4;
229  SmallString<ChunkSize> Buffer;
230  // Read into Buffer until we hit EOF.
231  for (;;) {
232  Buffer.reserve(Buffer.size() + ChunkSize);
234  FD, makeMutableArrayRef(Buffer.end(), ChunkSize));
235  if (!ReadBytes)
236  return errorToErrorCode(ReadBytes.takeError());
237  if (*ReadBytes == 0)
238  break;
239  Buffer.set_size(Buffer.size() + *ReadBytes);
240  }
241 
242  return getMemBufferCopyImpl(Buffer, BufferName);
243 }
244 
246 MemoryBuffer::getFile(const Twine &Filename, bool IsText,
247  bool RequiresNullTerminator, bool IsVolatile) {
248  return getFileAux<MemoryBuffer>(Filename, /*MapSize=*/-1, /*Offset=*/0,
249  IsText, RequiresNullTerminator, IsVolatile);
250 }
251 
252 template <typename MB>
254 getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
255  uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
256  bool IsVolatile);
257 
258 template <typename MB>
260 getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
261  bool IsText, bool RequiresNullTerminator, bool IsVolatile) {
263  Filename, IsText ? sys::fs::OF_TextWithCRLF : sys::fs::OF_None);
264  if (!FDOrErr)
265  return errorToErrorCode(FDOrErr.takeError());
266  sys::fs::file_t FD = *FDOrErr;
267  auto Ret = getOpenFileImpl<MB>(FD, Filename, /*FileSize=*/-1, MapSize, Offset,
268  RequiresNullTerminator, IsVolatile);
269  sys::fs::closeFile(FD);
270  return Ret;
271 }
272 
275  return getFileAux<WritableMemoryBuffer>(
276  Filename, /*MapSize=*/-1, /*Offset=*/0, /*IsText=*/false,
277  /*RequiresNullTerminator=*/false, IsVolatile);
278 }
279 
282  uint64_t Offset, bool IsVolatile) {
283  return getFileAux<WritableMemoryBuffer>(
284  Filename, MapSize, Offset, /*IsText=*/false,
285  /*RequiresNullTerminator=*/false, IsVolatile);
286 }
287 
288 std::unique_ptr<WritableMemoryBuffer>
290  using MemBuffer = MemoryBufferMem<WritableMemoryBuffer>;
291  // Allocate space for the MemoryBuffer, the data and the name. It is important
292  // that MemoryBuffer and data are aligned so PointerIntPair works with them.
293  // TODO: Is 16-byte alignment enough? We copy small object files with large
294  // alignment expectations into this buffer.
295  SmallString<256> NameBuf;
296  StringRef NameRef = BufferName.toStringRef(NameBuf);
297  size_t AlignedStringLen = alignTo(sizeof(MemBuffer) + NameRef.size() + 1, 16);
298  size_t RealLen = AlignedStringLen + Size + 1;
299  char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow));
300  if (!Mem)
301  return nullptr;
302 
303  // The name is stored after the class itself.
304  CopyStringRef(Mem + sizeof(MemBuffer), NameRef);
305 
306  // The buffer begins after the name and must be aligned.
307  char *Buf = Mem + AlignedStringLen;
308  Buf[Size] = 0; // Null terminate buffer.
309 
310  auto *Ret = new (Mem) MemBuffer(StringRef(Buf, Size), true);
311  return std::unique_ptr<WritableMemoryBuffer>(Ret);
312 }
313 
314 std::unique_ptr<WritableMemoryBuffer>
316  auto SB = WritableMemoryBuffer::getNewUninitMemBuffer(Size, BufferName);
317  if (!SB)
318  return nullptr;
319  memset(SB->getBufferStart(), 0, Size);
320  return SB;
321 }
322 
324  size_t FileSize,
325  size_t MapSize,
326  off_t Offset,
327  bool RequiresNullTerminator,
328  int PageSize,
329  bool IsVolatile) {
330  // mmap may leave the buffer without null terminator if the file size changed
331  // by the time the last page is mapped in, so avoid it if the file size is
332  // likely to change.
333  if (IsVolatile && RequiresNullTerminator)
334  return false;
335 
336  // We don't use mmap for small files because this can severely fragment our
337  // address space.
338  if (MapSize < 4 * 4096 || MapSize < (unsigned)PageSize)
339  return false;
340 
341  if (!RequiresNullTerminator)
342  return true;
343 
344  // If we don't know the file size, use fstat to find out. fstat on an open
345  // file descriptor is cheaper than stat on a random path.
346  // FIXME: this chunk of code is duplicated, but it avoids a fstat when
347  // RequiresNullTerminator = false and MapSize != -1.
348  if (FileSize == size_t(-1)) {
350  if (sys::fs::status(FD, Status))
351  return false;
352  FileSize = Status.getSize();
353  }
354 
355  // If we need a null terminator and the end of the map is inside the file,
356  // we cannot use mmap.
357  size_t End = Offset + MapSize;
358  assert(End <= FileSize);
359  if (End != FileSize)
360  return false;
361 
362  // Don't try to map files that are exactly a multiple of the system page size
363  // if we need a null terminator.
364  if ((FileSize & (PageSize -1)) == 0)
365  return false;
366 
367 #if defined(__CYGWIN__)
368  // Don't try to map files that are exactly a multiple of the physical page size
369  // if we need a null terminator.
370  // FIXME: We should reorganize again getPageSize() on Win32.
371  if ((FileSize & (4096 - 1)) == 0)
372  return false;
373 #endif
374 
375  return true;
376 }
377 
379 getReadWriteFile(const Twine &Filename, uint64_t FileSize, uint64_t MapSize,
380  uint64_t Offset) {
383  if (!FDOrErr)
384  return errorToErrorCode(FDOrErr.takeError());
385  sys::fs::file_t FD = *FDOrErr;
386 
387  // Default is to map the full file.
388  if (MapSize == uint64_t(-1)) {
389  // If we don't know the file size, use fstat to find out. fstat on an open
390  // file descriptor is cheaper than stat on a random path.
391  if (FileSize == uint64_t(-1)) {
393  std::error_code EC = sys::fs::status(FD, Status);
394  if (EC)
395  return EC;
396 
397  // If this not a file or a block device (e.g. it's a named pipe
398  // or character device), we can't mmap it, so error out.
399  sys::fs::file_type Type = Status.type();
403 
404  FileSize = Status.getSize();
405  }
406  MapSize = FileSize;
407  }
408 
409  std::error_code EC;
410  std::unique_ptr<WriteThroughMemoryBuffer> Result(
411  new (NamedBufferAlloc(Filename))
412  MemoryBufferMMapFile<WriteThroughMemoryBuffer>(false, FD, MapSize,
413  Offset, EC));
414  if (EC)
415  return EC;
416  return std::move(Result);
417 }
418 
420 WriteThroughMemoryBuffer::getFile(const Twine &Filename, int64_t FileSize) {
421  return getReadWriteFile(Filename, FileSize, FileSize, 0);
422 }
423 
424 /// Map a subrange of the specified file as a WritableMemoryBuffer.
427  uint64_t Offset) {
428  return getReadWriteFile(Filename, -1, MapSize, Offset);
429 }
430 
431 template <typename MB>
433 getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
434  uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
435  bool IsVolatile) {
437 
438  // Default is to map the full file.
439  if (MapSize == uint64_t(-1)) {
440  // If we don't know the file size, use fstat to find out. fstat on an open
441  // file descriptor is cheaper than stat on a random path.
442  if (FileSize == uint64_t(-1)) {
444  std::error_code EC = sys::fs::status(FD, Status);
445  if (EC)
446  return EC;
447 
448  // If this not a file or a block device (e.g. it's a named pipe
449  // or character device), we can't trust the size. Create the memory
450  // buffer by copying off the stream.
451  sys::fs::file_type Type = Status.type();
454  return getMemoryBufferForStream(FD, Filename);
455 
456  FileSize = Status.getSize();
457  }
458  MapSize = FileSize;
459  }
460 
461  if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
462  PageSize, IsVolatile)) {
463  std::error_code EC;
464  std::unique_ptr<MB> Result(
465  new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile<MB>(
466  RequiresNullTerminator, FD, MapSize, Offset, EC));
467  if (!EC)
468  return std::move(Result);
469  }
470 
471 #ifdef __MVS__
472  // Set codepage auto-conversion for z/OS.
473  if (auto EC = llvm::enableAutoConversion(FD))
474  return EC;
475 #endif
476 
477  auto Buf = WritableMemoryBuffer::getNewUninitMemBuffer(MapSize, Filename);
478  if (!Buf) {
479  // Failed to create a buffer. The only way it can fail is if
480  // new(std::nothrow) returns 0.
482  }
483 
484  // Read until EOF, zero-initialize the rest.
485  MutableArrayRef<char> ToRead = Buf->getBuffer();
486  while (!ToRead.empty()) {
487  Expected<size_t> ReadBytes =
489  if (!ReadBytes)
490  return errorToErrorCode(ReadBytes.takeError());
491  if (*ReadBytes == 0) {
492  std::memset(ToRead.data(), 0, ToRead.size());
493  break;
494  }
495  ToRead = ToRead.drop_front(*ReadBytes);
496  Offset += *ReadBytes;
497  }
498 
499  return std::move(Buf);
500 }
501 
504  bool RequiresNullTerminator, bool IsVolatile) {
505  return getOpenFileImpl<MemoryBuffer>(FD, Filename, FileSize, FileSize, 0,
506  RequiresNullTerminator, IsVolatile);
507 }
508 
511  int64_t Offset, bool IsVolatile) {
512  assert(MapSize != uint64_t(-1));
513  return getOpenFileImpl<MemoryBuffer>(FD, Filename, -1, MapSize, Offset, false,
514  IsVolatile);
515 }
516 
518  // Read in all of the data from stdin, we cannot mmap stdin.
519  //
520  // FIXME: That isn't necessarily true, we should try to mmap stdin and
521  // fallback if it fails.
523 
525 }
526 
529  Expected<sys::fs::file_t> FDOrErr =
531  if (!FDOrErr)
532  return errorToErrorCode(FDOrErr.takeError());
533  sys::fs::file_t FD = *FDOrErr;
535  getMemoryBufferForStream(FD, Filename);
536  sys::fs::closeFile(FD);
537  return Ret;
538 }
539 
542  StringRef Identifier = getBufferIdentifier();
543  return MemoryBufferRef(Data, Identifier);
544 }
545 
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
MemoryBuffer.h
llvm::alignTo
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:148
getReadWriteFile
static ErrorOr< std::unique_ptr< WriteThroughMemoryBuffer > > getReadWriteFile(const Twine &Filename, uint64_t FileSize, uint64_t MapSize, uint64_t Offset)
Definition: MemoryBuffer.cpp:379
llvm::sys::fs::file_t
int file_t
Definition: FileSystem.h:60
llvm::errc::invalid_argument
@ invalid_argument
MathExtras.h
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::MemoryBuffer::getOpenFile
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator=true, bool IsVolatile=false)
Given an already-open file descriptor, read the file and return a MemoryBuffer.
Definition: MemoryBuffer.cpp:503
FileSystem.h
llvm::sys::fs::OF_None
@ OF_None
Definition: FileSystem.h:757
shouldUseMmap
static bool shouldUseMmap(sys::fs::file_t FD, size_t FileSize, size_t MapSize, off_t Offset, bool RequiresNullTerminator, int PageSize, bool IsVolatile)
Definition: MemoryBuffer.cpp:323
llvm::MemoryBuffer::~MemoryBuffer
virtual ~MemoryBuffer()
Definition: MemoryBuffer.cpp:42
llvm::WritableMemoryBuffer::getFile
static ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getFile(const Twine &Filename, bool IsVolatile=false)
Definition: MemoryBuffer.cpp:274
llvm::MemoryBuffer::getOpenFileSlice
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFileSlice(sys::fs::file_t FD, const Twine &Filename, uint64_t MapSize, int64_t Offset, bool IsVolatile=false)
Given an already-open file descriptor, map some slice of it into a MemoryBuffer.
Definition: MemoryBuffer.cpp:510
Path.h
llvm::MemoryBuffer::getSTDIN
static ErrorOr< std::unique_ptr< MemoryBuffer > > getSTDIN()
Read all of stdin into a file buffer, and return it.
Definition: MemoryBuffer.cpp:517
SmallVectorMemoryBuffer.h
llvm::sys::fs::mapped_file_region::priv
@ priv
May modify via data, but changes are lost on destruction.
Definition: FileSystem.h:1249
Errc.h
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::sys::fs::file_type::regular_file
@ regular_file
llvm::sys::fs::CD_OpenExisting
@ CD_OpenExisting
CD_OpenExisting - When opening a file:
Definition: FileSystem.h:743
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::MipsISD::Ret
@ Ret
Definition: MipsISelLowering.h:116
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
p
the resulting code requires compare and branches when and if * p
Definition: README.txt:396
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::sys::fs::readNativeFile
Expected< size_t > readNativeFile(file_t FileHandle, MutableArrayRef< char > Buf)
Reads Buf.size() bytes from FileHandle into Buf.
llvm::ArrayRef::empty
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
llvm::sys::fs::mapped_file_region::readonly
@ readonly
May only access map via const_data as read only.
Definition: FileSystem.h:1247
getMemBufferCopyImpl
static ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getMemBufferCopyImpl(StringRef InputData, const Twine &BufferName)
Definition: MemoryBuffer.cpp:127
llvm::MemoryBuffer::getFileAsStream
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileAsStream(const Twine &Filename)
Read all of the specified file into a MemoryBuffer as a stream (i.e.
Definition: MemoryBuffer.cpp:528
llvm::MutableArrayRef::drop_front
MutableArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition: ArrayRef.h:387
llvm::MemoryBuffer::getFile
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
Definition: MemoryBuffer.cpp:246
getMemoryBufferForStream
static ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getMemoryBufferForStream(sys::fs::file_t FD, const Twine &BufferName)
Definition: MemoryBuffer.cpp:227
Process.h
llvm::MemoryBuffer::getMemBuffer
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
Definition: MemoryBuffer.cpp:113
PageSize
static cl::opt< int > PageSize("imp-null-check-page-size", cl::desc("The page size of the target in bytes"), cl::init(4096), cl::Hidden)
llvm::MemoryBuffer::init
void init(const char *BufStart, const char *BufEnd, bool RequiresNullTerminator)
init - Initialize this MemoryBuffer as a reference to externally allocated memory,...
Definition: MemoryBuffer.cpp:46
llvm::MutableArrayRef< char >
llvm::ModRefInfo::Ref
@ Ref
The access may reference the value stored in memory.
SmallString.h
llvm::MemoryBuffer::MemoryBuffer_MMap
@ MemoryBuffer_MMap
Definition: MemoryBuffer.h:148
llvm::WritableMemoryBuffer::getNewMemBuffer
static std::unique_ptr< WritableMemoryBuffer > getNewMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new zero-initialized MemoryBuffer of the specified size.
Definition: MemoryBuffer.cpp:315
llvm::sys::fs::openNativeFileForRead
Expected< file_t > openNativeFileForRead(const Twine &Name, OpenFlags Flags=OF_None, SmallVectorImpl< char > *RealPath=nullptr)
Opens the file with the given name in a read-only mode, returning its open file descriptor.
llvm::MemoryBuffer::getBuffer
StringRef getBuffer() const
Definition: MemoryBuffer.h:69
llvm::sys::fs::file_type
file_type
An enumeration for the file system's view of the type.
Definition: FileSystem.h:66
llvm::SmallString< 256 >
llvm::sys::fs::OF_TextWithCRLF
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
Definition: FileSystem.h:770
llvm::MemoryBuffer::getBufferIdentifier
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
Definition: MemoryBuffer.h:75
CopyStringRef
static void CopyStringRef(char *Memory, StringRef Data)
CopyStringRef - Copies contents of a StringRef into a block of memory and null-terminates it.
Definition: MemoryBuffer.cpp:60
llvm::errc::not_enough_memory
@ not_enough_memory
llvm::sys::fs::file_status
Represents the result of a call to sys::fs::status().
Definition: FileSystem.h:226
uint64_t
llvm::sys::fs::OF_Text
@ OF_Text
The file should be opened in text mode on platforms like z/OS that make this distinction.
Definition: FileSystem.h:761
llvm::SmallVectorMemoryBuffer::~SmallVectorMemoryBuffer
~SmallVectorMemoryBuffer() override
Definition: MemoryBuffer.cpp:546
llvm::StringRef::end
iterator end() const
Definition: StringRef.h:130
llvm::MemoryBuffer::getFileSlice
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset, bool IsVolatile=false)
Map a subrange of the specified file as a MemoryBuffer.
Definition: MemoryBuffer.cpp:156
llvm::sys::fs::file_type::block_file
@ block_file
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::MemoryBuffer::getMemBufferRef
MemoryBufferRef getMemBufferRef() const
Definition: MemoryBuffer.cpp:540
llvm::MemoryBuffer::BufferKind
BufferKind
The kind of memory backing used to support the MemoryBuffer.
Definition: MemoryBuffer.h:146
llvm::MemoryBuffer::getFileOrSTDIN
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
Definition: MemoryBuffer.cpp:144
llvm::sys::fs::mapped_file_region::const_data
const char * const_data() const
Get a const view of the data.
Definition: Path.cpp:1163
AutoConvert.h
llvm::sys::fs::mapped_file_region::mapmode
mapmode
Definition: FileSystem.h:1246
llvm::sys::fs::openNativeFileForReadWrite
Expected< file_t > openNativeFileForReadWrite(const Twine &Name, CreationDisposition Disp, OpenFlags Flags, unsigned Mode=0666)
Opens the file with the given name in a write-only or read-write mode, returning its open file descri...
Definition: FileSystem.h:1101
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::WritableMemoryBuffer::getNewUninitMemBuffer
static std::unique_ptr< WritableMemoryBuffer > getNewUninitMemBuffer(size_t Size, const Twine &BufferName="")
Allocate a new MemoryBuffer of the specified size that is not initialized.
Definition: MemoryBuffer.cpp:289
memcpy
<%struct.s * > cast struct s *S to sbyte *< sbyte * > sbyte uint cast struct s *agg result to sbyte *< sbyte * > sbyte uint cast struct s *memtmp to sbyte *< sbyte * > sbyte uint ret void llc ends up issuing two memcpy or custom lower memcpy(of small size) to be ldmia/stmia. I think option 2 is better but the current register allocator cannot allocate a chunk of registers at a time. A feasible temporary solution is to use specific physical registers at the lowering time for small(<
llvm::sys::fs::closeFile
std::error_code closeFile(file_t &F)
Close the file object.
llvm::codeview::CompileSym2Flags::EC
@ EC
getOpenFileImpl
static ErrorOr< std::unique_ptr< MB > > getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator, bool IsVolatile)
Definition: MemoryBuffer.cpp:433
getFileAux
static ErrorOr< std::unique_ptr< MB > > getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset, bool IsText, bool RequiresNullTerminator, bool IsVolatile)
Definition: MemoryBuffer.cpp:260
llvm::sys::fs::mapped_file_region::readwrite
@ readwrite
May access map via data and modify it. Written to path.
Definition: FileSystem.h:1248
Status
Definition: SIModeRegister.cpp:28
llvm::sys::fs::readNativeFileSlice
Expected< size_t > readNativeFileSlice(file_t FileHandle, MutableArrayRef< char > Buf, uint64_t Offset)
Reads Buf.size() bytes from FileHandle at offset Offset into Buf.
llvm::sys::Memory
This class provides various memory handling functions that manipulate MemoryBlock instances.
Definition: Memory.h:52
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::AMDGPU::HSAMD::Kernel::Arg::Key::IsVolatile
constexpr char IsVolatile[]
Key for Kernel::Arg::Metadata::mIsVolatile.
Definition: AMDGPUMetadata.h:194
llvm::MutableArrayRef::data
T * data() const
Definition: ArrayRef.h:354
llvm::sys::fs::getStdinHandle
file_t getStdinHandle()
Return an open handle to standard in.
llvm::make_error_code
std::error_code make_error_code(BitcodeError E)
Definition: BitcodeReader.h:270
llvm::sys::fs::mapped_file_region::alignment
static int alignment()
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::sys::ChangeStdinMode
std::error_code ChangeStdinMode(fs::OpenFlags Flags)
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::errorToErrorCode
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition: Error.cpp:93
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:589
llvm::sys::fs::mapped_file_region
This class represents a memory mapped file.
Definition: FileSystem.h:1244
llvm::sys::fs::status
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
N
#define N
Program.h
llvm::WriteThroughMemoryBuffer::getFileSlice
static ErrorOr< std::unique_ptr< WriteThroughMemoryBuffer > > getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset)
Map a subrange of the specified file as a ReadWriteMemoryBuffer.
Definition: MemoryBuffer.cpp:426
llvm::ErrorOr
Represents either an error or a value T.
Definition: ErrorOr.h:56
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
llvm::WritableMemoryBuffer::getFileSlice
static ErrorOr< std::unique_ptr< WritableMemoryBuffer > > getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset, bool IsVolatile=false)
Map a subrange of the specified file as a WritableMemoryBuffer.
Definition: MemoryBuffer.cpp:281
llvm::StringRef::data
const LLVM_NODISCARD char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:149
llvm::WriteThroughMemoryBuffer::getFile
static ErrorOr< std::unique_ptr< WriteThroughMemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1)
Definition: MemoryBuffer.cpp:420
llvm::Twine::toStringRef
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition: Twine.h:477
llvm::makeMutableArrayRef
MutableArrayRef< T > makeMutableArrayRef(T &OneElt)
Construct a MutableArrayRef from a single element.
Definition: ArrayRef.h:534
llvm::sys::Process::getPageSizeEstimate
static unsigned getPageSizeEstimate()
Get the process's estimated page size.
Definition: Process.h:62
llvm::StringRef::size
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:157
llvm::MemoryBuffer::getMemBufferCopy
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it.
Definition: MemoryBuffer.cpp:136
llvm::SmallVectorImpl< char >::reserve
void reserve(size_type N)
Definition: SmallVector.h:624
llvm::StringRef::begin
iterator begin() const
Definition: StringRef.h:128
Errno.h
llvm::MemoryBuffer::MemoryBuffer_Malloc
@ MemoryBuffer_Malloc
Definition: MemoryBuffer.h:147