File: load-combine-like-or.ll

package info (click to toggle)
llvm-toolchain-14 1%3A14.0.6-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,496,180 kB
  • sloc: cpp: 5,593,972; ansic: 986,872; asm: 585,869; python: 184,223; objc: 72,530; lisp: 31,119; f90: 27,793; javascript: 9,780; pascal: 9,762; sh: 9,482; perl: 7,468; ml: 5,432; awk: 3,523; makefile: 2,538; xml: 953; cs: 573; fortran: 567
file content (153 lines) | stat: -rw-r--r-- 5,215 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -reassociate -S | FileCheck %s

; Basic pattern where two contiguous i8 loads form a wider i16 load
define i16 @p0_i8_i8_i16(i8* %ptr) {
; CHECK-LABEL: @p0_i8_i8_i16(
; CHECK-NEXT:    [[I:%.*]] = getelementptr inbounds i8, i8* [[PTR:%.*]], i64 1
; CHECK-NEXT:    [[I2:%.*]] = load i8, i8* [[I]], align 1
; CHECK-NEXT:    [[I3:%.*]] = zext i8 [[I2]] to i16
; CHECK-NEXT:    [[I4:%.*]] = shl i16 [[I3]], 8
; CHECK-NEXT:    [[I5:%.*]] = load i8, i8* [[PTR]], align 1
; CHECK-NEXT:    [[I6:%.*]] = zext i8 [[I5]] to i16
; CHECK-NEXT:    [[I7:%.*]] = or i16 [[I4]], [[I6]]
; CHECK-NEXT:    [[I8:%.*]] = add i16 [[I7]], 42
; CHECK-NEXT:    ret i16 [[I8]]
;
  %i = getelementptr inbounds i8, i8* %ptr, i64 1
  %i2 = load i8, i8* %i
  %i3 = zext i8 %i2 to i16
  %i4 = shl i16 %i3, 8
  %i5 = load i8, i8* %ptr
  %i6 = zext i8 %i5 to i16
  %i7 = or i16 %i4, %i6
  %i8 = add i16 %i7, 42
  ret i16 %i8
}

; Basic pattern where two contiguous i8 loads form a wider i16 load, with swapped endianness
define i16 @p1_i8_i8_i16_swapped(i8* %ptr) {
; CHECK-LABEL: @p1_i8_i8_i16_swapped(
; CHECK-NEXT:    [[I:%.*]] = load i8, i8* [[PTR:%.*]], align 1
; CHECK-NEXT:    [[I2:%.*]] = zext i8 [[I]] to i16
; CHECK-NEXT:    [[I3:%.*]] = shl i16 [[I2]], 8
; CHECK-NEXT:    [[I4:%.*]] = getelementptr inbounds i8, i8* [[PTR]], i64 1
; CHECK-NEXT:    [[I5:%.*]] = load i8, i8* [[I4]], align 1
; CHECK-NEXT:    [[I6:%.*]] = zext i8 [[I5]] to i16
; CHECK-NEXT:    [[I7:%.*]] = or i16 [[I3]], [[I6]]
; CHECK-NEXT:    [[I8:%.*]] = add i16 [[I7]], 42
; CHECK-NEXT:    ret i16 [[I8]]
;
  %i = load i8, i8* %ptr
  %i2 = zext i8 %i to i16
  %i3 = shl i16 %i2, 8
  %i4 = getelementptr inbounds i8, i8* %ptr, i64 1
  %i5 = load i8, i8* %i4
  %i6 = zext i8 %i5 to i16
  %i7 = or i16 %i3, %i6
  %i8 = add i16 %i7, 42
  ret i16 %i8
}

; Loads are spaced out by a bit, but we don't check for that.
define i16 @p2(i8* %ptr) {
; CHECK-LABEL: @p2(
; CHECK-NEXT:    [[I:%.*]] = getelementptr inbounds i8, i8* [[PTR:%.*]], i64 1
; CHECK-NEXT:    [[I2:%.*]] = load i8, i8* [[I]], align 1
; CHECK-NEXT:    [[I3:%.*]] = zext i8 [[I2]] to i16
; CHECK-NEXT:    [[I4:%.*]] = shl i16 [[I3]], 9
; CHECK-NEXT:    [[I5:%.*]] = load i8, i8* [[PTR]], align 1
; CHECK-NEXT:    [[I6:%.*]] = zext i8 [[I5]] to i16
; CHECK-NEXT:    [[I7:%.*]] = or i16 [[I4]], [[I6]]
; CHECK-NEXT:    [[I8:%.*]] = add i16 [[I7]], 42
; CHECK-NEXT:    ret i16 [[I8]]
;
  %i = getelementptr inbounds i8, i8* %ptr, i64 1
  %i2 = load i8, i8* %i
  %i3 = zext i8 %i2 to i16
  %i4 = shl i16 %i3, 9 ; wrong shift amount
  %i5 = load i8, i8* %ptr
  %i6 = zext i8 %i5 to i16
  %i7 = or i16 %i4, %i6
  %i8 = add i16 %i7, 42
  ret i16 %i8
}

; Both bytes are the same, but we don't check for that.
define i16 @p3(i8* %ptr) {
; CHECK-LABEL: @p3(
; CHECK-NEXT:    [[I:%.*]] = load i8, i8* [[PTR:%.*]], align 1
; CHECK-NEXT:    [[I2:%.*]] = zext i8 [[I]] to i16
; CHECK-NEXT:    [[I3:%.*]] = shl i16 [[I2]], 8
; CHECK-NEXT:    [[I4:%.*]] = or i16 [[I3]], [[I2]]
; CHECK-NEXT:    [[I5:%.*]] = add i16 [[I4]], 42
; CHECK-NEXT:    ret i16 [[I5]]
;
  %i = load i8, i8* %ptr
  %i2 = zext i8 %i to i16
  %i3 = shl i16 %i2, 8
  %i4 = or i16 %i3, %i2
  %i5 = add i16 %i4, 42
  ret i16 %i5
}

; ---------------------------------------------------------------------------- ;
; Negative tests, should be transformed.

; Low bits are not a load
define i16 @n4(i8* %ptr) {
; CHECK-LABEL: @n4(
; CHECK-NEXT:    [[I:%.*]] = load i8, i8* [[PTR:%.*]], align 1
; CHECK-NEXT:    [[I2:%.*]] = zext i8 [[I]] to i16
; CHECK-NEXT:    [[I3:%.*]] = shl i16 [[I2]], 8
; CHECK-NEXT:    [[I5:%.*]] = add i16 [[I3]], 84
; CHECK-NEXT:    ret i16 [[I5]]
;
  %i = load i8, i8* %ptr
  %i2 = zext i8 %i to i16
  %i3 = shl i16 %i2, 8
  %i4 = or i16 %i3, 42 ; Second operand is bad
  %i5 = add i16 %i4, 42
  ret i16 %i5
}

; Low bits are not a load
define i16 @n5(i8* %ptr, i8 %lowbits) {
; CHECK-LABEL: @n5(
; CHECK-NEXT:    [[I:%.*]] = load i8, i8* [[PTR:%.*]], align 1
; CHECK-NEXT:    [[I2:%.*]] = zext i8 [[I]] to i16
; CHECK-NEXT:    [[I3:%.*]] = shl i16 [[I2]], 8
; CHECK-NEXT:    [[I4:%.*]] = zext i8 [[LOWBITS:%.*]] to i16
; CHECK-NEXT:    [[I5:%.*]] = add i16 [[I4]], 42
; CHECK-NEXT:    [[I6:%.*]] = add i16 [[I5]], [[I3]]
; CHECK-NEXT:    ret i16 [[I6]]
;
  %i = load i8, i8* %ptr
  %i2 = zext i8 %i to i16
  %i3 = shl i16 %i2, 8
  %i4 = zext i8 %lowbits to i16 ; base operand is bad
  %i5 = or i16 %i3, %i4
  %i6 = add i16 %i5, 42
  ret i16 %i6
}

; High bits are not a load
define i16 @n6(i8* %ptr, i8 %highbits) {
; CHECK-LABEL: @n6(
; CHECK-NEXT:    [[I:%.*]] = getelementptr inbounds i8, i8* [[PTR:%.*]], i64 1
; CHECK-NEXT:    [[I4:%.*]] = shl i16 42, 8
; CHECK-NEXT:    [[I5:%.*]] = load i8, i8* [[PTR]], align 1
; CHECK-NEXT:    [[I6:%.*]] = zext i8 [[I5]] to i16
; CHECK-NEXT:    [[I7:%.*]] = add i16 [[I4]], 42
; CHECK-NEXT:    [[I8:%.*]] = add i16 [[I7]], [[I6]]
; CHECK-NEXT:    ret i16 [[I8]]
;
  %i = getelementptr inbounds i8, i8* %ptr, i64 1
  %i2 = load i8, i8* %i
  %i4 = shl i16 42, 8 ; base operand is bad
  %i5 = load i8, i8* %ptr
  %i6 = zext i8 %i5 to i16
  %i7 = or i16 %i4, %i6
  %i8 = add i16 %i7, 42
  ret i16 %i8
}