1 //===- AddrModeMatcher.h - Addressing mode matching facility ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_TRANSFORMS_UTILS_ADDRMODEMATCHER_H
20 #define LLVM_TRANSFORMS_UTILS_ADDRMODEMATCHER_H
22 #include "llvm/AddressingMode.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/Target/TargetLowering.h"
35 /// ExtAddrMode - This is an extended version of TargetLowering::AddrMode
36 /// which holds actual Value*'s for register values.
37 struct ExtAddrMode : public AddrMode {
40 ExtAddrMode() : BaseReg(0), ScaledReg(0) {}
41 void print(raw_ostream &OS) const;
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);
51 static inline raw_ostream &operator<<(raw_ostream &OS, const ExtAddrMode &AM) {
56 class AddressingModeMatcher {
57 SmallVectorImpl<Instruction*> &AddrModeInsts;
58 const TargetLowering &TLI;
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.
63 Instruction *MemoryInst;
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;
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;
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;
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) {
92 AddressingModeMatcher(AddrModeInsts, TLI, AccessTy,
93 MemoryInst, Result).MatchAddr(V, 0);
94 (void)Success; assert(Success && "Couldn't select *anything*?");
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);
107 } // End llvm namespace