HPCToolkit
x86-addsub.c
Go to the documentation of this file.
1 // -*-Mode: C++;-*- // technically C99
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //************************* System Include Files ****************************
48 
49 //*************************** User Include Files ****************************
50 
52 #include "x86-decoder.h"
53 #include "x86-interval-arg.h"
54 
56 
57 //***************************************************************************
58 
60 process_addsub(xed_decoded_inst_t *xptr, const xed_inst_t *xi, interval_arg_t *iarg)
61 {
62  highwatermark_t *hw_tmp = &(iarg->highwatermark);
63 
64  unwind_interval *next = iarg->current;
65  const xed_operand_t* op0 = xed_inst_operand(xi,0);
66  const xed_operand_t* op1 = xed_inst_operand(xi,1);
67  xed_operand_enum_t op0_name = xed_operand_name(op0);
68  x86recipe_t *xr = UWI_RECIPE(iarg->current);
69  x86registers_t reg = xr->reg;
70  ra_loc istatus = xr->ra_status;
71 
72  if (op0_name == XED_OPERAND_REG0) {
73  xed_reg_enum_t reg0 = xed_decoded_inst_get_reg(xptr, op0_name);
74  if (x86_isReg_SP(reg0)) {
75  //-----------------------------------------------------------------------
76  // we are adjusting the stack pointer
77  //-----------------------------------------------------------------------
78 
79  if (xed_operand_name(op1) == XED_OPERAND_IMM0) {
80  int sign = (iclass_eq(xptr, XED_ICLASS_ADD)) ? -1 : 1;
81  long immedv = sign * xed_decoded_inst_get_signed_immediate(xptr);
82  if ((istatus == RA_STD_FRAME) && (immedv > 0) &&
83  (hw_tmp->state & HW_SP_DECREMENTED)) {
84  //-------------------------------------------------------------------
85  // if we are in a standard frame and we see a second subtract,
86  // it is time to convert interval to a BP frame to minimize
87  // the chance we get the wrong offset for the return address
88  // in a routine that manipulates SP frequently (as in
89  // leapfrog_mod_leapfrog_ in the SPEC CPU2006 benchmark
90  // 459.GemsFDTD, when compiled with PGI 7.0.3 with high levels
91  // of optimization).
92  //
93  // 9 December 2007 -- John Mellor-Crummey
94  //-------------------------------------------------------------------
95  }
96  reg.sp_ra_pos += immedv;
97  reg.sp_bp_pos += immedv;
98  next = new_ui(nextInsn(iarg, xptr), istatus, &reg);
99 
100  if (immedv > 0) {
101  if (HW_TEST_STATE(hw_tmp->state, 0, HW_SP_DECREMENTED)) {
102  //-----------------------------------------------------------------
103  // set the highwatermark and canonical interval upon seeing
104  // the FIRST subtract from SP; take no action on subsequent
105  // subtracts.
106  //
107  // test case: main in SPEC CPU2006 benchmark 470.lbm
108  // contains multiple subtracts from SP when compiled with
109  // PGI 7.0.3 with high levels of optimization. the first
110  // subtract from SP is to set up the frame; subsequent ones
111  // are to reserve space for arguments passed to functions.
112  //
113  // 9 December 2007 -- John Mellor-Crummey
114  //-----------------------------------------------------------------
115  hw_tmp->uwi = next;
116  hw_tmp->succ_inst_ptr = nextInsn(iarg, xptr);
117  hw_tmp->state = HW_NEW_STATE(hw_tmp->state, HW_SP_DECREMENTED);
118  iarg->canonical_interval = next;
119  }
120  }
121  } else {
122  if (istatus != RA_BP_FRAME){
123  //-------------------------------------------------------------------
124  // no immediate in add/subtract from stack pointer; switch to
125  // BP_FRAME
126  //
127  // 9 December 2007 -- John Mellor-Crummey
128  //-------------------------------------------------------------------
129  next = new_ui(nextInsn(iarg, xptr), RA_BP_FRAME, &reg);
130  iarg->bp_frames_found = true;
131  }
132  }
133  }
134  }
135  return next;
136 }
#define HW_NEW_STATE(state, set)
bitree_uwi_t * current
#define UWI_RECIPE(btuwi)
unwind_interval * process_addsub(xed_decoded_inst_t *xptr, const xed_inst_t *xi, interval_arg_t *iarg)
Definition: x86-addsub.c:60
x86registers_t reg
#define iclass_eq(xptr, class)
Definition: x86-decoder.h:71
bitree_uwi_t * canonical_interval
static char * nextInsn(uint32_t *insn)
#define HW_SP_DECREMENTED
Definition: x86-canonical.c:57
highwatermark_t highwatermark
unwind_interval * new_ui(char *startaddr, sp_ty_t sp_ty, ra_ty_t ra_ty, int sp_arg, int ra_arg)
#define HW_TEST_STATE(state, is_set, is_clear)
bitree_uwi_t unwind_interval