Chris Lattner | 1171ff4 | 2005-10-23 19:52:42 +0000 | [diff] [blame] | 1 | //===---------------------------------------------------------------------===// |
| 2 | // Random ideas for the X86 backend. |
| 3 | //===---------------------------------------------------------------------===// |
| 4 | |
| 5 | Add a MUL2U and MUL2S nodes to represent a multiply that returns both the |
| 6 | Hi and Lo parts (combination of MUL and MULH[SU] into one node). Add this to |
| 7 | X86, & make the dag combiner produce it when needed. This will eliminate one |
| 8 | imul from the code generated for: |
| 9 | |
| 10 | long long test(long long X, long long Y) { return X*Y; } |
| 11 | |
| 12 | by using the EAX result from the mul. We should add a similar node for |
| 13 | DIVREM. |
| 14 | |
Chris Lattner | 865874c | 2005-12-02 00:11:20 +0000 | [diff] [blame] | 15 | another case is: |
| 16 | |
| 17 | long long test(int X, int Y) { return (long long)X*Y; } |
| 18 | |
| 19 | ... which should only be one imul instruction. |
| 20 | |
Chris Lattner | 1171ff4 | 2005-10-23 19:52:42 +0000 | [diff] [blame] | 21 | //===---------------------------------------------------------------------===// |
| 22 | |
| 23 | This should be one DIV/IDIV instruction, not a libcall: |
| 24 | |
| 25 | unsigned test(unsigned long long X, unsigned Y) { |
| 26 | return X/Y; |
| 27 | } |
| 28 | |
| 29 | This can be done trivially with a custom legalizer. What about overflow |
| 30 | though? http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14224 |
| 31 | |
| 32 | //===---------------------------------------------------------------------===// |
| 33 | |
Chris Lattner | 1171ff4 | 2005-10-23 19:52:42 +0000 | [diff] [blame] | 34 | Some targets (e.g. athlons) prefer freep to fstp ST(0): |
| 35 | http://gcc.gnu.org/ml/gcc-patches/2004-04/msg00659.html |
| 36 | |
| 37 | //===---------------------------------------------------------------------===// |
| 38 | |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 39 | This should use fiadd on chips where it is profitable: |
Chris Lattner | 1171ff4 | 2005-10-23 19:52:42 +0000 | [diff] [blame] | 40 | double foo(double P, int *I) { return P+*I; } |
| 41 | |
| 42 | //===---------------------------------------------------------------------===// |
| 43 | |
| 44 | The FP stackifier needs to be global. Also, it should handle simple permutates |
| 45 | to reduce number of shuffle instructions, e.g. turning: |
| 46 | |
| 47 | fld P -> fld Q |
| 48 | fld Q fld P |
| 49 | fxch |
| 50 | |
| 51 | or: |
| 52 | |
| 53 | fxch -> fucomi |
| 54 | fucomi jl X |
| 55 | jg X |
| 56 | |
Chris Lattner | 1db4b4f | 2006-01-16 17:53:00 +0000 | [diff] [blame] | 57 | Ideas: |
| 58 | http://gcc.gnu.org/ml/gcc-patches/2004-11/msg02410.html |
| 59 | |
| 60 | |
Chris Lattner | 1171ff4 | 2005-10-23 19:52:42 +0000 | [diff] [blame] | 61 | //===---------------------------------------------------------------------===// |
| 62 | |
| 63 | Improvements to the multiply -> shift/add algorithm: |
| 64 | http://gcc.gnu.org/ml/gcc-patches/2004-08/msg01590.html |
| 65 | |
| 66 | //===---------------------------------------------------------------------===// |
| 67 | |
| 68 | Improve code like this (occurs fairly frequently, e.g. in LLVM): |
| 69 | long long foo(int x) { return 1LL << x; } |
| 70 | |
| 71 | http://gcc.gnu.org/ml/gcc-patches/2004-09/msg01109.html |
| 72 | http://gcc.gnu.org/ml/gcc-patches/2004-09/msg01128.html |
| 73 | http://gcc.gnu.org/ml/gcc-patches/2004-09/msg01136.html |
| 74 | |
| 75 | Another useful one would be ~0ULL >> X and ~0ULL << X. |
| 76 | |
Chris Lattner | ffff617 | 2005-10-23 21:44:59 +0000 | [diff] [blame] | 77 | //===---------------------------------------------------------------------===// |
| 78 | |
Chris Lattner | 1e4ed93 | 2005-11-28 04:52:39 +0000 | [diff] [blame] | 79 | Compile this: |
| 80 | _Bool f(_Bool a) { return a!=1; } |
| 81 | |
| 82 | into: |
| 83 | movzbl %dil, %eax |
| 84 | xorl $1, %eax |
| 85 | ret |
Evan Cheng | 8dee8cc | 2005-12-17 01:25:19 +0000 | [diff] [blame] | 86 | |
| 87 | //===---------------------------------------------------------------------===// |
| 88 | |
| 89 | Some isel ideas: |
| 90 | |
| 91 | 1. Dynamic programming based approach when compile time if not an |
| 92 | issue. |
| 93 | 2. Code duplication (addressing mode) during isel. |
| 94 | 3. Other ideas from "Register-Sensitive Selection, Duplication, and |
| 95 | Sequencing of Instructions". |
Chris Lattner | cb29890 | 2006-02-08 07:12:07 +0000 | [diff] [blame] | 96 | 4. Scheduling for reduced register pressure. E.g. "Minimum Register |
| 97 | Instruction Sequence Problem: Revisiting Optimal Code Generation for DAGs" |
| 98 | and other related papers. |
| 99 | http://citeseer.ist.psu.edu/govindarajan01minimum.html |
Evan Cheng | 8dee8cc | 2005-12-17 01:25:19 +0000 | [diff] [blame] | 100 | |
| 101 | //===---------------------------------------------------------------------===// |
| 102 | |
| 103 | Should we promote i16 to i32 to avoid partial register update stalls? |
Evan Cheng | 98abbfb | 2005-12-17 06:54:43 +0000 | [diff] [blame] | 104 | |
| 105 | //===---------------------------------------------------------------------===// |
| 106 | |
| 107 | Leave any_extend as pseudo instruction and hint to register |
| 108 | allocator. Delay codegen until post register allocation. |
Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 109 | |
| 110 | //===---------------------------------------------------------------------===// |
| 111 | |
| 112 | Add a target specific hook to DAG combiner to handle SINT_TO_FP and |
| 113 | FP_TO_SINT when the source operand is already in memory. |
| 114 | |
| 115 | //===---------------------------------------------------------------------===// |
| 116 | |
| 117 | Check if load folding would add a cycle in the dag. |
Evan Cheng | e08c270 | 2006-01-13 01:20:42 +0000 | [diff] [blame] | 118 | |
| 119 | //===---------------------------------------------------------------------===// |
| 120 | |
| 121 | Model X86 EFLAGS as a real register to avoid redudant cmp / test. e.g. |
| 122 | |
| 123 | cmpl $1, %eax |
| 124 | setg %al |
| 125 | testb %al, %al # unnecessary |
| 126 | jne .BB7 |
Chris Lattner | 1db4b4f | 2006-01-16 17:53:00 +0000 | [diff] [blame] | 127 | |
| 128 | //===---------------------------------------------------------------------===// |
| 129 | |
| 130 | Count leading zeros and count trailing zeros: |
| 131 | |
| 132 | int clz(int X) { return __builtin_clz(X); } |
| 133 | int ctz(int X) { return __builtin_ctz(X); } |
| 134 | |
| 135 | $ gcc t.c -S -o - -O3 -fomit-frame-pointer -masm=intel |
| 136 | clz: |
| 137 | bsr %eax, DWORD PTR [%esp+4] |
| 138 | xor %eax, 31 |
| 139 | ret |
| 140 | ctz: |
| 141 | bsf %eax, DWORD PTR [%esp+4] |
| 142 | ret |
| 143 | |
| 144 | however, check that these are defined for 0 and 32. Our intrinsics are, GCC's |
| 145 | aren't. |
| 146 | |
| 147 | //===---------------------------------------------------------------------===// |
| 148 | |
| 149 | Use push/pop instructions in prolog/epilog sequences instead of stores off |
| 150 | ESP (certain code size win, perf win on some [which?] processors). |
| 151 | |
| 152 | //===---------------------------------------------------------------------===// |
| 153 | |
| 154 | Only use inc/neg/not instructions on processors where they are faster than |
| 155 | add/sub/xor. They are slower on the P4 due to only updating some processor |
| 156 | flags. |
| 157 | |
| 158 | //===---------------------------------------------------------------------===// |
| 159 | |
| 160 | Open code rint,floor,ceil,trunc: |
| 161 | http://gcc.gnu.org/ml/gcc-patches/2004-08/msg02006.html |
| 162 | http://gcc.gnu.org/ml/gcc-patches/2004-08/msg02011.html |
| 163 | |
| 164 | //===---------------------------------------------------------------------===// |
| 165 | |
| 166 | Combine: a = sin(x), b = cos(x) into a,b = sincos(x). |
| 167 | |
Chris Lattner | 8f77b73 | 2006-02-08 06:52:06 +0000 | [diff] [blame] | 168 | Expand these to calls of sin/cos and stores: |
| 169 | double sincos(double x, double *sin, double *cos); |
| 170 | float sincosf(float x, float *sin, float *cos); |
| 171 | long double sincosl(long double x, long double *sin, long double *cos); |
| 172 | |
| 173 | Doing so could allow SROA of the destination pointers. See also: |
| 174 | http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17687 |
| 175 | |
Evan Cheng | e826a01 | 2006-01-27 22:11:01 +0000 | [diff] [blame] | 176 | //===---------------------------------------------------------------------===// |
| 177 | |
Chris Lattner | b638cd8 | 2006-01-29 09:08:15 +0000 | [diff] [blame] | 178 | The instruction selector sometimes misses folding a load into a compare. The |
| 179 | pattern is written as (cmp reg, (load p)). Because the compare isn't |
| 180 | commutative, it is not matched with the load on both sides. The dag combiner |
| 181 | should be made smart enough to cannonicalize the load into the RHS of a compare |
| 182 | when it can invert the result of the compare for free. |
| 183 | |
Chris Lattner | 6a28456 | 2006-01-29 09:14:47 +0000 | [diff] [blame] | 184 | //===---------------------------------------------------------------------===// |
| 185 | |
Chris Lattner | 5164a31 | 2006-01-29 09:42:20 +0000 | [diff] [blame] | 186 | LSR should be turned on for the X86 backend and tuned to take advantage of its |
| 187 | addressing modes. |
| 188 | |
Chris Lattner | c7097af | 2006-01-29 09:46:06 +0000 | [diff] [blame] | 189 | //===---------------------------------------------------------------------===// |
| 190 | |
| 191 | When compiled with unsafemath enabled, "main" should enable SSE DAZ mode and |
| 192 | other fast SSE modes. |
Chris Lattner | bdde465 | 2006-01-31 00:20:38 +0000 | [diff] [blame] | 193 | |
| 194 | //===---------------------------------------------------------------------===// |
| 195 | |
Chris Lattner | 594086d | 2006-01-31 00:45:37 +0000 | [diff] [blame] | 196 | Think about doing i64 math in SSE regs. |
| 197 | |
Chris Lattner | 8e38ae6 | 2006-01-31 02:10:06 +0000 | [diff] [blame] | 198 | //===---------------------------------------------------------------------===// |
| 199 | |
| 200 | The DAG Isel doesn't fold the loads into the adds in this testcase. The |
| 201 | pattern selector does. This is because the chain value of the load gets |
| 202 | selected first, and the loads aren't checking to see if they are only used by |
| 203 | and add. |
| 204 | |
| 205 | .ll: |
| 206 | |
| 207 | int %test(int* %x, int* %y, int* %z) { |
| 208 | %X = load int* %x |
| 209 | %Y = load int* %y |
| 210 | %Z = load int* %z |
| 211 | %a = add int %X, %Y |
| 212 | %b = add int %a, %Z |
| 213 | ret int %b |
| 214 | } |
| 215 | |
| 216 | dag isel: |
| 217 | |
| 218 | _test: |
| 219 | movl 4(%esp), %eax |
| 220 | movl (%eax), %eax |
| 221 | movl 8(%esp), %ecx |
| 222 | movl (%ecx), %ecx |
| 223 | addl %ecx, %eax |
| 224 | movl 12(%esp), %ecx |
| 225 | movl (%ecx), %ecx |
| 226 | addl %ecx, %eax |
| 227 | ret |
| 228 | |
| 229 | pattern isel: |
| 230 | |
| 231 | _test: |
| 232 | movl 12(%esp), %ecx |
| 233 | movl 4(%esp), %edx |
| 234 | movl 8(%esp), %eax |
| 235 | movl (%eax), %eax |
| 236 | addl (%edx), %eax |
| 237 | addl (%ecx), %eax |
| 238 | ret |
| 239 | |
| 240 | This is bad for register pressure, though the dag isel is producing a |
| 241 | better schedule. :) |
Chris Lattner | 3e1d5e5 | 2006-02-01 01:44:25 +0000 | [diff] [blame] | 242 | |
| 243 | //===---------------------------------------------------------------------===// |
| 244 | |
| 245 | This testcase should have no SSE instructions in it, and only one load from |
| 246 | a constant pool: |
| 247 | |
| 248 | double %test3(bool %B) { |
| 249 | %C = select bool %B, double 123.412, double 523.01123123 |
| 250 | ret double %C |
| 251 | } |
| 252 | |
| 253 | Currently, the select is being lowered, which prevents the dag combiner from |
| 254 | turning 'select (load CPI1), (load CPI2)' -> 'load (select CPI1, CPI2)' |
| 255 | |
| 256 | The pattern isel got this one right. |
| 257 | |
Chris Lattner | 1f7c630 | 2006-02-01 06:40:32 +0000 | [diff] [blame] | 258 | //===---------------------------------------------------------------------===// |
| 259 | |
Chris Lattner | 3e2b94a | 2006-02-01 21:44:48 +0000 | [diff] [blame] | 260 | We need to lower switch statements to tablejumps when appropriate instead of |
| 261 | always into binary branch trees. |
Chris Lattner | 4d7db40 | 2006-02-01 23:38:08 +0000 | [diff] [blame] | 262 | |
| 263 | //===---------------------------------------------------------------------===// |
| 264 | |
| 265 | SSE doesn't have [mem] op= reg instructions. If we have an SSE instruction |
| 266 | like this: |
| 267 | |
| 268 | X += y |
| 269 | |
| 270 | and the register allocator decides to spill X, it is cheaper to emit this as: |
| 271 | |
| 272 | Y += [xslot] |
| 273 | store Y -> [xslot] |
| 274 | |
| 275 | than as: |
| 276 | |
| 277 | tmp = [xslot] |
| 278 | tmp += y |
| 279 | store tmp -> [xslot] |
| 280 | |
| 281 | ..and this uses one fewer register (so this should be done at load folding |
| 282 | time, not at spiller time). *Note* however that this can only be done |
| 283 | if Y is dead. Here's a testcase: |
| 284 | |
| 285 | %.str_3 = external global [15 x sbyte] ; <[15 x sbyte]*> [#uses=0] |
| 286 | implementation ; Functions: |
| 287 | declare void %printf(int, ...) |
| 288 | void %main() { |
| 289 | build_tree.exit: |
| 290 | br label %no_exit.i7 |
| 291 | no_exit.i7: ; preds = %no_exit.i7, %build_tree.exit |
| 292 | %tmp.0.1.0.i9 = phi double [ 0.000000e+00, %build_tree.exit ], [ %tmp.34.i18, %no_exit.i7 ] ; <double> [#uses=1] |
| 293 | %tmp.0.0.0.i10 = phi double [ 0.000000e+00, %build_tree.exit ], [ %tmp.28.i16, %no_exit.i7 ] ; <double> [#uses=1] |
| 294 | %tmp.28.i16 = add double %tmp.0.0.0.i10, 0.000000e+00 |
| 295 | %tmp.34.i18 = add double %tmp.0.1.0.i9, 0.000000e+00 |
| 296 | br bool false, label %Compute_Tree.exit23, label %no_exit.i7 |
| 297 | Compute_Tree.exit23: ; preds = %no_exit.i7 |
| 298 | tail call void (int, ...)* %printf( int 0 ) |
| 299 | store double %tmp.34.i18, double* null |
| 300 | ret void |
| 301 | } |
| 302 | |
| 303 | We currently emit: |
| 304 | |
| 305 | .BBmain_1: |
| 306 | xorpd %XMM1, %XMM1 |
| 307 | addsd %XMM0, %XMM1 |
| 308 | *** movsd %XMM2, QWORD PTR [%ESP + 8] |
| 309 | *** addsd %XMM2, %XMM1 |
| 310 | *** movsd QWORD PTR [%ESP + 8], %XMM2 |
| 311 | jmp .BBmain_1 # no_exit.i7 |
| 312 | |
| 313 | This is a bugpoint reduced testcase, which is why the testcase doesn't make |
| 314 | much sense (e.g. its an infinite loop). :) |
| 315 | |
Evan Cheng | 8b6e4e6 | 2006-02-02 02:40:17 +0000 | [diff] [blame] | 316 | //===---------------------------------------------------------------------===// |
| 317 | |
| 318 | None of the FPStack instructions are handled in |
| 319 | X86RegisterInfo::foldMemoryOperand, which prevents the spiller from |
| 320 | folding spill code into the instructions. |
Chris Lattner | 9acddcd | 2006-02-02 19:16:34 +0000 | [diff] [blame] | 321 | |
| 322 | //===---------------------------------------------------------------------===// |
| 323 | |
| 324 | In many cases, LLVM generates code like this: |
| 325 | |
| 326 | _test: |
| 327 | movl 8(%esp), %eax |
| 328 | cmpl %eax, 4(%esp) |
| 329 | setl %al |
| 330 | movzbl %al, %eax |
| 331 | ret |
| 332 | |
| 333 | on some processors (which ones?), it is more efficient to do this: |
| 334 | |
| 335 | _test: |
| 336 | movl 8(%esp), %ebx |
| 337 | xor %eax, %eax |
| 338 | cmpl %ebx, 4(%esp) |
| 339 | setl %al |
| 340 | ret |
| 341 | |
| 342 | Doing this correctly is tricky though, as the xor clobbers the flags. |
| 343 | |
Chris Lattner | d395d09 | 2006-02-02 19:43:28 +0000 | [diff] [blame] | 344 | //===---------------------------------------------------------------------===// |
| 345 | |
| 346 | We should generate 'test' instead of 'cmp' in various cases, e.g.: |
| 347 | |
| 348 | bool %test(int %X) { |
| 349 | %Y = shl int %X, ubyte 1 |
| 350 | %C = seteq int %Y, 0 |
| 351 | ret bool %C |
| 352 | } |
| 353 | bool %test(int %X) { |
| 354 | %Y = and int %X, 8 |
| 355 | %C = seteq int %Y, 0 |
| 356 | ret bool %C |
| 357 | } |
| 358 | |
| 359 | This may just be a matter of using 'test' to write bigger patterns for X86cmp. |
| 360 | |
| 361 | //===---------------------------------------------------------------------===// |
| 362 | |
| 363 | Evaluate whether using movapd for SSE reg-reg moves is faster than using |
| 364 | movsd/movss for them. It may eliminate false partial register dependences by |
| 365 | writing the whole result register. |
| 366 | |
| 367 | //===---------------------------------------------------------------------===// |
| 368 | |
| 369 | SSE should implement 'select_cc' using 'emulated conditional moves' that use |
| 370 | pcmp/pand/pandn/por to do a selection instead of a conditional branch: |
| 371 | |
| 372 | double %X(double %Y, double %Z, double %A, double %B) { |
| 373 | %C = setlt double %A, %B |
| 374 | %z = add double %Z, 0.0 ;; select operand is not a load |
| 375 | %D = select bool %C, double %Y, double %z |
| 376 | ret double %D |
| 377 | } |
| 378 | |
| 379 | We currently emit: |
| 380 | |
| 381 | _X: |
| 382 | subl $12, %esp |
| 383 | xorpd %xmm0, %xmm0 |
| 384 | addsd 24(%esp), %xmm0 |
| 385 | movsd 32(%esp), %xmm1 |
| 386 | movsd 16(%esp), %xmm2 |
| 387 | ucomisd 40(%esp), %xmm1 |
| 388 | jb LBB_X_2 |
| 389 | LBB_X_1: |
| 390 | movsd %xmm0, %xmm2 |
| 391 | LBB_X_2: |
| 392 | movsd %xmm2, (%esp) |
| 393 | fldl (%esp) |
| 394 | addl $12, %esp |
| 395 | ret |
Chris Lattner | 9acddcd | 2006-02-02 19:16:34 +0000 | [diff] [blame] | 396 | |
Evan Cheng | 183fff9 | 2006-02-07 08:35:44 +0000 | [diff] [blame] | 397 | //===---------------------------------------------------------------------===// |
| 398 | |
| 399 | The x86 backend currently supports dynamic-no-pic. Need to add asm |
| 400 | printer support for static and PIC. |
Chris Lattner | 8f77b73 | 2006-02-08 06:52:06 +0000 | [diff] [blame] | 401 | |
| 402 | //===---------------------------------------------------------------------===// |
| 403 | |
| 404 | We should generate bts/btr/etc instructions on targets where they are cheap or |
| 405 | when codesize is important. e.g., for: |
| 406 | |
| 407 | void setbit(int *target, int bit) { |
| 408 | *target |= (1 << bit); |
| 409 | } |
| 410 | void clearbit(int *target, int bit) { |
| 411 | *target &= ~(1 << bit); |
| 412 | } |
| 413 | |
Chris Lattner | dba382b | 2006-02-08 17:47:22 +0000 | [diff] [blame^] | 414 | //===---------------------------------------------------------------------===// |
| 415 | |
| 416 | Easy: Global addresses are not always allowed as immediates. For this: |
| 417 | |
| 418 | int dst = 0; int *ptr = 0; |
| 419 | void foo() { ptr = &dst; } |
| 420 | |
| 421 | we get this: |
| 422 | |
| 423 | _foo: |
| 424 | movl $_dst, %eax |
| 425 | movl %eax, _ptr |
| 426 | ret |
| 427 | |
| 428 | When: "movl $_dst, _ptr" is sufficient. |
| 429 | |