| 
									
										
										
										
											2023-08-16 02:04:17 +08:00
										 |  |  | #include "Python.h"
 | 
					
						
							|  |  |  | #include "opcode.h"
 | 
					
						
							|  |  |  | #include "pycore_interp.h"
 | 
					
						
							|  |  |  | #include "pycore_opcode_metadata.h"
 | 
					
						
							|  |  |  | #include "pycore_opcode_utils.h"
 | 
					
						
							|  |  |  | #include "pycore_pystate.h"       // _PyInterpreterState_GET()
 | 
					
						
							|  |  |  | #include "pycore_uops.h"
 | 
					
						
							|  |  |  | #include "pycore_long.h"
 | 
					
						
							|  |  |  | #include "cpython/optimizer.h"
 | 
					
						
							|  |  |  | #include <stdbool.h>
 | 
					
						
							|  |  |  | #include <stdint.h>
 | 
					
						
							|  |  |  | #include <stddef.h>
 | 
					
						
							|  |  |  | #include "pycore_optimizer.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-06 11:28:52 +00:00
										 |  |  | static void | 
					
						
							|  |  |  | remove_unneeded_uops(_PyUOpInstruction *buffer, int buffer_size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int last_set_ip = -1; | 
					
						
							| 
									
										
										
										
											2023-11-09 11:19:51 +00:00
										 |  |  |     bool maybe_invalid = false; | 
					
						
							| 
									
										
										
										
											2023-11-06 11:28:52 +00:00
										 |  |  |     for (int pc = 0; pc < buffer_size; pc++) { | 
					
						
							|  |  |  |         int opcode = buffer[pc].opcode; | 
					
						
							|  |  |  |         if (opcode == _SET_IP) { | 
					
						
							| 
									
										
										
										
											2023-11-15 15:48:58 +00:00
										 |  |  |             buffer[pc].opcode = NOP; | 
					
						
							| 
									
										
										
										
											2023-11-06 11:28:52 +00:00
										 |  |  |             last_set_ip = pc; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-11-09 11:19:51 +00:00
										 |  |  |         else if (opcode == _CHECK_VALIDITY) { | 
					
						
							|  |  |  |             if (maybe_invalid) { | 
					
						
							|  |  |  |                 maybe_invalid = false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 buffer[pc].opcode = NOP; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-11-06 11:28:52 +00:00
										 |  |  |         else if (opcode == _JUMP_TO_TOP || opcode == _EXIT_TRACE) { | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2023-11-15 15:48:58 +00:00
										 |  |  |             if (OPCODE_HAS_ESCAPES(opcode)) { | 
					
						
							| 
									
										
										
										
											2023-11-09 11:19:51 +00:00
										 |  |  |                 maybe_invalid = true; | 
					
						
							| 
									
										
										
										
											2023-11-15 15:48:58 +00:00
										 |  |  |                 if (last_set_ip >= 0) { | 
					
						
							|  |  |  |                     buffer[last_set_ip].opcode = _SET_IP; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (OPCODE_HAS_ERROR(opcode) || opcode == _PUSH_FRAME) { | 
					
						
							|  |  |  |                 if (last_set_ip >= 0) { | 
					
						
							|  |  |  |                     buffer[last_set_ip].opcode = _SET_IP; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2023-11-09 11:19:51 +00:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-11-06 11:28:52 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-16 02:04:17 +08:00
										 |  |  | int | 
					
						
							|  |  |  | _Py_uop_analyze_and_optimize( | 
					
						
							|  |  |  |     PyCodeObject *co, | 
					
						
							| 
									
										
										
										
											2023-11-06 11:28:52 +00:00
										 |  |  |     _PyUOpInstruction *buffer, | 
					
						
							|  |  |  |     int buffer_size, | 
					
						
							| 
									
										
										
										
											2023-08-16 02:04:17 +08:00
										 |  |  |     int curr_stacklen | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-11-06 11:28:52 +00:00
										 |  |  |     remove_unneeded_uops(buffer, buffer_size); | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2023-08-16 02:04:17 +08:00
										 |  |  | } |