"LLVMContext* " --> "LLVMContext *"
[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/Support/Streams.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   
34 /// ExtAddrMode - This is an extended version of TargetLowering::AddrMode
35 /// which holds actual Value*'s for register values.
36 struct ExtAddrMode : public TargetLowering::AddrMode {
37   Value *BaseReg;
38   Value *ScaledReg;
39   ExtAddrMode() : BaseReg(0), ScaledReg(0) {}
40   void print(OStream &OS) const;
41   void dump() const;
42 };
43
44 static inline OStream &operator<<(OStream &OS, const ExtAddrMode &AM) {
45   AM.print(OS);
46   return OS;
47 }
48
49 class AddressingModeMatcher {
50   SmallVectorImpl<Instruction*> &AddrModeInsts;
51   const TargetLowering &TLI;
52
53   /// AccessTy/MemoryInst - This is the type for the access (e.g. double) and
54   /// the memory instruction that we're computing this address for.
55   const Type *AccessTy;
56   Instruction *MemoryInst;
57   
58   /// AddrMode - This is the addressing mode that we're building up.  This is
59   /// part of the return value of this addressing mode matching stuff.
60   ExtAddrMode &AddrMode;
61   
62   /// IgnoreProfitability - This is set to true when we should not do
63   /// profitability checks.  When true, IsProfitableToFoldIntoAddressingMode
64   /// always returns true.
65   bool IgnoreProfitability;
66   
67   AddressingModeMatcher(SmallVectorImpl<Instruction*> &AMI,
68                         const TargetLowering &T, const Type *AT,
69                         Instruction *MI, ExtAddrMode &AM)
70     : AddrModeInsts(AMI), TLI(T), AccessTy(AT), MemoryInst(MI), AddrMode(AM) {
71     IgnoreProfitability = false;
72   }
73 public:
74   
75   /// Match - Find the maximal addressing mode that a load/store of V can fold,
76   /// give an access type of AccessTy.  This returns a list of involved
77   /// instructions in AddrModeInsts.
78   static ExtAddrMode Match(Value *V, const Type *AccessTy,
79                            Instruction *MemoryInst,
80                            SmallVectorImpl<Instruction*> &AddrModeInsts,
81                            const TargetLowering &TLI) {
82     ExtAddrMode Result;
83
84     bool Success = 
85       AddressingModeMatcher(AddrModeInsts, TLI, AccessTy,
86                             MemoryInst, Result).MatchAddr(V, 0);
87     Success = Success; assert(Success && "Couldn't select *anything*?");
88     return Result;
89   }
90 private:
91   bool MatchScaledValue(Value *ScaleReg, int64_t Scale, unsigned Depth);
92   bool MatchAddr(Value *V, unsigned Depth);
93   bool MatchOperationAddr(User *Operation, unsigned Opcode, unsigned Depth);
94   bool IsProfitableToFoldIntoAddressingMode(Instruction *I,
95                                             ExtAddrMode &AMBefore,
96                                             ExtAddrMode &AMAfter);
97   bool ValueAlreadyLiveAtInst(Value *Val, Value *KnownLive1, Value *KnownLive2);
98 };
99
100 } // End llvm namespace
101
102 #endif