Skip to content

Commit cba8644

Browse files
committed
ir/constant: relax gep expression indices to allow *Int without wrapping
This is analogous to allowing value.Value for call instruction arguments, instead of requiring wrapping with *ir.Arg.
1 parent 63a2856 commit cba8644

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

asm/const_expr.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -665,9 +665,9 @@ func (gen *generator) irGetElementPtrExpr(t types.Type, old *ast.GetElementPtrEx
665665
return nil, errors.WithStack(err)
666666
}
667667
// Indices.
668-
var indices []*constant.Index
668+
var indices []constant.Constant
669669
if oldIndices := old.Indices(); len(oldIndices) > 0 {
670-
indices = make([]*constant.Index, len(oldIndices))
670+
indices = make([]constant.Constant, len(oldIndices))
671671
for i, oldIndex := range oldIndices {
672672
index, err := gen.irGEPIndex(oldIndex)
673673
if err != nil {

ir/constant/expr_memory.go

+18-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type ExprGetElementPtr struct {
1818
// Source address.
1919
Src Constant
2020
// Element indicies.
21-
Indices []*Index
21+
Indices []Constant // *Int, *Vector or *Index
2222

2323
// extra.
2424

@@ -31,7 +31,7 @@ type ExprGetElementPtr struct {
3131

3232
// NewGetElementPtr returns a new getelementptr expression based on the given
3333
// source address and element indices.
34-
func NewGetElementPtr(src Constant, indices ...*Index) *ExprGetElementPtr {
34+
func NewGetElementPtr(src Constant, indices ...Constant) *ExprGetElementPtr {
3535
e := &ExprGetElementPtr{Src: src, Indices: indices}
3636
// Compute type.
3737
e.Type()
@@ -96,7 +96,7 @@ func (e *ExprGetElementPtr) Simplify() Constant {
9696
// Index is an index of a getelementptr constant expression.
9797
type Index struct {
9898
// Element index.
99-
Index Constant
99+
Constant
100100

101101
// extra.
102102

@@ -108,26 +108,30 @@ type Index struct {
108108

109109
// NewIndex returns a new gep element index.
110110
func NewIndex(index Constant) *Index {
111-
return &Index{Index: index}
111+
return &Index{Constant: index}
112112
}
113113

114114
// String returns a string representation of the getelementptr index.
115115
func (index *Index) String() string {
116116
// OptInrange Type Constant
117117
if index.InRange {
118-
return fmt.Sprintf("inrange %s", index.Index)
118+
return fmt.Sprintf("inrange %s", index.Constant)
119119
}
120-
return index.Index.String()
120+
return index.Constant.String()
121121
}
122122

123123
// ### [ Helper functions ] ####################################################
124124

125125
// gepType returns the pointer type or vector of pointers type to the element at
126126
// the position in the type specified by the given indices, as calculated by the
127127
// getelementptr instruction.
128-
func gepType(elemType types.Type, indices []*Index) types.Type {
128+
func gepType(elemType types.Type, indices []Constant) types.Type {
129129
e := elemType
130130
for i, index := range indices {
131+
// unpack inrange indices.
132+
if idx, ok := index.(*Index); ok {
133+
index = idx.Constant
134+
}
131135
if i == 0 {
132136
// Ignore checking the 0th index as it simply follows the pointer of
133137
// src.
@@ -144,7 +148,7 @@ func gepType(elemType types.Type, indices []*Index) types.Type {
144148
case *types.ArrayType:
145149
e = t.ElemType
146150
case *types.StructType:
147-
switch index := index.Index.(type) {
151+
switch index := index.(type) {
148152
case *Int:
149153
e = t.Fields[index.X.Int64()]
150154
case *Vector:
@@ -183,7 +187,12 @@ func gepType(elemType types.Type, indices []*Index) types.Type {
183187
// %116 = bitcast i8** %115 to <2 x %struct.fileinfo*>*, !dbg !4738
184188
// store <2 x %struct.fileinfo*> %113, <2 x %struct.fileinfo*>* %116, align 8, !dbg !4738, !tbaa !1793
185189
if len(indices) > 0 {
186-
if t, ok := indices[0].Index.Type().(*types.VectorType); ok {
190+
index := indices[0]
191+
// unpack inrange index.
192+
if idx, ok := index.(*Index); ok {
193+
index = idx.Constant
194+
}
195+
if t, ok := index.Type().(*types.VectorType); ok {
187196
return types.NewVector(t.Len, types.NewPointer(e))
188197
}
189198
}

0 commit comments

Comments
 (0)