Sort the #include lines for the include/... tree with the script.
[oota-llvm.git] / include / llvm / Transforms / Utils / AddrModeMatcher.h
1 //===- AddrModeMatcher.h - Addressing mode matching facility ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // AddressingModeMatcher - This class exposes a single public method, which is
11 // used to construct a "maximal munch" of the addressing mode for the target
12 // specified by TLI for an access to "V" with an access type of AccessTy.  This
13 // returns the addressing mode that is actually matched by value, but also
14 // returns the list of instructions involved in that addressing computation in
15 // AddrModeInsts.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_TRANSFORMS_UTILS_ADDRMODEMATCHER_H
20 #define LLVM_TRANSFORMS_UTILS_ADDRMODEMATCHER_H
21
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/AddressingMode.h"
24 #include "llvm/Target/TargetLowering.h"
25
26 namespace llvm {
27
28 class GlobalValue;
29 class Instruction;
30 class Value;
31 class Type;
32 class User;
33 class raw_ostream;
34
35 /// ExtAddrMode - This is an extended version of TargetLowering::AddrMode
36 /// which holds actual Value*'s for register values.
37 struct ExtAddrMode : public AddrMode {
38   Value *BaseReg;
39   Value *ScaledReg;
40   ExtAddrMode() : BaseReg(0), ScaledReg(0) {}
41   void print(raw_ostream &OS) const;
42   void dump() const;
43   
44   bool operator==(const ExtAddrMode& O) const {
45     return (BaseReg == O.BaseReg) && (ScaledReg == O.ScaledReg) &&
46            (BaseGV == O.BaseGV) && (BaseOffs == O.BaseOffs) &&
47            (HasBaseReg == O.HasBaseReg) && (Scale == O.Scale);
48   }
49 };
50
51 static inline raw_ostream &operator<<(raw_ostream &OS, const ExtAddrMode &AM) {
52   AM.print(OS);
53   return OS;
54 }
55
56 class AddressingModeMatcher {
57   SmallVectorImpl<Instruction*> &AddrModeInsts;
58   const TargetLowering &TLI;
59
60   /// AccessTy/MemoryInst - This is the type for the access (e.g. double) and
61   /// the memory instruction that we're computing this address for.
62   Type *AccessTy;
63   Instruction *MemoryInst;
64   
65   /// AddrMode - This is the addressing mode that we're building up.  This is
66   /// part of the return value of this addressing mode matching stuff.
67   ExtAddrMode &AddrMode;
68   
69   /// IgnoreProfitability - This is set to true when we should not do
70   /// profitability checks.  When true, IsProfitableToFoldIntoAddressingMode
71   /// always returns true.
72   bool IgnoreProfitability;
73   
74   AddressingModeMatcher(SmallVectorImpl<Instruction*> &AMI,
75                         const TargetLowering &T, Type *AT,
76                         Instruction *MI, ExtAddrMode &AM)
77     : AddrModeInsts(AMI), TLI(T), AccessTy(AT), MemoryInst(MI), AddrMode(AM) {
78     IgnoreProfitability = false;
79   }
80 public:
81   
82   /// Match - Find the maximal addressing mode that a load/store of V can fold,
83   /// give an access type of AccessTy.  This returns a list of involved
84   /// instructions in AddrModeInsts.
85   static ExtAddrMode Match(Value *V, Type *AccessTy,
86                            Instruction *MemoryInst,
87                            SmallVectorImpl<Instruction*> &AddrModeInsts,
88                            const TargetLowering &TLI) {
89     ExtAddrMode Result;
90
91     bool Success = 
92       AddressingModeMatcher(AddrModeInsts, TLI, AccessTy,
93                             MemoryInst, Result).MatchAddr(V, 0);
94     (void)Success; assert(Success && "Couldn't select *anything*?");
95     return Result;
96   }
97 private:
98   bool MatchScaledValue(Value *ScaleReg, int64_t Scale, unsigned Depth);
99   bool MatchAddr(Value *V, unsigned Depth);
100   bool MatchOperationAddr(User *Operation, unsigned Opcode, unsigned Depth);
101   bool IsProfitableToFoldIntoAddressingMode(Instruction *I,
102                                             ExtAddrMode &AMBefore,
103                                             ExtAddrMode &AMAfter);
104   bool ValueAlreadyLiveAtInst(Value *Val, Value *KnownLive1, Value *KnownLive2);
105 };
106
107 } // End llvm namespace
108
109 #endif