README.txt 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279
  1. Target Independent Opportunities:
  2. //===---------------------------------------------------------------------===//
  3. We should recognized various "overflow detection" idioms and translate them into
  4. llvm.uadd.with.overflow and similar intrinsics. Here is a multiply idiom:
  5. unsigned int mul(unsigned int a,unsigned int b) {
  6. if ((unsigned long long)a*b>0xffffffff)
  7. exit(0);
  8. return a*b;
  9. }
  10. The legalization code for mul-with-overflow needs to be made more robust before
  11. this can be implemented though.
  12. //===---------------------------------------------------------------------===//
  13. Get the C front-end to expand hypot(x,y) -> llvm.sqrt(x*x+y*y) when errno and
  14. precision don't matter (ffastmath). Misc/mandel will like this. :) This isn't
  15. safe in general, even on darwin. See the libm implementation of hypot for
  16. examples (which special case when x/y are exactly zero to get signed zeros etc
  17. right).
  18. //===---------------------------------------------------------------------===//
  19. On targets with expensive 64-bit multiply, we could LSR this:
  20. for (i = ...; ++i) {
  21. x = 1ULL << i;
  22. into:
  23. long long tmp = 1;
  24. for (i = ...; ++i, tmp+=tmp)
  25. x = tmp;
  26. This would be a win on ppc32, but not x86 or ppc64.
  27. //===---------------------------------------------------------------------===//
  28. Shrink: (setlt (loadi32 P), 0) -> (setlt (loadi8 Phi), 0)
  29. //===---------------------------------------------------------------------===//
  30. Reassociate should turn things like:
  31. int factorial(int X) {
  32. return X*X*X*X*X*X*X*X;
  33. }
  34. into llvm.powi calls, allowing the code generator to produce balanced
  35. multiplication trees.
  36. First, the intrinsic needs to be extended to support integers, and second the
  37. code generator needs to be enhanced to lower these to multiplication trees.
  38. //===---------------------------------------------------------------------===//
  39. Interesting? testcase for add/shift/mul reassoc:
  40. int bar(int x, int y) {
  41. return x*x*x+y+x*x*x*x*x*y*y*y*y;
  42. }
  43. int foo(int z, int n) {
  44. return bar(z, n) + bar(2*z, 2*n);
  45. }
  46. This is blocked on not handling X*X*X -> powi(X, 3) (see note above). The issue
  47. is that we end up getting t = 2*X s = t*t and don't turn this into 4*X*X,
  48. which is the same number of multiplies and is canonical, because the 2*X has
  49. multiple uses. Here's a simple example:
  50. define i32 @test15(i32 %X1) {
  51. %B = mul i32 %X1, 47 ; X1*47
  52. %C = mul i32 %B, %B
  53. ret i32 %C
  54. }
  55. //===---------------------------------------------------------------------===//
  56. Reassociate should handle the example in GCC PR16157:
  57. extern int a0, a1, a2, a3, a4; extern int b0, b1, b2, b3, b4;
  58. void f () { /* this can be optimized to four additions... */
  59. b4 = a4 + a3 + a2 + a1 + a0;
  60. b3 = a3 + a2 + a1 + a0;
  61. b2 = a2 + a1 + a0;
  62. b1 = a1 + a0;
  63. }
  64. This requires reassociating to forms of expressions that are already available,
  65. something that reassoc doesn't think about yet.
  66. //===---------------------------------------------------------------------===//
  67. These two functions should generate the same code on big-endian systems:
  68. int g(int *j,int *l) { return memcmp(j,l,4); }
  69. int h(int *j, int *l) { return *j - *l; }
  70. this could be done in SelectionDAGISel.cpp, along with other special cases,
  71. for 1,2,4,8 bytes.
  72. //===---------------------------------------------------------------------===//
  73. It would be nice to revert this patch:
  74. http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20060213/031986.html
  75. And teach the dag combiner enough to simplify the code expanded before
  76. legalize. It seems plausible that this knowledge would let it simplify other
  77. stuff too.
  78. //===---------------------------------------------------------------------===//
  79. For vector types, DataLayout.cpp::getTypeInfo() returns alignment that is equal
  80. to the type size. It works but can be overly conservative as the alignment of
  81. specific vector types are target dependent.
  82. //===---------------------------------------------------------------------===//
  83. We should produce an unaligned load from code like this:
  84. v4sf example(float *P) {
  85. return (v4sf){P[0], P[1], P[2], P[3] };
  86. }
  87. //===---------------------------------------------------------------------===//
  88. Add support for conditional increments, and other related patterns. Instead
  89. of:
  90. movl 136(%esp), %eax
  91. cmpl $0, %eax
  92. je LBB16_2 #cond_next
  93. LBB16_1: #cond_true
  94. incl _foo
  95. LBB16_2: #cond_next
  96. emit:
  97. movl _foo, %eax
  98. cmpl $1, %edi
  99. sbbl $-1, %eax
  100. movl %eax, _foo
  101. //===---------------------------------------------------------------------===//
  102. Combine: a = sin(x), b = cos(x) into a,b = sincos(x).
  103. Expand these to calls of sin/cos and stores:
  104. double sincos(double x, double *sin, double *cos);
  105. float sincosf(float x, float *sin, float *cos);
  106. long double sincosl(long double x, long double *sin, long double *cos);
  107. Doing so could allow SROA of the destination pointers. See also:
  108. http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17687
  109. This is now easily doable with MRVs. We could even make an intrinsic for this
  110. if anyone cared enough about sincos.
  111. //===---------------------------------------------------------------------===//
  112. quantum_sigma_x in 462.libquantum contains the following loop:
  113. for(i=0; i<reg->size; i++)
  114. {
  115. /* Flip the target bit of each basis state */
  116. reg->node[i].state ^= ((MAX_UNSIGNED) 1 << target);
  117. }
  118. Where MAX_UNSIGNED/state is a 64-bit int. On a 32-bit platform it would be just
  119. so cool to turn it into something like:
  120. long long Res = ((MAX_UNSIGNED) 1 << target);
  121. if (target < 32) {
  122. for(i=0; i<reg->size; i++)
  123. reg->node[i].state ^= Res & 0xFFFFFFFFULL;
  124. } else {
  125. for(i=0; i<reg->size; i++)
  126. reg->node[i].state ^= Res & 0xFFFFFFFF00000000ULL
  127. }
  128. ... which would only do one 32-bit XOR per loop iteration instead of two.
  129. It would also be nice to recognize the reg->size doesn't alias reg->node[i],
  130. but this requires TBAA.
  131. //===---------------------------------------------------------------------===//
  132. This isn't recognized as bswap by instcombine (yes, it really is bswap):
  133. unsigned long reverse(unsigned v) {
  134. unsigned t;
  135. t = v ^ ((v << 16) | (v >> 16));
  136. t &= ~0xff0000;
  137. v = (v << 24) | (v >> 8);
  138. return v ^ (t >> 8);
  139. }
  140. //===---------------------------------------------------------------------===//
  141. [LOOP DELETION]
  142. We don't delete this output free loop, because trip count analysis doesn't
  143. realize that it is finite (if it were infinite, it would be undefined). Not
  144. having this blocks Loop Idiom from matching strlen and friends.
  145. void foo(char *C) {
  146. int x = 0;
  147. while (*C)
  148. ++x,++C;
  149. }
  150. //===---------------------------------------------------------------------===//
  151. [LOOP RECOGNITION]
  152. These idioms should be recognized as popcount (see PR1488):
  153. unsigned countbits_slow(unsigned v) {
  154. unsigned c;
  155. for (c = 0; v; v >>= 1)
  156. c += v & 1;
  157. return c;
  158. }
  159. unsigned int popcount(unsigned int input) {
  160. unsigned int count = 0;
  161. for (unsigned int i = 0; i < 4 * 8; i++)
  162. count += (input >> i) & i;
  163. return count;
  164. }
  165. This should be recognized as CLZ: rdar://8459039
  166. unsigned clz_a(unsigned a) {
  167. int i;
  168. for (i=0;i<32;i++)
  169. if (a & (1<<(31-i)))
  170. return i;
  171. return 32;
  172. }
  173. This sort of thing should be added to the loop idiom pass.
  174. //===---------------------------------------------------------------------===//
  175. These should turn into single 16-bit (unaligned?) loads on little/big endian
  176. processors.
  177. unsigned short read_16_le(const unsigned char *adr) {
  178. return adr[0] | (adr[1] << 8);
  179. }
  180. unsigned short read_16_be(const unsigned char *adr) {
  181. return (adr[0] << 8) | adr[1];
  182. }
  183. //===---------------------------------------------------------------------===//
  184. -instcombine should handle this transform:
  185. icmp pred (sdiv X / C1 ), C2
  186. when X, C1, and C2 are unsigned. Similarly for udiv and signed operands.
  187. Currently InstCombine avoids this transform but will do it when the signs of
  188. the operands and the sign of the divide match. See the FIXME in
  189. InstructionCombining.cpp in the visitSetCondInst method after the switch case
  190. for Instruction::UDiv (around line 4447) for more details.
  191. The SingleSource/Benchmarks/Shootout-C++/hash and hash2 tests have examples of
  192. this construct.
  193. //===---------------------------------------------------------------------===//
  194. [LOOP OPTIMIZATION]
  195. SingleSource/Benchmarks/Misc/dt.c shows several interesting optimization
  196. opportunities in its double_array_divs_variable function: it needs loop
  197. interchange, memory promotion (which LICM already does), vectorization and
  198. variable trip count loop unrolling (since it has a constant trip count). ICC
  199. apparently produces this very nice code with -ffast-math:
  200. ..B1.70: # Preds ..B1.70 ..B1.69
  201. mulpd %xmm0, %xmm1 #108.2
  202. mulpd %xmm0, %xmm1 #108.2
  203. mulpd %xmm0, %xmm1 #108.2
  204. mulpd %xmm0, %xmm1 #108.2
  205. addl $8, %edx #
  206. cmpl $131072, %edx #108.2
  207. jb ..B1.70 # Prob 99% #108.2
  208. It would be better to count down to zero, but this is a lot better than what we
  209. do.
  210. //===---------------------------------------------------------------------===//
  211. Consider:
  212. typedef unsigned U32;
  213. typedef unsigned long long U64;
  214. int test (U32 *inst, U64 *regs) {
  215. U64 effective_addr2;
  216. U32 temp = *inst;
  217. int r1 = (temp >> 20) & 0xf;
  218. int b2 = (temp >> 16) & 0xf;
  219. effective_addr2 = temp & 0xfff;
  220. if (b2) effective_addr2 += regs[b2];
  221. b2 = (temp >> 12) & 0xf;
  222. if (b2) effective_addr2 += regs[b2];
  223. effective_addr2 &= regs[4];
  224. if ((effective_addr2 & 3) == 0)
  225. return 1;
  226. return 0;
  227. }
  228. Note that only the low 2 bits of effective_addr2 are used. On 32-bit systems,
  229. we don't eliminate the computation of the top half of effective_addr2 because
  230. we don't have whole-function selection dags. On x86, this means we use one
  231. extra register for the function when effective_addr2 is declared as U64 than
  232. when it is declared U32.
  233. PHI Slicing could be extended to do this.
  234. //===---------------------------------------------------------------------===//
  235. Tail call elim should be more aggressive, checking to see if the call is
  236. followed by an uncond branch to an exit block.
  237. ; This testcase is due to tail-duplication not wanting to copy the return
  238. ; instruction into the terminating blocks because there was other code
  239. ; optimized out of the function after the taildup happened.
  240. ; RUN: llvm-as < %s | opt -tailcallelim | llvm-dis | not grep call
  241. define i32 @t4(i32 %a) {
  242. entry:
  243. %tmp.1 = and i32 %a, 1 ; <i32> [#uses=1]
  244. %tmp.2 = icmp ne i32 %tmp.1, 0 ; <i1> [#uses=1]
  245. br i1 %tmp.2, label %then.0, label %else.0
  246. then.0: ; preds = %entry
  247. %tmp.5 = add i32 %a, -1 ; <i32> [#uses=1]
  248. %tmp.3 = call i32 @t4( i32 %tmp.5 ) ; <i32> [#uses=1]
  249. br label %return
  250. else.0: ; preds = %entry
  251. %tmp.7 = icmp ne i32 %a, 0 ; <i1> [#uses=1]
  252. br i1 %tmp.7, label %then.1, label %return
  253. then.1: ; preds = %else.0
  254. %tmp.11 = add i32 %a, -2 ; <i32> [#uses=1]
  255. %tmp.9 = call i32 @t4( i32 %tmp.11 ) ; <i32> [#uses=1]
  256. br label %return
  257. return: ; preds = %then.1, %else.0, %then.0
  258. %result.0 = phi i32 [ 0, %else.0 ], [ %tmp.3, %then.0 ],
  259. [ %tmp.9, %then.1 ]
  260. ret i32 %result.0
  261. }
  262. //===---------------------------------------------------------------------===//
  263. Tail recursion elimination should handle:
  264. int pow2m1(int n) {
  265. if (n == 0)
  266. return 0;
  267. return 2 * pow2m1 (n - 1) + 1;
  268. }
  269. Also, multiplies can be turned into SHL's, so they should be handled as if
  270. they were associative. "return foo() << 1" can be tail recursion eliminated.
  271. //===---------------------------------------------------------------------===//
  272. Argument promotion should promote arguments for recursive functions, like
  273. this:
  274. ; RUN: llvm-as < %s | opt -argpromotion | llvm-dis | grep x.val
  275. define internal i32 @foo(i32* %x) {
  276. entry:
  277. %tmp = load i32* %x ; <i32> [#uses=0]
  278. %tmp.foo = call i32 @foo( i32* %x ) ; <i32> [#uses=1]
  279. ret i32 %tmp.foo
  280. }
  281. define i32 @bar(i32* %x) {
  282. entry:
  283. %tmp3 = call i32 @foo( i32* %x ) ; <i32> [#uses=1]
  284. ret i32 %tmp3
  285. }
  286. //===---------------------------------------------------------------------===//
  287. We should investigate an instruction sinking pass. Consider this silly
  288. example in pic mode:
  289. #include <assert.h>
  290. void foo(int x) {
  291. assert(x);
  292. //...
  293. }
  294. we compile this to:
  295. _foo:
  296. subl $28, %esp
  297. call "L1$pb"
  298. "L1$pb":
  299. popl %eax
  300. cmpl $0, 32(%esp)
  301. je LBB1_2 # cond_true
  302. LBB1_1: # return
  303. # ...
  304. addl $28, %esp
  305. ret
  306. LBB1_2: # cond_true
  307. ...
  308. The PIC base computation (call+popl) is only used on one path through the
  309. code, but is currently always computed in the entry block. It would be
  310. better to sink the picbase computation down into the block for the
  311. assertion, as it is the only one that uses it. This happens for a lot of
  312. code with early outs.
  313. Another example is loads of arguments, which are usually emitted into the
  314. entry block on targets like x86. If not used in all paths through a
  315. function, they should be sunk into the ones that do.
  316. In this case, whole-function-isel would also handle this.
  317. //===---------------------------------------------------------------------===//
  318. Investigate lowering of sparse switch statements into perfect hash tables:
  319. http://burtleburtle.net/bob/hash/perfect.html
  320. //===---------------------------------------------------------------------===//
  321. We should turn things like "load+fabs+store" and "load+fneg+store" into the
  322. corresponding integer operations. On a yonah, this loop:
  323. double a[256];
  324. void foo() {
  325. int i, b;
  326. for (b = 0; b < 10000000; b++)
  327. for (i = 0; i < 256; i++)
  328. a[i] = -a[i];
  329. }
  330. is twice as slow as this loop:
  331. long long a[256];
  332. void foo() {
  333. int i, b;
  334. for (b = 0; b < 10000000; b++)
  335. for (i = 0; i < 256; i++)
  336. a[i] ^= (1ULL << 63);
  337. }
  338. and I suspect other processors are similar. On X86 in particular this is a
  339. big win because doing this with integers allows the use of read/modify/write
  340. instructions.
  341. //===---------------------------------------------------------------------===//
  342. DAG Combiner should try to combine small loads into larger loads when
  343. profitable. For example, we compile this C++ example:
  344. struct THotKey { short Key; bool Control; bool Shift; bool Alt; };
  345. extern THotKey m_HotKey;
  346. THotKey GetHotKey () { return m_HotKey; }
  347. into (-m64 -O3 -fno-exceptions -static -fomit-frame-pointer):
  348. __Z9GetHotKeyv: ## @_Z9GetHotKeyv
  349. movq _m_HotKey@GOTPCREL(%rip), %rax
  350. movzwl (%rax), %ecx
  351. movzbl 2(%rax), %edx
  352. shlq $16, %rdx
  353. orq %rcx, %rdx
  354. movzbl 3(%rax), %ecx
  355. shlq $24, %rcx
  356. orq %rdx, %rcx
  357. movzbl 4(%rax), %eax
  358. shlq $32, %rax
  359. orq %rcx, %rax
  360. ret
  361. //===---------------------------------------------------------------------===//
  362. We should add an FRINT node to the DAG to model targets that have legal
  363. implementations of ceil/floor/rint.
  364. //===---------------------------------------------------------------------===//
  365. Consider:
  366. int test() {
  367. long long input[8] = {1,0,1,0,1,0,1,0};
  368. foo(input);
  369. }
  370. Clang compiles this into:
  371. call void @llvm.memset.p0i8.i64(i8* %tmp, i8 0, i64 64, i32 16, i1 false)
  372. %0 = getelementptr [8 x i64]* %input, i64 0, i64 0
  373. store i64 1, i64* %0, align 16
  374. %1 = getelementptr [8 x i64]* %input, i64 0, i64 2
  375. store i64 1, i64* %1, align 16
  376. %2 = getelementptr [8 x i64]* %input, i64 0, i64 4
  377. store i64 1, i64* %2, align 16
  378. %3 = getelementptr [8 x i64]* %input, i64 0, i64 6
  379. store i64 1, i64* %3, align 16
  380. Which gets codegen'd into:
  381. pxor %xmm0, %xmm0
  382. movaps %xmm0, -16(%rbp)
  383. movaps %xmm0, -32(%rbp)
  384. movaps %xmm0, -48(%rbp)
  385. movaps %xmm0, -64(%rbp)
  386. movq $1, -64(%rbp)
  387. movq $1, -48(%rbp)
  388. movq $1, -32(%rbp)
  389. movq $1, -16(%rbp)
  390. It would be better to have 4 movq's of 0 instead of the movaps's.
  391. //===---------------------------------------------------------------------===//
  392. http://llvm.org/PR717:
  393. The following code should compile into "ret int undef". Instead, LLVM
  394. produces "ret int 0":
  395. int f() {
  396. int x = 4;
  397. int y;
  398. if (x == 3) y = 0;
  399. return y;
  400. }
  401. //===---------------------------------------------------------------------===//
  402. The loop unroller should partially unroll loops (instead of peeling them)
  403. when code growth isn't too bad and when an unroll count allows simplification
  404. of some code within the loop. One trivial example is:
  405. #include <stdio.h>
  406. int main() {
  407. int nRet = 17;
  408. int nLoop;
  409. for ( nLoop = 0; nLoop < 1000; nLoop++ ) {
  410. if ( nLoop & 1 )
  411. nRet += 2;
  412. else
  413. nRet -= 1;
  414. }
  415. return nRet;
  416. }
  417. Unrolling by 2 would eliminate the '&1' in both copies, leading to a net
  418. reduction in code size. The resultant code would then also be suitable for
  419. exit value computation.
  420. //===---------------------------------------------------------------------===//
  421. We miss a bunch of rotate opportunities on various targets, including ppc, x86,
  422. etc. On X86, we miss a bunch of 'rotate by variable' cases because the rotate
  423. matching code in dag combine doesn't look through truncates aggressively
  424. enough. Here are some testcases reduces from GCC PR17886:
  425. unsigned long long f5(unsigned long long x, unsigned long long y) {
  426. return (x << 8) | ((y >> 48) & 0xffull);
  427. }
  428. unsigned long long f6(unsigned long long x, unsigned long long y, int z) {
  429. switch(z) {
  430. case 1:
  431. return (x << 8) | ((y >> 48) & 0xffull);
  432. case 2:
  433. return (x << 16) | ((y >> 40) & 0xffffull);
  434. case 3:
  435. return (x << 24) | ((y >> 32) & 0xffffffull);
  436. case 4:
  437. return (x << 32) | ((y >> 24) & 0xffffffffull);
  438. default:
  439. return (x << 40) | ((y >> 16) & 0xffffffffffull);
  440. }
  441. }
  442. //===---------------------------------------------------------------------===//
  443. This (and similar related idioms):
  444. unsigned int foo(unsigned char i) {
  445. return i | (i<<8) | (i<<16) | (i<<24);
  446. }
  447. compiles into:
  448. define i32 @foo(i8 zeroext %i) nounwind readnone ssp noredzone {
  449. entry:
  450. %conv = zext i8 %i to i32
  451. %shl = shl i32 %conv, 8
  452. %shl5 = shl i32 %conv, 16
  453. %shl9 = shl i32 %conv, 24
  454. %or = or i32 %shl9, %conv
  455. %or6 = or i32 %or, %shl5
  456. %or10 = or i32 %or6, %shl
  457. ret i32 %or10
  458. }
  459. it would be better as:
  460. unsigned int bar(unsigned char i) {
  461. unsigned int j=i | (i << 8);
  462. return j | (j<<16);
  463. }
  464. aka:
  465. define i32 @bar(i8 zeroext %i) nounwind readnone ssp noredzone {
  466. entry:
  467. %conv = zext i8 %i to i32
  468. %shl = shl i32 %conv, 8
  469. %or = or i32 %shl, %conv
  470. %shl5 = shl i32 %or, 16
  471. %or6 = or i32 %shl5, %or
  472. ret i32 %or6
  473. }
  474. or even i*0x01010101, depending on the speed of the multiplier. The best way to
  475. handle this is to canonicalize it to a multiply in IR and have codegen handle
  476. lowering multiplies to shifts on cpus where shifts are faster.
  477. //===---------------------------------------------------------------------===//
  478. We do a number of simplifications in simplify libcalls to strength reduce
  479. standard library functions, but we don't currently merge them together. For
  480. example, it is useful to merge memcpy(a,b,strlen(b)) -> strcpy. This can only
  481. be done safely if "b" isn't modified between the strlen and memcpy of course.
  482. //===---------------------------------------------------------------------===//
  483. We compile this program: (from GCC PR11680)
  484. http://gcc.gnu.org/bugzilla/attachment.cgi?id=4487
  485. Into code that runs the same speed in fast/slow modes, but both modes run 2x
  486. slower than when compile with GCC (either 4.0 or 4.2):
  487. $ llvm-g++ perf.cpp -O3 -fno-exceptions
  488. $ time ./a.out fast
  489. 1.821u 0.003s 0:01.82 100.0% 0+0k 0+0io 0pf+0w
  490. $ g++ perf.cpp -O3 -fno-exceptions
  491. $ time ./a.out fast
  492. 0.821u 0.001s 0:00.82 100.0% 0+0k 0+0io 0pf+0w
  493. It looks like we are making the same inlining decisions, so this may be raw
  494. codegen badness or something else (haven't investigated).
  495. //===---------------------------------------------------------------------===//
  496. Divisibility by constant can be simplified (according to GCC PR12849) from
  497. being a mulhi to being a mul lo (cheaper). Testcase:
  498. void bar(unsigned n) {
  499. if (n % 3 == 0)
  500. true();
  501. }
  502. This is equivalent to the following, where 2863311531 is the multiplicative
  503. inverse of 3, and 1431655766 is ((2^32)-1)/3+1:
  504. void bar(unsigned n) {
  505. if (n * 2863311531U < 1431655766U)
  506. true();
  507. }
  508. The same transformation can work with an even modulo with the addition of a
  509. rotate: rotate the result of the multiply to the right by the number of bits
  510. which need to be zero for the condition to be true, and shrink the compare RHS
  511. by the same amount. Unless the target supports rotates, though, that
  512. transformation probably isn't worthwhile.
  513. The transformation can also easily be made to work with non-zero equality
  514. comparisons: just transform, for example, "n % 3 == 1" to "(n-1) % 3 == 0".
  515. //===---------------------------------------------------------------------===//
  516. Better mod/ref analysis for scanf would allow us to eliminate the vtable and a
  517. bunch of other stuff from this example (see PR1604):
  518. #include <cstdio>
  519. struct test {
  520. int val;
  521. virtual ~test() {}
  522. };
  523. int main() {
  524. test t;
  525. std::scanf("%d", &t.val);
  526. std::printf("%d\n", t.val);
  527. }
  528. //===---------------------------------------------------------------------===//
  529. These functions perform the same computation, but produce different assembly.
  530. define i8 @select(i8 %x) readnone nounwind {
  531. %A = icmp ult i8 %x, 250
  532. %B = select i1 %A, i8 0, i8 1
  533. ret i8 %B
  534. }
  535. define i8 @addshr(i8 %x) readnone nounwind {
  536. %A = zext i8 %x to i9
  537. %B = add i9 %A, 6 ;; 256 - 250 == 6
  538. %C = lshr i9 %B, 8
  539. %D = trunc i9 %C to i8
  540. ret i8 %D
  541. }
  542. //===---------------------------------------------------------------------===//
  543. From gcc bug 24696:
  544. int
  545. f (unsigned long a, unsigned long b, unsigned long c)
  546. {
  547. return ((a & (c - 1)) != 0) || ((b & (c - 1)) != 0);
  548. }
  549. int
  550. f (unsigned long a, unsigned long b, unsigned long c)
  551. {
  552. return ((a & (c - 1)) != 0) | ((b & (c - 1)) != 0);
  553. }
  554. Both should combine to ((a|b) & (c-1)) != 0. Currently not optimized with
  555. "clang -emit-llvm-bc | opt -O3".
  556. //===---------------------------------------------------------------------===//
  557. From GCC Bug 20192:
  558. #define PMD_MASK (~((1UL << 23) - 1))
  559. void clear_pmd_range(unsigned long start, unsigned long end)
  560. {
  561. if (!(start & ~PMD_MASK) && !(end & ~PMD_MASK))
  562. f();
  563. }
  564. The expression should optimize to something like
  565. "!((start|end)&~PMD_MASK). Currently not optimized with "clang
  566. -emit-llvm-bc | opt -O3".
  567. //===---------------------------------------------------------------------===//
  568. unsigned int f(unsigned int i, unsigned int n) {++i; if (i == n) ++i; return
  569. i;}
  570. unsigned int f2(unsigned int i, unsigned int n) {++i; i += i == n; return i;}
  571. These should combine to the same thing. Currently, the first function
  572. produces better code on X86.
  573. //===---------------------------------------------------------------------===//
  574. From GCC Bug 15784:
  575. #define abs(x) x>0?x:-x
  576. int f(int x, int y)
  577. {
  578. return (abs(x)) >= 0;
  579. }
  580. This should optimize to x == INT_MIN. (With -fwrapv.) Currently not
  581. optimized with "clang -emit-llvm-bc | opt -O3".
  582. //===---------------------------------------------------------------------===//
  583. From GCC Bug 14753:
  584. void
  585. rotate_cst (unsigned int a)
  586. {
  587. a = (a << 10) | (a >> 22);
  588. if (a == 123)
  589. bar ();
  590. }
  591. void
  592. minus_cst (unsigned int a)
  593. {
  594. unsigned int tem;
  595. tem = 20 - a;
  596. if (tem == 5)
  597. bar ();
  598. }
  599. void
  600. mask_gt (unsigned int a)
  601. {
  602. /* This is equivalent to a > 15. */
  603. if ((a & ~7) > 8)
  604. bar ();
  605. }
  606. void
  607. rshift_gt (unsigned int a)
  608. {
  609. /* This is equivalent to a > 23. */
  610. if ((a >> 2) > 5)
  611. bar ();
  612. }
  613. All should simplify to a single comparison. All of these are
  614. currently not optimized with "clang -emit-llvm-bc | opt
  615. -O3".
  616. //===---------------------------------------------------------------------===//
  617. From GCC Bug 32605:
  618. int c(int* x) {return (char*)x+2 == (char*)x;}
  619. Should combine to 0. Currently not optimized with "clang
  620. -emit-llvm-bc | opt -O3" (although llc can optimize it).
  621. //===---------------------------------------------------------------------===//
  622. int a(unsigned b) {return ((b << 31) | (b << 30)) >> 31;}
  623. Should be combined to "((b >> 1) | b) & 1". Currently not optimized
  624. with "clang -emit-llvm-bc | opt -O3".
  625. //===---------------------------------------------------------------------===//
  626. unsigned a(unsigned x, unsigned y) { return x | (y & 1) | (y & 2);}
  627. Should combine to "x | (y & 3)". Currently not optimized with "clang
  628. -emit-llvm-bc | opt -O3".
  629. //===---------------------------------------------------------------------===//
  630. int a(int a, int b, int c) {return (~a & c) | ((c|a) & b);}
  631. Should fold to "(~a & c) | (a & b)". Currently not optimized with
  632. "clang -emit-llvm-bc | opt -O3".
  633. //===---------------------------------------------------------------------===//
  634. int a(int a,int b) {return (~(a|b))|a;}
  635. Should fold to "a|~b". Currently not optimized with "clang
  636. -emit-llvm-bc | opt -O3".
  637. //===---------------------------------------------------------------------===//
  638. int a(int a, int b) {return (a&&b) || (a&&!b);}
  639. Should fold to "a". Currently not optimized with "clang -emit-llvm-bc
  640. | opt -O3".
  641. //===---------------------------------------------------------------------===//
  642. int a(int a, int b, int c) {return (a&&b) || (!a&&c);}
  643. Should fold to "a ? b : c", or at least something sane. Currently not
  644. optimized with "clang -emit-llvm-bc | opt -O3".
  645. //===---------------------------------------------------------------------===//
  646. int a(int a, int b, int c) {return (a&&b) || (a&&c) || (a&&b&&c);}
  647. Should fold to a && (b || c). Currently not optimized with "clang
  648. -emit-llvm-bc | opt -O3".
  649. //===---------------------------------------------------------------------===//
  650. int a(int x) {return x | ((x & 8) ^ 8);}
  651. Should combine to x | 8. Currently not optimized with "clang
  652. -emit-llvm-bc | opt -O3".
  653. //===---------------------------------------------------------------------===//
  654. int a(int x) {return x ^ ((x & 8) ^ 8);}
  655. Should also combine to x | 8. Currently not optimized with "clang
  656. -emit-llvm-bc | opt -O3".
  657. //===---------------------------------------------------------------------===//
  658. int a(int x) {return ((x | -9) ^ 8) & x;}
  659. Should combine to x & -9. Currently not optimized with "clang
  660. -emit-llvm-bc | opt -O3".
  661. //===---------------------------------------------------------------------===//
  662. unsigned a(unsigned a) {return a * 0x11111111 >> 28 & 1;}
  663. Should combine to "a * 0x88888888 >> 31". Currently not optimized
  664. with "clang -emit-llvm-bc | opt -O3".
  665. //===---------------------------------------------------------------------===//
  666. unsigned a(char* x) {if ((*x & 32) == 0) return b();}
  667. There's an unnecessary zext in the generated code with "clang
  668. -emit-llvm-bc | opt -O3".
  669. //===---------------------------------------------------------------------===//
  670. unsigned a(unsigned long long x) {return 40 * (x >> 1);}
  671. Should combine to "20 * (((unsigned)x) & -2)". Currently not
  672. optimized with "clang -emit-llvm-bc | opt -O3".
  673. //===---------------------------------------------------------------------===//
  674. int g(int x) { return (x - 10) < 0; }
  675. Should combine to "x <= 9" (the sub has nsw). Currently not
  676. optimized with "clang -emit-llvm-bc | opt -O3".
  677. //===---------------------------------------------------------------------===//
  678. int g(int x) { return (x + 10) < 0; }
  679. Should combine to "x < -10" (the add has nsw). Currently not
  680. optimized with "clang -emit-llvm-bc | opt -O3".
  681. //===---------------------------------------------------------------------===//
  682. int f(int i, int j) { return i < j + 1; }
  683. int g(int i, int j) { return j > i - 1; }
  684. Should combine to "i <= j" (the add/sub has nsw). Currently not
  685. optimized with "clang -emit-llvm-bc | opt -O3".
  686. //===---------------------------------------------------------------------===//
  687. unsigned f(unsigned x) { return ((x & 7) + 1) & 15; }
  688. The & 15 part should be optimized away, it doesn't change the result. Currently
  689. not optimized with "clang -emit-llvm-bc | opt -O3".
  690. //===---------------------------------------------------------------------===//
  691. This was noticed in the entryblock for grokdeclarator in 403.gcc:
  692. %tmp = icmp eq i32 %decl_context, 4
  693. %decl_context_addr.0 = select i1 %tmp, i32 3, i32 %decl_context
  694. %tmp1 = icmp eq i32 %decl_context_addr.0, 1
  695. %decl_context_addr.1 = select i1 %tmp1, i32 0, i32 %decl_context_addr.0
  696. tmp1 should be simplified to something like:
  697. (!tmp || decl_context == 1)
  698. This allows recursive simplifications, tmp1 is used all over the place in
  699. the function, e.g. by:
  700. %tmp23 = icmp eq i32 %decl_context_addr.1, 0 ; <i1> [#uses=1]
  701. %tmp24 = xor i1 %tmp1, true ; <i1> [#uses=1]
  702. %or.cond8 = and i1 %tmp23, %tmp24 ; <i1> [#uses=1]
  703. later.
  704. //===---------------------------------------------------------------------===//
  705. [STORE SINKING]
  706. Store sinking: This code:
  707. void f (int n, int *cond, int *res) {
  708. int i;
  709. *res = 0;
  710. for (i = 0; i < n; i++)
  711. if (*cond)
  712. *res ^= 234; /* (*) */
  713. }
  714. On this function GVN hoists the fully redundant value of *res, but nothing
  715. moves the store out. This gives us this code:
  716. bb: ; preds = %bb2, %entry
  717. %.rle = phi i32 [ 0, %entry ], [ %.rle6, %bb2 ]
  718. %i.05 = phi i32 [ 0, %entry ], [ %indvar.next, %bb2 ]
  719. %1 = load i32* %cond, align 4
  720. %2 = icmp eq i32 %1, 0
  721. br i1 %2, label %bb2, label %bb1
  722. bb1: ; preds = %bb
  723. %3 = xor i32 %.rle, 234
  724. store i32 %3, i32* %res, align 4
  725. br label %bb2
  726. bb2: ; preds = %bb, %bb1
  727. %.rle6 = phi i32 [ %3, %bb1 ], [ %.rle, %bb ]
  728. %indvar.next = add i32 %i.05, 1
  729. %exitcond = icmp eq i32 %indvar.next, %n
  730. br i1 %exitcond, label %return, label %bb
  731. DSE should sink partially dead stores to get the store out of the loop.
  732. Here's another partial dead case:
  733. http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12395
  734. //===---------------------------------------------------------------------===//
  735. Scalar PRE hoists the mul in the common block up to the else:
  736. int test (int a, int b, int c, int g) {
  737. int d, e;
  738. if (a)
  739. d = b * c;
  740. else
  741. d = b - c;
  742. e = b * c + g;
  743. return d + e;
  744. }
  745. It would be better to do the mul once to reduce codesize above the if.
  746. This is GCC PR38204.
  747. //===---------------------------------------------------------------------===//
  748. This simple function from 179.art:
  749. int winner, numf2s;
  750. struct { double y; int reset; } *Y;
  751. void find_match() {
  752. int i;
  753. winner = 0;
  754. for (i=0;i<numf2s;i++)
  755. if (Y[i].y > Y[winner].y)
  756. winner =i;
  757. }
  758. Compiles into (with clang TBAA):
  759. for.body: ; preds = %for.inc, %bb.nph
  760. %indvar = phi i64 [ 0, %bb.nph ], [ %indvar.next, %for.inc ]
  761. %i.01718 = phi i32 [ 0, %bb.nph ], [ %i.01719, %for.inc ]
  762. %tmp4 = getelementptr inbounds %struct.anon* %tmp3, i64 %indvar, i32 0
  763. %tmp5 = load double* %tmp4, align 8, !tbaa !4
  764. %idxprom7 = sext i32 %i.01718 to i64
  765. %tmp10 = getelementptr inbounds %struct.anon* %tmp3, i64 %idxprom7, i32 0
  766. %tmp11 = load double* %tmp10, align 8, !tbaa !4
  767. %cmp12 = fcmp ogt double %tmp5, %tmp11
  768. br i1 %cmp12, label %if.then, label %for.inc
  769. if.then: ; preds = %for.body
  770. %i.017 = trunc i64 %indvar to i32
  771. br label %for.inc
  772. for.inc: ; preds = %for.body, %if.then
  773. %i.01719 = phi i32 [ %i.01718, %for.body ], [ %i.017, %if.then ]
  774. %indvar.next = add i64 %indvar, 1
  775. %exitcond = icmp eq i64 %indvar.next, %tmp22
  776. br i1 %exitcond, label %for.cond.for.end_crit_edge, label %for.body
  777. It is good that we hoisted the reloads of numf2's, and Y out of the loop and
  778. sunk the store to winner out.
  779. However, this is awful on several levels: the conditional truncate in the loop
  780. (-indvars at fault? why can't we completely promote the IV to i64?).
  781. Beyond that, we have a partially redundant load in the loop: if "winner" (aka
  782. %i.01718) isn't updated, we reload Y[winner].y the next time through the loop.
  783. Similarly, the addressing that feeds it (including the sext) is redundant. In
  784. the end we get this generated assembly:
  785. LBB0_2: ## %for.body
  786. ## =>This Inner Loop Header: Depth=1
  787. movsd (%rdi), %xmm0
  788. movslq %edx, %r8
  789. shlq $4, %r8
  790. ucomisd (%rcx,%r8), %xmm0
  791. jbe LBB0_4
  792. movl %esi, %edx
  793. LBB0_4: ## %for.inc
  794. addq $16, %rdi
  795. incq %rsi
  796. cmpq %rsi, %rax
  797. jne LBB0_2
  798. All things considered this isn't too bad, but we shouldn't need the movslq or
  799. the shlq instruction, or the load folded into ucomisd every time through the
  800. loop.
  801. On an x86-specific topic, if the loop can't be restructure, the movl should be a
  802. cmov.
  803. //===---------------------------------------------------------------------===//
  804. [STORE SINKING]
  805. GCC PR37810 is an interesting case where we should sink load/store reload
  806. into the if block and outside the loop, so we don't reload/store it on the
  807. non-call path.
  808. for () {
  809. *P += 1;
  810. if ()
  811. call();
  812. else
  813. ...
  814. ->
  815. tmp = *P
  816. for () {
  817. tmp += 1;
  818. if () {
  819. *P = tmp;
  820. call();
  821. tmp = *P;
  822. } else ...
  823. }
  824. *P = tmp;
  825. We now hoist the reload after the call (Transforms/GVN/lpre-call-wrap.ll), but
  826. we don't sink the store. We need partially dead store sinking.
  827. //===---------------------------------------------------------------------===//
  828. [LOAD PRE CRIT EDGE SPLITTING]
  829. GCC PR37166: Sinking of loads prevents SROA'ing the "g" struct on the stack
  830. leading to excess stack traffic. This could be handled by GVN with some crazy
  831. symbolic phi translation. The code we get looks like (g is on the stack):
  832. bb2: ; preds = %bb1
  833. ..
  834. %9 = getelementptr %struct.f* %g, i32 0, i32 0
  835. store i32 %8, i32* %9, align bel %bb3
  836. bb3: ; preds = %bb1, %bb2, %bb
  837. %c_addr.0 = phi %struct.f* [ %g, %bb2 ], [ %c, %bb ], [ %c, %bb1 ]
  838. %b_addr.0 = phi %struct.f* [ %b, %bb2 ], [ %g, %bb ], [ %b, %bb1 ]
  839. %10 = getelementptr %struct.f* %c_addr.0, i32 0, i32 0
  840. %11 = load i32* %10, align 4
  841. %11 is partially redundant, an in BB2 it should have the value %8.
  842. GCC PR33344 and PR35287 are similar cases.
  843. //===---------------------------------------------------------------------===//
  844. [LOAD PRE]
  845. There are many load PRE testcases in testsuite/gcc.dg/tree-ssa/loadpre* in the
  846. GCC testsuite, ones we don't get yet are (checked through loadpre25):
  847. [CRIT EDGE BREAKING]
  848. predcom-4.c
  849. [PRE OF READONLY CALL]
  850. loadpre5.c
  851. [TURN SELECT INTO BRANCH]
  852. loadpre14.c loadpre15.c
  853. actually a conditional increment: loadpre18.c loadpre19.c
  854. //===---------------------------------------------------------------------===//
  855. [LOAD PRE / STORE SINKING / SPEC HACK]
  856. This is a chunk of code from 456.hmmer:
  857. int f(int M, int *mc, int *mpp, int *tpmm, int *ip, int *tpim, int *dpp,
  858. int *tpdm, int xmb, int *bp, int *ms) {
  859. int k, sc;
  860. for (k = 1; k <= M; k++) {
  861. mc[k] = mpp[k-1] + tpmm[k-1];
  862. if ((sc = ip[k-1] + tpim[k-1]) > mc[k]) mc[k] = sc;
  863. if ((sc = dpp[k-1] + tpdm[k-1]) > mc[k]) mc[k] = sc;
  864. if ((sc = xmb + bp[k]) > mc[k]) mc[k] = sc;
  865. mc[k] += ms[k];
  866. }
  867. }
  868. It is very profitable for this benchmark to turn the conditional stores to mc[k]
  869. into a conditional move (select instr in IR) and allow the final store to do the
  870. store. See GCC PR27313 for more details. Note that this is valid to xform even
  871. with the new C++ memory model, since mc[k] is previously loaded and later
  872. stored.
  873. //===---------------------------------------------------------------------===//
  874. [SCALAR PRE]
  875. There are many PRE testcases in testsuite/gcc.dg/tree-ssa/ssa-pre-*.c in the
  876. GCC testsuite.
  877. //===---------------------------------------------------------------------===//
  878. There are some interesting cases in testsuite/gcc.dg/tree-ssa/pred-comm* in the
  879. GCC testsuite. For example, we get the first example in predcom-1.c, but
  880. miss the second one:
  881. unsigned fib[1000];
  882. unsigned avg[1000];
  883. __attribute__ ((noinline))
  884. void count_averages(int n) {
  885. int i;
  886. for (i = 1; i < n; i++)
  887. avg[i] = (((unsigned long) fib[i - 1] + fib[i] + fib[i + 1]) / 3) & 0xffff;
  888. }
  889. which compiles into two loads instead of one in the loop.
  890. predcom-2.c is the same as predcom-1.c
  891. predcom-3.c is very similar but needs loads feeding each other instead of
  892. store->load.
  893. //===---------------------------------------------------------------------===//
  894. [ALIAS ANALYSIS]
  895. Type based alias analysis:
  896. http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14705
  897. We should do better analysis of posix_memalign. At the least it should
  898. no-capture its pointer argument, at best, we should know that the out-value
  899. result doesn't point to anything (like malloc). One example of this is in
  900. SingleSource/Benchmarks/Misc/dt.c
  901. //===---------------------------------------------------------------------===//
  902. Interesting missed case because of control flow flattening (should be 2 loads):
  903. http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26629
  904. With: llvm-gcc t2.c -S -o - -O0 -emit-llvm | llvm-as |
  905. opt -mem2reg -gvn -instcombine | llvm-dis
  906. we miss it because we need 1) CRIT EDGE 2) MULTIPLE DIFFERENT
  907. VALS PRODUCED BY ONE BLOCK OVER DIFFERENT PATHS
  908. //===---------------------------------------------------------------------===//
  909. http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19633
  910. We could eliminate the branch condition here, loading from null is undefined:
  911. struct S { int w, x, y, z; };
  912. struct T { int r; struct S s; };
  913. void bar (struct S, int);
  914. void foo (int a, struct T b)
  915. {
  916. struct S *c = 0;
  917. if (a)
  918. c = &b.s;
  919. bar (*c, a);
  920. }
  921. //===---------------------------------------------------------------------===//
  922. simplifylibcalls should do several optimizations for strspn/strcspn:
  923. strcspn(x, "a") -> inlined loop for up to 3 letters (similarly for strspn):
  924. size_t __strcspn_c3 (__const char *__s, int __reject1, int __reject2,
  925. int __reject3) {
  926. register size_t __result = 0;
  927. while (__s[__result] != '\0' && __s[__result] != __reject1 &&
  928. __s[__result] != __reject2 && __s[__result] != __reject3)
  929. ++__result;
  930. return __result;
  931. }
  932. This should turn into a switch on the character. See PR3253 for some notes on
  933. codegen.
  934. 456.hmmer apparently uses strcspn and strspn a lot. 471.omnetpp uses strspn.
  935. //===---------------------------------------------------------------------===//
  936. simplifylibcalls should turn these snprintf idioms into memcpy (GCC PR47917)
  937. char buf1[6], buf2[6], buf3[4], buf4[4];
  938. int i;
  939. int foo (void) {
  940. int ret = snprintf (buf1, sizeof buf1, "abcde");
  941. ret += snprintf (buf2, sizeof buf2, "abcdef") * 16;
  942. ret += snprintf (buf3, sizeof buf3, "%s", i++ < 6 ? "abc" : "def") * 256;
  943. ret += snprintf (buf4, sizeof buf4, "%s", i++ > 10 ? "abcde" : "defgh")*4096;
  944. return ret;
  945. }
  946. //===---------------------------------------------------------------------===//
  947. "gas" uses this idiom:
  948. else if (strchr ("+-/*%|&^:[]()~", *intel_parser.op_string))
  949. ..
  950. else if (strchr ("<>", *intel_parser.op_string)
  951. Those should be turned into a switch. SimplifyLibCalls only gets the second
  952. case.
  953. //===---------------------------------------------------------------------===//
  954. 252.eon contains this interesting code:
  955. %3072 = getelementptr [100 x i8]* %tempString, i32 0, i32 0
  956. %3073 = call i8* @strcpy(i8* %3072, i8* %3071) nounwind
  957. %strlen = call i32 @strlen(i8* %3072) ; uses = 1
  958. %endptr = getelementptr [100 x i8]* %tempString, i32 0, i32 %strlen
  959. call void @llvm.memcpy.i32(i8* %endptr,
  960. i8* getelementptr ([5 x i8]* @"\01LC42", i32 0, i32 0), i32 5, i32 1)
  961. %3074 = call i32 @strlen(i8* %endptr) nounwind readonly
  962. This is interesting for a couple reasons. First, in this:
  963. The memcpy+strlen strlen can be replaced with:
  964. %3074 = call i32 @strlen([5 x i8]* @"\01LC42") nounwind readonly
  965. Because the destination was just copied into the specified memory buffer. This,
  966. in turn, can be constant folded to "4".
  967. In other code, it contains:
  968. %endptr6978 = bitcast i8* %endptr69 to i32*
  969. store i32 7107374, i32* %endptr6978, align 1
  970. %3167 = call i32 @strlen(i8* %endptr69) nounwind readonly
  971. Which could also be constant folded. Whatever is producing this should probably
  972. be fixed to leave this as a memcpy from a string.
  973. Further, eon also has an interesting partially redundant strlen call:
  974. bb8: ; preds = %_ZN18eonImageCalculatorC1Ev.exit
  975. %682 = getelementptr i8** %argv, i32 6 ; <i8**> [#uses=2]
  976. %683 = load i8** %682, align 4 ; <i8*> [#uses=4]
  977. %684 = load i8* %683, align 1 ; <i8> [#uses=1]
  978. %685 = icmp eq i8 %684, 0 ; <i1> [#uses=1]
  979. br i1 %685, label %bb10, label %bb9
  980. bb9: ; preds = %bb8
  981. %686 = call i32 @strlen(i8* %683) nounwind readonly
  982. %687 = icmp ugt i32 %686, 254 ; <i1> [#uses=1]
  983. br i1 %687, label %bb10, label %bb11
  984. bb10: ; preds = %bb9, %bb8
  985. %688 = call i32 @strlen(i8* %683) nounwind readonly
  986. This could be eliminated by doing the strlen once in bb8, saving code size and
  987. improving perf on the bb8->9->10 path.
  988. //===---------------------------------------------------------------------===//
  989. I see an interesting fully redundant call to strlen left in 186.crafty:InputMove
  990. which looks like:
  991. %movetext11 = getelementptr [128 x i8]* %movetext, i32 0, i32 0
  992. bb62: ; preds = %bb55, %bb53
  993. %promote.0 = phi i32 [ %169, %bb55 ], [ 0, %bb53 ]
  994. %171 = call i32 @strlen(i8* %movetext11) nounwind readonly align 1
  995. %172 = add i32 %171, -1 ; <i32> [#uses=1]
  996. %173 = getelementptr [128 x i8]* %movetext, i32 0, i32 %172
  997. ... no stores ...
  998. br i1 %or.cond, label %bb65, label %bb72
  999. bb65: ; preds = %bb62
  1000. store i8 0, i8* %173, align 1
  1001. br label %bb72
  1002. bb72: ; preds = %bb65, %bb62
  1003. %trank.1 = phi i32 [ %176, %bb65 ], [ -1, %bb62 ]
  1004. %177 = call i32 @strlen(i8* %movetext11) nounwind readonly align 1
  1005. Note that on the bb62->bb72 path, that the %177 strlen call is partially
  1006. redundant with the %171 call. At worst, we could shove the %177 strlen call
  1007. up into the bb65 block moving it out of the bb62->bb72 path. However, note
  1008. that bb65 stores to the string, zeroing out the last byte. This means that on
  1009. that path the value of %177 is actually just %171-1. A sub is cheaper than a
  1010. strlen!
  1011. This pattern repeats several times, basically doing:
  1012. A = strlen(P);
  1013. P[A-1] = 0;
  1014. B = strlen(P);
  1015. where it is "obvious" that B = A-1.
  1016. //===---------------------------------------------------------------------===//
  1017. 186.crafty has this interesting pattern with the "out.4543" variable:
  1018. call void @llvm.memcpy.i32(
  1019. i8* getelementptr ([10 x i8]* @out.4543, i32 0, i32 0),
  1020. i8* getelementptr ([7 x i8]* @"\01LC28700", i32 0, i32 0), i32 7, i32 1)
  1021. %101 = call@printf(i8* ... @out.4543, i32 0, i32 0)) nounwind
  1022. It is basically doing:
  1023. memcpy(globalarray, "string");
  1024. printf(..., globalarray);
  1025. Anyway, by knowing that printf just reads the memory and forward substituting
  1026. the string directly into the printf, this eliminates reads from globalarray.
  1027. Since this pattern occurs frequently in crafty (due to the "DisplayTime" and
  1028. other similar functions) there are many stores to "out". Once all the printfs
  1029. stop using "out", all that is left is the memcpy's into it. This should allow
  1030. globalopt to remove the "stored only" global.
  1031. //===---------------------------------------------------------------------===//
  1032. This code:
  1033. define inreg i32 @foo(i8* inreg %p) nounwind {
  1034. %tmp0 = load i8* %p
  1035. %tmp1 = ashr i8 %tmp0, 5
  1036. %tmp2 = sext i8 %tmp1 to i32
  1037. ret i32 %tmp2
  1038. }
  1039. could be dagcombine'd to a sign-extending load with a shift.
  1040. For example, on x86 this currently gets this:
  1041. movb (%eax), %al
  1042. sarb $5, %al
  1043. movsbl %al, %eax
  1044. while it could get this:
  1045. movsbl (%eax), %eax
  1046. sarl $5, %eax
  1047. //===---------------------------------------------------------------------===//
  1048. GCC PR31029:
  1049. int test(int x) { return 1-x == x; } // --> return false
  1050. int test2(int x) { return 2-x == x; } // --> return x == 1 ?
  1051. Always foldable for odd constants, what is the rule for even?
  1052. //===---------------------------------------------------------------------===//
  1053. PR 3381: GEP to field of size 0 inside a struct could be turned into GEP
  1054. for next field in struct (which is at same address).
  1055. For example: store of float into { {{}}, float } could be turned into a store to
  1056. the float directly.
  1057. //===---------------------------------------------------------------------===//
  1058. The arg promotion pass should make use of nocapture to make its alias analysis
  1059. stuff much more precise.
  1060. //===---------------------------------------------------------------------===//
  1061. The following functions should be optimized to use a select instead of a
  1062. branch (from gcc PR40072):
  1063. char char_int(int m) {if(m>7) return 0; return m;}
  1064. int int_char(char m) {if(m>7) return 0; return m;}
  1065. //===---------------------------------------------------------------------===//
  1066. int func(int a, int b) { if (a & 0x80) b |= 0x80; else b &= ~0x80; return b; }
  1067. Generates this:
  1068. define i32 @func(i32 %a, i32 %b) nounwind readnone ssp {
  1069. entry:
  1070. %0 = and i32 %a, 128 ; <i32> [#uses=1]
  1071. %1 = icmp eq i32 %0, 0 ; <i1> [#uses=1]
  1072. %2 = or i32 %b, 128 ; <i32> [#uses=1]
  1073. %3 = and i32 %b, -129 ; <i32> [#uses=1]
  1074. %b_addr.0 = select i1 %1, i32 %3, i32 %2 ; <i32> [#uses=1]
  1075. ret i32 %b_addr.0
  1076. }
  1077. However, it's functionally equivalent to:
  1078. b = (b & ~0x80) | (a & 0x80);
  1079. Which generates this:
  1080. define i32 @func(i32 %a, i32 %b) nounwind readnone ssp {
  1081. entry:
  1082. %0 = and i32 %b, -129 ; <i32> [#uses=1]
  1083. %1 = and i32 %a, 128 ; <i32> [#uses=1]
  1084. %2 = or i32 %0, %1 ; <i32> [#uses=1]
  1085. ret i32 %2
  1086. }
  1087. This can be generalized for other forms:
  1088. b = (b & ~0x80) | (a & 0x40) << 1;
  1089. //===---------------------------------------------------------------------===//
  1090. These two functions produce different code. They shouldn't:
  1091. #include <stdint.h>
  1092. uint8_t p1(uint8_t b, uint8_t a) {
  1093. b = (b & ~0xc0) | (a & 0xc0);
  1094. return (b);
  1095. }
  1096. uint8_t p2(uint8_t b, uint8_t a) {
  1097. b = (b & ~0x40) | (a & 0x40);
  1098. b = (b & ~0x80) | (a & 0x80);
  1099. return (b);
  1100. }
  1101. define zeroext i8 @p1(i8 zeroext %b, i8 zeroext %a) nounwind readnone ssp {
  1102. entry:
  1103. %0 = and i8 %b, 63 ; <i8> [#uses=1]
  1104. %1 = and i8 %a, -64 ; <i8> [#uses=1]
  1105. %2 = or i8 %1, %0 ; <i8> [#uses=1]
  1106. ret i8 %2
  1107. }
  1108. define zeroext i8 @p2(i8 zeroext %b, i8 zeroext %a) nounwind readnone ssp {
  1109. entry:
  1110. %0 = and i8 %b, 63 ; <i8> [#uses=1]
  1111. %.masked = and i8 %a, 64 ; <i8> [#uses=1]
  1112. %1 = and i8 %a, -128 ; <i8> [#uses=1]
  1113. %2 = or i8 %1, %0 ; <i8> [#uses=1]
  1114. %3 = or i8 %2, %.masked ; <i8> [#uses=1]
  1115. ret i8 %3
  1116. }
  1117. //===---------------------------------------------------------------------===//
  1118. IPSCCP does not currently propagate argument dependent constants through
  1119. functions where it does not not all of the callers. This includes functions
  1120. with normal external linkage as well as templates, C99 inline functions etc.
  1121. Specifically, it does nothing to:
  1122. define i32 @test(i32 %x, i32 %y, i32 %z) nounwind {
  1123. entry:
  1124. %0 = add nsw i32 %y, %z
  1125. %1 = mul i32 %0, %x
  1126. %2 = mul i32 %y, %z
  1127. %3 = add nsw i32 %1, %2
  1128. ret i32 %3
  1129. }
  1130. define i32 @test2() nounwind {
  1131. entry:
  1132. %0 = call i32 @test(i32 1, i32 2, i32 4) nounwind
  1133. ret i32 %0
  1134. }
  1135. It would be interesting extend IPSCCP to be able to handle simple cases like
  1136. this, where all of the arguments to a call are constant. Because IPSCCP runs
  1137. before inlining, trivial templates and inline functions are not yet inlined.
  1138. The results for a function + set of constant arguments should be memoized in a
  1139. map.
  1140. //===---------------------------------------------------------------------===//
  1141. The libcall constant folding stuff should be moved out of SimplifyLibcalls into
  1142. libanalysis' constantfolding logic. This would allow IPSCCP to be able to
  1143. handle simple things like this:
  1144. static int foo(const char *X) { return strlen(X); }
  1145. int bar() { return foo("abcd"); }
  1146. //===---------------------------------------------------------------------===//
  1147. function-attrs doesn't know much about memcpy/memset. This function should be
  1148. marked readnone rather than readonly, since it only twiddles local memory, but
  1149. function-attrs doesn't handle memset/memcpy/memmove aggressively:
  1150. struct X { int *p; int *q; };
  1151. int foo() {
  1152. int i = 0, j = 1;
  1153. struct X x, y;
  1154. int **p;
  1155. y.p = &i;
  1156. x.q = &j;
  1157. p = __builtin_memcpy (&x, &y, sizeof (int *));
  1158. return **p;
  1159. }
  1160. This can be seen at:
  1161. $ clang t.c -S -o - -mkernel -O0 -emit-llvm | opt -function-attrs -S
  1162. //===---------------------------------------------------------------------===//
  1163. Missed instcombine transformation:
  1164. define i1 @a(i32 %x) nounwind readnone {
  1165. entry:
  1166. %cmp = icmp eq i32 %x, 30
  1167. %sub = add i32 %x, -30
  1168. %cmp2 = icmp ugt i32 %sub, 9
  1169. %or = or i1 %cmp, %cmp2
  1170. ret i1 %or
  1171. }
  1172. This should be optimized to a single compare. Testcase derived from gcc.
  1173. //===---------------------------------------------------------------------===//
  1174. Missed instcombine or reassociate transformation:
  1175. int a(int a, int b) { return (a==12)&(b>47)&(b<58); }
  1176. The sgt and slt should be combined into a single comparison. Testcase derived
  1177. from gcc.
  1178. //===---------------------------------------------------------------------===//
  1179. Missed instcombine transformation:
  1180. %382 = srem i32 %tmp14.i, 64 ; [#uses=1]
  1181. %383 = zext i32 %382 to i64 ; [#uses=1]
  1182. %384 = shl i64 %381, %383 ; [#uses=1]
  1183. %385 = icmp slt i32 %tmp14.i, 64 ; [#uses=1]
  1184. The srem can be transformed to an and because if %tmp14.i is negative, the
  1185. shift is undefined. Testcase derived from 403.gcc.
  1186. //===---------------------------------------------------------------------===//
  1187. This is a range comparison on a divided result (from 403.gcc):
  1188. %1337 = sdiv i32 %1336, 8 ; [#uses=1]
  1189. %.off.i208 = add i32 %1336, 7 ; [#uses=1]
  1190. %1338 = icmp ult i32 %.off.i208, 15 ; [#uses=1]
  1191. We already catch this (removing the sdiv) if there isn't an add, we should
  1192. handle the 'add' as well. This is a common idiom with it's builtin_alloca code.
  1193. C testcase:
  1194. int a(int x) { return (unsigned)(x/16+7) < 15; }
  1195. Another similar case involves truncations on 64-bit targets:
  1196. %361 = sdiv i64 %.046, 8 ; [#uses=1]
  1197. %362 = trunc i64 %361 to i32 ; [#uses=2]
  1198. ...
  1199. %367 = icmp eq i32 %362, 0 ; [#uses=1]
  1200. //===---------------------------------------------------------------------===//
  1201. Missed instcombine/dagcombine transformation:
  1202. define void @lshift_lt(i8 zeroext %a) nounwind {
  1203. entry:
  1204. %conv = zext i8 %a to i32
  1205. %shl = shl i32 %conv, 3
  1206. %cmp = icmp ult i32 %shl, 33
  1207. br i1 %cmp, label %if.then, label %if.end
  1208. if.then:
  1209. tail call void @bar() nounwind
  1210. ret void
  1211. if.end:
  1212. ret void
  1213. }
  1214. declare void @bar() nounwind
  1215. The shift should be eliminated. Testcase derived from gcc.
  1216. //===---------------------------------------------------------------------===//
  1217. These compile into different code, one gets recognized as a switch and the
  1218. other doesn't due to phase ordering issues (PR6212):
  1219. int test1(int mainType, int subType) {
  1220. if (mainType == 7)
  1221. subType = 4;
  1222. else if (mainType == 9)
  1223. subType = 6;
  1224. else if (mainType == 11)
  1225. subType = 9;
  1226. return subType;
  1227. }
  1228. int test2(int mainType, int subType) {
  1229. if (mainType == 7)
  1230. subType = 4;
  1231. if (mainType == 9)
  1232. subType = 6;
  1233. if (mainType == 11)
  1234. subType = 9;
  1235. return subType;
  1236. }
  1237. //===---------------------------------------------------------------------===//
  1238. The following test case (from PR6576):
  1239. define i32 @mul(i32 %a, i32 %b) nounwind readnone {
  1240. entry:
  1241. %cond1 = icmp eq i32 %b, 0 ; <i1> [#uses=1]
  1242. br i1 %cond1, label %exit, label %bb.nph
  1243. bb.nph: ; preds = %entry
  1244. %tmp = mul i32 %b, %a ; <i32> [#uses=1]
  1245. ret i32 %tmp
  1246. exit: ; preds = %entry
  1247. ret i32 0
  1248. }
  1249. could be reduced to:
  1250. define i32 @mul(i32 %a, i32 %b) nounwind readnone {
  1251. entry:
  1252. %tmp = mul i32 %b, %a
  1253. ret i32 %tmp
  1254. }
  1255. //===---------------------------------------------------------------------===//
  1256. We should use DSE + llvm.lifetime.end to delete dead vtable pointer updates.
  1257. See GCC PR34949
  1258. Another interesting case is that something related could be used for variables
  1259. that go const after their ctor has finished. In these cases, globalopt (which
  1260. can statically run the constructor) could mark the global const (so it gets put
  1261. in the readonly section). A testcase would be:
  1262. #include <complex>
  1263. using namespace std;
  1264. const complex<char> should_be_in_rodata (42,-42);
  1265. complex<char> should_be_in_data (42,-42);
  1266. complex<char> should_be_in_bss;
  1267. Where we currently evaluate the ctors but the globals don't become const because
  1268. the optimizer doesn't know they "become const" after the ctor is done. See
  1269. GCC PR4131 for more examples.
  1270. //===---------------------------------------------------------------------===//
  1271. In this code:
  1272. long foo(long x) {
  1273. return x > 1 ? x : 1;
  1274. }
  1275. LLVM emits a comparison with 1 instead of 0. 0 would be equivalent
  1276. and cheaper on most targets.
  1277. LLVM prefers comparisons with zero over non-zero in general, but in this
  1278. case it choses instead to keep the max operation obvious.
  1279. //===---------------------------------------------------------------------===//
  1280. define void @a(i32 %x) nounwind {
  1281. entry:
  1282. switch i32 %x, label %if.end [
  1283. i32 0, label %if.then
  1284. i32 1, label %if.then
  1285. i32 2, label %if.then
  1286. i32 3, label %if.then
  1287. i32 5, label %if.then
  1288. ]
  1289. if.then:
  1290. tail call void @foo() nounwind
  1291. ret void
  1292. if.end:
  1293. ret void
  1294. }
  1295. declare void @foo()
  1296. Generated code on x86-64 (other platforms give similar results):
  1297. a:
  1298. cmpl $5, %edi
  1299. ja LBB2_2
  1300. cmpl $4, %edi
  1301. jne LBB2_3
  1302. .LBB0_2:
  1303. ret
  1304. .LBB0_3:
  1305. jmp foo # TAILCALL
  1306. If we wanted to be really clever, we could simplify the whole thing to
  1307. something like the following, which eliminates a branch:
  1308. xorl $1, %edi
  1309. cmpl $4, %edi
  1310. ja .LBB0_2
  1311. ret
  1312. .LBB0_2:
  1313. jmp foo # TAILCALL
  1314. //===---------------------------------------------------------------------===//
  1315. We compile this:
  1316. int foo(int a) { return (a & (~15)) / 16; }
  1317. Into:
  1318. define i32 @foo(i32 %a) nounwind readnone ssp {
  1319. entry:
  1320. %and = and i32 %a, -16
  1321. %div = sdiv i32 %and, 16
  1322. ret i32 %div
  1323. }
  1324. but this code (X & -A)/A is X >> log2(A) when A is a power of 2, so this case
  1325. should be instcombined into just "a >> 4".
  1326. We do get this at the codegen level, so something knows about it, but
  1327. instcombine should catch it earlier:
  1328. _foo: ## @foo
  1329. ## %bb.0: ## %entry
  1330. movl %edi, %eax
  1331. sarl $4, %eax
  1332. ret
  1333. //===---------------------------------------------------------------------===//
  1334. This code (from GCC PR28685):
  1335. int test(int a, int b) {
  1336. int lt = a < b;
  1337. int eq = a == b;
  1338. if (lt)
  1339. return 1;
  1340. return eq;
  1341. }
  1342. Is compiled to:
  1343. define i32 @test(i32 %a, i32 %b) nounwind readnone ssp {
  1344. entry:
  1345. %cmp = icmp slt i32 %a, %b
  1346. br i1 %cmp, label %return, label %if.end
  1347. if.end: ; preds = %entry
  1348. %cmp5 = icmp eq i32 %a, %b
  1349. %conv6 = zext i1 %cmp5 to i32
  1350. ret i32 %conv6
  1351. return: ; preds = %entry
  1352. ret i32 1
  1353. }
  1354. it could be:
  1355. define i32 @test__(i32 %a, i32 %b) nounwind readnone ssp {
  1356. entry:
  1357. %0 = icmp sle i32 %a, %b
  1358. %retval = zext i1 %0 to i32
  1359. ret i32 %retval
  1360. }
  1361. //===---------------------------------------------------------------------===//
  1362. This code can be seen in viterbi:
  1363. %64 = call noalias i8* @malloc(i64 %62) nounwind
  1364. ...
  1365. %67 = call i64 @llvm.objectsize.i64(i8* %64, i1 false) nounwind
  1366. %68 = call i8* @__memset_chk(i8* %64, i32 0, i64 %62, i64 %67) nounwind
  1367. llvm.objectsize.i64 should be taught about malloc/calloc, allowing it to
  1368. fold to %62. This is a security win (overflows of malloc will get caught)
  1369. and also a performance win by exposing more memsets to the optimizer.
  1370. This occurs several times in viterbi.
  1371. Note that this would change the semantics of @llvm.objectsize which by its
  1372. current definition always folds to a constant. We also should make sure that
  1373. we remove checking in code like
  1374. char *p = malloc(strlen(s)+1);
  1375. __strcpy_chk(p, s, __builtin_object_size(p, 0));
  1376. //===---------------------------------------------------------------------===//
  1377. clang -O3 currently compiles this code
  1378. int g(unsigned int a) {
  1379. unsigned int c[100];
  1380. c[10] = a;
  1381. c[11] = a;
  1382. unsigned int b = c[10] + c[11];
  1383. if(b > a*2) a = 4;
  1384. else a = 8;
  1385. return a + 7;
  1386. }
  1387. into
  1388. define i32 @g(i32 a) nounwind readnone {
  1389. %add = shl i32 %a, 1
  1390. %mul = shl i32 %a, 1
  1391. %cmp = icmp ugt i32 %add, %mul
  1392. %a.addr.0 = select i1 %cmp, i32 11, i32 15
  1393. ret i32 %a.addr.0
  1394. }
  1395. The icmp should fold to false. This CSE opportunity is only available
  1396. after GVN and InstCombine have run.
  1397. //===---------------------------------------------------------------------===//
  1398. memcpyopt should turn this:
  1399. define i8* @test10(i32 %x) {
  1400. %alloc = call noalias i8* @malloc(i32 %x) nounwind
  1401. call void @llvm.memset.p0i8.i32(i8* %alloc, i8 0, i32 %x, i32 1, i1 false)
  1402. ret i8* %alloc
  1403. }
  1404. into a call to calloc. We should make sure that we analyze calloc as
  1405. aggressively as malloc though.
  1406. //===---------------------------------------------------------------------===//
  1407. clang -O3 doesn't optimize this:
  1408. void f1(int* begin, int* end) {
  1409. std::fill(begin, end, 0);
  1410. }
  1411. into a memset. This is PR8942.
  1412. //===---------------------------------------------------------------------===//
  1413. clang -O3 -fno-exceptions currently compiles this code:
  1414. void f(int N) {
  1415. std::vector<int> v(N);
  1416. extern void sink(void*); sink(&v);
  1417. }
  1418. into
  1419. define void @_Z1fi(i32 %N) nounwind {
  1420. entry:
  1421. %v2 = alloca [3 x i32*], align 8
  1422. %v2.sub = getelementptr inbounds [3 x i32*]* %v2, i64 0, i64 0
  1423. %tmpcast = bitcast [3 x i32*]* %v2 to %"class.std::vector"*
  1424. %conv = sext i32 %N to i64
  1425. store i32* null, i32** %v2.sub, align 8, !tbaa !0
  1426. %tmp3.i.i.i.i.i = getelementptr inbounds [3 x i32*]* %v2, i64 0, i64 1
  1427. store i32* null, i32** %tmp3.i.i.i.i.i, align 8, !tbaa !0
  1428. %tmp4.i.i.i.i.i = getelementptr inbounds [3 x i32*]* %v2, i64 0, i64 2
  1429. store i32* null, i32** %tmp4.i.i.i.i.i, align 8, !tbaa !0
  1430. %cmp.i.i.i.i = icmp eq i32 %N, 0
  1431. br i1 %cmp.i.i.i.i, label %_ZNSt12_Vector_baseIiSaIiEEC2EmRKS0_.exit.thread.i.i, label %cond.true.i.i.i.i
  1432. _ZNSt12_Vector_baseIiSaIiEEC2EmRKS0_.exit.thread.i.i: ; preds = %entry
  1433. store i32* null, i32** %v2.sub, align 8, !tbaa !0
  1434. store i32* null, i32** %tmp3.i.i.i.i.i, align 8, !tbaa !0
  1435. %add.ptr.i5.i.i = getelementptr inbounds i32* null, i64 %conv
  1436. store i32* %add.ptr.i5.i.i, i32** %tmp4.i.i.i.i.i, align 8, !tbaa !0
  1437. br label %_ZNSt6vectorIiSaIiEEC1EmRKiRKS0_.exit
  1438. cond.true.i.i.i.i: ; preds = %entry
  1439. %cmp.i.i.i.i.i = icmp slt i32 %N, 0
  1440. br i1 %cmp.i.i.i.i.i, label %if.then.i.i.i.i.i, label %_ZNSt12_Vector_baseIiSaIiEEC2EmRKS0_.exit.i.i
  1441. if.then.i.i.i.i.i: ; preds = %cond.true.i.i.i.i
  1442. call void @_ZSt17__throw_bad_allocv() noreturn nounwind
  1443. unreachable
  1444. _ZNSt12_Vector_baseIiSaIiEEC2EmRKS0_.exit.i.i: ; preds = %cond.true.i.i.i.i
  1445. %mul.i.i.i.i.i = shl i64 %conv, 2
  1446. %call3.i.i.i.i.i = call noalias i8* @_Znwm(i64 %mul.i.i.i.i.i) nounwind
  1447. %0 = bitcast i8* %call3.i.i.i.i.i to i32*
  1448. store i32* %0, i32** %v2.sub, align 8, !tbaa !0
  1449. store i32* %0, i32** %tmp3.i.i.i.i.i, align 8, !tbaa !0
  1450. %add.ptr.i.i.i = getelementptr inbounds i32* %0, i64 %conv
  1451. store i32* %add.ptr.i.i.i, i32** %tmp4.i.i.i.i.i, align 8, !tbaa !0
  1452. call void @llvm.memset.p0i8.i64(i8* %call3.i.i.i.i.i, i8 0, i64 %mul.i.i.i.i.i, i32 4, i1 false)
  1453. br label %_ZNSt6vectorIiSaIiEEC1EmRKiRKS0_.exit
  1454. This is just the handling the construction of the vector. Most surprising here
  1455. is the fact that all three null stores in %entry are dead (because we do no
  1456. cross-block DSE).
  1457. Also surprising is that %conv isn't simplified to 0 in %....exit.thread.i.i.
  1458. This is a because the client of LazyValueInfo doesn't simplify all instruction
  1459. operands, just selected ones.
  1460. //===---------------------------------------------------------------------===//
  1461. clang -O3 -fno-exceptions currently compiles this code:
  1462. void f(char* a, int n) {
  1463. __builtin_memset(a, 0, n);
  1464. for (int i = 0; i < n; ++i)
  1465. a[i] = 0;
  1466. }
  1467. into:
  1468. define void @_Z1fPci(i8* nocapture %a, i32 %n) nounwind {
  1469. entry:
  1470. %conv = sext i32 %n to i64
  1471. tail call void @llvm.memset.p0i8.i64(i8* %a, i8 0, i64 %conv, i32 1, i1 false)
  1472. %cmp8 = icmp sgt i32 %n, 0
  1473. br i1 %cmp8, label %for.body.lr.ph, label %for.end
  1474. for.body.lr.ph: ; preds = %entry
  1475. %tmp10 = add i32 %n, -1
  1476. %tmp11 = zext i32 %tmp10 to i64
  1477. %tmp12 = add i64 %tmp11, 1
  1478. call void @llvm.memset.p0i8.i64(i8* %a, i8 0, i64 %tmp12, i32 1, i1 false)
  1479. ret void
  1480. for.end: ; preds = %entry
  1481. ret void
  1482. }
  1483. This shouldn't need the ((zext (%n - 1)) + 1) game, and it should ideally fold
  1484. the two memset's together.
  1485. The issue with the addition only occurs in 64-bit mode, and appears to be at
  1486. least partially caused by Scalar Evolution not keeping its cache updated: it
  1487. returns the "wrong" result immediately after indvars runs, but figures out the
  1488. expected result if it is run from scratch on IR resulting from running indvars.
  1489. //===---------------------------------------------------------------------===//
  1490. clang -O3 -fno-exceptions currently compiles this code:
  1491. struct S {
  1492. unsigned short m1, m2;
  1493. unsigned char m3, m4;
  1494. };
  1495. void f(int N) {
  1496. std::vector<S> v(N);
  1497. extern void sink(void*); sink(&v);
  1498. }
  1499. into poor code for zero-initializing 'v' when N is >0. The problem is that
  1500. S is only 6 bytes, but each element is 8 byte-aligned. We generate a loop and
  1501. 4 stores on each iteration. If the struct were 8 bytes, this gets turned into
  1502. a memset.
  1503. In order to handle this we have to:
  1504. A) Teach clang to generate metadata for memsets of structs that have holes in
  1505. them.
  1506. B) Teach clang to use such a memset for zero init of this struct (since it has
  1507. a hole), instead of doing elementwise zeroing.
  1508. //===---------------------------------------------------------------------===//
  1509. clang -O3 currently compiles this code:
  1510. extern const int magic;
  1511. double f() { return 0.0 * magic; }
  1512. into
  1513. @magic = external constant i32
  1514. define double @_Z1fv() nounwind readnone {
  1515. entry:
  1516. %tmp = load i32* @magic, align 4, !tbaa !0
  1517. %conv = sitofp i32 %tmp to double
  1518. %mul = fmul double %conv, 0.000000e+00
  1519. ret double %mul
  1520. }
  1521. We should be able to fold away this fmul to 0.0. More generally, fmul(x,0.0)
  1522. can be folded to 0.0 if we can prove that the LHS is not -0.0, not a NaN, and
  1523. not an INF. The CannotBeNegativeZero predicate in value tracking should be
  1524. extended to support general "fpclassify" operations that can return
  1525. yes/no/unknown for each of these predicates.
  1526. In this predicate, we know that uitofp is trivially never NaN or -0.0, and
  1527. we know that it isn't +/-Inf if the floating point type has enough exponent bits
  1528. to represent the largest integer value as < inf.
  1529. //===---------------------------------------------------------------------===//
  1530. When optimizing a transformation that can change the sign of 0.0 (such as the
  1531. 0.0*val -> 0.0 transformation above), it might be provable that the sign of the
  1532. expression doesn't matter. For example, by the above rules, we can't transform
  1533. fmul(sitofp(x), 0.0) into 0.0, because x might be -1 and the result of the
  1534. expression is defined to be -0.0.
  1535. If we look at the uses of the fmul for example, we might be able to prove that
  1536. all uses don't care about the sign of zero. For example, if we have:
  1537. fadd(fmul(sitofp(x), 0.0), 2.0)
  1538. Since we know that x+2.0 doesn't care about the sign of any zeros in X, we can
  1539. transform the fmul to 0.0, and then the fadd to 2.0.
  1540. //===---------------------------------------------------------------------===//
  1541. We should enhance memcpy/memcpy/memset to allow a metadata node on them
  1542. indicating that some bytes of the transfer are undefined. This is useful for
  1543. frontends like clang when lowering struct copies, when some elements of the
  1544. struct are undefined. Consider something like this:
  1545. struct x {
  1546. char a;
  1547. int b[4];
  1548. };
  1549. void foo(struct x*P);
  1550. struct x testfunc() {
  1551. struct x V1, V2;
  1552. foo(&V1);
  1553. V2 = V1;
  1554. return V2;
  1555. }
  1556. We currently compile this to:
  1557. $ clang t.c -S -o - -O0 -emit-llvm | opt -sroa -S
  1558. %struct.x = type { i8, [4 x i32] }
  1559. define void @testfunc(%struct.x* sret %agg.result) nounwind ssp {
  1560. entry:
  1561. %V1 = alloca %struct.x, align 4
  1562. call void @foo(%struct.x* %V1)
  1563. %tmp1 = bitcast %struct.x* %V1 to i8*
  1564. %0 = bitcast %struct.x* %V1 to i160*
  1565. %srcval1 = load i160* %0, align 4
  1566. %tmp2 = bitcast %struct.x* %agg.result to i8*
  1567. %1 = bitcast %struct.x* %agg.result to i160*
  1568. store i160 %srcval1, i160* %1, align 4
  1569. ret void
  1570. }
  1571. This happens because SRoA sees that the temp alloca has is being memcpy'd into
  1572. and out of and it has holes and it has to be conservative. If we knew about the
  1573. holes, then this could be much much better.
  1574. Having information about these holes would also improve memcpy (etc) lowering at
  1575. llc time when it gets inlined, because we can use smaller transfers. This also
  1576. avoids partial register stalls in some important cases.
  1577. //===---------------------------------------------------------------------===//
  1578. We don't fold (icmp (add) (add)) unless the two adds only have a single use.
  1579. There are a lot of cases that we're refusing to fold in (e.g.) 256.bzip2, for
  1580. example:
  1581. %indvar.next90 = add i64 %indvar89, 1 ;; Has 2 uses
  1582. %tmp96 = add i64 %tmp95, 1 ;; Has 1 use
  1583. %exitcond97 = icmp eq i64 %indvar.next90, %tmp96
  1584. We don't fold this because we don't want to introduce an overlapped live range
  1585. of the ivar. However if we can make this more aggressive without causing
  1586. performance issues in two ways:
  1587. 1. If *either* the LHS or RHS has a single use, we can definitely do the
  1588. transformation. In the overlapping liverange case we're trading one register
  1589. use for one fewer operation, which is a reasonable trade. Before doing this
  1590. we should verify that the llc output actually shrinks for some benchmarks.
  1591. 2. If both ops have multiple uses, we can still fold it if the operations are
  1592. both sinkable to *after* the icmp (e.g. in a subsequent block) which doesn't
  1593. increase register pressure.
  1594. There are a ton of icmp's we aren't simplifying because of the reg pressure
  1595. concern. Care is warranted here though because many of these are induction
  1596. variables and other cases that matter a lot to performance, like the above.
  1597. Here's a blob of code that you can drop into the bottom of visitICmp to see some
  1598. missed cases:
  1599. { Value *A, *B, *C, *D;
  1600. if (match(Op0, m_Add(m_Value(A), m_Value(B))) &&
  1601. match(Op1, m_Add(m_Value(C), m_Value(D))) &&
  1602. (A == C || A == D || B == C || B == D)) {
  1603. errs() << "OP0 = " << *Op0 << " U=" << Op0->getNumUses() << "\n";
  1604. errs() << "OP1 = " << *Op1 << " U=" << Op1->getNumUses() << "\n";
  1605. errs() << "CMP = " << I << "\n\n";
  1606. }
  1607. }
  1608. //===---------------------------------------------------------------------===//
  1609. define i1 @test1(i32 %x) nounwind {
  1610. %and = and i32 %x, 3
  1611. %cmp = icmp ult i32 %and, 2
  1612. ret i1 %cmp
  1613. }
  1614. Can be folded to (x & 2) == 0.
  1615. define i1 @test2(i32 %x) nounwind {
  1616. %and = and i32 %x, 3
  1617. %cmp = icmp ugt i32 %and, 1
  1618. ret i1 %cmp
  1619. }
  1620. Can be folded to (x & 2) != 0.
  1621. SimplifyDemandedBits shrinks the "and" constant to 2 but instcombine misses the
  1622. icmp transform.
  1623. //===---------------------------------------------------------------------===//
  1624. This code:
  1625. typedef struct {
  1626. int f1:1;
  1627. int f2:1;
  1628. int f3:1;
  1629. int f4:29;
  1630. } t1;
  1631. typedef struct {
  1632. int f1:1;
  1633. int f2:1;
  1634. int f3:30;
  1635. } t2;
  1636. t1 s1;
  1637. t2 s2;
  1638. void func1(void)
  1639. {
  1640. s1.f1 = s2.f1;
  1641. s1.f2 = s2.f2;
  1642. }
  1643. Compiles into this IR (on x86-64 at least):
  1644. %struct.t1 = type { i8, [3 x i8] }
  1645. @s2 = global %struct.t1 zeroinitializer, align 4
  1646. @s1 = global %struct.t1 zeroinitializer, align 4
  1647. define void @func1() nounwind ssp noredzone {
  1648. entry:
  1649. %0 = load i32* bitcast (%struct.t1* @s2 to i32*), align 4
  1650. %bf.val.sext5 = and i32 %0, 1
  1651. %1 = load i32* bitcast (%struct.t1* @s1 to i32*), align 4
  1652. %2 = and i32 %1, -4
  1653. %3 = or i32 %2, %bf.val.sext5
  1654. %bf.val.sext26 = and i32 %0, 2
  1655. %4 = or i32 %3, %bf.val.sext26
  1656. store i32 %4, i32* bitcast (%struct.t1* @s1 to i32*), align 4
  1657. ret void
  1658. }
  1659. The two or/and's should be merged into one each.
  1660. //===---------------------------------------------------------------------===//
  1661. Machine level code hoisting can be useful in some cases. For example, PR9408
  1662. is about:
  1663. typedef union {
  1664. void (*f1)(int);
  1665. void (*f2)(long);
  1666. } funcs;
  1667. void foo(funcs f, int which) {
  1668. int a = 5;
  1669. if (which) {
  1670. f.f1(a);
  1671. } else {
  1672. f.f2(a);
  1673. }
  1674. }
  1675. which we compile to:
  1676. foo: # @foo
  1677. # %bb.0: # %entry
  1678. pushq %rbp
  1679. movq %rsp, %rbp
  1680. testl %esi, %esi
  1681. movq %rdi, %rax
  1682. je .LBB0_2
  1683. # %bb.1: # %if.then
  1684. movl $5, %edi
  1685. callq *%rax
  1686. popq %rbp
  1687. ret
  1688. .LBB0_2: # %if.else
  1689. movl $5, %edi
  1690. callq *%rax
  1691. popq %rbp
  1692. ret
  1693. Note that bb1 and bb2 are the same. This doesn't happen at the IR level
  1694. because one call is passing an i32 and the other is passing an i64.
  1695. //===---------------------------------------------------------------------===//
  1696. I see this sort of pattern in 176.gcc in a few places (e.g. the start of
  1697. store_bit_field). The rem should be replaced with a multiply and subtract:
  1698. %3 = sdiv i32 %A, %B
  1699. %4 = srem i32 %A, %B
  1700. Similarly for udiv/urem. Note that this shouldn't be done on X86 or ARM,
  1701. which can do this in a single operation (instruction or libcall). It is
  1702. probably best to do this in the code generator.
  1703. //===---------------------------------------------------------------------===//
  1704. unsigned foo(unsigned x, unsigned y) { return (x & y) == 0 || x == 0; }
  1705. should fold to (x & y) == 0.
  1706. //===---------------------------------------------------------------------===//
  1707. unsigned foo(unsigned x, unsigned y) { return x > y && x != 0; }
  1708. should fold to x > y.
  1709. //===---------------------------------------------------------------------===//