README_ALTIVEC.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. //===- README_ALTIVEC.txt - Notes for improving Altivec code gen ----------===//
  2. Implement PPCInstrInfo::isLoadFromStackSlot/isStoreToStackSlot for vector
  3. registers, to generate better spill code.
  4. //===----------------------------------------------------------------------===//
  5. The first should be a single lvx from the constant pool, the second should be
  6. a xor/stvx:
  7. void foo(void) {
  8. int x[8] __attribute__((aligned(128))) = { 1, 1, 1, 17, 1, 1, 1, 1 };
  9. bar (x);
  10. }
  11. #include <string.h>
  12. void foo(void) {
  13. int x[8] __attribute__((aligned(128)));
  14. memset (x, 0, sizeof (x));
  15. bar (x);
  16. }
  17. //===----------------------------------------------------------------------===//
  18. Altivec: Codegen'ing MUL with vector FMADD should add -0.0, not 0.0:
  19. http://gcc.gnu.org/bugzilla/show_bug.cgi?id=8763
  20. When -ffast-math is on, we can use 0.0.
  21. //===----------------------------------------------------------------------===//
  22. Consider this:
  23. v4f32 Vector;
  24. v4f32 Vector2 = { Vector.X, Vector.X, Vector.X, Vector.X };
  25. Since we know that "Vector" is 16-byte aligned and we know the element offset
  26. of ".X", we should change the load into a lve*x instruction, instead of doing
  27. a load/store/lve*x sequence.
  28. //===----------------------------------------------------------------------===//
  29. Implement passing vectors by value into calls and receiving them as arguments.
  30. //===----------------------------------------------------------------------===//
  31. GCC apparently tries to codegen { C1, C2, Variable, C3 } as a constant pool load
  32. of C1/C2/C3, then a load and vperm of Variable.
  33. //===----------------------------------------------------------------------===//
  34. We need a way to teach tblgen that some operands of an intrinsic are required to
  35. be constants. The verifier should enforce this constraint.
  36. //===----------------------------------------------------------------------===//
  37. We currently codegen SCALAR_TO_VECTOR as a store of the scalar to a 16-byte
  38. aligned stack slot, followed by a load/vperm. We should probably just store it
  39. to a scalar stack slot, then use lvsl/vperm to load it. If the value is already
  40. in memory this is a big win.
  41. //===----------------------------------------------------------------------===//
  42. extract_vector_elt of an arbitrary constant vector can be done with the
  43. following instructions:
  44. vTemp = vec_splat(v0,2); // 2 is the element the src is in.
  45. vec_ste(&destloc,0,vTemp);
  46. We can do an arbitrary non-constant value by using lvsr/perm/ste.
  47. //===----------------------------------------------------------------------===//
  48. If we want to tie instruction selection into the scheduler, we can do some
  49. constant formation with different instructions. For example, we can generate
  50. "vsplti -1" with "vcmpequw R,R" and 1,1,1,1 with "vsubcuw R,R", and 0,0,0,0 with
  51. "vsplti 0" or "vxor", each of which use different execution units, thus could
  52. help scheduling.
  53. This is probably only reasonable for a post-pass scheduler.
  54. //===----------------------------------------------------------------------===//
  55. For this function:
  56. void test(vector float *A, vector float *B) {
  57. vector float C = (vector float)vec_cmpeq(*A, *B);
  58. if (!vec_any_eq(*A, *B))
  59. *B = (vector float){0,0,0,0};
  60. *A = C;
  61. }
  62. we get the following basic block:
  63. ...
  64. lvx v2, 0, r4
  65. lvx v3, 0, r3
  66. vcmpeqfp v4, v3, v2
  67. vcmpeqfp. v2, v3, v2
  68. bne cr6, LBB1_2 ; cond_next
  69. The vcmpeqfp/vcmpeqfp. instructions currently cannot be merged when the
  70. vcmpeqfp. result is used by a branch. This can be improved.
  71. //===----------------------------------------------------------------------===//
  72. The code generated for this is truly aweful:
  73. vector float test(float a, float b) {
  74. return (vector float){ 0.0, a, 0.0, 0.0};
  75. }
  76. LCPI1_0: ; float
  77. .space 4
  78. .text
  79. .globl _test
  80. .align 4
  81. _test:
  82. mfspr r2, 256
  83. oris r3, r2, 4096
  84. mtspr 256, r3
  85. lis r3, ha16(LCPI1_0)
  86. addi r4, r1, -32
  87. stfs f1, -16(r1)
  88. addi r5, r1, -16
  89. lfs f0, lo16(LCPI1_0)(r3)
  90. stfs f0, -32(r1)
  91. lvx v2, 0, r4
  92. lvx v3, 0, r5
  93. vmrghw v3, v3, v2
  94. vspltw v2, v2, 0
  95. vmrghw v2, v2, v3
  96. mtspr 256, r2
  97. blr
  98. //===----------------------------------------------------------------------===//
  99. int foo(vector float *x, vector float *y) {
  100. if (vec_all_eq(*x,*y)) return 3245;
  101. else return 12;
  102. }
  103. A predicate compare being used in a select_cc should have the same peephole
  104. applied to it as a predicate compare used by a br_cc. There should be no
  105. mfcr here:
  106. _foo:
  107. mfspr r2, 256
  108. oris r5, r2, 12288
  109. mtspr 256, r5
  110. li r5, 12
  111. li r6, 3245
  112. lvx v2, 0, r4
  113. lvx v3, 0, r3
  114. vcmpeqfp. v2, v3, v2
  115. mfcr r3, 2
  116. rlwinm r3, r3, 25, 31, 31
  117. cmpwi cr0, r3, 0
  118. bne cr0, LBB1_2 ; entry
  119. LBB1_1: ; entry
  120. mr r6, r5
  121. LBB1_2: ; entry
  122. mr r3, r6
  123. mtspr 256, r2
  124. blr
  125. //===----------------------------------------------------------------------===//
  126. CodeGen/PowerPC/vec_constants.ll has an and operation that should be
  127. codegen'd to andc. The issue is that the 'all ones' build vector is
  128. SelectNodeTo'd a VSPLTISB instruction node before the and/xor is selected
  129. which prevents the vnot pattern from matching.
  130. //===----------------------------------------------------------------------===//
  131. An alternative to the store/store/load approach for illegal insert element
  132. lowering would be:
  133. 1. store element to any ol' slot
  134. 2. lvx the slot
  135. 3. lvsl 0; splat index; vcmpeq to generate a select mask
  136. 4. lvsl slot + x; vperm to rotate result into correct slot
  137. 5. vsel result together.
  138. //===----------------------------------------------------------------------===//
  139. Should codegen branches on vec_any/vec_all to avoid mfcr. Two examples:
  140. #include <altivec.h>
  141. int f(vector float a, vector float b)
  142. {
  143. int aa = 0;
  144. if (vec_all_ge(a, b))
  145. aa |= 0x1;
  146. if (vec_any_ge(a,b))
  147. aa |= 0x2;
  148. return aa;
  149. }
  150. vector float f(vector float a, vector float b) {
  151. if (vec_any_eq(a, b))
  152. return a;
  153. else
  154. return b;
  155. }
  156. //===----------------------------------------------------------------------===//
  157. We should do a little better with eliminating dead stores.
  158. The stores to the stack are dead since %a and %b are not needed
  159. ; Function Attrs: nounwind
  160. define <16 x i8> @test_vpmsumb() #0 {
  161. entry:
  162. %a = alloca <16 x i8>, align 16
  163. %b = alloca <16 x i8>, align 16
  164. store <16 x i8> <i8 1, i8 2, i8 3, i8 4, i8 5, i8 6, i8 7, i8 8, i8 9, i8 10, i8 11, i8 12, i8 13, i8 14, i8 15, i8 16>, <16 x i8>* %a, align 16
  165. store <16 x i8> <i8 113, i8 114, i8 115, i8 116, i8 117, i8 118, i8 119, i8 120, i8 121, i8 122, i8 123, i8 124, i8 125, i8 126, i8 127, i8 112>, <16 x i8>* %b, align 16
  166. %0 = load <16 x i8>* %a, align 16
  167. %1 = load <16 x i8>* %b, align 16
  168. %2 = call <16 x i8> @llvm.ppc.altivec.crypto.vpmsumb(<16 x i8> %0, <16 x i8> %1)
  169. ret <16 x i8> %2
  170. }
  171. ; Function Attrs: nounwind readnone
  172. declare <16 x i8> @llvm.ppc.altivec.crypto.vpmsumb(<16 x i8>, <16 x i8>) #1
  173. Produces the following code with -mtriple=powerpc64-unknown-linux-gnu:
  174. # %bb.0: # %entry
  175. addis 3, 2, .LCPI0_0@toc@ha
  176. addis 4, 2, .LCPI0_1@toc@ha
  177. addi 3, 3, .LCPI0_0@toc@l
  178. addi 4, 4, .LCPI0_1@toc@l
  179. lxvw4x 0, 0, 3
  180. addi 3, 1, -16
  181. lxvw4x 35, 0, 4
  182. stxvw4x 0, 0, 3
  183. ori 2, 2, 0
  184. lxvw4x 34, 0, 3
  185. addi 3, 1, -32
  186. stxvw4x 35, 0, 3
  187. vpmsumb 2, 2, 3
  188. blr
  189. .long 0
  190. .quad 0
  191. The two stxvw4x instructions are not needed.
  192. With -mtriple=powerpc64le-unknown-linux-gnu, the associated permutes
  193. are present too.
  194. //===----------------------------------------------------------------------===//
  195. The following example is found in test/CodeGen/PowerPC/vec_add_sub_doubleword.ll:
  196. define <2 x i64> @increment_by_val(<2 x i64> %x, i64 %val) nounwind {
  197. %tmpvec = insertelement <2 x i64> <i64 0, i64 0>, i64 %val, i32 0
  198. %tmpvec2 = insertelement <2 x i64> %tmpvec, i64 %val, i32 1
  199. %result = add <2 x i64> %x, %tmpvec2
  200. ret <2 x i64> %result
  201. This will generate the following instruction sequence:
  202. std 5, -8(1)
  203. std 5, -16(1)
  204. addi 3, 1, -16
  205. ori 2, 2, 0
  206. lxvd2x 35, 0, 3
  207. vaddudm 2, 2, 3
  208. blr
  209. This will almost certainly cause a load-hit-store hazard.
  210. Since val is a value parameter, it should not need to be saved onto
  211. the stack, unless it's being done set up the vector register. Instead,
  212. it would be better to splat the value into a vector register, and then
  213. remove the (dead) stores to the stack.
  214. //===----------------------------------------------------------------------===//
  215. At the moment we always generate a lxsdx in preference to lfd, or stxsdx in
  216. preference to stfd. When we have a reg-immediate addressing mode, this is a
  217. poor choice, since we have to load the address into an index register. This
  218. should be fixed for P7/P8.
  219. //===----------------------------------------------------------------------===//
  220. Right now, ShuffleKind 0 is supported only on BE, and ShuffleKind 2 only on LE.
  221. However, we could actually support both kinds on either endianness, if we check
  222. for the appropriate shufflevector pattern for each case ... this would cause
  223. some additional shufflevectors to be recognized and implemented via the
  224. "swapped" form.
  225. //===----------------------------------------------------------------------===//
  226. There is a utility program called PerfectShuffle that generates a table of the
  227. shortest instruction sequence for implementing a shufflevector operation on
  228. PowerPC. However, this was designed for big-endian code generation. We could
  229. modify this program to create a little endian version of the table. The table
  230. is used in PPCISelLowering.cpp, PPCTargetLowering::LOWERVECTOR_SHUFFLE().
  231. //===----------------------------------------------------------------------===//
  232. Opportunies to use instructions from PPCInstrVSX.td during code gen
  233. - Conversion instructions (Sections 7.6.1.5 and 7.6.1.6 of ISA 2.07)
  234. - Scalar comparisons (xscmpodp and xscmpudp)
  235. - Min and max (xsmaxdp, xsmindp, xvmaxdp, xvmindp, xvmaxsp, xvminsp)
  236. Related to this: we currently do not generate the lxvw4x instruction for either
  237. v4f32 or v4i32, probably because adding a dag pattern to the recognizer requires
  238. a single target type. This should probably be addressed in the PPCISelDAGToDAG logic.
  239. //===----------------------------------------------------------------------===//
  240. Currently EXTRACT_VECTOR_ELT and INSERT_VECTOR_ELT are type-legal only
  241. for v2f64 with VSX available. We should create custom lowering
  242. support for the other vector types. Without this support, we generate
  243. sequences with load-hit-store hazards.
  244. v4f32 can be supported with VSX by shifting the correct element into
  245. big-endian lane 0, using xscvspdpn to produce a double-precision
  246. representation of the single-precision value in big-endian
  247. double-precision lane 0, and reinterpreting lane 0 as an FPR or
  248. vector-scalar register.
  249. v2i64 can be supported with VSX and P8Vector in the same manner as
  250. v2f64, followed by a direct move to a GPR.
  251. v4i32 can be supported with VSX and P8Vector by shifting the correct
  252. element into big-endian lane 1, using a direct move to a GPR, and
  253. sign-extending the 32-bit result to 64 bits.
  254. v8i16 can be supported with VSX and P8Vector by shifting the correct
  255. element into big-endian lane 3, using a direct move to a GPR, and
  256. sign-extending the 16-bit result to 64 bits.
  257. v16i8 can be supported with VSX and P8Vector by shifting the correct
  258. element into big-endian lane 7, using a direct move to a GPR, and
  259. sign-extending the 8-bit result to 64 bits.