From d7ea0efb9d8e8b27357e983c4231a2a50605555a Mon Sep 17 00:00:00 2001
From: Peter Zotov <whitequark@whitequark.org>
Date: Sun, 26 May 2013 18:27:17 +0400
Subject: [PATCH] Expose {Load,Store}Inst {get,set}Alignment to the C API.

Existing LLVM{Get,Set}Alignment functions do not work on instructions,
as they use unwrap<GlobalValue>.

There are three variants of shortening "Instruction" in the C API:
 * LLVMGetInstructionCallConv
 * LLVMAddInstrAttribute
 * LLVMSetInstDebugLocation

This commit uses the conservative variant, "Instruction".
---
 include/llvm-c/Core.h |    2 ++
 lib/IR/Core.cpp       |   14 ++++++++++++++
 2 files changed, 16 insertions(+)

diff --git a/include/llvm-c/Core.h b/include/llvm-c/Core.h
index 6b62f33..b7da080 100644
--- a/include/llvm-c/Core.h
+++ b/include/llvm-c/Core.h
@@ -2492,6 +2492,8 @@ LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
                                       const char *Name);
 LLVMBool LLVMGetVolatile(LLVMValueRef MemoryAccessInst);
 void LLVMSetVolatile(LLVMValueRef MemoryAccessInst, LLVMBool IsVolatile);
+unsigned LLVMGetInstructionAlignment(LLVMValueRef MemoryAccessInst);
+void LLVMSetInstructionAlignment(LLVMValueRef MemoryAccessInst, unsigned Alignment);
 
 /* Casts */
 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef, LLVMValueRef Val,
diff --git a/lib/IR/Core.cpp b/lib/IR/Core.cpp
index 889d574..ce66329 100644
--- a/lib/IR/Core.cpp
+++ b/lib/IR/Core.cpp
@@ -2201,6 +2201,20 @@ void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
   return cast<StoreInst>(P)->setVolatile(isVolatile);
 }
 
+unsigned LLVMGetInstructionAlignment(LLVMValueRef MemAccessInst) {
+	Value *P = unwrap<Value>(MemAccessInst);
+	if (LoadInst *LI = dyn_cast<LoadInst>(P))
+		return LI->getAlignment();
+	return cast<StoreInst>(P)->getAlignment();
+}
+
+void LLVMSetInstructionAlignment(LLVMValueRef MemAccessInst, LLVMBool Alignment) {
+	Value *P = unwrap<Value>(MemAccessInst);
+	if (LoadInst *LI = dyn_cast<LoadInst>(P))
+		return LI->setAlignment(Alignment);
+	return cast<StoreInst>(P)->setAlignment(Alignment);
+}
+
 /*--.. Casts ...............................................................--*/
 
 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
-- 
1.7.10.4

