[PATCH] OCaml bindings: volatile/alignment for load and store instructions

Peter Zotov whitequark at whitequark.org
Sat Oct 12 09:57:22 PDT 2013


Hi sylvestre.ledru,

This commit adds the following functions for manipulating load/store instructions:
  - is_volatile
  - set_volatile
  - instr_alignment
  - set_instr_alignment

http://llvm-reviews.chandlerc.com/D1920

Files:
  bindings/ocaml/llvm/llvm.ml
  bindings/ocaml/llvm/llvm.mli
  bindings/ocaml/llvm/llvm_ocaml.c
  test/Bindings/Ocaml/vmcore.ml

Index: bindings/ocaml/llvm/llvm.ml
===================================================================
--- bindings/ocaml/llvm/llvm.ml
+++ bindings/ocaml/llvm/llvm.ml
@@ -988,6 +988,14 @@
 external is_tail_call : llvalue -> bool = "llvm_is_tail_call"
 external set_tail_call : bool -> llvalue -> unit = "llvm_set_tail_call"
 
+(*--... Operations on load/store instructions (only) .......................--*)
+external is_volatile : llvalue -> bool = "llvm_is_volatile"
+external set_volatile : bool -> llvalue -> unit = "llvm_set_volatile"
+
+external instr_alignment : llvalue -> int = "llvm_instr_alignment"
+external set_instr_alignment : int -> llvalue -> unit
+                             = "llvm_set_instr_alignment"
+
 (*--... Operations on phi nodes ............................................--*)
 external add_incoming : (llvalue * llbasicblock) -> llvalue -> unit
                       = "llvm_add_incoming"
Index: bindings/ocaml/llvm/llvm.mli
===================================================================
--- bindings/ocaml/llvm/llvm.mli
+++ bindings/ocaml/llvm/llvm.mli
@@ -1716,6 +1716,33 @@
 val set_tail_call : bool -> llvalue -> unit
 
 
+(** {Operations on load/store instructions (only)} *)
+
+(** [is_volatile i] is [true] if the load or store instruction [i] is marked
+    as volatile.
+    See the methods [llvm::LoadInst::isVolatile] and
+    [llvm::StoreInst::isVolatile]. *)
+val is_volatile : llvalue -> bool
+
+(** [set_volatile v i] marks the load or store instruction [i] as volatile
+    if [v] is [true], unmarks otherwise.
+    See the methods [llvm::LoadInst::setVolatile] and
+    [llvm::StoreInst::setVolatile]. *)
+val set_volatile : bool -> llvalue -> unit
+
+(** [instr_alignment i] is the memory access alignment of the load or
+    store instruction [i].
+    See the methods [llvm::LoadInst::getAlignment] and
+    [llvm::StoreInst::getAlignment]. *)
+val instr_alignment : llvalue -> int
+
+(** [set_instr_alignment a i] sets the memory access alignment of
+    the load or store instruction [i].
+    See the methods [llvm::LoadInst::setAlignment] and
+    [llvm::StoreInst::setAlignment]. *)
+val set_instr_alignment : int -> llvalue -> unit
+
+
 (** {7 Operations on phi nodes} *)
 
 (** [add_incoming (v, bb) pn] adds the value [v] to the phi node [pn] for use
Index: bindings/ocaml/llvm/llvm_ocaml.c
===================================================================
--- bindings/ocaml/llvm/llvm_ocaml.c
+++ bindings/ocaml/llvm/llvm_ocaml.c
@@ -1283,6 +1283,32 @@
   return Val_unit;
 }
 
+/*--... Operations on load/store instructions (only)........................--*/
+
+/* llvalue -> bool */
+CAMLprim value llvm_is_volatile(LLVMValueRef MemoryInst) {
+  return Val_bool(LLVMGetVolatile(MemoryInst));
+}
+
+/* bool -> llvalue -> unit */
+CAMLprim value llvm_set_volatile(value IsVolatile,
+                                  LLVMValueRef MemoryInst) {
+  LLVMSetVolatile(MemoryInst, Bool_val(IsVolatile));
+  return Val_unit;
+}
+
+/* llvalue -> int */
+CAMLprim value llvm_instr_alignment(LLVMValueRef MemoryInst) {
+  return Val_int(LLVMGetInstructionAlignment(MemoryInst));
+}
+
+/* int -> llvalue -> unit */
+CAMLprim value llvm_set_instr_alignment(value Alignment,
+                                        LLVMValueRef MemoryInst) {
+  LLVMSetInstructionAlignment(MemoryInst, Int_val(Alignment));
+  return Val_unit;
+}
+
 /*--... Operations on phi nodes ............................................--*/
 
 /* (llvalue * llbasicblock) -> llvalue -> unit */
Index: test/Bindings/Ocaml/vmcore.ml
===================================================================
--- test/Bindings/Ocaml/vmcore.ml
+++ test/Bindings/Ocaml/vmcore.ml
@@ -1266,7 +1266,7 @@
     (* CHECK: %build_alloca = alloca i32
      * CHECK: %build_array_alloca = alloca i32, i32 %P2
      * CHECK: %build_load = load i32* %build_array_alloca
-     * CHECK: store i32 %P2, i32* %build_alloca
+     * CHECK: store volatile i32 %P2, i32* %build_alloca, align 4
      * CHECK: %build_gep = getelementptr i32* %build_array_alloca, i32 %P2
      * CHECK: %build_in_bounds_gep = getelementptr inbounds i32* %build_array_alloca, i32 %P2
      * CHECK: %build_struct_gep = getelementptr inbounds{{.*}}%build_alloca2, i32 0, i32 1
@@ -1274,7 +1274,11 @@
     let alloca = build_alloca i32_type "build_alloca" b in
     let array_alloca = build_array_alloca i32_type p2 "build_array_alloca" b in
     ignore(build_load array_alloca "build_load" b);
-    ignore(build_store p2 alloca b);
+    let store = build_store p2 alloca b in
+    ignore(set_volatile true store);
+    insist(true = is_volatile store);
+    ignore(set_instr_alignment 4 store);
+    insist(4 = instr_alignment store);
     ignore(build_gep array_alloca [| p2 |] "build_gep" b);
     ignore(build_in_bounds_gep array_alloca [| p2 |] "build_in_bounds_gep" b);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1920.1.patch
Type: text/x-patch
Size: 4855 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20131012/0fabd437/attachment.bin>


More information about the llvm-commits mailing list