Calvin Lin The University of Texas at Austin

Similar documents
Interprocedural Analysis. Motivation. Interprocedural Analysis. Function Calls and Pointers

INTERPROCEDURAL ANALYSIS

Interprocedural Analysis. Dealing with Procedures. Course so far: Terminology

Context-Sensitive Pointer Analysis. Recall Context Sensitivity. Partial Transfer Functions [Wilson et. al. 95] Emami 1994

Alias Analysis. Last time Interprocedural analysis. Today Intro to alias analysis (pointer analysis) CS553 Lecture Alias Analysis I 1

Advanced Compiler Construction

CSE 501 Midterm Exam: Sketch of Some Plausible Solutions Winter 1997

Interprocedural Analysis with Data-Dependent Calls. Circularity dilemma. A solution: optimistic iterative analysis. Example

Alias Analysis. Advanced Topics. What is pointer analysis? Last Time

Interprocedural Analysis with Data-Dependent Calls. Circularity dilemma. A solution: optimistic iterative analysis. Example

Field Analysis. Last time Exploit encapsulation to improve memory system performance

CS 4120 Lecture 31 Interprocedural analysis, fixed-point algorithms 9 November 2011 Lecturer: Andrew Myers

Static Program Analysis Part 9 pointer analysis. Anders Møller & Michael I. Schwartzbach Computer Science, Aarhus University

Flow-sensitive rewritings and Inliner improvements for the Graal JIT compiler

More Dataflow Analysis

CS553 Lecture Generalizing Data-flow Analysis 3

Wednesday, October 15, 14. Functions

Lecture 16 Pointer Analysis

Compiler Structure. Data Flow Analysis. Control-Flow Graph. Available Expressions. Data Flow Facts

Lecture 27. Pros and Cons of Pointers. Basics Design Options Pointer Analysis Algorithms Pointer Analysis Using BDDs Probabilistic Pointer Analysis

Lecture 20 Pointer Analysis

Lecture 14 Pointer Analysis

Calvin Lin The University of Texas at Austin

Compiler Design Spring 2017

Register Allocation. CS 502 Lecture 14 11/25/08

Static Program Analysis Part 7 interprocedural analysis

Simone Campanoni Alias Analysis

Run-time Environments - 2

System Software Assignment 1 Runtime Support for Procedures

Interprocedural Analysis. CS252r Fall 2015

Bottom-up Context-Sensitive Pointer Analysis for Java

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

G Programming Languages - Fall 2012

CS558 Programming Languages

Flow-sensitive Alias Analysis

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Today More register allocation Clarifications from last time Finish improvements on basic graph coloring concept Procedure calls Interprocedural

CS711 Advanced Programming Languages Pointer Analysis Overview and Flow-Sensitive Analysis

Data Flow Testing. CSCE Lecture 10-02/09/2017

Introduction to optimizations. CS Compiler Design. Phases inside the compiler. Optimization. Introduction to Optimizations. V.

Interprocedural Dataflow Analysis. Galeotti/Gorla/Rau Saarland University

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

CS61C : Machine Structures

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler

Siloed Reference Analysis

Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Dependence and Data Flow Models. (c) 2007 Mauro Pezzè & Michal Young Ch 6, slide 1

CO444H. Ben Livshits. Datalog Pointer analysis

Redundant Computation Elimination Optimizations. Redundancy Elimination. Value Numbering CS2210

A Context-Sensitive Memory Model for Verification of C/C++ Programs

procedure definition (or declaration - some make a distinction, we generally won t), e.g., void foo(int x) {... }

register allocation saves energy register allocation reduces memory accesses.

R O O T S Interprocedural Analysis

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

CS153: Compilers Lecture 15: Local Optimization

USC 227 Office hours: 3-4 Monday and Wednesday CS553 Lecture 1 Introduction 4

Last Time. Response time analysis Blocking terms Priority inversion. Other extensions. And solutions

Obfuscating Transformations. What is Obfuscator? Obfuscation Library. Obfuscation vs. Deobfuscation. Summary

