common_subexpression_elimination.sol 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. // RUN: --target substrate --emit cfg
  2. // Tests control commands
  3. contract c1 {
  4. //BEGIN-CHECK: c1::function::test1
  5. function test1(int a, int b) public pure returns (int) {
  6. int x;
  7. // CHECK: ty:int256 %1.cse_temp = ((arg #0) + (arg #1))
  8. // CHECK: ty:int256 %x = (%1.cse_temp - int256 54)
  9. x = a+b-54;
  10. // CHECK: ty:int256 %d = (%x * %1.cse_temp)
  11. int d = x*(a+b);
  12. // CHECK: ty:int256 %2.cse_temp = ((arg #0) - (arg #1))
  13. if (x + d > 0) {
  14. // NOT-CHECK: ty:int256 %t = ((arg #0) - (arg #1))
  15. // CHECK: ty:int256 %t = %2.cse_temp
  16. int t = a-b;
  17. bool e1 = t>3;
  18. }
  19. else {
  20. // NOT-CHECK: ty:int256 %e = ((arg #0) - (arg #1))
  21. // CHECK: ty:int256 %e = %2.cse_temp
  22. int e = a-b;
  23. bool e2 = e > 3;
  24. }
  25. return x-d + (a-b);
  26. // CHECK: return ((%x - %d) + %2.cse_temp)
  27. }
  28. // BEGIN-CHECK: c1::function::test2
  29. function test2(int a, int b) public pure returns (int) {
  30. int x;
  31. // CHECK: ty:int256 %1.cse_temp = ((arg #0) + (arg #1))
  32. x = a+b-54;
  33. // CHECK: ty:int256 %x = (%1.cse_temp - int256 54)
  34. int d = x*(a+b);
  35. // CHECK: ty:int256 %d = (%x * %1.cse_temp)
  36. // CHECK: ty:int256 %2.cse_temp = (%x + %d)
  37. // CHECK: ty:int256 %3.cse_temp = ((arg #0) - (arg #1))
  38. // CEHCK: branchcond (%2.cse_temp > int256 0), block1, block2
  39. if (x + d > 0) {
  40. int t = a-b;
  41. // CHECK: ty:int256 %t = %3.cse_temp
  42. bool e1 = t>3;
  43. // CHECK: return ((%x - %d) + %3.cse_temp)
  44. }
  45. else if (x+d < 0) {
  46. int e = a-b;
  47. // CHECK: ty:int256 %e = %3.cse_temp
  48. bool e2 = e > 3;
  49. } else {
  50. int k = a-b;
  51. // CHECK: ty:int256 %k = %3.cse_temp
  52. bool e3 = k < 4;
  53. }
  54. return x-d + (a-b);
  55. }
  56. // BEGIN-CHECK: c1::function::test3
  57. function test3(int a, int b) public pure returns (int) {
  58. int x;
  59. x = a+b-54;
  60. int d = x*(a+b);
  61. // CHECK: branchcond (%2.cse_temp > int256 0), block1, block2
  62. if (x + d > 0) {
  63. // CHECK: ty:int256 %t = ((arg #0) - (arg #1))
  64. int t = a-b;
  65. bool e1 = t>3;
  66. // CHECK: branchcond (%2.cse_temp < int256 0), block4, block5
  67. // CHECK: return ((%x - %d) + ((arg #0) - (arg #1)))
  68. }
  69. else if (x+d < 0) {
  70. // CHECK: ty:int256 %e = ((arg #0) - (arg #1))
  71. int e = a-b;
  72. bool e2 = e > 3;
  73. // CHECK: branchcond (%2.cse_temp == int256 0), block7, block8
  74. } else if (x + d == 0){
  75. // CHECK: ty:int256 %k = %1.cse_temp
  76. int k = a+b;
  77. bool e3 = k < 4;
  78. }
  79. return x-d + (a-b);
  80. }
  81. // BEGIN-CHECK: c1::function::test4
  82. function test4(int a, int b) public pure returns (int) {
  83. int x;
  84. x = a+b-54;
  85. int d = x*(a+b);
  86. int p = x+d;
  87. // CHECK: ty:int256 %2.cse_temp = (%x + %d)
  88. // CHECK:branchcond (%2.cse_temp > int256 0), block2, block3
  89. while (x+d > 0) {
  90. // CHECK: ty:int256 %t = ((arg #0) - (arg #1))
  91. int t = a-b;
  92. bool e1 = t > 3;
  93. // CHECK: ty:int256 %x = %2.cse_temp
  94. x = x+d;
  95. }
  96. // CHECK: return (((%x - %d) + ((arg #0) - (arg #1))) - %p)
  97. return x-d + (a-b) - p;
  98. }
  99. // BEGIN-CHECK: c1::function::test5
  100. function test5(int a, int b) public pure returns (int) {
  101. int x;
  102. x = a+b-54;
  103. int d = x*(a+b);
  104. for(int i=0; i<10; i++) {
  105. // CHECK: ty:int256 %t = ((arg #0) - (arg #1))
  106. int t = a-b;
  107. // CHECK: ty:int256 %i = (%temp.186 + int256 1)
  108. bool e1 = t > 3;
  109. }
  110. // CHECK: return ((%x - %d) + ((arg #0) - (arg #1)))
  111. return x-d + (a-b);
  112. }
  113. // BEGIN-CHECK: c1::function::test6
  114. function test6(int a, int b) public pure returns (int) {
  115. int x;
  116. x = a+b-54;
  117. int d = x*(a+b);
  118. do {
  119. int t = a-b;
  120. bool e1 = t > 3;
  121. // CHECK: ty:int256 %x = (%x + %d)
  122. x = x+d;
  123. // CHECK: branchcond ((%x + %d) > int256 0), block1, block3
  124. } while(x+d > 0);
  125. int t = 3;
  126. bool p = t < 2;
  127. // CHECK: return ((%x - %d) + %t
  128. return x-d + (a-b);
  129. }
  130. // BEGIN-CHECK: c1::function::test7
  131. function test7(int a, int b) public pure returns (int) {
  132. int x;
  133. // CHECK: ty:int256 %1.cse_temp = ((arg #0) + (arg #1))
  134. x = a+b-54;
  135. // CHECK: ty:int256 %x = (%1.cse_temp - int256 54)
  136. int d = x*(a+b);
  137. // CHECK: ty:int256 %d = (%x * %1.cse_temp)
  138. // CHECK: ty:int256 %2.cse_temp = (%x + %d)
  139. // CHECK: ty:int256 %3.cse_temp = ((arg #0) - (arg #1))
  140. // CEHCK: branchcond (%2.cse_temp > int256 0), block1, block2
  141. if (x + d > 0) {
  142. int t = a-b;
  143. // CHECK: ty:int256 %t = %3.cse_temp
  144. bool e1 = t>3;
  145. // CHECK: return ((%x - %d) + %3.cse_temp)
  146. }
  147. else if (x+d < 0) {
  148. int e = a-b;
  149. // CHECK: ty:int256 %e = %3.cse_temp
  150. bool e2 = e > 3;
  151. } else if (x+d == 0){
  152. int k = a-b;
  153. // CHECK: ty:int256 %k = %3.cse_temp
  154. bool e3 = k < 4;
  155. } else {
  156. int k1 = a-b;
  157. // CHECK: ty:int256 %k1 = %3.cse_temp
  158. bool e4 = k1 < 4;
  159. }
  160. return x-d + (a-b);
  161. }
  162. int k=2;
  163. // BEGIN-CHECK: c1::function::test8
  164. function test8(int a, int b) public view returns (int ret) {
  165. int x = a + b +k;
  166. // CHECK: ty:int256 %x = (%1.cse_temp + %temp.
  167. if(x + k < 0) {
  168. // CHECK: ty:uint256 %p = uint256((%1.cse_temp + %temp.
  169. uint p = uint(a+b+k);
  170. bool e = p > 50;
  171. }
  172. // CHECK: ty:uint256 %p2 = uint256((%1.cse_temp + %temp.
  173. uint p2 = uint(a+b+k);
  174. // CHECK: ty:int256 %2.cse_temp = int256((%p2 + uint256 9))
  175. int r1 = int(p2+9) -4;
  176. // CHECK: ty:int256 %r1 = (%2.cse_temp - int256 4)
  177. int r2= int(p2+9) -9;
  178. // CHECK: ty:int256 %r2 = (%2.cse_temp - int256 9)
  179. ret = -r1;
  180. // CHECK: ty:int256 %ret = (%ret + %r2)
  181. ret = -r1 + r2;
  182. }
  183. struct stTest {
  184. int a;
  185. uint b;
  186. }
  187. // BEGIN-CHECK: c1::function::test9
  188. function test9(int a, int b) public view returns (int ret) {
  189. stTest instance = stTest(2, 3);
  190. // CHECK: ty:int256 %1.cse_temp = ((arg #0) + (arg #1))
  191. int x = a + b + instance.a;
  192. // CHECK: ty:int256 %x = (%1.cse_temp + (load (struct %instance field 0)))
  193. // CHECK: branchcond ((%x + int256((load (struct %instance field 1)))) < int256 0)
  194. if(x + int(instance.b) < 0) {
  195. // CHECK: ty:uint256 %p = uint256((%1.cse_temp + (load (struct %instance field 0))))
  196. uint p = uint(a+b+instance.a);
  197. bool e = p > 50;
  198. }
  199. int8 trunc = int8(x);
  200. // CHECK: ty:bool %e2 = ((sext int16 %trunc) > int16 2)
  201. bool e2 = trunc > 2;
  202. int8 trunc2 = 8 + int8(x);
  203. bool e3 = trunc2 < trunc;
  204. bool e4 = e2 || e3;
  205. // CHECK: branchcond %e3, block3, block4
  206. if (trunc2 < trunc && trunc > 2) {
  207. // CHECK: = %e2
  208. // CHECK: ty:int256 %p2 = %1.cse_temp
  209. int p2 = a+b;
  210. int p3 = p2 - x + a + b;
  211. int p4 = p2-x;
  212. // CHECK: ty:int256 %2.cse_temp = ((arg #0) * (arg #1))
  213. int p5 = p3 + a*b+45;
  214. // CHECK: ty:int256 %p5 = ((%p3 + %2.cse_temp) + int256 45)
  215. // CHECK: return %2.cse_temp
  216. if (p5 !=0) {
  217. // CHECK: ty:uint16 %t1 = (trunc uint16 %p5)
  218. uint16 t1 = uint16(p3 + a*b +45);
  219. // CHECK: ty:uint32 %t2 = (trunc uint32 %2.cse_temp)
  220. uint32 t2 = uint32(a*b);
  221. bool e5 = t2 < t1;
  222. }
  223. // CHECK: ty:int256 %ret = %p5
  224. ret = p3 + a*b + 45;
  225. }
  226. ret = a*b;
  227. }
  228. // BEGIN-CHECK: c1::function::test10
  229. function test10(int a, int b) public pure returns (int) {
  230. int x;
  231. x = a+b-54;
  232. int d = x*(a+b);
  233. int k = x+d;
  234. bool e = k < 0;
  235. do {
  236. int t = a-b;
  237. bool e1 = t > 3;
  238. // CHECK: ty:int256 %x = (%x + %d)
  239. x = x+d;
  240. // CHECK: branchcond ((%x + %d) > int256 0), block1, block3
  241. } while(x+d > 0);
  242. int t = 3;
  243. bool p = t < 2;
  244. // CHECK: return ((%x - %d) + %t
  245. return x-d + (a-b);
  246. }
  247. function get(int a, int b) private pure returns (int) {
  248. return a+b+1;
  249. }
  250. event testEvent(int a, int b, string str);
  251. // BEGIN-CHECK: c1::function::test11
  252. function test11(int a, int b) public returns (int) {
  253. string ast = "Hello!";
  254. string bst = "from Solang";
  255. string cst = ast + bst;
  256. // CHECK: ty:int256 %1.cse_temp = ((arg #0) / (int256 2 * (arg #1)))
  257. // CHECK: call c1::c1::function::get__int256_int256 %1.cse_temp, (arg #1)
  258. int p = a + get(a/(2*b), b);
  259. bool e = (ast == bst) || p < 2;
  260. // CHECK: ty:bool %2.cse_temp = (strcmp (%ast) (%bst))
  261. // CHECK: branchcond %2.cse_temp, block2, block1
  262. bool e2 = e;
  263. // CHECK: branchcond (strcmp (%cst) (%cst)), block3, block4
  264. if (ast + bst == cst) {
  265. // CHECK: call c1::c1::function::get__int256_int256 %1.cse_temp, (arg #1)
  266. require(a + get(a/(2*b), b) < 0);
  267. emit testEvent(a + get(a/(2*b) -p, b), p, ast+bst);
  268. }
  269. // CHECK: branchcond %2.cse_temp, block7, block8
  270. if (ast == bst) {
  271. ast = ast + "b";
  272. }
  273. // CHECK: call c1::c1::function::get__int256_int256 (%1.cse_temp - %p), (arg #1)
  274. // CHECK: branchcond (strcmp (%ast) (%bst)), block10, block11
  275. while (ast == bst) {
  276. ast = ast + "a";
  277. }
  278. // CHECK: call c1::c1::function::get__int256_int256 (arg #1), ((arg #0) / (arg #1))
  279. return get(b, a/b);
  280. }
  281. // BEGIN-CHECK: c1::function::test12
  282. function test12(int a, int b) public returns (int) {
  283. int x = a+b;
  284. bool e = (x == a);
  285. // NOT-CHECK: %1.cse_temp =
  286. bool e2 = e;
  287. // CHECK: branchcond (%x == (arg #0))
  288. while(x == a) {
  289. x = x+1;
  290. // NOT-CHECK: cse_temp
  291. x+=1;
  292. x++;
  293. x--;
  294. x++;
  295. x--;
  296. --x;
  297. ++x;
  298. }
  299. return x;
  300. }
  301. function testing(bytes b) public returns (string) {
  302. return string(b);
  303. }
  304. // BEGIN-CHECK: c1::function::test13
  305. function test13(int a, int b) public returns (int) {
  306. string c = "Hello";
  307. bytes b1 = bytes(c);
  308. string b2 = string(b1);
  309. string b3 = b2;
  310. int[4] vec = [1, 2, 3, 4];
  311. // CHECK: ty:int256 %1.cse_temp = ((arg #0) + (arg #1))
  312. int x = (a+b) - (vec[1]-vec[2]);
  313. // CHECK: ty:int256 %x = (%1.cse_temp - ((load (subscript int256[4] %vec[uint32 1])) - (load (subscript int256[4] %vec[uint32 2]))))
  314. bool k3 = x < 1;
  315. // CHECK: = uint256(%1.cse_temp)
  316. vec[uint(a+b)] = 54*(a+b);
  317. // CHECK: = (int256 54 * %1.cse_temp)
  318. // CHECK: = uint256((int256 1 - %1.cse_temp))
  319. vec[uint(1-(a+b))] = vec.length - (a+b);
  320. // CHECK: = (int256 4 - %1.cse_temp)
  321. if(vec.length - (a+b) == 1) {
  322. // CHECK: call c1::c1::function::testing__bytes %c
  323. string k = testing(bytes(c));
  324. string p = "a" +k;
  325. // CHECK: ty:string %p = (concat ((alloc string uint32 1 "a")) (%k))
  326. // CHECK: branchcond ((builtin ArrayLength (%p)) == uint32 2), block11, block12
  327. if(p.length == 2) {
  328. // CHECK: ty:string %p1 = (concat ((alloc string uint32 1 "a")) (%k))
  329. string p1 = "a" + k;
  330. string l = p1;
  331. }
  332. }
  333. // CHECK: branchcond ((%a + (arg #1)) < int256 0), block14, block15
  334. while(a+b < 0) {
  335. // CHECK: branchcond (strcmp (%c) ("a")), block16, block17
  336. if("a" == c) {
  337. a = a+b;
  338. }
  339. }
  340. do {
  341. // CHECK: branchcond (strcmp (%c) ("a")), block21, block22
  342. if("a" == c) {
  343. a = a+b;
  344. }
  345. // CHECK: branchcond ((%a + (arg #1)) > int256 0), block18, block20
  346. } while(a+b > 0);
  347. for(int p=0; p<a; ++p) {
  348. b1.push();
  349. // CHECK: = call c1::c1::function::testing__bytes %b1
  350. string k1 = testing(bytes(string(b1)));
  351. string k2 = k1;
  352. }
  353. return 2;
  354. }
  355. function doNothing(bytes32 b) private {
  356. b = hex"abcd";
  357. }
  358. // BEGIN-CHECK: c1::function::test14
  359. function test14(int a, uint b) public returns (int) {
  360. string c = "Hello";
  361. bytes b3 = bytes(c);
  362. bytes32 b1 = bytes32(b3);
  363. for(int p=0; p<a; ++p) {
  364. doNothing(b1);
  365. // CHECK: ty:bytes32 %b2 = %b1
  366. bytes32 b2 = bytes32(b3);
  367. doNothing(b2);
  368. }
  369. b3 = bytes("d");
  370. for(int p=0; p<a; ++p) {
  371. doNothing(b1);
  372. // CHECK: ty:bytes32 %b2.155 = bytes from:bytes32 (%b3)
  373. bytes32 b2 = bytes32(b3);
  374. doNothing(b2);
  375. }
  376. return 2;
  377. }
  378. // BEGIN-CHECK: c1::function::test15
  379. function test15(uint a, uint b) public pure returns (uint) {
  380. uint c = a << b;
  381. bool b1 = c > 0;
  382. // CHECK: ty:bool %1.cse_temp = !%b1
  383. // CHECK: branchcond %1.cse_temp, block1, block2
  384. if (!b1) {
  385. // CHECK: return (%c + uint256 1)
  386. return (a << b) + 1;
  387. }
  388. // CHECK: branchcond %1.cse_temp, block4, block3
  389. if(!b1 || c > 0) {
  390. // CHECK: = %b1
  391. // CHECK: return ((arg #0) << ((arg #1) + uint256 1))
  392. return a << b + 1;
  393. }
  394. // CHECK: branchcond (%c > uint256 0), block11, block12
  395. for(int i=0; c > 0 && i<10; ++i) {
  396. c++;
  397. }
  398. // CHECK: ty:uint256 %2.cse_temp = ((arg #0) & (arg #1))
  399. // CHECK: branchcond (%2.cse_temp == uint256 0), block13, block14
  400. if (a & b == 0) {
  401. return c--;
  402. }
  403. // CHECK: branchcond (%2.cse_temp > uint256 1), block15, block16
  404. if (a & b > 1) {
  405. return a;
  406. }
  407. return c;
  408. }
  409. // BEGIN-CHECK: c1::function::test16
  410. function test16(int a, int b) public pure returns (int) {
  411. int k = (a-b);
  412. bool e = k>0;
  413. for(int i=1; a-b < 0; i++) {
  414. // CHECK: ty:int256 %p = ((%1.cse_temp * int256 5) - (%k / (arg #0)))
  415. int p = (a-b)*5-k/a;
  416. b++;
  417. // CHECK: ty:int256 %1.cse_temp = ((arg #0) - %b)
  418. // CHECK: branchcond (%1.cse_temp < int256 0), block1, block4
  419. // CHECK: ty:int256 %2.cse_temp = ((arg #0) - %b)
  420. // CHECK: branchcond (%2.cse_temp > int256 0), block6, block7
  421. while(a-b > 0) {
  422. // CHECK: ty:int256 %p = (%2.cse_temp * int256 5)
  423. p = (a-b)*5;
  424. b--;
  425. }
  426. bool e2 = p<1;
  427. }
  428. do {
  429. // CHECK: ty:int256 %4.cse_temp = (%k / (arg #0)
  430. // CHECK: ty:int256 %p.170 = ((((arg #0) - %b) * int256 5) - %4.cse_temp)
  431. int p = (a-b)*5-k/a;
  432. b++;
  433. bool e2 = p<1;
  434. // CHECK: branchcond (((arg #0) - %b) < int256 0), block8, block10
  435. }while(a - b < 0);
  436. int g = b;
  437. // CHECK: ty:uint256 %p1 = (uint256((arg #0)) ** uint256(%g))
  438. uint p1 = uint(a)**uint(g);
  439. bool e9 = p1 == 0;
  440. // CHECK: ty:int256 %3.cse_temp = ((arg #0) - %b)
  441. // CHECK: branchcond (%3.cse_temp < int256 0), block12, block13
  442. while(a - b < 0) {
  443. // CHECK: = ((%3.cse_temp * int256 5) - %4.cse_temp)
  444. int p = (a-b)*5-k/a;
  445. b=4;
  446. // CHECK: ty:int256 %5.cse_temp = ((arg #0) - int256 4)
  447. // CHECK: branchcond (%5.cse_temp > int256 0), block14, block15
  448. if (a-b > 0) {
  449. // CHECK: return (%4.cse_temp + int256(%p1))
  450. // CHECK: = (%5.cse_temp * int256 4)
  451. p = (a-b)*4;
  452. b++;
  453. }
  454. bool e2 = p<1;
  455. }
  456. return k/a + int(uint(a)**uint(g));
  457. }
  458. }