-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFold.h
More file actions
56 lines (46 loc) · 2.12 KB
/
Copy pathFold.h
File metadata and controls
56 lines (46 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// peephole constant folding and algebraic simplification, applied as local
// graph rewrites in the spirit of parse-time pessimistic peepholes
//
// references:
// - C. Click and M. Paleczny, "A Simple Graph-Based Intermediate
// Representation", ACM SIGPLAN Workshop on IRs, 1995
// - C. Click, "Combining Analyses, Combining Optimizations", PhD thesis,
// Rice University, 1995 (peephole rewriting on the sea-of-nodes graph)
#ifndef RAT_PASS_OPT_FOLD_H
#define RAT_PASS_OPT_FOLD_H
#include "Core.h"
#include "IR/Opcode.h"
#include "Pass/Pass.h"
namespace rat {
struct Function;
struct Node;
struct ConstantNode;
struct Type;
Node* constant(Function& fn, Type* type, I64 value);
Node* foldBinary(Function& fn, Opcode op, Node* lhs, Node* rhs);
Node* foldUnary(Function& fn, Opcode op, Node* operand);
Node* foldCompare(Function& fn, Opcode op, Node* lhs, Node* rhs);
Node* foldConvert(Function& fn, Opcode op, Node* operand, Type* destType);
Node* foldBinaryConst(Function& fn, Opcode op, Type* ty, U32 w, I64 a, I64 b);
Node* foldBinaryIdentity(Function& fn, Opcode op, Type* ty, U32 w, Node* lhs, Node* rhs);
Node* foldBinaryReassoc(Function& fn, Opcode op, Type* ty, U32 w, Node* lhs, ConstantNode* cr);
Node* foldBinaryStrength(Function& fn, Opcode op, Type* ty, U32 w, Node* lhs, Node* rhs);
Node* foldShiftOfShift(Function& fn, Opcode op, Type* ty, U32 w, Node* lhs, Node* rhs);
Node* simplify(Function& fn, Node* n); // dispatch to the matching fold*
struct FoldPass : FunctionPass {
const C8* name() const override;
U32 runOnFunction(Function& fn) override;
static U64 maskW(I64 v, U32 w); // zero-extend low w bits
static B32 wouldSignedDivOverflow(I64 a, I64 b); // div by 0 or INT_MIN-1
static I64 normalizeConst(I64 v, U32 w); // canonical w-bit representative
static B32 isConstWithValue(Node* n, I64 want);
static B32 isZeroConst(Node* n);
static B32 isOneConst(Node* n);
static B32 isAllOnesConst(Node* n, U32 w);
static I32 pow2Log(Node* n, U32 w);
static B32 matchVarConst(Node* n, Opcode want, Node*& base, I64& c);
private:
U32 foldFunction(Function& fn);
};
} // namespace rat
#endif