Why Data Flow Models? Dependence and Data Flow Models. Learning objectives. Def-Use Pairs (1) Models from Chapter 5 emphasized control

Making Context-sensitive Points-to Analysis with Heap Cloning Practical For The Real World

Program Static Analysis. Overview

Lecture08: Scope and Lexical Address

CS553 Lecture Dynamic Optimizations 2

Outline. Issues with the Memory System Loop Transformations Data Transformations Prefetching Alias Analysis

Dependence and Data Flow Models. (c) 2007 Mauro Pezzè & Michal Young Ch 6, slide 1

OOPLs - call graph construction. Example executed calls

Stacks, Queues (cont d)

COMP 515: Advanced Compilation for Vector and Parallel Processors. Vivek Sarkar Department of Computer Science Rice University

Weeks 6&7: Procedures and Parameter Passing

Alias Analysis & Points-to Analysis. Hwansoo Han

Hash-Based Indexes. Chapter 11

Flow- Context- Sensitive Pointer Analysis

Code generation and local optimization

CS 406/534 Compiler Construction Putting It All Together

Chapter 9 Subprograms

CSE 501: Compiler Construction. Course outline. Goals for language implementation. Why study compilers? Models of compilation

Code generation and local optimization

Compiling Java For High Performance on Servers

A Simple SQL Injection Pattern

Names, Scope, and Bindings

Names, Scope, and Bindings

Compiler Optimization and Code Generation

Polyèdres et compilation

Escape Analysis for Java

April 15, 2015 More Register Allocation 1. Problem Register values may change across procedure calls The allocator must be sensitive to this

Tiling: A Data Locality Optimizing Algorithm

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

4/24/18. Overview. Program Static Analysis. Has anyone done static analysis? What is static analysis? Why static analysis?

Procedures and Stacks

Lecture 9 Dynamic Compilation

Static Analysis and Dataflow Analysis

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

Software Analysis. Asymptotic Performance Analysis

ELEC 876: Software Reengineering

Instance keys: A technique for sharpening whole-program pointer analyses with intraprocedural information

CS 314 Principles of Programming Languages

Office Hours: Mon/Wed 3:30-4:30 GDC Office Hours: Tue 3:30-4:30 Thu 3:30-4:30 GDC 5.

JAVA PERFORMANCE. PR SW2 S18 Dr. Prähofer DI Leopoldseder

CS553 Lecture Profile-Guided Optimizations 3

Transcription:

Interprocedural Analysis Last time Introduction to alias analysis Today Interprocedural analysis March 4, 2015 Interprocedural Analysis 1 Motivation Procedural abstraction Cornerstone of programming Introduces barriers to analysis Example x = 5; foo(p); y = x+1; What is the calling context of f()? Does foo() modify x? Example void f(int x) if (x) foo(); else bar();... f(0); f(1); March 4, 2015 Interprocedural Analysis 2 CS380 C Compilers 1

Function Calls and Pointers Recall Function calls can affect our points-to sets e.g., p1 = &x; p2 = &p1;... foo(); Be conservative Lose a lot of information (p1 x), (p2 p1)??? March 4, 2015 Interprocedural Analysis 3 Interprocedural Analysis Goal Avoid making conservative assumptions about the effects of procedures and the state at call sites Terminology int a, e; void foo(int &b, &c) b = c; main() int d; foo(a, d); // Globals // Formal parameters // Local variables // Actual parameters March 4, 2015 Interprocedural Analysis 4 CS380 C Compilers 2

Interprocedural Analysis vs. Interprocedural Optimization Interprocedural analysis Gather information across multiple procedures (typically across the entire program) Use this information to improve intra-procedural analyses and optimization (e.g., CSE) Interprocedural optimizations Optimizations that involve multiple procedures e.g., Inlining, procedure cloning, interprocedural register allocation Optimizations that use interprocedural analysis March 4, 2015 Interprocedural Analysis 5 Dimensions of Interprocedural Analysis Flow-sensitive vs. flow-insensitive Context-sensitive vs. context-insensitive Path-sensitive vs. path-insensitive March 4, 2015 Interprocedural Analysis 6 CS380 C Compilers 3

