LLVM API Documentation

AddrModeMatcher.h

Go to the documentation of this file.
00001 //===- AddrModeMatcher.h - Addressing mode matching facility ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // AddressingModeMatcher - This class exposes a single public method, which is
00011 // used to construct a "maximal munch" of the addressing mode for the target
00012 // specified by TLI for an access to "V" with an access type of AccessTy.  This
00013 // returns the addressing mode that is actually matched by value, but also
00014 // returns the list of instructions involved in that addressing computation in
00015 // AddrModeInsts.
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #ifndef LLVM_TRANSFORMS_UTILS_ADDRMODEMATCHER_H
00020 #define LLVM_TRANSFORMS_UTILS_ADDRMODEMATCHER_H
00021 
00022 #include "llvm/ADT/SmallVector.h"
00023 #include "llvm/Target/TargetLowering.h"
00024 
00025 namespace llvm {
00026 
00027 class GlobalValue;
00028 class Instruction;
00029 class Value;
00030 class Type;
00031 class User;
00032 class raw_ostream;
00033 
00034 /// ExtAddrMode - This is an extended version of TargetLowering::AddrMode
00035 /// which holds actual Value*'s for register values.
00036 struct ExtAddrMode : public TargetLowering::AddrMode {
00037   Value *BaseReg;
00038   Value *ScaledReg;
00039   ExtAddrMode() : BaseReg(0), ScaledReg(0) {}
00040   void print(raw_ostream &OS) const;
00041   void dump() const;
00042 };
00043 
00044 static inline raw_ostream &operator<<(raw_ostream &OS, const ExtAddrMode &AM) {
00045   AM.print(OS);
00046   return OS;
00047 }
00048 
00049 class AddressingModeMatcher {
00050   SmallVectorImpl<Instruction*> &AddrModeInsts;
00051   const TargetLowering &TLI;
00052 
00053   /// AccessTy/MemoryInst - This is the type for the access (e.g. double) and
00054   /// the memory instruction that we're computing this address for.
00055   const Type *AccessTy;
00056   Instruction *MemoryInst;
00057   
00058   /// AddrMode - This is the addressing mode that we're building up.  This is
00059   /// part of the return value of this addressing mode matching stuff.
00060   ExtAddrMode &AddrMode;
00061   
00062   /// IgnoreProfitability - This is set to true when we should not do
00063   /// profitability checks.  When true, IsProfitableToFoldIntoAddressingMode
00064   /// always returns true.
00065   bool IgnoreProfitability;
00066   
00067   AddressingModeMatcher(SmallVectorImpl<Instruction*> &AMI,
00068                         const TargetLowering &T, const Type *AT,
00069                         Instruction *MI, ExtAddrMode &AM)
00070     : AddrModeInsts(AMI), TLI(T), AccessTy(AT), MemoryInst(MI), AddrMode(AM) {
00071     IgnoreProfitability = false;
00072   }
00073 public:
00074   
00075   /// Match - Find the maximal addressing mode that a load/store of V can fold,
00076   /// give an access type of AccessTy.  This returns a list of involved
00077   /// instructions in AddrModeInsts.
00078   static ExtAddrMode Match(Value *V, const Type *AccessTy,
00079                            Instruction *MemoryInst,
00080                            SmallVectorImpl<Instruction*> &AddrModeInsts,
00081                            const TargetLowering &TLI) {
00082     ExtAddrMode Result;
00083 
00084     bool Success = 
00085       AddressingModeMatcher(AddrModeInsts, TLI, AccessTy,
00086                             MemoryInst, Result).MatchAddr(V, 0);
00087     Success = Success; assert(Success && "Couldn't select *anything*?");
00088     return Result;
00089   }
00090 private:
00091   bool MatchScaledValue(Value *ScaleReg, int64_t Scale, unsigned Depth);
00092   bool MatchAddr(Value *V, unsigned Depth);
00093   bool MatchOperationAddr(User *Operation, unsigned Opcode, unsigned Depth);
00094   bool IsProfitableToFoldIntoAddressingMode(Instruction *I,
00095                                             ExtAddrMode &AMBefore,
00096                                             ExtAddrMode &AMAfter);
00097   bool ValueAlreadyLiveAtInst(Value *Val, Value *KnownLive1, Value *KnownLive2);
00098 };
00099 
00100 } // End llvm namespace
00101 
00102 #endif