From 1779f2213f108bc688f9fe32c1fa58b95a0a4aa9 Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Fri, 6 Mar 2009 22:26:07 +0000 Subject: [PATCH] Add IRBuilder::CreateConstGEP{1, 2}_{32, 64}. - Names are lame, but it seems better to be explicit. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@66295 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/IRBuilder.h | 40 ++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/include/llvm/Support/IRBuilder.h b/include/llvm/Support/IRBuilder.h index fb78eee8e1d..61e12af811c 100644 --- a/include/llvm/Support/IRBuilder.h +++ b/include/llvm/Support/IRBuilder.h @@ -318,16 +318,48 @@ public: return Folder.CreateGetElementPtr(PC, &IC, 1); return Insert(GetElementPtrInst::Create(Ptr, Idx), Name); } - Value *CreateStructGEP(Value *Ptr, unsigned Idx, const char *Name = "") { + Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const char *Name = "") { + llvm::Value *Idx = ConstantInt::get(llvm::Type::Int32Ty, Idx0); + + if (Constant *PC = dyn_cast(Ptr)) + return Folder.CreateGetElementPtr(PC, &Idx, 1); + + return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name); + } + Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1, + const char *Name = "") { llvm::Value *Idxs[] = { - ConstantInt::get(llvm::Type::Int32Ty, 0), - ConstantInt::get(llvm::Type::Int32Ty, Idx) + ConstantInt::get(llvm::Type::Int32Ty, Idx0), + ConstantInt::get(llvm::Type::Int32Ty, Idx1) }; if (Constant *PC = dyn_cast(Ptr)) return Folder.CreateGetElementPtr(PC, Idxs, 2); - return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name); + return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name); + } + Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const char *Name = "") { + llvm::Value *Idx = ConstantInt::get(llvm::Type::Int64Ty, Idx0); + + if (Constant *PC = dyn_cast(Ptr)) + return Folder.CreateGetElementPtr(PC, &Idx, 1); + + return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name); + } + Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1, + const char *Name = "") { + llvm::Value *Idxs[] = { + ConstantInt::get(llvm::Type::Int64Ty, Idx0), + ConstantInt::get(llvm::Type::Int64Ty, Idx1) + }; + + if (Constant *PC = dyn_cast(Ptr)) + return Folder.CreateGetElementPtr(PC, Idxs, 2); + + return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name); + } + Value *CreateStructGEP(Value *Ptr, unsigned Idx, const char *Name = "") { + return CreateConstGEP2_32(Ptr, 0, Idx, Name); } Value *CreateGlobalString(const char *Str = "", const char *Name = "") { Constant *StrConstant = ConstantArray::get(Str, true);