README.txt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. //===- README.txt - Notes for improving PowerPC-specific code gen ---------===//
  2. TODO:
  3. * lmw/stmw pass a la arm load store optimizer for prolog/epilog
  4. ===-------------------------------------------------------------------------===
  5. This code:
  6. unsigned add32carry(unsigned sum, unsigned x) {
  7. unsigned z = sum + x;
  8. if (sum + x < x)
  9. z++;
  10. return z;
  11. }
  12. Should compile to something like:
  13. addc r3,r3,r4
  14. addze r3,r3
  15. instead we get:
  16. add r3, r4, r3
  17. cmplw cr7, r3, r4
  18. mfcr r4 ; 1
  19. rlwinm r4, r4, 29, 31, 31
  20. add r3, r3, r4
  21. Ick.
  22. ===-------------------------------------------------------------------------===
  23. We compile the hottest inner loop of viterbi to:
  24. li r6, 0
  25. b LBB1_84 ;bb432.i
  26. LBB1_83: ;bb420.i
  27. lbzx r8, r5, r7
  28. addi r6, r7, 1
  29. stbx r8, r4, r7
  30. LBB1_84: ;bb432.i
  31. mr r7, r6
  32. cmplwi cr0, r7, 143
  33. bne cr0, LBB1_83 ;bb420.i
  34. The CBE manages to produce:
  35. li r0, 143
  36. mtctr r0
  37. loop:
  38. lbzx r2, r2, r11
  39. stbx r0, r2, r9
  40. addi r2, r2, 1
  41. bdz later
  42. b loop
  43. This could be much better (bdnz instead of bdz) but it still beats us. If we
  44. produced this with bdnz, the loop would be a single dispatch group.
  45. ===-------------------------------------------------------------------------===
  46. Lump the constant pool for each function into ONE pic object, and reference
  47. pieces of it as offsets from the start. For functions like this (contrived
  48. to have lots of constants obviously):
  49. double X(double Y) { return (Y*1.23 + 4.512)*2.34 + 14.38; }
  50. We generate:
  51. _X:
  52. lis r2, ha16(.CPI_X_0)
  53. lfd f0, lo16(.CPI_X_0)(r2)
  54. lis r2, ha16(.CPI_X_1)
  55. lfd f2, lo16(.CPI_X_1)(r2)
  56. fmadd f0, f1, f0, f2
  57. lis r2, ha16(.CPI_X_2)
  58. lfd f1, lo16(.CPI_X_2)(r2)
  59. lis r2, ha16(.CPI_X_3)
  60. lfd f2, lo16(.CPI_X_3)(r2)
  61. fmadd f1, f0, f1, f2
  62. blr
  63. It would be better to materialize .CPI_X into a register, then use immediates
  64. off of the register to avoid the lis's. This is even more important in PIC
  65. mode.
  66. Note that this (and the static variable version) is discussed here for GCC:
  67. http://gcc.gnu.org/ml/gcc-patches/2006-02/msg00133.html
  68. Here's another example (the sgn function):
  69. double testf(double a) {
  70. return a == 0.0 ? 0.0 : (a > 0.0 ? 1.0 : -1.0);
  71. }
  72. it produces a BB like this:
  73. LBB1_1: ; cond_true
  74. lis r2, ha16(LCPI1_0)
  75. lfs f0, lo16(LCPI1_0)(r2)
  76. lis r2, ha16(LCPI1_1)
  77. lis r3, ha16(LCPI1_2)
  78. lfs f2, lo16(LCPI1_2)(r3)
  79. lfs f3, lo16(LCPI1_1)(r2)
  80. fsub f0, f0, f1
  81. fsel f1, f0, f2, f3
  82. blr
  83. ===-------------------------------------------------------------------------===
  84. PIC Code Gen IPO optimization:
  85. Squish small scalar globals together into a single global struct, allowing the
  86. address of the struct to be CSE'd, avoiding PIC accesses (also reduces the size
  87. of the GOT on targets with one).
  88. Note that this is discussed here for GCC:
  89. http://gcc.gnu.org/ml/gcc-patches/2006-02/msg00133.html
  90. ===-------------------------------------------------------------------------===
  91. Fold add and sub with constant into non-extern, non-weak addresses so this:
  92. static int a;
  93. void bar(int b) { a = b; }
  94. void foo(unsigned char *c) {
  95. *c = a;
  96. }
  97. So that
  98. _foo:
  99. lis r2, ha16(_a)
  100. la r2, lo16(_a)(r2)
  101. lbz r2, 3(r2)
  102. stb r2, 0(r3)
  103. blr
  104. Becomes
  105. _foo:
  106. lis r2, ha16(_a+3)
  107. lbz r2, lo16(_a+3)(r2)
  108. stb r2, 0(r3)
  109. blr
  110. ===-------------------------------------------------------------------------===
  111. We should compile these two functions to the same thing:
  112. #include <stdlib.h>
  113. void f(int a, int b, int *P) {
  114. *P = (a-b)>=0?(a-b):(b-a);
  115. }
  116. void g(int a, int b, int *P) {
  117. *P = abs(a-b);
  118. }
  119. Further, they should compile to something better than:
  120. _g:
  121. subf r2, r4, r3
  122. subfic r3, r2, 0
  123. cmpwi cr0, r2, -1
  124. bgt cr0, LBB2_2 ; entry
  125. LBB2_1: ; entry
  126. mr r2, r3
  127. LBB2_2: ; entry
  128. stw r2, 0(r5)
  129. blr
  130. GCC produces:
  131. _g:
  132. subf r4,r4,r3
  133. srawi r2,r4,31
  134. xor r0,r2,r4
  135. subf r0,r2,r0
  136. stw r0,0(r5)
  137. blr
  138. ... which is much nicer.
  139. This theoretically may help improve twolf slightly (used in dimbox.c:142?).
  140. ===-------------------------------------------------------------------------===
  141. PR5945: This:
  142. define i32 @clamp0g(i32 %a) {
  143. entry:
  144. %cmp = icmp slt i32 %a, 0
  145. %sel = select i1 %cmp, i32 0, i32 %a
  146. ret i32 %sel
  147. }
  148. Is compile to this with the PowerPC (32-bit) backend:
  149. _clamp0g:
  150. cmpwi cr0, r3, 0
  151. li r2, 0
  152. blt cr0, LBB1_2
  153. ; %bb.1: ; %entry
  154. mr r2, r3
  155. LBB1_2: ; %entry
  156. mr r3, r2
  157. blr
  158. This could be reduced to the much simpler:
  159. _clamp0g:
  160. srawi r2, r3, 31
  161. andc r3, r3, r2
  162. blr
  163. ===-------------------------------------------------------------------------===
  164. int foo(int N, int ***W, int **TK, int X) {
  165. int t, i;
  166. for (t = 0; t < N; ++t)
  167. for (i = 0; i < 4; ++i)
  168. W[t / X][i][t % X] = TK[i][t];
  169. return 5;
  170. }
  171. We generate relatively atrocious code for this loop compared to gcc.
  172. We could also strength reduce the rem and the div:
  173. http://www.lcs.mit.edu/pubs/pdf/MIT-LCS-TM-600.pdf
  174. ===-------------------------------------------------------------------------===
  175. We generate ugly code for this:
  176. void func(unsigned int *ret, float dx, float dy, float dz, float dw) {
  177. unsigned code = 0;
  178. if(dx < -dw) code |= 1;
  179. if(dx > dw) code |= 2;
  180. if(dy < -dw) code |= 4;
  181. if(dy > dw) code |= 8;
  182. if(dz < -dw) code |= 16;
  183. if(dz > dw) code |= 32;
  184. *ret = code;
  185. }
  186. ===-------------------------------------------------------------------------===
  187. %struct.B = type { i8, [3 x i8] }
  188. define void @bar(%struct.B* %b) {
  189. entry:
  190. %tmp = bitcast %struct.B* %b to i32* ; <uint*> [#uses=1]
  191. %tmp = load i32* %tmp ; <uint> [#uses=1]
  192. %tmp3 = bitcast %struct.B* %b to i32* ; <uint*> [#uses=1]
  193. %tmp4 = load i32* %tmp3 ; <uint> [#uses=1]
  194. %tmp8 = bitcast %struct.B* %b to i32* ; <uint*> [#uses=2]
  195. %tmp9 = load i32* %tmp8 ; <uint> [#uses=1]
  196. %tmp4.mask17 = shl i32 %tmp4, i8 1 ; <uint> [#uses=1]
  197. %tmp1415 = and i32 %tmp4.mask17, 2147483648 ; <uint> [#uses=1]
  198. %tmp.masked = and i32 %tmp, 2147483648 ; <uint> [#uses=1]
  199. %tmp11 = or i32 %tmp1415, %tmp.masked ; <uint> [#uses=1]
  200. %tmp12 = and i32 %tmp9, 2147483647 ; <uint> [#uses=1]
  201. %tmp13 = or i32 %tmp12, %tmp11 ; <uint> [#uses=1]
  202. store i32 %tmp13, i32* %tmp8
  203. ret void
  204. }
  205. We emit:
  206. _foo:
  207. lwz r2, 0(r3)
  208. slwi r4, r2, 1
  209. or r4, r4, r2
  210. rlwimi r2, r4, 0, 0, 0
  211. stw r2, 0(r3)
  212. blr
  213. We could collapse a bunch of those ORs and ANDs and generate the following
  214. equivalent code:
  215. _foo:
  216. lwz r2, 0(r3)
  217. rlwinm r4, r2, 1, 0, 0
  218. or r2, r2, r4
  219. stw r2, 0(r3)
  220. blr
  221. ===-------------------------------------------------------------------------===
  222. Consider a function like this:
  223. float foo(float X) { return X + 1234.4123f; }
  224. The FP constant ends up in the constant pool, so we need to get the LR register.
  225. This ends up producing code like this:
  226. _foo:
  227. .LBB_foo_0: ; entry
  228. mflr r11
  229. *** stw r11, 8(r1)
  230. bl "L00000$pb"
  231. "L00000$pb":
  232. mflr r2
  233. addis r2, r2, ha16(.CPI_foo_0-"L00000$pb")
  234. lfs f0, lo16(.CPI_foo_0-"L00000$pb")(r2)
  235. fadds f1, f1, f0
  236. *** lwz r11, 8(r1)
  237. mtlr r11
  238. blr
  239. This is functional, but there is no reason to spill the LR register all the way
  240. to the stack (the two marked instrs): spilling it to a GPR is quite enough.
  241. Implementing this will require some codegen improvements. Nate writes:
  242. "So basically what we need to support the "no stack frame save and restore" is a
  243. generalization of the LR optimization to "callee-save regs".
  244. Currently, we have LR marked as a callee-save reg. The register allocator sees
  245. that it's callee save, and spills it directly to the stack.
  246. Ideally, something like this would happen:
  247. LR would be in a separate register class from the GPRs. The class of LR would be
  248. marked "unspillable". When the register allocator came across an unspillable
  249. reg, it would ask "what is the best class to copy this into that I *can* spill"
  250. If it gets a class back, which it will in this case (the gprs), it grabs a free
  251. register of that class. If it is then later necessary to spill that reg, so be
  252. it.
  253. ===-------------------------------------------------------------------------===
  254. We compile this:
  255. int test(_Bool X) {
  256. return X ? 524288 : 0;
  257. }
  258. to:
  259. _test:
  260. cmplwi cr0, r3, 0
  261. lis r2, 8
  262. li r3, 0
  263. beq cr0, LBB1_2 ;entry
  264. LBB1_1: ;entry
  265. mr r3, r2
  266. LBB1_2: ;entry
  267. blr
  268. instead of:
  269. _test:
  270. addic r2,r3,-1
  271. subfe r0,r2,r3
  272. slwi r3,r0,19
  273. blr
  274. This sort of thing occurs a lot due to globalopt.
  275. ===-------------------------------------------------------------------------===
  276. We compile:
  277. define i32 @bar(i32 %x) nounwind readnone ssp {
  278. entry:
  279. %0 = icmp eq i32 %x, 0 ; <i1> [#uses=1]
  280. %neg = sext i1 %0 to i32 ; <i32> [#uses=1]
  281. ret i32 %neg
  282. }
  283. to:
  284. _bar:
  285. cntlzw r2, r3
  286. slwi r2, r2, 26
  287. srawi r3, r2, 31
  288. blr
  289. it would be better to produce:
  290. _bar:
  291. addic r3,r3,-1
  292. subfe r3,r3,r3
  293. blr
  294. ===-------------------------------------------------------------------------===
  295. We generate horrible ppc code for this:
  296. #define N 2000000
  297. double a[N],c[N];
  298. void simpleloop() {
  299. int j;
  300. for (j=0; j<N; j++)
  301. c[j] = a[j];
  302. }
  303. LBB1_1: ;bb
  304. lfdx f0, r3, r4
  305. addi r5, r5, 1 ;; Extra IV for the exit value compare.
  306. stfdx f0, r2, r4
  307. addi r4, r4, 8
  308. xoris r6, r5, 30 ;; This is due to a large immediate.
  309. cmplwi cr0, r6, 33920
  310. bne cr0, LBB1_1
  311. //===---------------------------------------------------------------------===//
  312. This:
  313. #include <algorithm>
  314. inline std::pair<unsigned, bool> full_add(unsigned a, unsigned b)
  315. { return std::make_pair(a + b, a + b < a); }
  316. bool no_overflow(unsigned a, unsigned b)
  317. { return !full_add(a, b).second; }
  318. Should compile to:
  319. __Z11no_overflowjj:
  320. add r4,r3,r4
  321. subfc r3,r3,r4
  322. li r3,0
  323. adde r3,r3,r3
  324. blr
  325. (or better) not:
  326. __Z11no_overflowjj:
  327. add r2, r4, r3
  328. cmplw cr7, r2, r3
  329. mfcr r2
  330. rlwinm r2, r2, 29, 31, 31
  331. xori r3, r2, 1
  332. blr
  333. //===---------------------------------------------------------------------===//
  334. We compile some FP comparisons into an mfcr with two rlwinms and an or. For
  335. example:
  336. #include <math.h>
  337. int test(double x, double y) { return islessequal(x, y);}
  338. int test2(double x, double y) { return islessgreater(x, y);}
  339. int test3(double x, double y) { return !islessequal(x, y);}
  340. Compiles into (all three are similar, but the bits differ):
  341. _test:
  342. fcmpu cr7, f1, f2
  343. mfcr r2
  344. rlwinm r3, r2, 29, 31, 31
  345. rlwinm r2, r2, 31, 31, 31
  346. or r3, r2, r3
  347. blr
  348. GCC compiles this into:
  349. _test:
  350. fcmpu cr7,f1,f2
  351. cror 30,28,30
  352. mfcr r3
  353. rlwinm r3,r3,31,1
  354. blr
  355. which is more efficient and can use mfocr. See PR642 for some more context.
  356. //===---------------------------------------------------------------------===//
  357. void foo(float *data, float d) {
  358. long i;
  359. for (i = 0; i < 8000; i++)
  360. data[i] = d;
  361. }
  362. void foo2(float *data, float d) {
  363. long i;
  364. data--;
  365. for (i = 0; i < 8000; i++) {
  366. data[1] = d;
  367. data++;
  368. }
  369. }
  370. These compile to:
  371. _foo:
  372. li r2, 0
  373. LBB1_1: ; bb
  374. addi r4, r2, 4
  375. stfsx f1, r3, r2
  376. cmplwi cr0, r4, 32000
  377. mr r2, r4
  378. bne cr0, LBB1_1 ; bb
  379. blr
  380. _foo2:
  381. li r2, 0
  382. LBB2_1: ; bb
  383. addi r4, r2, 4
  384. stfsx f1, r3, r2
  385. cmplwi cr0, r4, 32000
  386. mr r2, r4
  387. bne cr0, LBB2_1 ; bb
  388. blr
  389. The 'mr' could be eliminated to folding the add into the cmp better.
  390. //===---------------------------------------------------------------------===//
  391. Codegen for the following (low-probability) case deteriorated considerably
  392. when the correctness fixes for unordered comparisons went in (PR 642, 58871).
  393. It should be possible to recover the code quality described in the comments.
  394. ; RUN: llvm-as < %s | llc -march=ppc32 | grep or | count 3
  395. ; This should produce one 'or' or 'cror' instruction per function.
  396. ; RUN: llvm-as < %s | llc -march=ppc32 | grep mfcr | count 3
  397. ; PR2964
  398. define i32 @test(double %x, double %y) nounwind {
  399. entry:
  400. %tmp3 = fcmp ole double %x, %y ; <i1> [#uses=1]
  401. %tmp345 = zext i1 %tmp3 to i32 ; <i32> [#uses=1]
  402. ret i32 %tmp345
  403. }
  404. define i32 @test2(double %x, double %y) nounwind {
  405. entry:
  406. %tmp3 = fcmp one double %x, %y ; <i1> [#uses=1]
  407. %tmp345 = zext i1 %tmp3 to i32 ; <i32> [#uses=1]
  408. ret i32 %tmp345
  409. }
  410. define i32 @test3(double %x, double %y) nounwind {
  411. entry:
  412. %tmp3 = fcmp ugt double %x, %y ; <i1> [#uses=1]
  413. %tmp34 = zext i1 %tmp3 to i32 ; <i32> [#uses=1]
  414. ret i32 %tmp34
  415. }
  416. //===---------------------------------------------------------------------===//
  417. for the following code:
  418. void foo (float *__restrict__ a, int *__restrict__ b, int n) {
  419. a[n] = b[n] * 2.321;
  420. }
  421. we load b[n] to GPR, then move it VSX register and convert it float. We should
  422. use vsx scalar integer load instructions to avoid direct moves
  423. //===----------------------------------------------------------------------===//
  424. ; RUN: llvm-as < %s | llc -march=ppc32 | not grep fneg
  425. ; This could generate FSEL with appropriate flags (FSEL is not IEEE-safe, and
  426. ; should not be generated except with -enable-finite-only-fp-math or the like).
  427. ; With the correctness fixes for PR642 (58871) LowerSELECT_CC would need to
  428. ; recognize a more elaborate tree than a simple SETxx.
  429. define double @test_FNEG_sel(double %A, double %B, double %C) {
  430. %D = fsub double -0.000000e+00, %A ; <double> [#uses=1]
  431. %Cond = fcmp ugt double %D, -0.000000e+00 ; <i1> [#uses=1]
  432. %E = select i1 %Cond, double %B, double %C ; <double> [#uses=1]
  433. ret double %E
  434. }
  435. //===----------------------------------------------------------------------===//
  436. The save/restore sequence for CR in prolog/epilog is terrible:
  437. - Each CR subreg is saved individually, rather than doing one save as a unit.
  438. - On Darwin, the save is done after the decrement of SP, which means the offset
  439. from SP of the save slot can be too big for a store instruction, which means we
  440. need an additional register (currently hacked in 96015+96020; the solution there
  441. is correct, but poor).
  442. - On SVR4 the same thing can happen, and I don't think saving before the SP
  443. decrement is safe on that target, as there is no red zone. This is currently
  444. broken AFAIK, although it's not a target I can exercise.
  445. The following demonstrates the problem:
  446. extern void bar(char *p);
  447. void foo() {
  448. char x[100000];
  449. bar(x);
  450. __asm__("" ::: "cr2");
  451. }
  452. //===-------------------------------------------------------------------------===
  453. Naming convention for instruction formats is very haphazard.
  454. We have agreed on a naming scheme as follows:
  455. <INST_form>{_<OP_type><OP_len>}+
  456. Where:
  457. INST_form is the instruction format (X-form, etc.)
  458. OP_type is the operand type - one of OPC (opcode), RD (register destination),
  459. RS (register source),
  460. RDp (destination register pair),
  461. RSp (source register pair), IM (immediate),
  462. XO (extended opcode)
  463. OP_len is the length of the operand in bits
  464. VSX register operands would be of length 6 (split across two fields),
  465. condition register fields of length 3.
  466. We would not need denote reserved fields in names of instruction formats.
  467. //===----------------------------------------------------------------------===//
  468. Instruction fusion was introduced in ISA 2.06 and more opportunities added in
  469. ISA 2.07. LLVM needs to add infrastructure to recognize fusion opportunities
  470. and force instruction pairs to be scheduled together.
  471. -----------------------------------------------------------------------------
  472. More general handling of any_extend and zero_extend:
  473. See https://reviews.llvm.org/D24924#555306