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/ADT/SmallVector.h"
23 #include "llvm/Target/TargetLowering.h"
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 {
39 ExtAddrMode() : BaseReg(0), ScaledReg(0) {}
40 void print(raw_ostream &OS) const;
43 bool operator==(const ExtAddrMode& O) const {
44 return (BaseReg == O.BaseReg) && (ScaledReg == O.ScaledReg) &&
45 (BaseGV == O.BaseGV) && (BaseOffs == O.BaseOffs) &&
46 (HasBaseReg == O.HasBaseReg) && (Scale == O.Scale);
50 static inline raw_ostream &operator<<(raw_ostream &OS, const ExtAddrMode &AM) {
55 class AddressingModeMatcher {
56 SmallVectorImpl<Instruction*> &AddrModeInsts;
57 const TargetLowering &TLI;
59 /// AccessTy/MemoryInst - This is the type for the access (e.g. double) and
60 /// the memory instruction that we're computing this address for.
62 Instruction *MemoryInst;
64 /// AddrMode - This is the addressing mode that we're building up. This is
65 /// part of the return value of this addressing mode matching stuff.
66 ExtAddrMode &AddrMode;
68 /// IgnoreProfitability - This is set to true when we should not do
69 /// profitability checks. When true, IsProfitableToFoldIntoAddressingMode
70 /// always returns true.
71 bool IgnoreProfitability;
73 AddressingModeMatcher(SmallVectorImpl<Instruction*> &AMI,
74 const TargetLowering &T, const Type *AT,
75 Instruction *MI, ExtAddrMode &AM)
76 : AddrModeInsts(AMI), TLI(T), AccessTy(AT), MemoryInst(MI), AddrMode(AM) {
77 IgnoreProfitability = false;
81 /// Match - Find the maximal addressing mode that a load/store of V can fold,
82 /// give an access type of AccessTy. This returns a list of involved
83 /// instructions in AddrModeInsts.
84 static ExtAddrMode Match(Value *V, const Type *AccessTy,
85 Instruction *MemoryInst,
86 SmallVectorImpl<Instruction*> &AddrModeInsts,
87 const TargetLowering &TLI) {
91 AddressingModeMatcher(AddrModeInsts, TLI, AccessTy,
92 MemoryInst, Result).MatchAddr(V, 0);
93 (void)Success; assert(Success && "Couldn't select *anything*?");
97 bool MatchScaledValue(Value *ScaleReg, int64_t Scale, unsigned Depth);
98 bool MatchAddr(Value *V, unsigned Depth);
99 bool MatchOperationAddr(User *Operation, unsigned Opcode, unsigned Depth);
100 bool IsProfitableToFoldIntoAddressingMode(Instruction *I,
101 ExtAddrMode &AMBefore,
102 ExtAddrMode &AMAfter);
103 bool ValueAlreadyLiveAtInst(Value *Val, Value *KnownLive1, Value *KnownLive2);
106 } // End llvm namespace