From b913a5137e8c02fa9eb55868cf95482d314c3dd6 Mon Sep 17 00:00:00 2001 From: Reid Spencer Date: Fri, 9 Feb 2007 17:56:02 +0000 Subject: [PATCH] int -> i32, etc. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@34102 91177308-0d34-0410-b5e6-96231b3b80d8 --- docs/GetElementPtr.html | 84 ++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/docs/GetElementPtr.html b/docs/GetElementPtr.html index dacc341bf1c..710273fb6da 100644 --- a/docs/GetElementPtr.html +++ b/docs/GetElementPtr.html @@ -120,13 +120,13 @@
   void %munge(%struct.munger_struct* %P) {
   entry:
-    %tmp = getelementptr %struct.munger_struct* %P, int 1, uint 0
-    %tmp = load int* %tmp
-    %tmp6 = getelementptr %struct.munger_struct* %P, int 2, uint 1
-    %tmp7 = load int* %tmp6
-    %tmp8 = add int %tmp7, %tmp
-    %tmp9 = getelementptr %struct.munger_struct* %P, int 0, uint 0
-    store int %tmp8, int* %tmp9
+    %tmp = getelementptr %struct.munger_struct* %P, i32 1, i32 0
+    %tmp = load i32* %tmp
+    %tmp6 = getelementptr %struct.munger_struct* %P, i32 2, i32 1
+    %tmp7 = load i32* %tmp6
+    %tmp8 = add i32 %tmp7, %tmp
+    %tmp9 = getelementptr %struct.munger_struct* %P, i32 0, i32 0
+    store i32 %tmp8, i32* %tmp9
     ret void
   }

In each case the first operand is the pointer through which the GEP @@ -134,11 +134,11 @@ argument, allocated memory, or a global variable.

To make this clear, let's consider a more obtuse example:

-  %MyVar = unintialized global int
+  %MyVar = unintialized global i32
   ...
-  %idx1 = getelementptr int* %MyVar, long 0
-  %idx2 = getelementptr int* %MyVar, long 1
-  %idx3 = getelementptr int* %MyVar, long 2
+ %idx1 = getelementptr i32* %MyVar, i64 0 + %idx2 = getelementptr i32* %MyVar, i64 1 + %idx3 = getelementptr i32* %MyVar, i64 2

These GEP instructions are simply making address computations from the base address of MyVar. They compute, as follows (using C syntax):