Flow Sensitivity Flow-sensitive analysis Computes one answer for every program point Requires iterative data-flow analysis or similar technique Flow-insensitive analysis Ignores control flow Computes one answer for every procedure Faster but less accurate than flow-sensitive analysis March 4, 2015 Interprocedural Analysis 7 Flow Sensitivity Example Is x constant? void f(int x) x = 4;... x = 5; Flow-sensitive analysis Computes an answer at every program point: x is 4 after the first assignment x is 5 after the second assignment Flow-insensitive analysis Computes one answer for the entire procedure: x is not constant Where have we seen examples of flow-insensitive analysis? Address Taken pointer analysis March 4, 2015 Interprocedural Analysis 8 CS380 C Compilers 4

Context Sensitivity Context-sensitive analysis Re-analyzes callee for each caller Also known as polyvariant analysis Context-insensitive analysis Perform one analysis independent of callers Also known as monovariant analysis March 4, 2015 Interprocedural Analysis 9 Context Sensitivity Example Is x constant? a = id(4); a = id(4); 4 5 b = id(5); id(x) return x; 4,5 4,5 b = id(5); id(x) return x; Context-sensitive analysis Computes an answer for every callsite: x is 4 in the first call x is 5 in the second call Context-insensitive analysis Computes one answer for all callsites: x is not constant Suffers from unrealizable paths: Can mistakenly conclude that id(4) can return 5 because we merge (smear) information from all callsites March 4, 2015 Interprocedural Analysis 10 CS380 C Compilers 5

Path Sensitivity Path-sensitive analysis Computes one answer for every execution path Subsumes flow-sensitivity and context-sensitivity Extremely expensive Path-insensitive Not path-sensitive March 4, 2015 Interprocedural Analysis 11 Path Sensitivity Example Is x constant? if (x==0) x = 4; x = 5; 4 5 print(x) Path-sensitive analysis Computes an answer for every path: x is 4 at the end of the left path x is 5 at the end of the right path Path-insensitive analysis Computes one answer for all paths: x is not constant March 4, 2015 Interprocedural Analysis 12 CS380 C Compilers 6

Dimensions of Interprocedural Analysis (cont) Flow-insensitive context-insensitive (FICI) int** foo(int **p, **q) int **x; x = p;... x = q; return x; p b, d q f, g x b, d, f, g int main() int **a, *b, *d, *f, c, e; a = foo(&b, &f); *a = &c; a = foo(&d, &g); *a = &e; a b, d, f, g b c, e d c, e f c, e g c, e March 4, 2015 Interprocedural Analysis 13 Dimensions of Interprocedural Analysis (cont) Flow-sensitive context-insensitive (FSCI) int** foo(int **p, **q) int **x; x = p;... x = q; return x; FICI p b, d q f, g x b, d, f, g FSCI p b, d q f, g x 1 b, d x 2 f, g int main() int **a, *b, *d, *f, c, e; a = foo(&b, &f); *a = &c; a = foo(&d, &g); *a = &e; a b, d, f, g b c, e d c, e f c, e g c, e Weak update a 1 f, g a 2 f, g f 1 c g 1 c f 2 c, e g 2 c, e March 4, 2015 Interprocedural Analysis 14 CS380 C Compilers 7

Interprocedural Analysis: Supergraphs Compose the CFGs for all procedures via the call graph Connect call nodes to entry nodes of callees x=3 Connect return nodes of callees back to calls Called control-flow supergraph foo(x) foo(1) Pros Simple Intraprocedural analysis algorithms work unchanged Reasonably effective foo() y=x+1... return March 4, 2015 Interprocedural Analysis 15 Monday s Example Revisited int x, y, a; int *p; p = &a; x = 5; foo(&x); y = x + 1; foo (int *p) return p; Is x constant? With a supergraph, run our same IDFA algorithm Determine that x = 5 March 4, 2015 Interprocedural Analysis 16 CS380 C Compilers 8

Supergraphs (cont) Compose the CFGs for all procedures via the call graph Connect call nodes to entry nodes of callees x=3 Connect return nodes of callees back to calls Called control-flow supergraph foo(x) foo() y=x+1 foo(1)... return Cons Accuracy? Smears information from different contexts. Performance? IDFA is O(n 4 ), graphs can be huge No separate compilation IDFA converges in d+2 iterations, where d is the Number of nested loops [Kam & Ullman 76]. Graphs will have many cycles (one per callsite) March 4, 2015 Interprocedural Analysis 17 Brute Force: Full Context-Sensitive Interprocedural Analysis Invocation Graph [Emami94] Use an invocation graph, which distinguishes all calling chains Re-analyze callee for each distinct calling paths void foo(int b) hoo(b); void goo(int c) hoo(c); main() int x, y; foo(x); goo(y); foo hoo main goo hoo Pros Precise Cons Exponentially expensive Recursion is tricky March 4, 2015 Interprocedural Analysis 18 CS380 C Compilers 9

Middle Ground: Use Call Graph and Compute Summaries 1 procedure f() 2 begin Goal 3 call g() f Represent procedure 4 call g() 3,4 5 call relationships 5 call h() 6 end 9 g h 7 procedure g() 8 begin 10 9 call h() 20 10 call i() 11 end i j 12 procedure h() Definition 21 13 begin If program P consists of n procedures: p 1,..., p n 14 end 15 procedure i() Static call graph of P is G P = (N,S,E,r) 16 procedure j() N = p 1,..., p n 17 begin 18 end S = call-site labels 19 begin E N N S 20 call g() 21 call j() r N is start node 22 end March 4, 2015 Interprocedural Analysis 19 Interprocedural Analysis: Summaries Compute summary information for each procedure Summarize effect of called procedure for callers Summarize effect of callers for called procedure Store summaries in database Use later when optimizing procedures Pros Concise Can be fast to compute and use Separate compilation practical Cons Imprecise if there s only have one summary per procedure March 4, 2015 Interprocedural Analysis 20 CS380 C Compilers 10

Two Types of Information Track information that flows into a procedure Sometimes known as propagation problems e.g., What formals are constant? e.g., Which formals are aliased to globals? Useful for optimizing the body of a procedure Track information that flows out of a procedure Sometimes known as side effect problems e.g., Which globals are def d/used by a procedure? e.g., Which locals are def d/used by a procedure? e.g., Which actual parameters are def d by a procedure? Useful for optimizing the code that calls a procedure proc (x,y)... March 4, 2015 Interprocedural Analysis 21 Examples Propagation Summaries May-Alias: The set of formals that may be aliased to globals and to each other Must-Alias: The set of formals that are definitely aliased to globals and to each other Constant: The set of formals that have constant value Side-effect Summaries Mod: The set of variables possibly modified (defined) by a call to a procedure Ref: The set of variables possibly read by a call to a procedure Kill: The set of variables that are definitely killed by a procedure (e.g., in the liveness sense) March 4, 2015 Interprocedural Analysis 22 CS380 C Compilers 11

Computing Interprocedural Summaries Top-down Summarize information about the caller (May-Alias, Must-Alias) Use this information inside the procedure body int a; void foo(int &b, &c)... foo(a,a); Bottom-up Summarize the effects of a call (Mod, Ref, Kill) Use this information around procedure calls x = 7; foo(x); y = x + 3; March 4, 2015 Interprocedural Analysis 23 Context-Sensitivity of Summaries None (zero levels of the call path) Forward propagation: Meet (or smear) information from all callers to particular callee Side-effects: Use side-effect information for callee at all callsites Callsite (one level of the call path) Forward propagation: Label data-flow information with callsite Side-effects: Affects alias analysis, which in turn affects side-effects March 4, 2015 Interprocedural Analysis 24 CS380 C Compilers 12