@@ -147,14 +147,14 @@
  • idx2 = (char*) &MyVar + 4
  • idx3 = (char*) &MyVar + 8
  • -

    Since the type int is known to be four bytes long, the indices +

    Since the type i32 is known to be four bytes long, the indices 0, 1 and 2 translate into memory offsets of 0, 4, and 8, respectively. No memory is accessed to make these computations because the address of %MyVar is passed directly to the GEP instructions.

    The obtuse part of this example is in the cases of %idx2 and %idx3. They result in the computation of addresses that point to memory past the end of the %MyVar global, which is only one - int long, not three ints long. While this is legal in LLVM, + i32 long, not three i32s long. While this is legal in LLVM, it is inadvisable because any load or store with the pointer that results from these GEP instructions would produce undefined results.

    @@ -169,29 +169,29 @@

    This question arises most often when the GEP instruction is applied to a global variable which is always a pointer type. For example, consider this:

    -  %MyStruct = uninitialized global { float*, int }
    +  %MyStruct = uninitialized global { float*, i32 }
       ...
    -  %idx = getelementptr { float*, int }* %MyStruct, long 0, ubyte 1
    -

    The GEP above yields an int* by indexing the int typed + %idx = getelementptr { float*, i32 }* %MyStruct, i64 0, i32 1 +

    The GEP above yields an i32* by indexing the i32 typed field of the structure %MyStruct. When people first look at it, they - wonder why the long 0 index is needed. However, a closer inspection + wonder why the i64 0 index is needed. However, a closer inspection of how globals and GEPs work reveals the need. Becoming aware of the following facts will dispell the confusion:

      -
    1. The type of %MyStruct is not { float*, int } - but rather { float*, int }*. That is, %MyStruct is a +
    2. The type of %MyStruct is not { float*, i32 } + but rather { float*, i32 }*. That is, %MyStruct is a pointer to a structure containing a pointer to a float and an - int.
    3. + i32.
    4. Point #1 is evidenced by noticing the type of the first operand of the GEP instruction (%MyStruct) which is - { float*, int }*.
    5. -
    6. The first index, long 0 is required to step over the global + { float*, i32 }*.
    7. +
    8. The first index, i64 0 is required to step over the global variable %MyStruct. Since the first argument to the GEP instruction must always be a value of pointer type, the first index steps through that pointer. A value of 0 means 0 elements offset from that pointer.
    9. -
    10. The second index, ubyte 1 selects the second field of the - structure (the int).
    11. +
    12. The second index, i32 1 selects the second field of the + structure (the i32).
    @@ -206,9 +206,9 @@ GEP is only involved in the computation of addresses. For example, consider this:

    -  %MyVar = uninitialized global { [40 x int ]* }
    +  %MyVar = uninitialized global { [40 x i32 ]* }
       ...
    -  %idx = getelementptr { [40 x int]* }* %MyVar, long 0, ubyte 0, long 0, long 17
    + %idx = getelementptr { [40 x i32]* }* %MyVar, i64 0, i32 0, i64 0, i64 17

    In this example, we have a global variable, %MyVar that is a pointer to a structure containing a pointer to an array of 40 ints. The GEP instruction seems to be accessing the 18th integer of the structure's @@ -219,19 +219,19 @@

    In order to access the 18th integer in the array, you would need to do the following:

    -  %idx = getelementptr { [40 x int]* }* %, long 0, ubyte 0
    -  %arr = load [40 x int]** %idx
    -  %idx = getelementptr [40 x int]* %arr, long 0, long 17
    + %idx = getelementptr { [40 x i32]* }* %, i64 0, i32 0 + %arr = load [40 x i32]** %idx + %idx = getelementptr [40 x i32]* %arr, i64 0, i64 17

    In this case, we have to load the pointer in the structure with a load instruction before we can index into the array. If the example was changed to:

    -  %MyVar = uninitialized global { [40 x int ] }
    +  %MyVar = uninitialized global { [40 x i32 ] }
       ...
    -  %idx = getelementptr { [40 x int] }*, long 0, ubyte 0, long 17
    + %idx = getelementptr { [40 x i32] }*, i64 0, i32 0, i64 17

    then everything works fine. In this case, the structure does not contain a pointer and the GEP instruction can index through the global variable, - into the first field of the structure and access the 18th int in the + into the first field of the structure and access the 18th i32 in the array there.

    @@ -245,14 +245,14 @@ instructions you find that they are different (0 and 1), therefore the address computation diverges with that index. Consider this example:

    -  %MyVar = global { [10 x int ] }
    -  %idx1 = getlementptr { [10 x int ] }* %MyVar, long 0, ubyte 0, long 1
    -  %idx2 = getlementptr { [10 x int ] }* %MyVar, long 1
    + %MyVar = global { [10 x i32 ] } + %idx1 = getlementptr { [10 x i32 ] }* %MyVar, i64 0, i32 0, i64 1 + %idx2 = getlementptr { [10 x i32 ] }* %MyVar, i64 1

    In this example, idx1 computes the address of the second integer in the array that is in the structure in %MyVar, that is MyVar+4. The - type of idx1 is int*. However, idx2 computes the + type of idx1 is i32*. However, idx2 computes the address of the next structure after %MyVar. The type of - idx2 is { [10 x int] }* and its value is equivalent + idx2 is { [10 x i32] }* and its value is equivalent to MyVar + 40 because it indexes past the ten 4-byte integers in MyVar. Obviously, in such a situation, the pointers don't alias.

    @@ -268,12 +268,12 @@ through the 0th element does not change the address. However, it does change the type. Consider this example:

    -  %MyVar = global { [10 x int ] }
    -  %idx1 = getlementptr { [10 x int ] }* %MyVar, long 1, ubyte 0, long 0
    -  %idx2 = getlementptr { [10 x int ] }* %MyVar, long 1
    + %MyVar = global { [10 x i32 ] } + %idx1 = getlementptr { [10 x i32 ] }* %MyVar, i64 1, i32 0, i64 0 + %idx2 = getlementptr { [10 x i32 ] }* %MyVar, i64 1

    In this example, the value of %idx1 is %MyVar+40 and - its type is int*. The value of %idx2 is also - MyVar+40 but its type is { [10 x int] }*.

    + its type is i32*. The value of %idx2 is also + MyVar+40 but its type is { [10 x i32] }*.

    -- 2.34.1