Context-Sensitivity of Summaries (cont) k levels of call path (k-limiting) Forward propagation: Label data-flow information with k levels of the call path Side-effects: Affects alias analysis, which in turn affects side-effects main foo goo hoo hoo k-levels of the call chain yoo yoo March 4, 2015 Interprocedural Analysis 25 Bi-Directional Interprocedural Summaries Interprocedural Constant Propagation (ICP) Information flows from caller to callee and back int a,b,c,d; void foo(e) a = b + c; d = e + 2; foo(3); The calling context tells us that the formal e is bound to the constant 3, which enables constant propagation within foo() After calling foo() we know that the constant 5 (3+2) propagates to the global d Interprocedural Alias Analysis Forward propagation: aliasing due to reference parameters Side-effects: points-to relationships due to multi-level pointers March 4, 2015 Interprocedural Analysis 26 CS380 C Compilers 13

Alternative to Interprocedural Analysis: Inlining Idea Replace call with procedure body Pros Reduces call overhead Exposes calling context to procedure body Exposes side effects of procedure to caller Simple! Cons Code bloat (decreases the efficacy of caches, branch predictor, etc) Can t always statically determine callee (e.g., in OO languages) Library source is usually unavailable Can t always inline (recursion) March 4, 2015 Interprocedural Analysis 27 Inlining Policies The hard question How do we decide which calls to inline? Many possible heuristics Only inline small functions Let the programmer decide using an inline directive Oblivious to callsite Use a code expansion budget [Ayers, et al 97] Use profiling or instrumentation to identify hot paths inline along the hot paths [Chang, et al 92] JIT compilers do this Use inlining trials for object oriented languages [Dean & Chambers 94] Keep a database of functions, their parameter types, and the benefit of inlining Keeps track of indirect benefit of inlining Effective in an incrementally compiled language March 4, 2015 Interprocedural Analysis 28 CS380 C Compilers 14

Alternative to Interprocedural Analysis: Cloning Procedure Cloning/Specialization Create a customized version of procedure for particular call sites Compromise between inlining and interprocedural optimization Pros Less code bloat than inlining Recursion is not an issue (as compared to inlining) Better caller/callee optimization potential (versus interprocedural analysis) Cons Still some code bloat (versus interprocedural analysis) May have to do interprocedural analysis anyway e.g. Interprocedural constant propagation can guide cloning March 4, 2015 Interprocedural Analysis 29 Evaluation Most compilers avoid interprocedural analysis It s expensive and complex Not beneficial for most classical optimizations Separate compilation + interprocedural analysis requires recompilation analysis [Burke and Torczon 93] Can t analyze library code When is it useful? Pointer analysis Constant propagation Object oriented class analysis Security and error checking Program understanding and re-factoring Code compaction Parallelization Modern uses of compilers March 4, 2015 Interprocedural Analysis 30 CS380 C Compilers 15

Other Trends Cost of procedures is growing More of them and they re smaller (OO languages) Modern machines demand precise information (memory aliasing) Cost of inlining is growing Code bloat degrades efficacy of many modern structures Procedures are being used more extensively Programs are becoming larger Cost of interprocedural analysis is shrinking Faster machines Better methods March 4, 2015 Interprocedural Analysis 31 Concepts Call graphs, invocation graphs Analysis versus optimization Characteristic of interprocedural analysis Flow sensitivity, context sensitivity, path sensitivity Smearing Approaches Context sensitive, supergraph, summaries Bottom-up, top-down, bi-directional, iterative Propagation versus side-effect problems Alternatives to interprocedural analysis Inlining Procedure cloning March 4, 2015 Interprocedural Analysis 32 CS380 C Compilers 16

Next Time Lecture Flow-insensitive analysis Look at pointer analysis as an important special case March 4, 2015 Interprocedural Analysis 33 CS380 C Compilers 17